TableTower/tests/test_book.gd

83 lines
3.1 KiB
GDScript3
Raw Normal View History

extends SceneTree
## The Book: pages saved from results, linked results following a page's edits,
## dropping a page onto a result or a card, unlinking, saving and undo.
## Run: godot --headless --path . -s res://tests/test_book.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
main = load("res://ui/main.tscn").instantiate()
root.add_child(main)
await _frames(5)
main.set_mode(Main.Mode.BUILD)
await _frames()
await _test_book()
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
quit(1 if _failures else 0)
func _test_book() -> void:
var weapons := main.data.get_table("weapons")
var sword := weapons.entries[1]
var p := main.book.save_result(weapons, sword)
await _frames()
_check(main.book.visible and sword.page == p.id and p.text == sword.text, "Save to the Book makes a page and links the result")
main.book.editor.show_page(p)
p.text = "[b]The Sword of Holy Light[/b]"
p.details = "Glows near the undead."
main.book.editor._changed()
_check(sword.text == p.text and sword.details == p.details, "editing the page changes the linked result")
var loot := main.data.get_table("loot")
var target := loot.entries[0]
await _frames(2)
var row: Control = main.card_for("loot").rows[target.id]
_check(main.book.drop_page(p.id, row.get_global_rect().get_center()), "dropping a page on a result lands")
_check(target.page == p.id and target.text == p.text, "and links that result")
var n := loot.entries.size()
var card := main.card_for("loot")
main.book.drop_page(p.id, card.get_global_rect().position + Vector2(20, 12))
_check(loot.entries.size() == n + 1 and loot.entries[n].page == p.id, "dropping on the card's header adds a new linked result")
p.text = "Holy Sword"
main.book.editor._changed()
_check(target.text == "Holy Sword" and loot.entries[n].text == "Holy Sword", "one edit reaches every copy")
_check(main.data.page_uses(p.id) == 3, "the page counts its uses")
main.select_result("loot", target.id)
await _frames()
main.inspector.set_entry_text("A plain sword")
_check(target.page == "", "editing a result's own text unlinks it")
var d := main.data.to_dict()
var back := TowerBoard.new()
back.load_dict(d)
_check(back.book.size() == 1 and back.get_page(p.id).text == "Holy Sword", "the Book is saved in the spread")
main.book.list.clear()
main.book.refresh()
main.book._search.text = "nothing like it"
main.book.refresh()
_check(main.book.list.item_count == 0, "search filters pages")
main.book._search.text = ""
main.undo_redo._begin_edit()
main.data.remove_page(p.id)
main.mark_dirty()
main.undo_redo._end_edit()
_check(sword.page == "" and main.data.book.is_empty(), "deleting a page leaves its results as plain text")
main.undo_redo.undo()
await _frames()
_check(main.data.book.size() == 1, "undo brings the page back")
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)