object tags

This commit is contained in:
2026-06-07 01:26:18 -05:00
parent 9c2212520c
commit a8d2cb8303
8 changed files with 240 additions and 6 deletions
@@ -51,6 +51,83 @@ bg = "#000000"
assert_eq!(obj2.glyph.bg, obj.glyph.bg);
}
#[test]
fn object_tags_round_trip_through_toml() {
// Tags declared in the map file must survive a save→load cycle, sorted.
let toml = r##"
[map]
name = "Test"
width = 3
height = 3
player_start = [0, 0]
[palette]
"." = { archetype = "empty", tile = 46, fg = "#000000", bg = "#000000" }
[grid]
content = """
...
...
...
"""
[[objects]]
x = 1
y = 1
tile = 64
fg = "#ffffff"
bg = "#000000"
tags = ["enemy", "boss"]
"##;
let mf: MapFile = toml::from_str(toml).unwrap();
let board = Board::try_from(mf).unwrap();
let obj = &board.objects[&1];
assert!(obj.tags.contains("enemy"));
assert!(obj.tags.contains("boss"));
assert_eq!(obj.tags.len(), 2);
// Save and reload; tags must survive, and be sorted alphabetically.
let toml_out = toml::to_string_pretty(&MapFile::from(&board)).unwrap();
// Both tags must appear; "boss" must come before "enemy" (sorted).
let boss_pos = toml_out.find("\"boss\"").expect("boss in TOML");
let enemy_pos = toml_out.find("\"enemy\"").expect("enemy in TOML");
assert!(boss_pos < enemy_pos, "tags must be sorted: boss before enemy");
let mf2: MapFile = toml::from_str(&toml_out).unwrap();
let board2 = Board::try_from(mf2).unwrap();
let obj2 = &board2.objects[&1];
assert_eq!(obj2.tags, obj.tags);
}
#[test]
fn object_empty_tags_omitted_from_toml() {
// An object with no tags must not emit a `tags` key in TOML.
let toml = r##"
[map]
name = "Test"
width = 1
height = 1
player_start = [0, 0]
[palette]
"." = { archetype = "empty", tile = 46, fg = "#000000", bg = "#000000" }
[grid]
content = """
.
"""
[[objects]]
x = 0
y = 0
tile = 64
fg = "#ffffff"
bg = "#000000"
"##;
let board = load_board(toml);
let toml_out = toml::to_string_pretty(&MapFile::from(&board)).unwrap();
assert!(!toml_out.contains("tags"), "empty tags must not appear in TOML output");
}
#[test]
fn floor_spec_round_trips_through_toml() {
// A 2×2 board with a grid floor (a generator and a fixed glyph) must survive