image module; mocks module

This commit is contained in:
Max Bradbury 2020-04-12 12:48:07 +01:00
parent 92ab40e013
commit a3bfdc68b1
3 changed files with 144 additions and 135 deletions

67
src/image.rs Normal file
View File

@ -0,0 +1,67 @@
use crate::mocks;
#[derive(Debug, Eq, PartialEq)]
pub struct Image {
pub(crate) pixels: Vec<u8>, // 64 for SD, 256 for HD
}
impl From<String> for Image {
#[inline]
fn from(string: String) -> Image {
let string = string.replace("\n", "");
let pixels: Vec<&str> = string.split("").collect();
// the above seems to add an extra "" at the start and end of the vec, so strip them below
let pixels = &pixels[1..(pixels.len() - 1)];
let pixels: Vec<u8> = pixels.iter().map(|&pixel| {
pixel.parse::<u8>().unwrap()
}).collect();
Image { pixels }
}
}
impl ToString for Image {
#[inline]
fn to_string(&self) -> String {
let mut string = String::new();
let sqrt = (self.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD
for line in self.pixels.chunks(sqrt) {
for pixel in line {
string.push_str(&format!("{}", *pixel));
}
string.push('\n');
}
string.pop(); // remove trailing newline
string
}
}
#[test]
fn test_image_from_string() {
let output = Image::from(include_str!("../test/resources/image").to_string());
let expected = Image {
pixels: vec![
1,1,1,1,1,1,1,1,
1,1,0,0,1,1,1,1,
1,0,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
]
};
assert_eq!(output, expected)
}
#[test]
fn test_image_to_string() {
let output = mocks::image::chequers_1().to_string();
let expected = include_str!("../test/resources/image-chequers-1").to_string();
assert_eq!(output, expected);
}

View File

@ -1,13 +1,11 @@
pub mod colour;
pub mod palette;
pub mod image;
pub mod mocks;
use colour::Colour;
use palette::Palette;
#[derive(Debug, Eq, PartialEq)]
struct Image {
pixels: Vec<u8>, // 64 for SD, 256 for HD
}
use image::Image;
#[derive(Debug, Eq, PartialEq)]
struct Tile {
@ -53,7 +51,7 @@ struct Sprite {
/// avatar is a "sprite" in the game data but with a specific id
#[derive(Debug, Eq, PartialEq)]
struct Avatar {
pub struct Avatar {
animation_frames: Vec<Image>,
room: String, /// room id
position: Position,
@ -114,69 +112,6 @@ struct Game {
variables: Vec<Variable>,
}
fn example_image_chequers_1() -> Image {
Image {
pixels: vec![
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
]
}
}
fn example_image_chequers_2() -> Image {
Image {
pixels: vec![
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
]
}
}
fn example_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 example_sprite() -> Sprite {
Sprite {
id: "a".to_string(),
@ -416,67 +351,6 @@ fn example_game_default() -> Game {
}
}
impl From<String> for Image {
#[inline]
fn from(string: String) -> Image {
let string = string.replace("\n", "");
let pixels: Vec<&str> = string.split("").collect();
// the above seems to add an extra "" at the start and end of the vec, so strip them below
let pixels = &pixels[1..(pixels.len() - 1)];
let pixels: Vec<u8> = pixels.iter().map(|&pixel| {
pixel.parse::<u8>().unwrap()
}).collect();
Image { pixels }
}
}
#[test]
fn test_image_from_string() {
let output = Image::from(include_str!("../test/resources/image").to_string());
let expected = Image {
pixels: vec![
1,1,1,1,1,1,1,1,
1,1,0,0,1,1,1,1,
1,0,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
]
};
assert_eq!(output, expected)
}
impl ToString for Image {
#[inline]
fn to_string(&self) -> String {
let mut string = String::new();
let sqrt = (self.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD
for line in self.pixels.chunks(sqrt) {
for pixel in line {
string.push_str(&format!("{}", *pixel));
}
string.push('\n');
}
string.pop(); // remove trailing newline
string
}
}
#[test]
fn test_image_to_string() {
let output = example_image_chequers_1().to_string();
let expected = include_str!("../test/resources/image-chequers-1").to_string();
assert_eq!(output, expected);
}
pub trait AnimationFrames {
fn to_string(&self) -> String;
}
@ -571,8 +445,8 @@ fn test_tile_to_string() {
name: Some("chequers".to_string()),
wall: false,
animation_frames: vec![
example_image_chequers_1(),
example_image_chequers_2(),
mocks::image::chequers_1(),
mocks::image::chequers_2(),
]
}.to_string();
@ -633,8 +507,7 @@ fn test_avatar_from_string() {
include_str!("../test/resources/avatar").to_string()
);
let expected = example_avatar();
assert_eq!(output, expected);
assert_eq!(output, mocks::avatar());
}
impl ToString for Avatar {
@ -651,7 +524,7 @@ impl ToString for Avatar {
#[test]
fn test_avatar_to_string() {
assert_eq!(example_avatar().to_string(), include_str!("../test/resources/avatar"));
assert_eq!(mocks::avatar().to_string(), include_str!("../test/resources/avatar"));
}
impl From<String> for Sprite {

69
src/mocks.rs Normal file
View File

@ -0,0 +1,69 @@
use crate::{Avatar, Position};
use crate::image::Image;
pub mod image {
use crate::image::Image;
pub(crate) fn chequers_1() -> Image {
Image {
pixels: vec![
1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1,
]
}
}
pub fn chequers_2() -> Image {
Image {
pixels: vec![
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
]
}
}
}
pub fn 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 }
}
}