fix some false assumptions about tile IDs (0 is reserved!)
This commit is contained in:
parent
62f488c24e
commit
b88f69af17
21
src/game.rs
21
src/game.rs
|
@ -360,7 +360,11 @@ impl Game {
|
|||
/// if current tile IDs are [0, 1, 2] the result will be `3`
|
||||
#[inline]
|
||||
pub fn new_tile_id(&self) -> String {
|
||||
new_unique_id(self.tile_ids())
|
||||
// don't allow `0` - this is a reserved ID
|
||||
// todo new_sprite_id will need to avoid A too
|
||||
let mut ids = self.tile_ids();
|
||||
ids.push("0".to_string());
|
||||
new_unique_id(ids)
|
||||
}
|
||||
|
||||
/// adds a tile safely and returns the new tile ID
|
||||
|
@ -456,13 +460,16 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn test_new_tile_id() {
|
||||
// default tile has an id of 10 ("a"), so 0 is available
|
||||
assert_eq!(crate::mock::game_default().new_tile_id(), "0".to_string());
|
||||
// default tile has an id of 10 ("a"), and 0 is reserved
|
||||
assert_eq!(crate::mock::game_default().new_tile_id(), "1".to_string());
|
||||
|
||||
// for a game with a gap in the tile IDs, check the gap is used
|
||||
|
||||
let mut game = crate::mock::game_default();
|
||||
let mut tiles: Vec<Tile> = Vec::new();
|
||||
|
||||
for n in 0..9 {
|
||||
// 0 is reserved; upper bound is non-inclusive
|
||||
for n in 1..10 {
|
||||
if n != 4 {
|
||||
let mut new_tile = crate::mock::tile_default();
|
||||
new_tile.id = format!("{}", n).to_string();
|
||||
|
@ -474,7 +481,7 @@ mod test {
|
|||
|
||||
assert_eq!(game.new_tile_id(), "4".to_string());
|
||||
|
||||
// fill in the space created above, and test that tile IDs get sorted
|
||||
// fill in the space created above, then test that tile IDs get sorted
|
||||
|
||||
let mut new_tile = crate::mock::tile_default();
|
||||
new_tile.id = "4".to_string();
|
||||
|
@ -487,10 +494,10 @@ mod test {
|
|||
fn test_add_tile() {
|
||||
let mut game = crate::mock::game_default();
|
||||
let new_id = game.add_tile(crate::mock::tile_default());
|
||||
assert_eq!(new_id, "0".to_string());
|
||||
assert_eq!(new_id, "1".to_string());
|
||||
assert_eq!(game.tiles.len(), 2);
|
||||
let new_id = game.add_tile(crate::mock::tile_default());
|
||||
assert_eq!(new_id, "1".to_string());
|
||||
assert_eq!(new_id, "2".to_string());
|
||||
assert_eq!(game.tiles.len(), 3);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue