TableTower/tests/test_spread_links.gd

40 lines
2.3 KiB
GDScript3
Raw Normal View History

extends TestBase
## Rolling a card from another spread: [[Treasure::Loot]] rolls Loot in the
## Treasure spread (its own references resolve there), changes this spread's
## values, and a missing spread or card shows the missing marker.
## Run: tools/run_tests.sh spread_links
func run() -> void:
var treasure := TowerBoard.new()
treasure.id = "treasure"
treasure.name = "Treasure"
treasure.load_dict({"tables": [
{"id": "loot", "name": "Loot", "entries": [{"id": "l1", "text": "A [[Gem]] worth gold", "effects": "@gold += 10"}]},
{"id": "gem", "name": "Gem", "entries": [{"id": "g1", "text": "ruby"}]},
]})
var dungeon := TowerBoard.new()
dungeon.id = "dungeon"
dungeon.name = "Dungeon"
dungeon.load_dict({"tables": [
{"id": "room", "name": "Room", "entries": [{"id": "r1", "text": "A chest: [[Treasure::Loot]]"}]},
{"id": "lost", "name": "Lost", "entries": [{"id": "x1", "text": "[[Nowhere::Loot]] and [[Treasure::Nothing]]"}]},
]})
SpreadLinks.loader = func(key: String) -> TowerBoard:
return treasure if key.to_lower() in ["treasure", "treasure"] else null
_eq(Roller.resolve(dungeon, "Treasure::Loot"), "treasure::loot", "a link resolves to the other spread's card")
_check(Roller.ref_targets(dungeon, "[[Treasure::Loot]]").is_empty(), "links draw no reference line on this spread")
var ctx := Roller.new_context(dungeon, RandomNumberGenerator.new(), {"gold": {"value": 1}})
var tree: Dictionary = await Roller.roll_async(dungeon, "room", ctx)
_eq(Formatter.strip_tags(str(tree["summary"])), "A chest: A ruby worth gold", "the other spread's card rolls, with its own references")
_eq(int(ctx.lookup("@gold")), 11, "its changes land on this spread's values")
_check(ctx.board == dungeon, "the roll ends back on this spread")
var linked: Dictionary = (tree["children"] as Array)[0]
_check(linked.get("spread") == "Treasure" and ((linked["children"] as Array)[0] as Dictionary).get("spread") == "Treasure", "every node from there is marked with its spread")
var sync_tree := Roller.roll(dungeon, "room", RandomNumberGenerator.new())
_check(Formatter.strip_tags(str(sync_tree["summary"])).contains("ruby"), "headless rolls follow links too")
var lost := Roller.roll(dungeon, "lost", RandomNumberGenerator.new())
_check(str(lost["summary"]).contains("missing:"), "a missing spread or card shows the missing marker")
SpreadLinks.loader = Callable()
finish()