Browse Source

Separate out tests

Kevin Lee 1 year ago
parent
commit
f864393059
7 changed files with 126 additions and 96 deletions
  1. 8 0
      Cargo.toml
  2. 2 39
      src/ezcad/basic_field.rs
  3. 21 55
      src/ezcad/field_of.rs
  4. 3 0
      src/lib.rs
  5. 0 2
      src/main.rs
  6. 36 0
      tests/basic_field.rs
  7. 56 0
      tests/field_of.rs

+ 8 - 0
Cargo.toml

@@ -3,6 +3,14 @@ name = "ezcad_patcher"
 version = "0.1.0"
 edition = "2021"
 
+[lib]
+name ="ezcad"
+path = "src/lib.rs"
+
+[[bin]]
+name = "ezcad_patcher"
+path = "src/main.rs"
+
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]

+ 2 - 39
src/ezcad/basic_field.rs

@@ -1,7 +1,8 @@
 use binrw::{BinRead, BinWrite};
 
+/// Wrapper for basic types determined by initial u32 size
 #[derive(BinRead, BinWrite, PartialEq, Debug)]
-enum BasicField {
+pub enum BasicField {
     #[brw(magic(2u32))]
     U16(u16),
     #[brw(magic(4u32))]
@@ -9,41 +10,3 @@ enum BasicField {
     #[brw(magic(8u32))]
     Double(f64),
 }
-
-#[cfg(test)]
-mod tests {
-    use super::*;
-    use std::io::Cursor;
-
-    #[test]
-    fn field_u32_be() {
-        let encoded: Vec<u8> = vec![0x00, 0x00, 0x00, 0x04, 0x0A, 0x0B, 0x0C, 0x0D];
-        let decoded: u32 = 0x0A0B0C0D;
-
-        let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
-        assert_eq!(
-            BasicField::read_be(&mut stream).unwrap(),
-            BasicField::U32(decoded)
-        );
-
-        let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
-        BasicField::U32(decoded).write_be(&mut buffer).unwrap();
-        assert_eq!(buffer.into_inner(), encoded);
-    }
-
-    #[test]
-    fn field_u32_le() {
-        let encoded: Vec<u8> = vec![0x04, 0x00, 0x00, 0x00, 0x0D, 0x0C, 0x0B, 0x0A];
-        let decoded: u32 = 0x0A0B0C0D;
-
-        let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
-        assert_eq!(
-            BasicField::read_le(&mut stream).unwrap(),
-            BasicField::U32(decoded)
-        );
-
-        let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
-        BasicField::U32(decoded).write_le(&mut buffer).unwrap();
-        assert_eq!(buffer.into_inner(), encoded);
-    }
-}

+ 21 - 55
src/ezcad/field_of.rs

@@ -1,75 +1,41 @@
 use binrw::{binread, binwrite, BinRead, BinWrite};
-use std::mem::size_of;
+use std::{mem::size_of, ops::{DerefMut, Deref}};
 
+/// Wrapper to prefix arbitrary structs with size of struct as u32
 #[binwrite]
 #[binread]
 #[derive(Debug, PartialEq)]
-struct FieldOf<T>
+pub struct FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()> + Default,
     T: for<'a> BinWrite<Args<'a> = ()> + Default,
 {
+    // Temporary variable that must match size of value
     #[br(temp, assert(size == size_of::<T>().try_into().unwrap()))]
     #[bw(try_calc(u32::try_from(size_of::<T>())))]
     size: u32,
+
     pub value: T,
 }
 
-#[cfg(test)]
-mod tests {
-    use std::io::Cursor;
-
-    use super::*;
-
-    #[test]
-    fn field_of_be() {
-        #[derive(BinRead, BinWrite, Debug, PartialEq, Default)]
-        struct Test {
-            a: u32,
-            b: u32,
-        }
-
-        let decoded: FieldOf<Test> = FieldOf {
-            value: Test {
-                a: 0x0A0B0C0D,
-                b: 0x01020304,
-            },
-        };
-        let encoded: Vec<u8> = vec![
-            0x00, 0x00, 0x00, 0x08, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
-        ];
-
-        let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
-        assert_eq!(FieldOf::<Test>::read_be(&mut stream).unwrap(), decoded);
+impl<T> Deref for FieldOf<T> 
+where
+    T: for<'a> BinRead<Args<'a> = ()> + Default,
+    T: for<'a> BinWrite<Args<'a> = ()> + Default,
+{
+    type Target = T;
 
-        let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
-        decoded.write_be(&mut buffer).unwrap();
-        assert_eq!(buffer.into_inner(), encoded);
+    fn deref(&self) -> &Self::Target {
+        &self.value
     }
+}
 
-    #[test]
-    fn field_of_le() {
-        #[derive(BinRead, BinWrite, Debug, PartialEq, Default)]
-        struct Test {
-            a: u32,
-            b: u32,
-        }
-
-        let decoded: FieldOf<Test> = FieldOf {
-            value: Test {
-                a: 0x0A0B0C0D,
-                b: 0x01020304,
-            },
-        };
-        let encoded: Vec<u8> = vec![
-            0x08, 0x00, 0x00, 0x00, 0x0D, 0x0C, 0x0B, 0x0A, 0x04, 0x03, 0x02, 0x01,
-        ];
-
-        let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
-        assert_eq!(FieldOf::<Test>::read_le(&mut stream).unwrap(), decoded);
-
-        let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
-        decoded.write_le(&mut buffer).unwrap();
-        assert_eq!(buffer.into_inner(), encoded);
+impl<T> DerefMut for FieldOf<T>
+where
+    T: for<'a> BinRead<Args<'a> = ()> + Default,
+    T: for<'a> BinWrite<Args<'a> = ()> + Default,
+{
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.value
     }
 }

+ 3 - 0
src/lib.rs

@@ -0,0 +1,3 @@
+pub mod ezcad;
+
+pub use ezcad::*;

+ 0 - 2
src/main.rs

@@ -2,8 +2,6 @@ use std::{path::PathBuf, fs::read};
 
 use clap::Parser;
 
-mod ezcad;
-
 #[derive(Debug, Parser)]
 struct Cli {
     /// Input file to parse

+ 36 - 0
tests/basic_field.rs

@@ -0,0 +1,36 @@
+use std::io::Cursor;
+
+use binrw::{BinRead, BinWrite};
+use ezcad::basic_field::BasicField;
+
+#[test]
+fn field_u32_be() {
+    let encoded: Vec<u8> = vec![0x00, 0x00, 0x00, 0x04, 0x0A, 0x0B, 0x0C, 0x0D];
+    let decoded: u32 = 0x0A0B0C0D;
+
+    let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
+    assert_eq!(
+        BasicField::read_be(&mut stream).unwrap(),
+        BasicField::U32(decoded)
+    );
+
+    let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
+    BasicField::U32(decoded).write_be(&mut buffer).unwrap();
+    assert_eq!(buffer.into_inner(), encoded);
+}
+
+#[test]
+fn field_u32_le() {
+    let encoded: Vec<u8> = vec![0x04, 0x00, 0x00, 0x00, 0x0D, 0x0C, 0x0B, 0x0A];
+    let decoded: u32 = 0x0A0B0C0D;
+
+    let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
+    assert_eq!(
+        BasicField::read_le(&mut stream).unwrap(),
+        BasicField::U32(decoded)
+    );
+
+    let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
+    BasicField::U32(decoded).write_le(&mut buffer).unwrap();
+    assert_eq!(buffer.into_inner(), encoded);
+}

+ 56 - 0
tests/field_of.rs

@@ -0,0 +1,56 @@
+use std::io::Cursor;
+
+use binrw::{BinRead, BinWrite};
+use ezcad::field_of::FieldOf;
+
+#[test]
+fn field_of_be() {
+    #[derive(BinRead, BinWrite, Debug, PartialEq, Default)]
+    struct Test {
+        a: u32,
+        b: u32,
+    }
+
+    let decoded: FieldOf<Test> = FieldOf {
+        value: Test {
+            a: 0x0A0B0C0D,
+            b: 0x01020304,
+        },
+    };
+    let encoded: Vec<u8> = vec![
+        0x00, 0x00, 0x00, 0x08, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
+    ];
+
+    let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
+    assert_eq!(FieldOf::<Test>::read_be(&mut stream).unwrap(), decoded);
+
+    let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
+    decoded.write_be(&mut buffer).unwrap();
+    assert_eq!(buffer.into_inner(), encoded);
+}
+
+#[test]
+fn field_of_le() {
+    #[derive(BinRead, BinWrite, Debug, PartialEq, Default)]
+    struct Test {
+        a: u32,
+        b: u32,
+    }
+
+    let decoded: FieldOf<Test> = FieldOf {
+        value: Test {
+            a: 0x0A0B0C0D,
+            b: 0x01020304,
+        },
+    };
+    let encoded: Vec<u8> = vec![
+        0x08, 0x00, 0x00, 0x00, 0x0D, 0x0C, 0x0B, 0x0A, 0x04, 0x03, 0x02, 0x01,
+    ];
+
+    let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
+    assert_eq!(FieldOf::<Test>::read_le(&mut stream).unwrap(), decoded);
+
+    let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
+    decoded.write_le(&mut buffer).unwrap();
+    assert_eq!(buffer.into_inner(), encoded);
+}