diff --git a/src/main.rs b/src/main.rs index 97cf982..8bc07d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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(); diff --git a/test/resources/avatar b/test/resources/avatar new file mode 100644 index 0000000..970ff20 --- /dev/null +++ b/test/resources/avatar @@ -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 \ No newline at end of file