Skip to content

Commit

Permalink
types/motion_config: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlettman committed Aug 13, 2024
1 parent 257f4e3 commit 57eda22
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/types/motion_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,90 @@ impl BinWrite for MotionConfig {
self.write(writer)
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;

use log::info;
use test_log::test;

const BINARY_ENDIAN: Endian = Endian::NATIVE;
const BINARY_CASES: [(MotionConfig, [u8; 1]); 3] = [
(
MotionConfig {
direction: Direction::Counterclockwise,
transducer: Transducer::Down,
mode: Mode::Sector,
step_size: StepSize::Slow,
},
[0b0000_0000],
),
(
MotionConfig {
direction: Direction::Clockwise,
transducer: Transducer::Up,
mode: Mode::Polar,
step_size: StepSize::Medium,
},
[0b1100_1001],
),
(
MotionConfig {
direction: Direction::Clockwise,
transducer: Transducer::Up,
mode: Mode::Polar,
step_size: StepSize::Fast,
},
[0b1100_1010],
),
];

#[test]
fn test_parse() {
for (want, bytes) in BINARY_CASES {
info!("Parsing {bytes:?}, want {want:?}");
let mut cursor = Cursor::new(bytes);
let got = MotionConfig::read(&mut cursor).expect("It should not return an error");
assert_eq!(want, got);
}
}

#[test]
fn test_parse_options() {
for (want, bytes) in BINARY_CASES {
info!("Parsing {bytes:?}, want {want:?}");
let mut cursor = Cursor::new(bytes);
let got = MotionConfig::read_options(&mut cursor, BINARY_ENDIAN, ())
.expect("It should not return an error");
assert_eq!(want, got);
}
}

#[test]
fn test_write() {
for (motion_config, want) in BINARY_CASES {
info!("Writing {motion_config:?}, want {want:?}");
let mut cursor = Cursor::new(Vec::new());
motion_config.write(&mut cursor).expect("It should not return an error");
let inner = cursor.into_inner();
let got = inner.as_slice();
assert_eq!(want, got);
}
}

#[test]
fn test_write_options() {
for (motion_config, want) in BINARY_CASES {
info!("Writing {motion_config:?}, want {want:?}");
let mut cursor = Cursor::new(Vec::new());
motion_config
.write_options(&mut cursor, BINARY_ENDIAN, ())
.expect("It should not return an error");
let inner = cursor.into_inner();
let got = inner.as_slice();
assert_eq!(want, got);
}
}
}

0 comments on commit 57eda22

Please sign in to comment.