get_palette() function and test

This commit is contained in:
Max Bradbury 2020-11-06 16:00:43 +00:00
parent b9415ade9e
commit de15ccbaa2
1 changed files with 23 additions and 1 deletions

View File

@ -680,6 +680,10 @@ impl Game {
new_unique_id(self.variable_ids())
}
pub fn get_palette(&self, id: &str) -> Option<&Palette> {
self.palettes.iter().find(|palette| palette.id == id)
}
/// todo refactor?
pub fn get_tile_id(&self, matching_tile: &Tile) -> Option<String> {
for tile in &self.tiles {
@ -858,7 +862,7 @@ impl Game {
#[cfg(test)]
mod test {
use crate::{TextDirection, Font, Version, Game, Tile, Image};
use crate::{TextDirection, Font, Version, Game, Tile, Image, Palette, Colour};
#[test]
fn game_from_string() {
@ -1086,4 +1090,22 @@ mod test {
assert_eq!(Game::from("".to_string() ).unwrap_err(), crate::error::NotFound::Anything);
assert_eq!(Game::from(" \n \r\n".to_string()).unwrap_err(), crate::error::NotFound::Anything);
}
#[test]
fn get_palette() {
let mut game = crate::mock::game_default();
let new_palette = Palette {
id: "1".to_string(),
name: Some("sadness".to_string()),
colours: vec![
Colour { red: 133, green: 131, blue: 111 },
Colour { red: 105, green: 93, blue: 104 },
Colour { red: 62, green: 74, blue: 76 },
]
};
game.add_palette(new_palette.clone());
assert_eq!(game.get_palette("0").unwrap(), &crate::mock::game_default().palettes[0]);
assert_eq!(game.get_palette("1").unwrap(), &new_palette);
assert_eq!(game.get_palette("2"), None);
}
}