TableTower/tests/test_choice_dialog.gd
2026-11-08 16:39:56 -06:00

282 lines
10 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extends SceneTree
## The choice pop-up's minimize pill and hover preview, and TablePreview.
## Run: godot --headless --path . -s res://tests/test_choice_dialog.gd -- --sample
var _failures := 0
var main: Main
func _init() -> void:
if not OS.get_cmdline_user_args().has("--sample"):
print("Run with -- --sample so the test can't touch saved data.")
quit(2)
return
main = load("res://ui/main.tscn").instantiate()
root.add_child(main)
for i in 5:
await process_frame
if main._saving_enabled:
print("Saving is enabled; refusing to roll.")
quit(2)
return
# Headless windows are tiny; give the app a real size.
main.set_anchors_preset(Control.PRESET_TOP_LEFT)
main.size = Vector2(1280, 800)
main.rng.seed = 5
main.prompt_hook = main._ask_player
_add_tables()
await process_frame
_test_table_preview()
await _test_preview_content()
await _test_keyboard_focus()
await _test_minimize()
await _test_waiting_question()
await _test_cancel_from_pill()
await _test_state_kept()
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
quit(1 if _failures else 0)
func _add_tables() -> void:
var ask := RollTable.create(RollTable.KIND_CHOICE, "t_ask", "Where to?", "pick")
ask.prompt = "Where do you go?"
ask.entries.clear()
var a := TableEntry.new("a", "Visit the [[Bazaar]]")
a.details = "A **busy** place"
a.effects = "@gold -= 5"
a.add_pipe("t_loot", 2)
var b := TableEntry.new("b", "Take the fork")
b.add_pipe("t_fork", "@terms")
var c := TableEntry.new("c", "Stay put")
ask.entries.append_array([a, b, c] as Array[TableEntry])
var loot := RollTable.create(RollTable.KIND_ROLL, "t_loot", "Loot", "")
loot.dice = "d6"
loot.details = "What the stalls sell"
loot.entries.clear()
for spec in [[1, 2, "Trinket"], [3, 5, "Coin"], [6, 6, "Relic"]]:
var e := TableEntry.new("l%d" % spec[0], spec[2])
e.has_range = true
e.range_lo = spec[0]
e.range_hi = spec[1]
loot.entries.append(e)
loot.entries[2].add_pipe("t_big")
var fork := RollTable.create(RollTable.KIND_CHOICE, "t_fork", "Which way?", "pick")
fork.prompt = "Left or right?"
var bazaar := RollTable.create(RollTable.KIND_ROLL, "t_bazaar", "Bazaar", "")
var big := RollTable.create(RollTable.KIND_ROLL, "t_big", "Big table", "")
big.entries.clear()
for n in 15:
big.entries.append(TableEntry.new("g%d" % n, "Thing %d" % n))
for t in [ask, loot, fork, bazaar, big]:
main.data.tables.append(t)
main._rebuild_cards()
## All the text in a control tree.
func _texts(n: Node, out: PackedStringArray = PackedStringArray()) -> PackedStringArray:
if n is Label:
out.append((n as Label).text)
elif n is RichTextLabel:
out.append((n as RichTextLabel).text)
for c in n.get_children():
_texts(c, out)
return out
func _has(n: Node, needle: String) -> bool:
for s in _texts(n):
if s.contains(needle):
return true
return false
func _ask() -> ChoiceDialog:
main.start_roll("t_ask")
await process_frame
await process_frame
return main.pending_choice()
func _key(code: Key, ctrl: bool = false) -> InputEventKey:
var ev := InputEventKey.new()
ev.keycode = code
ev.pressed = true
ev.ctrl_pressed = ctrl
return ev
# ---------------------------------------------------------------- TablePreview
func _test_table_preview() -> void:
var p := TablePreview.build(main.data, "t_big")
_check(_has(p, "Big table") and _has(p, "Roll table · d15"), "the preview names the table, its kind and dice")
_check(_has(p, "Thing 11") and not _has(p, "Thing 12"), "the preview shows 12 rows")
_check(_has(p, "+3 more"), "the row cap says how many more there are")
_check(_has(p, "7%"), "plain tables show chances")
p.free()
var m := TablePreview.build(main.data, "nope")
_check(_has(m, "Missing"), "a missing table says so")
m.free()
# ---------------------------------------------------------------- Preview content
func _test_preview_content() -> void:
var d := await _ask()
_check(d != null and d.kind == "pick", "the pick question opens")
if d == null:
return
_check(d.board == main.data, "the dialog finds the board")
# Option 1 pipes to a ranged table, twice.
var p := d.preview_for_option(0)
_check(p != null, "an option that pipes on has a preview")
_check(_has(p, "A **busy** place") or _has(p, "busy"), "the preview shows the option's details")
_check(_has(p, "@gold -= 5"), "the preview lists the option's effects")
_check(_has(p, "Loot") and _has(p, "d6"), "the piped table's name and dice show")
_check(_has(p, "×2"), "the pipe count shows")
_check(_has(p, "What the stalls sell"), "the piped table's details show")
_check(_has(p, "1–2") and _has(p, "3–5") and _has(p, "33%"), "ranged entries show their range and chance")
_check(_has(p, TablePreview.NEXT_HINT), "an entry that leads further gets a next-table hint")
# The text mentions [[Bazaar]].
_check(_has(p, "MENTIONED IN THE TEXT") and _has(p, "Bazaar"), "a table named inline in the text is previewed")
p.free()
# Option 2 pipes to a choice table with an expression count.
var q := d.preview_for_option(1)
_check(_has(q, "Which way?") and _has(q, "Choice card"), "a choice target shows its kind")
_check(_has(q, "Asks: Left or right?"), "a choice target shows its question")
_check(_has(q, "×@terms"), "an expression pipe count shows")
q.free()
_check(d.preview_for_option(2) == null, "an option with nothing to show has no preview")
main.cancel_choice()
await process_frame
func _test_keyboard_focus() -> void:
var d := await _ask()
if d == null:
_check(false, "the question opens for the focus test")
return
d._options[0].grab_focus()
_check(d.preview_panel.visible and _has(d.preview_panel, "Loot"), "focusing an option shows its preview")
var vr := d.get_global_rect()
var cr := d.card.get_global_rect()
var pr := d.preview_panel.get_global_rect()
_check(not pr.intersects(cr) and vr.encloses(pr), "the preview sits beside the card, on screen")
d._options[1].grab_focus()
_check(_has(d.preview_panel, "Which way?") and not _has(d.preview_panel, "Loot"), "moving focus changes the preview")
d._options[2].grab_focus()
await process_frame
_check(not d.preview_panel.visible, "an option with nothing to show hides the panel")
main.cancel_choice()
await process_frame
# ---------------------------------------------------------------- Minimize
func _test_minimize() -> void:
var d := await _ask()
if d == null:
_check(false, "the question opens for the minimize test")
return
d._options[1].grab_focus()
d._input(_key(KEY_M))
_check(d.minimized and not d.card.is_visible_in_tree(), "M hides the dialog")
_check(d.pill.visible and _has(d.pill, "Question waiting: Where to?"), "the pill names the question")
_check(d.mouse_filter == Control.MOUSE_FILTER_IGNORE, "the board gets the mouse while minimized")
_check(not d._dim.visible, "the dim goes away")
_check(not d.preview_panel.visible, "the preview hides")
_check(not d.reply.finished and main.pending_choice() == d, "the question is still waiting")
d._input(_key(KEY_M))
_check(not d.minimized and d.card.is_visible_in_tree() and not d.pill.visible, "M again restores it")
_check(d._options[1].has_focus(), "the focused option is focused again")
d._input(_key(KEY_TAB))
_check(d.minimized, "Tab minimizes too")
d._input(_key(KEY_1))
_check(not d.reply.finished, "number keys do nothing while minimized")
d._input(_key(KEY_ESCAPE))
_check(not d.reply.finished, "Esc does nothing while minimized")
d._input(_key(KEY_TAB))
_check(not d.minimized, "Tab restores")
main.cancel_choice()
await process_frame
func _test_waiting_question() -> void:
var d := await _ask()
if d == null:
_check(false, "the question opens for the waiting test")
return
var token: int = main._token
d.minimize()
main.start_roll("t_loot")
await process_frame
_check(main.pending_choice() == d and not d.reply.finished and main._token == token, "a die click doesn't cancel the waiting question")
_check(d.toast_panel.visible and _has(d.toast_panel, "Answer the waiting question first"), "a toast asks for the answer first")
var has_show := false
for c in (d.toast_panel.get_child(0) as Node).get_children():
has_show = has_show or (c is Button and (c as Button).text == "Show")
_check(has_show, "the toast has a Show button")
main.set_mode(Main.Mode.BUILD)
_check(main.mode == Main.Mode.PLAY, "Build is refused while a question waits")
d.restore()
_check(not d.toast_panel.visible, "restoring hides the toast")
main.set_mode(Main.Mode.BUILD)
_check(main.mode == Main.Mode.BUILD, "Build is a normal mode switch again")
main.set_mode(Main.Mode.PLAY)
main.cancel_choice()
await process_frame
func _test_cancel_from_pill() -> void:
var d := await _ask()
if d == null:
_check(false, "the question opens for the cancel test")
return
d.minimize()
var x: Button
for c in (d.pill.get_child(0) as Node).get_children():
if c is Button and (c as Button).text == "Cancel":
x = c
_check(x != null, "the pill has a cancel button")
var got: Array = ["unset"]
d.reply.done.connect(func(v: Variant) -> void: got[0] = v)
x.pressed.emit()
_check(got[0] == null and d.reply.finished, "the pill's Cancel answers Cancel")
await process_frame
await process_frame
_check(main.pending_choice() == null, "the dialog is gone")
# A roll can start again.
main.cancel_choice()
main.start_roll("t_ask")
await process_frame
await process_frame
_check(main.pending_choice() != null, "a new roll can start after cancelling")
main.cancel_choice()
await process_frame
func _test_state_kept() -> void:
var d := ChoiceDialog.new({"table_id": "x", "kind": "number", "title": "How many?", "prompt": "", "min": 0, "max": 20, "default": 3})
main.add_child(d)
await process_frame
d._spin.value = 9
d._spin.get_line_edit().grab_focus()
d._input(_key(KEY_M))
_check(not d.minimized, "plain M in a number field types instead of minimizing")
d._input(_key(KEY_M, true))
_check(d.minimized, "Ctrl+M minimizes from a field")
d._input(_key(KEY_M, true))
_check(not d.minimized and int(d._spin.value) == 9, "the typed value survives minimize and restore")
d.answer(null)
await process_frame
func _check(ok: bool, what: String) -> void:
if not ok:
_failures += 1
print(" ✗ ", what)