Move unit tests inside conditionally compiled mod tests { } blocks
Idiomatic rust organizes unit tests into `mod tests { }` blocks
using conditional compilation `[cfg(test)]` to decide whether to
compile the code in that block.
This commit moves "bare" test functions into such blocks, and puts
the block at the bottom of respective file.
This commit is contained in:
45
src/body.rs
45
src/body.rs
@@ -150,26 +150,6 @@ fn copy_chunked<R: Read, W: Write>(reader: &mut R, writer: &mut W) -> io::Result
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_copy_chunked() {
|
||||
let mut source = Vec::<u8>::new();
|
||||
source.resize(CHUNK_MAX_PAYLOAD_SIZE, 33);
|
||||
source.extend_from_slice(b"hello world");
|
||||
|
||||
let mut dest = Vec::<u8>::new();
|
||||
copy_chunked(&mut &source[..], &mut dest).unwrap();
|
||||
|
||||
let mut dest_expected = Vec::<u8>::new();
|
||||
dest_expected.extend_from_slice(format!("{:x}\r\n", CHUNK_MAX_PAYLOAD_SIZE).as_bytes());
|
||||
dest_expected.resize(dest_expected.len() + CHUNK_MAX_PAYLOAD_SIZE, 33);
|
||||
dest_expected.extend_from_slice(b"\r\n");
|
||||
|
||||
dest_expected.extend_from_slice(b"b\r\nhello world\r\n");
|
||||
dest_expected.extend_from_slice(b"0\r\n\r\n");
|
||||
|
||||
assert_eq!(dest, dest_expected);
|
||||
}
|
||||
|
||||
/// Helper to send a body, either as chunked or not.
|
||||
pub(crate) fn send_body(
|
||||
mut body: SizedReader,
|
||||
@@ -184,3 +164,28 @@ pub(crate) fn send_body(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_copy_chunked() {
|
||||
let mut source = Vec::<u8>::new();
|
||||
source.resize(CHUNK_MAX_PAYLOAD_SIZE, 33);
|
||||
source.extend_from_slice(b"hello world");
|
||||
|
||||
let mut dest = Vec::<u8>::new();
|
||||
copy_chunked(&mut &source[..], &mut dest).unwrap();
|
||||
|
||||
let mut dest_expected = Vec::<u8>::new();
|
||||
dest_expected.extend_from_slice(format!("{:x}\r\n", CHUNK_MAX_PAYLOAD_SIZE).as_bytes());
|
||||
dest_expected.resize(dest_expected.len() + CHUNK_MAX_PAYLOAD_SIZE, 33);
|
||||
dest_expected.extend_from_slice(b"\r\n");
|
||||
|
||||
dest_expected.extend_from_slice(b"b\r\nhello world\r\n");
|
||||
dest_expected.extend_from_slice(b"0\r\n\r\n");
|
||||
|
||||
assert_eq!(dest, dest_expected);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user