From aa24a5cad91e199e91fcc2d8324b1fdeaff8a32b Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Sat, 11 Jul 2020 21:30:05 +0100 Subject: [PATCH] Added unit tests for `Move`. --- src/game/card/property/moves.rs | 4 +++ src/game/card/property/moves/test.rs | 46 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/game/card/property/moves/test.rs diff --git a/src/game/card/property/moves.rs b/src/game/card/property/moves.rs index 763f688..6ca0447 100644 --- a/src/game/card/property/moves.rs +++ b/src/game/card/property/moves.rs @@ -1,5 +1,9 @@ #![doc(include = "move.md")] +// Modules +#[cfg(test)] +mod test; + // byteorder use byteorder::{ByteOrder, LittleEndian}; diff --git a/src/game/card/property/moves/test.rs b/src/game/card/property/moves/test.rs new file mode 100644 index 0000000..2aab5da --- /dev/null +++ b/src/game/card/property/moves/test.rs @@ -0,0 +1,46 @@ +// Unit tests + +// Lints +#![allow(clippy::panic)] // Unit tests are supposed to panic on error. + +// Imports +use super::*; + +#[test] +fn bytes() { + // Valid moves with no warnings + #[rustfmt::skip] + let valid_moves: &[(Move, ::ByteArray)] = &[( + Move { + name: ascii::AsciiString::from_ascii("Digimon").expect("Unable to convert string to ascii"), + power: LittleEndian::read_u16(&[1, 2]), + unknown: LittleEndian::read_u32(&[1, 2, 3, 4]), + }, + [ + // Power + 1, 2, + + // Unknown, + 1, 2, 3, 4, + + // Name + b'D', b'i', b'g', b'i', b'm', b'o', b'n', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', + b'\0', b'\0', + ], + )]; + + for (mov, move_bytes) in valid_moves { + // Check that we can create the move from bytes + assert_eq!(&Move::from_bytes(move_bytes).expect("Unable to convert move from bytes"), mov); + + // Make sure the validation succeeds + let validation = mov.validate(); + assert!(validation.successful()); + assert!(validation.warnings().is_empty()); + + // Then serialize it to bytes and make sure it's equal + let mut bytes = ::ByteArray::default(); + Move::to_bytes(mov, &mut bytes).expect("Unable to convert move to bytes"); + assert_eq!(&bytes, move_bytes); + } +}