extends SceneTree ## Help's demo spreads: every demo builds and rolls cleanly, Help links to ## them, and opening one shows it unsaved with a way back. ## Run: godot --headless --path . -s res://tests/test_demos.gd -- --sample var _failures := 0 var main: Main func _init() -> void: if not OS.get_cmdline_user_args().has("--sample"): print("Run with -- --sample so the test can't touch saved data.") quit(2) return _test_builds() main = load("res://ui/main.tscn").instantiate() root.add_child(main) await _frames(5) await _test_open() print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) quit(1 if _failures else 0) func _test_builds() -> void: var rng := RandomNumberGenerator.new() for key: String in DemoSpreads.TITLES: var b := DemoSpreads.build(key) _check(not b.tables.is_empty() and b.id == "demo-" + key, "demo %s builds" % key) var start := b.tables[0] if b.tables[0].kind != RollTable.KIND_VARIABLE else b.tables[1] var tree := Roller.roll(b, start.id, rng) var bad := Roller.flatten(tree, []).filter(func(s: Dictionary) -> bool: return s.get("stop", false) and not s.get("deck_empty", false)) _check(bad.is_empty(), "demo %s rolls cleanly: %s" % [key, str(bad.map(func(s: Dictionary) -> String: return str(s.get("text", ""))))]) var book := DemoSpreads.build("book") _check(book.page_uses("p_sword") == 2, "the Book demo links two results to its page") var linked := book.get_table("goblin").entries[1] book.sync_page("p_sword") _check(linked.text.contains("Holy Light"), "and they show the page's text") var help := FileAccess.get_file_as_string("res://help/topics.txt") var re := RegEx.create_from_string("\\[demo\\]([a-z_]+)\\[/demo\\]") for m in re.search_all(help): _check(DemoSpreads.has(m.get_string(1)), "help's demo link %s exists" % m.get_string(1)) _check(HelpDialog._inline("[demo]deck[/demo]").contains("url=demo:deck"), "a demo marker becomes a link") func _test_open() -> void: var before := main.data.id main.demo.open("deck") await _frames(3) _check(main.data.id == "demo-deck" and main.demo.active() and not main._saving_enabled, "a demo opens unsaved, with its bar") main.demo.back() await _frames(3) _check(not main.demo.active() and main.data.id == before, "Back to my spread returns") func _frames(n: int = 2) -> void: for i in n: await process_frame func _check(ok: bool, what: String) -> void: if not ok: _failures += 1 print("FAIL ", what)