From 21e94a4b52c29c84f8cf4c48d3aa1fef68bb7e16 Mon Sep 17 00:00:00 2001 From: John Lettman Date: Fri, 16 Aug 2024 13:43:29 -0400 Subject: [PATCH] types/sonar_return_magic: add tests --- src/types/sonar_return_magic.rs | 80 ++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/src/types/sonar_return_magic.rs b/src/types/sonar_return_magic.rs index a83a6f7..82befd2 100644 --- a/src/types/sonar_return_magic.rs +++ b/src/types/sonar_return_magic.rs @@ -1,3 +1,4 @@ +use std::fmt::{Display, Formatter}; use binrw::meta::{EndianKind, ReadEndian, WriteEndian}; use binrw::{BinRead, BinResult, BinWrite, Endian, Error}; use std::io::{Error as IOError, ErrorKind::InvalidData, Read, Seek, Write}; @@ -6,8 +7,7 @@ use std::io::{Error as IOError, ErrorKind::InvalidData, Read, Seek, Write}; #[cfg_attr( target_family = "wasm", derive(tsify::Tsify, serde::Serialize, serde::Deserialize), - tsify(into_wasm_abi, from_wasm_abi), - serde(rename_all = "UPPERCASE") + tsify(into_wasm_abi, from_wasm_abi) )] pub enum SonarReturnMagic { IMX, @@ -23,6 +23,15 @@ impl SonarReturnMagic { } } +impl Display for SonarReturnMagic { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match *self { + Self::IMX => "IMX", + Self::IPX => "IPX" + }) + } +} + impl ReadEndian for SonarReturnMagic { const ENDIAN: EndianKind = EndianKind::None; } @@ -53,6 +62,7 @@ impl BinRead for SonarReturnMagic { } } + #[inline] fn read_options( reader: &mut R, _: Endian, @@ -78,6 +88,7 @@ impl BinWrite for SonarReturnMagic { Ok(()) } + #[inline] fn write_options( &self, writer: &mut W, @@ -87,3 +98,68 @@ impl BinWrite for SonarReturnMagic { self.write(writer) } } + +#[cfg(test)] +mod tests { + use std::io::Cursor; + use super::*; + + use test_log::test; + use log::info; + + #[test] + fn test_data_length() { + let cases = vec![ + (SonarReturnMagic::IMX, 250), + (SonarReturnMagic::IPX, 0) + ]; + + for (magic, want) in cases { + info!("Getting data length for {magic:?}, want {want:?}"); + let got = magic.data_length(); + assert_eq!(want, got); + } + } + + #[test] + fn test_display() { + let cases = vec![ + (SonarReturnMagic::IMX, "IMX"), + (SonarReturnMagic::IPX, "IPX") + ]; + + for (magic, want) in cases { + info!("Displaying {magic:?}, want {want:?}"); + let got = format!("{magic}"); + assert_eq!(want, got); + } + } + + const BINARY_ENDIAN: Endian = Endian::NATIVE; + const BINARY_CASES: [(SonarReturnMagic, &[u8;3]); 2] = [ + (SonarReturnMagic::IMX, b"IMX"), + (SonarReturnMagic::IPX, b"IPX") + ]; + + #[test] + fn test_parse() { + for (want, bytes) in BINARY_CASES { + info!("Parsing {bytes:?}, want {want:?}"); + let mut cursor = Cursor::new(bytes); + let got = SonarReturnMagic::read_options(&mut cursor, BINARY_ENDIAN, ()).expect("It should not return an error"); + assert_eq!(want, got); + } + } + + #[test] + fn test_write() { + for (magic, want) in BINARY_CASES { + info!("Writing {magic:?}, want {want:?}"); + let mut cursor = Cursor::new(Vec::new()); + magic.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); + } + } +}