From 9d723d661c00669d0c21763ed5871ecf43a2723a Mon Sep 17 00:00:00 2001 From: John Lettman Date: Fri, 16 Aug 2024 14:46:40 -0400 Subject: [PATCH] types/motor_calibrate: add primitive tests --- src/types/motor_calibrate.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/types/motor_calibrate.rs b/src/types/motor_calibrate.rs index be9cb38..a75e0f6 100644 --- a/src/types/motor_calibrate.rs +++ b/src/types/motor_calibrate.rs @@ -36,10 +36,32 @@ impl Display for MotorCalibrate { #[cfg(test)] mod tests { use super::*; + use num_traits::{FromPrimitive, ToPrimitive}; use log::info; use test_log::test; + const PRIMITIVE_CASES: [(MotorCalibrate, u8); 2] = + [(MotorCalibrate::Normal, 0), (MotorCalibrate::Calibrate, 1)]; + + #[test] + fn test_from_primitive() { + for (want, primitive) in PRIMITIVE_CASES { + info!("From primitive {primitive:?}, want {want:?}"); + let got = MotorCalibrate::from_u8(primitive).expect("It should return a value"); + assert_eq!(want, got); + } + } + + #[test] + fn test_to_primitive() { + for (motor_calibrate, want) in PRIMITIVE_CASES { + info!("{motor_calibrate:?} to primitive, want {want:?}"); + let got = motor_calibrate.to_u8().expect("It should return a value"); + assert_eq!(want, got); + } + } + #[test] fn test_default() { let got = MotorCalibrate::default();