diff --git a/proto/src/core/mod.rs b/proto/src/core/mod.rs index 02782ed..2137cde 100644 --- a/proto/src/core/mod.rs +++ b/proto/src/core/mod.rs @@ -1,12 +1,16 @@ mod frame; mod prefix; +#[cfg(test)] +pub mod testing; mod u32; mod user; -// TODO: Remove `pub` qualifier, depend on re-exports. -pub mod value; +mod value; mod worker; pub use frame::{FrameReader, FrameWriter}; pub use user::{User, UserStatus}; -pub use value::{ValueDecode, ValueEncode}; +pub use value::{ + ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncodeError, + ValueEncoder, +}; pub use worker::{Worker, WorkerError}; diff --git a/proto/src/core/testing.rs b/proto/src/core/testing.rs new file mode 100644 index 0000000..4415977 --- /dev/null +++ b/proto/src/core/testing.rs @@ -0,0 +1,20 @@ +//! Shared testing utilities for protocol code. + +use std::fmt; + +use bytes::BytesMut; + +use crate::core::{ValueDecode, ValueDecoder, ValueEncode, ValueEncoder}; + +/// Encodes `input`, decodes it, asserts equality of `input` and the result. +pub fn roundtrip(input: T) +where + T: fmt::Debug + Eq + PartialEq + ValueEncode + ValueDecode, +{ + let mut bytes = BytesMut::new(); + + ValueEncoder::new(&mut bytes).encode(&input).unwrap(); + let output = ValueDecoder::new(&bytes).decode::().unwrap(); + + assert_eq!(output, input); +} diff --git a/proto/src/core/value.rs b/proto/src/core/value.rs index bf585a3..1b6c0bf 100644 --- a/proto/src/core/value.rs +++ b/proto/src/core/value.rs @@ -506,32 +506,19 @@ impl ValueEncode for Vec { #[cfg(test)] pub mod tests { - use std::fmt; use std::net; use std::u16; use std::u32; use bytes::{BufMut, BytesMut}; - use super::{ - ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncoder, - }; + use crate::core::testing::roundtrip; + + use super::{ValueDecodeError, ValueDecoder, ValueEncoder}; // Declared here because assert_eq!(bytes, &[]) fails to infer types. const EMPTY_BYTES: &'static [u8] = &[]; - pub fn roundtrip(input: T) - where - T: fmt::Debug + Eq + PartialEq + ValueEncode + ValueDecode, - { - let mut bytes = BytesMut::new(); - - ValueEncoder::new(&mut bytes).encode(&input).unwrap(); - let output = ValueDecoder::new(&bytes).decode::().unwrap(); - - assert_eq!(output, input); - } - // A few integers and their corresponding byte encodings. const U32_ENCODINGS: [(u32, [u8; 4]); 8] = [ (0, [0, 0, 0, 0]), diff --git a/proto/src/core/worker.rs b/proto/src/core/worker.rs index 124685c..ad344fd 100644 --- a/proto/src/core/worker.rs +++ b/proto/src/core/worker.rs @@ -7,8 +7,8 @@ use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; use tokio::net::TcpStream; use tokio::sync::mpsc; -use crate::core::{FrameReader, FrameWriter}; use crate::core::value::{ValueDecode, ValueEncode}; +use crate::core::{FrameReader, FrameWriter}; /// An error that arose while exchanging messages over a `Channel`. #[derive(Debug, Error)] diff --git a/proto/src/peer/message.rs b/proto/src/peer/message.rs index 55bfac7..6d4f916 100644 --- a/proto/src/peer/message.rs +++ b/proto/src/peer/message.rs @@ -1,4 +1,4 @@ -use crate::core::value::{ +use crate::core::{ ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncodeError, ValueEncoder, }; @@ -99,8 +99,8 @@ impl ValueDecode for PeerInit { mod tests { use bytes::BytesMut; - use crate::core::value::tests::roundtrip; - use crate::core::value::{ValueDecodeError, ValueDecoder}; + use crate::core::testing::roundtrip; + use crate::core::{ValueDecodeError, ValueDecoder}; use super::*; diff --git a/proto/src/server/request.rs b/proto/src/server/request.rs index a8cdc07..59c6729 100644 --- a/proto/src/server/request.rs +++ b/proto/src/server/request.rs @@ -1,4 +1,4 @@ -use crate::core::value::{ +use crate::core::{ ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncodeError, ValueEncoder, }; @@ -496,8 +496,8 @@ impl ValueDecode for UserStatusRequest { mod tests { use bytes::BytesMut; - use crate::core::value::tests::roundtrip; - use crate::core::value::{ValueDecodeError, ValueDecoder}; + use crate::core::testing::roundtrip; + use crate::core::{ValueDecodeError, ValueDecoder}; use super::*; diff --git a/proto/src/server/response.rs b/proto/src/server/response.rs index 8d72c69..ccb6a13 100644 --- a/proto/src/server/response.rs +++ b/proto/src/server/response.rs @@ -2,7 +2,7 @@ use std::net; use log::{debug, warn}; -use crate::core::value::{ +use crate::core::{ ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncodeError, ValueEncoder, }; @@ -1118,8 +1118,8 @@ mod tests { use bytes::BytesMut; - use crate::core::value::tests::roundtrip; - use crate::core::value::{ValueDecodeError, ValueDecoder}; + use crate::core::testing::roundtrip; + use crate::core::{ValueDecodeError, ValueDecoder}; use super::*;