Revised some unit tests for Move.

This commit is contained in:
2020-09-11 14:09:20 +01:00
parent e85527fdbf
commit 32c2de695d

View File

@@ -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, <Move as Bytes>::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: &[(<Move as Bytes>::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));
}
}