diff --git a/src/lib.rs b/src/lib.rs index 7da5366..ab4d5d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,7 +101,7 @@ pub trait PSOPacket: std::fmt::Debug { #[cfg(test)] mod test { use super::*; - use psopacket::pso_packet; + use psopacket::{pso_packet, PSOPacketData}; #[test] fn test_basic_pso_packet() { @@ -161,7 +161,7 @@ mod test { #[test] fn test_custom_type_in_packet() { #[derive(Clone, Debug, PartialEq)] - struct MyType { + pub struct MyType { k: u8, j: u32, } @@ -318,4 +318,75 @@ mod test { let test2 = Test::from_bytes(&bytes); assert!(test2 == Err(PacketParseError::WrongPacketCommand)); } + + #[test] + fn test_derive_pso_data_packet() { + #[derive(PSOPacketData, Debug, PartialEq)] + struct Test { + a: u8, + b: u32, + c: u16, + } + + let test = Test { + a: 12, + b: 34567, + c: 890, + }; + + let mut bytes = test.as_bytes(); + assert!(bytes == vec![12, 7, 135, 0, 0, 122, 3]); + bytes[2] = 17; + let mut cur = std::io::Cursor::new(bytes.clone()); + let test2 = Test::from_bytes(&mut cur).unwrap(); + assert!(test2 == Test { + a: 12, + b: 4359, + c: 890 + }); + } + + #[test] + fn test_derive_pso_data_packet_not_enough_data() { + #[derive(PSOPacketData, Debug, PartialEq)] + struct Test { + a: u8, + b: u32, + c: u16, + } + + let test = Test { + a: 12, + b: 34567, + c: 890, + }; + + let mut bytes = test.as_bytes(); + bytes.extend(test.as_bytes()); + + let mut cur = std::io::Cursor::new(bytes.clone()); + let test2 = Test::from_bytes(&mut cur).unwrap(); + assert!(test2 == Test { + a: 12, + b: 34567, + c: 890, + }); + + let bytes2 = (0..14).collect::>(); + let mut cur = std::io::Cursor::new(bytes2); + let test3 = Test::from_bytes(&mut cur); + assert!(test3 == Ok(Test { + a: 0, + b: 67305985, + c: 1541, + })); + let test4 = Test::from_bytes(&mut cur); + assert!(test4 == Ok(Test { + a: 7, + b: 185207048, + c: 3340, + })); + let test5 = Test::from_bytes(&mut cur); + assert!(test5 == Err(PacketParseError::NotEnoughBytes)); + } }