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; ## My library (LoreLibrary) and item sheets of Lore Entries (SheetItems). ## 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() _test_links() _test_share_file() _test_lore_items() _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 ## The spread bridge: names with and without a prefix, what a roll may write, ## and locks. func _test_links() -> void: var doc := SheetDemo.create() var board := TowerBoard.new() var card := RollTable.create(RollTable.KIND_SHEET, "c1", "Brin") card.sheet_id = doc.id board.tables.append(card) var vars := {} var given := SheetLinks.inject(board, vars, func(_id: String) -> SheetDoc: return doc) _eq(vars["str"]["value"], 14, "a linked box becomes @str") _check(given.has("str_mod"), "formulas are lent too") card.sheet_prefix = "brin" vars = {} given = SheetLinks.inject(board, vars, func(_id: String) -> SheetDoc: return doc) _check(vars.has("brin_str") and not vars.has("str"), "a prefix makes it @brin_str") vars["brin_str"] = {"value": 16, "source": "roll"} var found := SheetLinks.changes(given, vars) _eq(found.size(), 1, "only the changed value is seen") var strength := doc.find_box(found[0]["box"]) _check(SheetLinks.write(strength, 16) and int(strength.value) == 16, "a number box takes the rolled value") strength.locked = true _check(not SheetLinks.write(strength, 3) and int(strength.value) == 16, "a locked box keeps its value") var mod: SheetBox = null for b: SheetBox in doc.tabs[0]["boxes"]: if b.var_name == "str_mod": mod = b _check(not SheetLinks.write(mod, 9), "a formula can't be written by a roll") var saved := TowerBoard.new() saved.load_dict(board.to_dict()) _check(saved.tables[0].sheet_id == doc.id and saved.tables[0].sheet_prefix == "brin", "a Sheet card keeps its sheet and prefix") var text := SheetText.summary(doc, "brin") _check(text.contains("[b]Strength:[/b] {@brin_str}"), "a roll shows linked boxes as {@prefix_name}") _check(text.contains("[b]Talents:[/b] Shield Wall, Keen Eye, Second Wind"), "and lists by their names") var tab := doc.tabs[0] var first: SheetBox = tab["boxes"][1] _check(SheetDoc.overlaps(tab, first.rect()), "a box's own place overlaps it") var spot := SheetDoc.free_spot(tab, first.rect(), SheetKinds.GRID) _check(not SheetDoc.overlaps(tab, Rect2(spot, first.rect().size)), "free_spot finds a place that covers nothing") func _test_share_file() -> void: var store := SheetStore.new(ROOT.path_join("mine")) var doc := store.create("Brin") doc.from_template = "fighter" var source := ROOT.path_join("portrait.png") Image.create(4, 4, false, Image.FORMAT_RGB8).save_png(source) var portrait := SheetKinds.make_box(SheetKinds.PICTURE, doc.new_box_id(), Vector2.ZERO) portrait.value = store.keep_picture(source) doc.tabs[0]["boxes"].append(portrait) var heading := SheetKinds.make_box(SheetKinds.HEADING, doc.new_box_id(), Vector2(0, 200)) heading.title = "Gear" doc.add_tab("Spells")["boxes"].append(heading) var text := SheetFile.share_text(doc) _check(text.contains("\"pictures\"") and not text.contains("fighter"), "a share file carries its pictures, not our template") var theirs := SheetStore.new(ROOT.path_join("theirs")) var got := SheetFile.import_text(theirs, text) _check(got != null and got.name == "Brin" and got.tabs.size() == 2 and got.from_template == "", "a share file imports with its tabs") var picture := str(got.find_box(portrait.id).value) _check(picture.begins_with(theirs.pictures_dir()), "the picture is written into the receiver's pictures") _eq(FileAccess.get_file_as_bytes(picture), FileAccess.get_file_as_bytes(source), "the picture arrives whole") _check(theirs.load_sheet(got.id) != null, "the imported sheet is saved") _eq(SheetFile.import_text(store, text).id, "brin-2", "importing beside the original gets a new id") _check(SheetFile.import_text(store, "{\"name\": 1}") == null, "other files are refused") var page := SheetExport.sheet_html(got, func(_doc: SheetDoc, line: String) -> String: return line) _eq(page.count("
"), 2, "the print page has one section per tab") _check(page.contains("

Spells

") and page.contains("Gear"), "it shows the tab names and boxes") ## My library survives a reload; a Lore Entry keeps its item sheet link in the ## spread file; a dropped copy gets its own copy of the sheet, a linked one doesn't. func _test_lore_items() -> void: var store := SheetStore.new(ROOT.path_join("lore")) var library := LoreLibrary.new(store.root) var blade := library.add_page("[b]Ashen Blade[/b]", "Burns what it cuts.") blade.tags.assign(["weapon"]) var doc := SheetItems.make_sheet(store, blade) library.save() _check(store.load_sheet(blade.sheet) != null and (doc.tabs[0]["boxes"][2] as SheetBox).title == "Charges", "an entry gets an item sheet with Damage, Charges and Notes") var back := LoreLibrary.new(store.root).get_page(blade.id) _check(back != null and back.sheet == doc.id and back.tags == blade.tags, "My library comes back from its file, with the sheet link") var board := TowerBoard.new() var page := board.add_page("Ashen Blade") page.sheet = doc.id var again := TowerBoard.new() again.load_dict(JsonIO.canonical(JSON.parse_string(board.to_json()))) _eq(again.get_page(page.id).sheet, doc.id, "a spread's Lore Entry saves its sheet link") var copied := SheetItems.with_links({"name": "Ashen Blade"}, blade, "lib:" + blade.id, false, store) _check(str(copied.get("sheet", "")) not in ["", doc.id] and not copied.has("lore"), "a copy gets its own item sheet") var linked := SheetItems.with_links({"name": "Ashen Blade"}, blade, "lib:" + blade.id, true, store) _check(linked["sheet"] == doc.id and linked["lore"] == "lib:" + blade.id, "a linked item keeps the entry and its sheet")