avatar functions, tests and test resources

This commit is contained in:
Max Bradbury 2020-04-11 18:17:29 +01:00
parent 87a9ca5273
commit ee0ef8bbdf
2 changed files with 93 additions and 0 deletions

View File

@ -149,6 +149,39 @@ fn test_image_chequers_2() -> Image {
}
}
fn test_avatar() -> Avatar {
Avatar {
animation_frames: vec![
Image {
pixels: vec![
0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,
0,1,1,1,1,1,1,0,
1,1,1,0,1,1,1,0,
1,0,0,1,1,0,0,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,0,
]
},
Image {
pixels: vec![
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,
0,1,1,1,1,1,1,0,
1,1,1,0,1,1,1,0,
1,0,0,1,1,0,0,1,
1,1,1,1,1,1,1,1,
0,1,1,1,0,1,1,0,
]
},
],
room: "0".to_string(),
position: Position { x: 2, y: 5 }
}
}
fn test_sprite() -> Sprite {
Sprite {
id: "a".to_string(),
@ -514,6 +547,47 @@ fn test_position_to_string() {
)
}
fn avatar_from_string(string: String) -> Avatar {
let string = string.replace("SPR A\n", "");
let mut lines: Vec<&str> = string.lines().collect();
let room_pos = lines.pop().unwrap().replace("POS ", "");
let room_pos: Vec<&str> = room_pos.split_whitespace().collect();
let room = room_pos[0].to_string();
let position = position_from_string(room_pos[1].to_string());
let animation_frames: String = lines.join("\n");
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
image_from_string(frame.to_string())
}).collect();
Avatar { animation_frames, room, position }
}
#[test]
fn test_avatar_from_string() {
let output = avatar_from_string(
include_str!("../test/resources/avatar").to_string()
);
let expected = test_avatar();
assert_eq!(output, expected);
}
fn avatar_to_string(avatar: Avatar) -> String {
format!(
"SPR A\n{}\nPOS {} {}",
animation_frames_to_string(avatar.animation_frames),
avatar.room,
position_to_string(avatar.position)
)
}
#[test]
fn test_avatar_to_string() {
let output = avatar_to_string(test_avatar());
let expected = include_str!("../test/resources/avatar");
assert_eq!(output, expected);
}
fn sprite_from_string(string: String) -> Sprite {
let mut lines: Vec<&str> = string.split("\n").collect();

19
test/resources/avatar Normal file
View File

@ -0,0 +1,19 @@
SPR A
00000000
00111100
01111110
11101110
10011001
11111111
11111111
01111110
>
00000000
00000000
00111100
01111110
11101110
10011001
11111111
01110110
POS 0 2,5