implement ToString for colour; test for palette to string

This commit is contained in:
Max Bradbury 2020-04-12 10:06:08 +01:00
parent e2e2f65913
commit 1221df2634
1 changed files with 24 additions and 6 deletions

View File

@ -594,14 +594,17 @@ fn test_colour_from_string() {
);
}
fn colour_to_string(colour: Colour) -> String {
format!("{},{},{}", colour.red, colour.green, colour.blue)
impl ToString for Colour {
#[inline]
fn to_string(&self) -> String {
format!("{},{},{}", self.red, self.green, self.blue)
}
}
#[test]
fn test_colour_to_string() {
assert_eq!(
colour_to_string(Colour { red: 22, green: 33, blue: 44 }),
Colour { red: 22, green: 33, blue: 44 }.to_string(),
"22,33,44".to_string()
);
}
@ -670,14 +673,29 @@ fn palette_to_string(palette: Palette) -> String {
"".to_string()
};
let colours: Vec<String> = palette.colours.into_iter().map(|colour| {
colour_to_string(colour)
}).collect();
let colours: Vec<String> = palette.colours.into_iter().map(|colour|
colour.to_string()
).collect();
let colours = colours.join("\n");
format!("PAL {}\n{}{}", palette.id, name, colours)
}
#[test]
fn test_palette_to_string() {
let output = palette_to_string(Palette {
id: "g".to_string(),
name: Some("moss".to_string()),
colours: vec![
Colour {red: 1, green: 2, blue: 3 },
Colour {red: 255, green: 254, blue: 253},
Colour {red: 126, green: 127, blue: 128},
]
});
let expected = "PAL g\nNAME moss\n1,2,3\n255,254,253\n126,127,128".to_string();
assert_eq!(output, expected);
}
fn position_from_string(string: String) -> Position {
// e.g. "2,5"
let xy: Vec<&str> = string.split(',').collect();