TableTower/tests/test_sheets.gd

117 lines
4.7 KiB
GDScript3
Raw Normal View History

extends TestBase
## Headless checks for sheets (no UI): the file format round trip, boxes and
## tabs, the store (save, list, templates, new from template) and snapshot undo.
## Run: tools/run_tests.sh sheets
const ROOT := "user://test_sheets/"
func run() -> void:
_wipe(ROOT)
_test_round_trip()
_test_boxes()
_test_store()
_test_history()
_test_values()
_wipe(ROOT)
finish()
func _test_round_trip() -> void:
var doc := SheetDoc.new("brin", "Brin Hollowmere")
doc.tint = "#3a7bd5"
var box := SheetKinds.make_box(SheetKinds.NUMBER, doc.new_box_id(), Vector2(37, 9))
box.title = "Strength"
box.value = 14
box.var_name = "str"
box.config = {"min": 3}
doc.tabs[0]["boxes"].append(box)
doc.add_tab("Spells")
var back := SheetDoc.from_dict(JSON.parse_string(JSON.stringify(doc.to_dict())))
_eq(back.name, "Brin Hollowmere", "the name survives")
_eq(back.tint, "#3a7bd5", "the tint survives")
_eq(back.tabs.size(), 2, "both tabs survive")
var b2 := back.find_box(box.id)
_check(b2 != null and b2.title == "Strength" and int(b2.value) == 14 and b2.var_name == "str", "a box keeps its title, value and variable")
_eq(Vector2(b2.x, b2.y), Vector2(32, 16), "a new box snaps to the grid")
_eq(int(b2.config.get("min", 0)), 3, "a box keeps its settings")
func _test_boxes() -> void:
var doc := SheetDoc.new("t", "T")
var a := SheetKinds.make_box(SheetKinds.TEXT, doc.new_box_id(), Vector2.ZERO)
doc.tabs[0]["boxes"].append(a)
var b := SheetKinds.make_box(SheetKinds.HEADING, doc.new_box_id(), Vector2.ZERO)
doc.tabs[0]["boxes"].append(b)
_check(a.id != b.id, "boxes get different ids")
doc.remove_box(a.id)
_check(doc.find_box(a.id) == null and doc.find_box(b.id) != null, "removing a box keeps the others")
var t2 := doc.add_tab("Notes")
_check(t2["id"] != doc.tabs[0]["id"], "tabs get different ids")
func _test_store() -> void:
var store := SheetStore.new(ROOT)
var doc := store.create("Brin")
_eq(doc.id, "brin", "the file id comes from the name")
var box := SheetKinds.make_box(SheetKinds.NUMBER, doc.new_box_id(), Vector2.ZERO)
box.value = 9
doc.tabs[0]["boxes"].append(box)
store.save(doc)
_eq(store.create("Brin").id, "brin-2", "a second sheet with the same name gets its own id")
_eq(store.list().size(), 2, "the list shows both sheets")
var tpl_id := store.save_as_template(doc, "Knight")
_eq(store.list(true).size(), 1, "the template is listed apart")
var made := store.create("Sir Ada", tpl_id)
_eq(made.from_template, tpl_id, "a sheet remembers its template")
_eq(int(made.tabs[0]["boxes"][0].value), 9, "a sheet from a template starts with its boxes")
_eq(store.load_sheet(made.id).name, "Sir Ada", "it is saved")
store.delete("brin-2")
_eq(store.list().size(), 2, "deleting removes it from the list")
_check(store.load_sheet("../x") == null, "bad ids are refused")
func _test_history() -> void:
var hist := SnapshotHistory.new()
var doc := SheetDoc.new("h", "One")
hist.record(doc.to_dict(), "Rename")
doc.name = "Two"
_check(hist.can_undo() and hist.undo_label() == "Rename", "an edit can be undone")
doc = SheetDoc.from_dict(hist.undo(doc.to_dict()))
_eq(doc.name, "One", "undo puts it back")
doc = SheetDoc.from_dict(hist.redo(doc.to_dict()))
_eq(doc.name, "Two", "redo does it again")
hist.record(doc.to_dict(), "Again")
_check(not hist.can_redo(), "a new edit clears redo")
func _test_values() -> void:
var doc := SheetDemo.create()
var values := SheetValues.vars(doc)
_eq(values.get("str"), 14.0, "a number reads as its value")
_eq(values.get("str_mod"), 2, "a formula reads other boxes: floor((14 - 10) / 2)")
var talents := _box(doc, "Talents")
var p := SheetValues.pool(talents)
_check(p["total"] == 20.0 and p["spent"] == 18.0 and p["left"] == 2.0, "the pool takes each cost off: %s" % str(p))
(talents.value as Array).append({"Name": "Big", "Cost": 5})
_eq(SheetValues.pool(talents)["left"], -3.0, "going over the pool is allowed")
var purse := _box(doc, "Purse")
_check(is_equal_approx(SheetValues.currency_total(purse), 12.0 + 0.7 + 0.3), "currency adds up in the first coin")
var xp := _box(doc, "XP")
var lv := SheetValues.xp_level(xp)
_check(lv["level"] == 2 and lv["next"] == 4000.0, "XP 2600 is level 2, next at 4000: %s" % str(lv))
_eq(SheetValues.slots_left(_box(doc, "Spell slots")), 5, "spell slots left: 4 + 2 - 1 used")
var loop := _box(doc, "STR mod")
loop.config["expr"] = "@str_mod + 1"
_check(SheetValues.formula(doc, loop)["error"] != "", "a formula that uses itself gives an error, not a hang")
var rolled := SheetValues.evaluate(doc, "1d1 + @str")
_eq(rolled["value"], 15, "dice in a formula roll with sheet values")
func _box(doc: SheetDoc, title: String) -> SheetBox:
for tab in doc.tabs:
for b: SheetBox in tab["boxes"]:
if b.title == title:
return b
return null