From 32c2de695d554187484118a9df9840d0cc6d9255 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Fri, 11 Sep 2020 14:09:20 +0100 Subject: [PATCH] Revised some unit tests for `Move`. --- src/game/card/property/moves/test.rs | 44 +++++++++++++++++++++------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/game/card/property/moves/test.rs b/src/game/card/property/moves/test.rs index 875d56d..7a41227 100644 --- a/src/game/card/property/moves/test.rs +++ b/src/game/card/property/moves/test.rs @@ -8,8 +8,9 @@ use super::*; use crate::Validatable; #[test] -fn bytes() { +fn valid_bytes() { // Valid moves with no warnings + #[rustfmt::skip] let valid_moves: &[(Move, ::ByteArray)] = &[ ( Move { @@ -18,7 +19,10 @@ fn bytes() { unknown: LittleEndian::read_u32(&[1, 2, 3, 4]), }, [ - 4, 1, 1, 2, 3, 4, b'D', b'i', b'g', b'i', b'm', b'o', b'n', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 4, 1, // Power + 1, 2, 3, 4, // Unknown + b'D', b'i', b'g', b'i', b'm', b'o', b'n', 0, // Name + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ], ), ( @@ -28,24 +32,21 @@ fn bytes() { unknown: 0, }, [ - 0, 0, 0, 0, 0, 0, b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', - b'0', b'1', 0, + 0, 0, // Power + 0, 0, 0, 0, // Unknown + b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'0', // Name + b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'0', // ^^^^ + b'1', 0, // ^^^^ ], ), ]; for (mov, bytes) in valid_moves { - // Print the move and move bytes - println!("Move: {:?}", mov); - println!("Bytes: {:?}", bytes); - // Check that we can create the move from bytes assert_eq!(&Move::from_bytes(bytes).expect("Unable to convert move from bytes"), mov); // Make sure the validation succeeds let validation = mov.validate(); - println!("Errors: {:?}", validation.errors()); - println!("Warnings: {:?}", validation.warnings()); assert!(validation.successful()); assert!(validation.warnings().is_empty()); @@ -55,3 +56,26 @@ fn bytes() { assert_eq!(&mov_bytes, bytes); } } + +#[test] +fn invalid_bytes() { + // Valid moves with no warnings + #[rustfmt::skip] + let invalid_moves: &[(::ByteArray, FromBytesError)] = &[ + ( + [ + 0, 0, // Power + 0, 0, 0, 0, // Unknown + b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'0', // Name + b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'0', // ^^^^ + b'1', 1, // ^^^^ + ], + FromBytesError::Name(null_ascii_string::ReadError::NoNull), + ), + ]; + + for (bytes, err) in invalid_moves { + // Check that we can create the move from bytes + assert_eq!(Move::from_bytes(bytes), Err(*err)); + } +}