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() _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")