extends TestBase ## Cards with columns on the spread and in the inspector: the header and the ## cells, the lit cells after a roll, inline cell editing, and the inspector's ## Columns section (add, rename, move, remove, roll mode, one phrase, save as, ## only if) with the result's cell fields and roll twice and combine, all undoable. ## Run: godot --headless --path . -s res://tests/test_ui_columns.gd -- --sample var insp: Inspector func _init() -> void: if not await start_main(): return main.set_anchors_and_offsets_preset(Control.PRESET_TOP_LEFT) main.size = Vector2(1440, 900) insp = main.inspector await _load() await _test_card() await _test_lit_cells() await _test_real_roll() await _test_build_cells() await _test_inspector_columns() await _test_result_cells() await _test_join_and_repeat() finish() func _board_dict() -> Dictionary: return {"format_version": 2, "id": "cols-test", "name": "Columns test", "tables": [ {"id": "world", "name": "World", "x": 40, "y": 40, "column_rolls": "each", "columns": [{"name": "Type"}, {"name": "Resources", "save_as": "@res"}, {"name": "Water"}, {"name": "Life", "when": "@res == \"Rich\""}], "entries": [ {"id": "e1", "text": "Rocky", "cells": ["Poor", "None", "No"]}, {"id": "e2", "text": "Ocean", "cells": ["Fair", "Plentiful", "Yes"]}, {"id": "e3", "text": "Garden", "cells": ["Rich", "Some", "Yes"]}]}, {"id": "rowcard", "name": "Row card", "x": 700, "y": 40, "columns": [{"name": "Name"}, {"name": "Trait"}], "entries": [{"id": "e1", "text": "Ana", "cells": ["Bold"]}]}, {"id": "plain", "name": "Plain", "x": 40, "y": 400, "entries": [{"id": "e1", "text": "One"}, {"id": "e2", "text": "Two"}]}, ]} func _load() -> void: main.data.load_dict(_board_dict()) main.lit_entries.clear() main.lit_pipes.clear() main.lit_texts.clear() main.lit_columns.clear() main.persistent_vars.clear() main.set_mode(Main.Mode.PLAY) main._rebuild_cards() main.clear_inspector() await _frames(4) func _t(id: String) -> RollTable: return main.data.get_table(id) func _cells(tid: String, eid: String) -> Array: return main.card_for(tid).rows_ui.columns_ui.cells.get(eid, []) func _header_names(card: TableCard) -> Array: var out: Array = [] for c in card.find_children("*", "MarginContainer", true, false): if c.has_meta("columns_header"): for l in c.find_children("*", "Label", true, false): out.append((l as Label).text) return out # ---------- The card ---------- func _test_card() -> void: print("Card") var card := main.card_for("world") _eq(_header_names(card), ["Type", "Resources", "Water", "Life"], "the header names every column") _eq(_header_names(main.card_for("plain")), [], "a plain card has no header") var cs := _cells("world", "e2") _eq(cs.size(), 3, "a row shows a cell per extra column") _eq((cs[1]["label"] as RichTextLabel).text, "Plentiful", "a cell shows its text") _check(card.card_width() > main.card_for("plain").card_width() + 3 * 100.0, "a card with columns is wider") var text: RichTextLabel = card.rows_ui.parts["e2"]["text"] _eq(text.text, "Ocean", "the row's own text is column 0") func _test_lit_cells() -> void: print("Lit cells") var card := main.card_for("world") main.lit_entries["world:e1"] = true main.lit_texts["world:e1"] = "Type: Rocky · Resources: Rich · Water: Plentiful" main.lit_columns["world:e1"] = [{"name": "Type", "text": "Rocky", "index": 0}, {"name": "Resources", "text": "Rich", "index": 2}, {"name": "Water", "text": "Plentiful", "index": 1}, {"name": "Life", "text": "", "index": 0, "skipped": true}] card.refresh_lit() var text: RichTextLabel = card.rows_ui.parts["e1"]["text"] _eq(text.text, "Rocky", "the lit row shows column 0, not the whole summary") var rich: Dictionary = _cells("world", "e3")[0] _check(bool((rich["box"] as Control).get_meta("lit", false)), "each column lights the cell it landed on") _eq((rich["label"] as RichTextLabel).text, "Rich", "with the rolled text") _check(bool((_cells("world", "e2")[1]["box"] as Control).get_meta("lit", false)), "a second column lights another row") _check(not bool((_cells("world", "e1")[0]["box"] as Control).get_meta("lit", false)), "the lit row's own cell for that column isn't lit") _check(not bool((_cells("world", "e1")[2]["box"] as Control).get_meta("lit", false)), "a skipped column lights nothing") main.lit_entries.clear() main.lit_texts.clear() main.lit_columns.clear() card.refresh_lit() _check(not bool((rich["box"] as Control).get_meta("lit", false)), "clearing the roll unlights the cells") func _test_real_roll() -> void: print("A real roll") main.rolling.start_roll("rowcard") for i in 200: if main.current.get("done", false): break await _frames() _check(main.is_entry_lit("rowcard", "e1"), "the row lights") var card := main.card_for("rowcard") _eq((card.rows_ui.parts["e1"]["text"] as RichTextLabel).text, "Ana", "the row shows its own text") _eq((_cells("rowcard", "e1")[0]["label"] as RichTextLabel).text, "Bold", "and its cell") _eq(main.lit_text("rowcard", "e1"), "Name: Ana · Trait: Bold", "(the summary names the columns)") func _test_build_cells() -> void: print("Build cells") main.set_mode(Main.Mode.BUILD) await _frames(2) var card := main.card_for("world") _check(card.card_width() > TableCard.BUILD_WIDTH + 3 * 100.0, "Build keeps the room for the columns") var cs := _cells("world", "e1") var le: LineEdit = cs[1]["edit"] _eq(le.text, "None", "Build shows a field per cell") main.undo_redo._commit_edits() le.text = "A little" le.text_changed.emit("A little") main.undo_redo._commit_edits() _eq(_t("world").find_entry("e1").cells[1], "A little", "typing in a cell edits it") main.undo_redo.undo() await _frames(2) _eq(_t("world").find_entry("e1").cells[1], "None", "undo puts the cell back") func _test_inspector_columns() -> void: print("Inspector columns") var t := _t("plain") main.select_card("plain") await _frames(2) main.undo_redo._commit_edits() _press("+ Add columns") _eq(t.column_names(), ["Result", "Column 2"] as Array[String], "Add columns makes Result and one more") _eq(t.entries[0].cells, ["" ] as Array[String], "every result gets a cell") _press("+ Add column") _eq(t.columns.size(), 3, "+ Add column") _eq(t.entries[1].cells.size(), 2, "the cells keep in step") var nm: LineEdit = insp.fields["column_name_1"] nm.text = "Water" nm.text_changed.emit("Water") _eq(t.column_names()[1], "Water", "renaming a column") _eq(_header_names(main.card_for("plain"))[1], "Water", "the card's header follows") t.entries[0].cells.assign(["wet", "big"]) (insp.fields["column_name_2"] as LineEdit).text_changed.emit("Size") InspectorColumns.move_column(t, 2, -1) insp.wrote(t, false, true) _eq(t.column_names(), ["Result", "Size", "Water"] as Array[String], "moving a column") _eq(t.entries[0].cells, ["big", "wet"] as Array[String], "moves its cells too") InspectorColumns.remove_column(t, 1) insp.wrote(t, false, true) _eq(t.column_names(), ["Result", "Water"] as Array[String], "removing a column") _eq(t.entries[0].cells, ["wet"] as Array[String], "removes its cell from every result") var mode: OptionButton = insp.fields["column_rolls"] mode.item_selected.emit(1) _eq(t.column_rolls, RollTable.COLUMNS_EACH, "Roll each column separately") (insp.fields["column_save_1"] as ValuePicker).choose("@water") _eq(str(t.columns[1].get("save_as", "")), "@water", "Save as picks a value") main.undo_redo._commit_edits() var cond: ConditionBuilder = insp.fields["column_when_1"] cond.changed.emit("@luck >= 2") _eq(str(t.columns[1].get("when", "")), "@luck >= 2", "Only if sets the column's condition") main.undo_redo._commit_edits() main.undo_redo.undo() await _frames(2) # Quick edits to one card merge into one step (MainHistory), so undo may go further back. _check(not JSON.stringify(_t("plain").to_dict()).contains("@luck"), "undo takes the condition back") for i in 12: main.undo_redo.undo() await _frames() if _t("plain").columns.is_empty(): break _check(_t("plain").columns.is_empty() and _t("plain").entries[0].cells.is_empty(), "undo goes back to a plain card") main.select_card("plain") await _frames(2) _press("+ Add columns") InspectorColumns.remove_column(_t("plain"), 1) _check(_t("plain").columns.is_empty() and _t("plain").entries[0].cells.is_empty(), "removing the last extra column turns columns off") func _test_result_cells() -> void: print("Result cells") main.select_result("world", "e2") await _frames(2) _check(insp.fields.has("cell_1") and insp.fields.has("cell_3"), "the result shows a field per extra column") var le: LineEdit = insp.fields["cell_2"] _eq(le.text, "Plentiful", "with the cell's text") main.undo_redo._commit_edits() le.text_changed.emit("Seas") main.undo_redo._commit_edits() _eq(_t("world").find_entry("e2").cells[1], "Seas", "editing it changes the cell") var card_edit: LineEdit = _cells("world", "e2")[1]["edit"] _eq(card_edit.text, "Seas", "and the card") main.undo_redo.undo() await _frames(2) _eq(_t("world").find_entry("e2").cells[1], "Plentiful", "undo") func _test_join_and_repeat() -> void: print("One phrase, roll twice and combine") main.select_card("world") await _frames(2) (insp.fields["column_join"] as OptionButton).item_selected.emit(1) _eq(_t("world").column_join, " ", "Read as one phrase joins with spaces") main.select_result("world", "e3") await _frames(2) (insp.fields["repeat"] as CheckBox).toggled.emit(true) await _frames(2) _eq(_t("world").find_entry("e3").repeat, {"count": 2}, "Roll twice and combine turns on") (insp.fields["repeat_count"] as LineEdit).text_changed.emit("3") (insp.fields["repeat_skip"] as CheckBox).toggled.emit(true) _eq(_t("world").find_entry("e3").repeat, {"count": 3, "reroll": ["e3"]}, "count and roll again on it") main.undo_redo._commit_edits() func _press(label: String) -> void: for b in insp.find_children("*", "Button", true, false): if (b as Button).text == label: (b as Button).pressed.emit() return _check(false, "no button %s" % label)