TableTower/tests/test_ui_scales.gd
RobinFiredrake d41a6e40aa Tests: 15 s budget, shared Godot workers; a freed Main cancels its roll
tools/run_tests.sh stops every run at 15 s and lists what was not run.
Suites are nodes run by tests/run_suites.gd, several per Godot process;
TestBase no longer compiles the app (AppTestBase does). Audit: fewer
repeats and less setup; 51 suites in about 12 s (was 45 s).

Fix a crash when a roll was still animating as its Main was freed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 16:19:30 -06:00

191 lines
8.2 KiB
GDScript

extends AppTestBase
## Scale values in the UI (Scales): making one (+ Card, the inspector's steps
## and starting step, switching type), showing one (the card's value and row
## of steps, the Variables tab), the effect and condition rows for a scale,
## and the help topic.
## Run: tools/run_tests.sh ui_scales
const CLIMATE := ["Supercold", "Cold", "Temperate", "Hot", "Superhot"]
var insp: Inspector
func run() -> void:
if not await start_main(false):
return
main.set_anchors_and_offsets_preset(Control.PRESET_TOP_LEFT)
main.size = Vector2(1440, 900)
main.set_mode(Main.Mode.BUILD)
insp = main.inspector
await _load()
await _test_add_card()
await _test_inspector_fields()
await _test_settings_dialog()
await _test_card_shows_steps()
await _test_effect_rows()
await _test_condition_rows()
_test_help()
finish()
func _load() -> void:
main.data.load_dict({"format_version": 2, "id": "scales-test", "name": "Scales test", "tables": [
{"id": "climate", "name": "Climate", "kind": "variable", "var_name": "climate", "var_type": "scale",
"steps": CLIMATE, "initial": "Temperate", "x": 40, "y": 40, "entries": []},
{"id": "luck", "name": "Luck", "kind": "variable", "var_name": "luck", "var_type": "number", "initial": "2",
"x": 40, "y": 300, "entries": []},
{"id": "weather", "name": "Weather", "x": 340, "y": 40, "entries": [
{"id": "e1", "text": "Unexpectedly warmer", "effects": "@climate += 1", "pipes": [{"table": "luck_t", "when": "@climate >= \"Cold\""}]},
{"id": "e2", "text": "Calm"}]},
{"id": "luck_t", "name": "Lucky", "x": 700, "y": 40, "entries": [{"id": "e1", "text": "Yes"}]},
]})
main.persistent_vars.clear()
main._rebuild_cards()
main.clear_inspector()
await _frames(4)
func _t(id: String) -> RollTable:
return main.data.get_table(id)
func _test_add_card() -> void:
var found := false
for k: Array in MainEditing.ADD_KINDS:
found = found or (k[1] == RollTable.KIND_VARIABLE and k[2] == RollTable.VAR_SCALE and str(k[0]).begins_with("Scale"))
_check(found, "+ Card lists Scale (ordered steps)")
var t := main.editing.add_table(RollTable.KIND_VARIABLE, RollTable.VAR_SCALE)
_check(t.is_scale() and t.steps == (["Low", "Medium", "High"] as Array[String]) and t.initial == "Low", "a new scale has Low, Medium, High and starts Low")
main.data.remove_table(t.id)
main._rebuild_cards()
await _frames(2)
func _test_inspector_fields() -> void:
main.select_card("climate")
await _frames(2)
var te := insp.fields.get("steps") as TextEdit
_check(te != null and te.text == "\n".join(CLIMATE), "the inspector lists the steps, one per line")
var start := insp.fields.get("initial") as OptionButton
_check(start != null and start.item_count == 5 and start.selected == 2, "the starting value is a dropdown of the steps, on Temperate")
start.item_selected.emit(3)
_eq(_t("climate").initial, "Hot", "picking a step sets the starting value")
_eq(insp.apply_settings({"steps": "Only one"}), "A scale needs at least two steps, one per line.", "one step isn't a scale")
te.text = "Cold\nMild\nWarm"
te.text_changed.emit()
_eq(_t("climate").steps, ["Cold", "Mild", "Warm"] as Array[String], "editing the lines sets the steps")
_eq(_t("climate").initial, "Cold", "a starting step that's gone falls back to the first")
_eq((insp.fields.get("initial") as OptionButton).item_count, 3, "the starting dropdown follows the steps")
insp.apply_settings({"steps": "Semi-arid\nWet"})
_eq(_t("climate").initial, "\"Semi-arid\"", "a step with a dash starts as quoted text")
_eq(Roller.variable_display(main.data, main.persistent_vars, _t("climate"))["text"], "Semi-arid", "and still shows as the step")
# Switching types.
main.select_card("luck")
await _frames(2)
insp.apply_settings({"var_type": "scale"}, true)
_check(_t("luck").is_scale() and _t("luck").steps.size() == 3 and _t("luck").initial == "Low", "a number turned into a scale gets the default steps")
insp.apply_settings({"var_type": "number", "initial": ""}, true)
_eq(_t("luck").var_type, "number", "and back")
insp.apply_settings({"initial": "2"})
_t("climate").steps.assign(CLIMATE)
_t("climate").initial = "Temperate"
main.mark_dirty()
await _frames(2)
func _test_settings_dialog() -> void:
var d := TableSettingsDialog.new(_t("climate"))
main.add_child(d)
await _frames(1)
_check(d._kind._steps_field.visible and d._kind._steps.text == "\n".join(CLIMATE), "the settings dialog shows the steps for a scale")
_eq(d.settings()["steps"], "\n".join(CLIMATE), "and passes them on")
d.queue_free()
d = TableSettingsDialog.new(_t("luck"))
main.add_child(d)
await _frames(1)
_check(not d._kind._steps_field.visible and not d.settings().has("steps"), "but not for a number")
d.queue_free()
await _frames(1)
func _test_card_shows_steps() -> void:
main.set_variable("climate", "4")
await _frames(2)
_eq(main.persistent_vars["climate"]["value"], "Hot", "typing a step number in the Variables tab stores the step")
main.set_variable("climate", "cold")
_eq(main.persistent_vars["climate"]["value"], "Cold", "a name is matched without caring about capitals")
main._rebuild_cards()
await _frames(2)
var ui := main.card_for("climate").value_ui
_eq(ui.var_value.text, "Cold", "the card shows the current step")
_eq(ui.steps_row.get_child_count(), 5, "and every step in a row")
var lit := []
for chip in ui.steps_row.get_children():
var l := chip.get_child(0) as Label
if (chip as PanelContainer).get_theme_stylebox("panel").bg_color.a > 0.0:
lit.append(l.text)
_eq(lit, ["Cold"], "with the current one marked")
main.clear_variable("climate")
await _frames(2)
func _test_effect_rows() -> void:
main.select_result("weather", "e1")
await _frames(2)
var fx := insp.effects
var e := _t("weather").find_entry("e1")
_eq([fx.rows[0]["op"], fx.rows[0]["amount"]], ["step_up", "1"], "@climate += 1 is a Move up row")
_check(InspectorKit.find_tagged(fx, "res.fx.0.amount") != null, "with a count of steps")
fx.set_row_amount(0, "2")
_eq(e.effects, "@climate += 2", "Move up 2 steps")
fx.set_row_op(0, "step_down")
_eq(e.effects, "@climate -= 2", "Move down")
fx.set_row_op(0, "toward")
_eq(e.effects, "@climate = toward(@climate, \"Supercold\")", "Move toward starts at the first step")
_check(InspectorKit.find_tagged(fx, "res.fx.0.step") is OptionButton, "and picks the step from a dropdown")
fx.set_row_step(0, "Temperate")
_eq(e.effects, "@climate = toward(@climate, \"Temperate\")", "picking a step writes it")
fx.set_row_op(0, "at_least")
_eq(e.effects, "@climate = max(@climate, \"Temperate\")", "Raise to at least keeps the step")
fx.set_row_op(0, "at_most")
_eq(e.effects, "@climate = min(@climate, \"Temperate\")", "Lower to at most")
fx.set_row_op(0, "set")
_eq(e.effects, "@climate = \"Temperate\"", "Set to a step")
_eq(Describe.effect(e.effects, main.data), "Climate = Temperate", "reads in plain words")
fx.add_row()
fx.set_row_var(1, "@climate")
_eq(e.effects, "@climate = \"Temperate\"\n@climate += 1", "a new row on a scale is Move up 1")
fx.set_row_var(1, "@luck")
_eq(e.effects, "@climate = \"Temperate\"\n@luck += 1", "on a number it's Add again")
fx.remove_row(1)
e.effects = "@climate += 1"
main.mark_dirty()
await _frames(2)
func _test_condition_rows() -> void:
main.select_result("weather", "e1")
await _frames(2)
var cond: ConditionBuilder = insp.pipe_parts["luck_t"]["when"]
var e := _t("weather").find_entry("e1")
_check(InspectorKit.find_tagged(cond, "cond.0.step") is OptionButton or _has_step_dropdown(cond), "a scale clause picks its step from a dropdown")
_eq(Describe.condition(e.when_for("luck_t"), main.data), "Climate is at least Cold", "reads in plain words")
cond.set_clause_step(0, "Hot")
_eq(e.when_for("luck_t"), "@climate >= \"Hot\"", "picking a step writes it")
cond.add_clause()
cond.set_clause_var(1, "@climate")
_eq(cond.clauses[1]["value"], "\"Supercold\"", "a new scale clause starts at the first step")
func _has_step_dropdown(n: Node) -> bool:
for c in n.get_children():
if (c is OptionButton and (c as OptionButton).item_count == CLIMATE.size()) or _has_step_dropdown(c):
return true
return false
func _test_help() -> void:
var help := FileAccess.get_file_as_string("res://help/topics.txt")
var a := help.find("# Values and changes")
var b := help.find("# Scales")
_check(a >= 0 and b > a and help.find("\n# ", a + 1) == b - 1, "the Scales topic follows Values and changes")