TableTower/tests/test_book.gd

130 lines
5.3 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()
_test_pictures()
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 _test_pictures() -> void:
var img := Image.create(900, 600, false, Image.FORMAT_RGBA8)
img.fill(Color.MEDIUM_PURPLE)
var r := BoardImages.from_image(img)
_check(r.has("id") and not r.has("error"), "a picture is stored")
main.data.images[r["id"]] = r["data"]
var tex := main.data.picture(r["id"])
_check(tex != null and maxi(tex.get_width(), tex.get_height()) == BoardImages.MAX_SIDE, "and shrunk to %d px" % BoardImages.MAX_SIDE)
main.data.images["img_unused"] = r["data"]
var e := main.data.get_table("weapons").entries[0]
e.image = r["id"]
var d := main.data.to_dict()
_check((d.get("images", {}) as Dictionary).has(r["id"]) and not (d["images"] as Dictionary).has("img_unused"), "only used pictures are saved")
var back := TowerBoard.new()
back.load_dict(d)
_check(back.picture(back.get_table("weapons").entries[0].image) != null, "pictures load with the spread")
var p := main.data.add_page("Pic page")
p.image = r["id"]
var e2 := main.data.get_table("weapons").entries[2]
main.data.link_page(e2, p.id)
_check(e2.image == r["id"], "a page's picture comes with it")
# Everything new survives an export and import of the spread file.
var t := main.data.get_table("weapons")
t.color = "#2f8a4f"
t.width = 300
t.roll_floor = "2"
e2.cost_value = "@gold"
e2.cost_amount = "3"
e2.add_pipe("loot")
e2.pipe_combine["loot"] = "max"
e2.pipe_pause["loot"] = true
e2.pipe_color["loot"] = "#c04a86"
var g := main.data.add_group("Outer", [])
var g2 := main.data.add_group("Inner", [])
g2.parent = g.id
var canon := JsonIO.canonical(JSON.parse_string(JSON.stringify(main.data.to_dict())))
var b2 := TowerBoard.new()
b2.load_dict(canon)
var t2 := b2.get_table("weapons")
var e3 := t2.entries[2]
_check(t2.color == "#2f8a4f" and t2.width == 300.0 and t2.roll_floor == "2", "card colour, size and limits import")
_check(e3.page == p.id and e3.image == r["id"] and e3.cost_amount == "3", "page links, pictures and costs import")
_check(e3.combine_for("loot") == "max" and e3.pipe_pause.has("loot") and e3.pipe_color.get("loot") == "#c04a86", "thread options import")
_check(b2.get_group(g2.id).parent == g.id and b2.book.size() == main.data.book.size() and b2.images.has(r["id"]), "nesting, the Book and pictures import")
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)