202 lines
8.7 KiB
GDScript
202 lines
8.7 KiB
GDScript
extends TestBase
|
|
## BoardTester: static checks, simulated runs, isolation from the caller's
|
|
## variables and decks, batches, start-table detection, and the Test board
|
|
## dialog driven through its methods.
|
|
## Run: godot --headless --path . -s res://tests/test_board_tester.gd
|
|
|
|
|
|
|
|
## Stands in for Main: just enough for the dialog.
|
|
class FakeMain extends Control:
|
|
var data: TowerBoard
|
|
var history: Array = []
|
|
var focused: Array = []
|
|
|
|
func focus_table(table_id: String, entry_id: String = "") -> void:
|
|
focused.append([table_id, entry_id])
|
|
|
|
|
|
func _init() -> void:
|
|
var b := _board()
|
|
|
|
# ----- Static checks -----
|
|
var probs := BoardTester.static_checks(b)
|
|
_check(_has(probs, "effect", "start", "e1"), "a bad effect is found with its table and entry: %s" % str(probs))
|
|
_check(_has(probs, "expression", "badexpr", ""), "a bad dice expression is found")
|
|
_check(_has(probs, "expression", "pick", "") and _text_has(probs, "Prompt"), "a bad {= } in a prompt is found")
|
|
_check(_has(probs, "expression", "loop", "") and _text_has(probs, "Repeat rule"), "a bad loop rule is found")
|
|
_check(_has(probs, "expression", "start", "e1") and _text_has(probs, "Thread count"), "a bad thread count is found")
|
|
_check(_has(probs, "range", "gap", "") and _text_has(probs, "No result for 3"), "a range gap is found")
|
|
_check(_has(probs, "missing_pipe", "start", "e1"), "a thread to a missing table is found")
|
|
_check(_has(probs, "empty_choice", "empty", ""), "a choice card with no options is found")
|
|
_check(_has(probs, "cycle", "ping", ""), "an endless cycle is found")
|
|
_check(not _has(probs, "range", "okrange", "") and not _has(probs, "effect", "mine", ""), "good cards give no problems")
|
|
|
|
# ----- Simulation -----
|
|
var caller_vars := {"gold": {"value": 99, "source": "x"}}
|
|
var r: Dictionary = await BoardTester.simulate(b, "start", {"runs": 80, "seed": 2, "prefer": "^Gap$"})
|
|
_eq(r["runs"], 80, "80 runs done")
|
|
_check((r["dead_ends"] as Array).size() >= 1 and str(r["dead_ends"][0]["text"]).contains("No result for 3") and r["dead_ends"][0]["table_id"] == "gap",
|
|
"the dead end names its table: %s" % str(r["dead_ends"]))
|
|
_check((r["unreached"] as Array).has("island") and not (r["unreached"] as Array).has("start"), "the island is never reached: %s" % str(r["unreached"]))
|
|
_check((r["errors"] as Array).size() >= 1 and r["errors"][0]["table_id"] == "start" and r["errors"][0]["entry_id"] == "e1" and str(r["errors"][0]["text"]).contains("nope"),
|
|
"the runtime error has table and entry ids: %s" % str(r["errors"]))
|
|
_check(r["actions"]["max"] >= r["actions"]["p95"] and r["prompts_mean"] > 0.0, "action and prompt stats exist")
|
|
_check(not BoardTester.is_clean(r), "not clean")
|
|
# A starting vars dictionary is copied for every run, never changed.
|
|
var r2: Dictionary = await BoardTester.simulate(b, "start", {"runs": 5, "seed": 3, "vars": caller_vars})
|
|
_eq(caller_vars["gold"]["value"], 99, "the caller's variables are untouched")
|
|
_eq(caller_vars.size(), 1, "no variables were added to the caller's dictionary")
|
|
_eq(r2["runs"], 5, "runs with a starting vars copy")
|
|
|
|
# ----- Batches -----
|
|
var t := BoardTester.new(b, "start", {"runs": 25, "seed": 4})
|
|
var did := 0
|
|
var calls := 0
|
|
while not t.is_done():
|
|
did += await t.run_batch(7)
|
|
calls += 1
|
|
_eq(did, 25, "batches add up to the planned runs")
|
|
_eq(calls, 4, "25 runs in slices of 7 is 4 calls")
|
|
_eq(t.report()["runs"], 25, "report counts all runs")
|
|
var whole: Dictionary = await BoardTester.simulate(b, "start", {"runs": 25, "seed": 4})
|
|
_eq(whole["completed"], t.report()["completed"], "sliced and whole runs agree (same seed)")
|
|
_eq(whole["actions"], t.report()["actions"], "same action stats")
|
|
var t2 := BoardTester.new(b, "start", {"runs": 100, "seed": 5})
|
|
await t2.run_batch(10)
|
|
t2.cancel()
|
|
_check(t2.is_done() and t2.report()["runs"] == 10 and t2.report()["cancelled"], "cancel stops after the finished slice")
|
|
_eq(await t2.run_batch(5), 0, "no runs after cancel")
|
|
|
|
# ----- Start tables -----
|
|
var starts := BoardTester.start_candidates(b)
|
|
_check(starts.has("start") and starts.has("island") and starts.has("badexpr") and not starts.has("gap") and not starts.has("pick") and not starts.has("v_gold"),
|
|
"start candidates are roots without variable cards: %s" % str(starts))
|
|
_check(not starts.has("mine"), "a table referenced as [[Mine]] is not a root: %s" % str(starts))
|
|
_check(BoardTester.start_candidates(b, "gap")[0] == "gap", "the last rolled table comes first")
|
|
_check(not BoardTester.start_candidates(b, "v_gold").has("v_gold"), "a variable card is never offered")
|
|
|
|
# ----- Dialog -----
|
|
var fm := FakeMain.new()
|
|
fm.data = b
|
|
root.add_child(fm)
|
|
var d := TestBoardDialog.new(fm)
|
|
fm.add_child(d)
|
|
await process_frame
|
|
_eq(d.selected_start(), "start", "the dialog defaults to the first root")
|
|
d.runs = 100
|
|
d.run_test()
|
|
var frames := 0
|
|
while d.running and frames < 400:
|
|
await process_frame
|
|
frames += 1
|
|
_check(not d.running, "the run finishes")
|
|
_check(d.sections.has("Problems") and d.sections["Problems"]["count"] >= 6, "problems section filled")
|
|
_check(d.sections["Errors"]["count"] >= 1, "errors section filled")
|
|
_check(d.sections["Dead ends"]["count"] >= 1, "dead ends section filled")
|
|
_check(d.sections["Never reached"]["count"] >= 1, "never reached section filled")
|
|
_check(d.sections.has("Never chosen"), "never chosen section exists")
|
|
_check(d.summary.text.begins_with("100 runs · ") and d.summary.text.contains("never reached"), "summary line: %s" % d.summary.text)
|
|
_check(d.report_text().contains("Static problems:") and d.report_text().contains("Dead ends"), "report text can be copied")
|
|
var first_summary := d.summary.text
|
|
d.click_item("gap", "")
|
|
_eq(fm.focused, [["gap", ""]], "clicking an item calls focus_table")
|
|
await process_frame
|
|
await process_frame
|
|
# Reopening shows the last report.
|
|
var d2 := TestBoardDialog.new(fm)
|
|
fm.add_child(d2)
|
|
await process_frame
|
|
_check(d2.sections.has("Problems") and d2.summary.text == first_summary, "the last report is shown again on reopen")
|
|
# Cancel mid-run.
|
|
d2.runs = 1000
|
|
d2.run_test()
|
|
await process_frame
|
|
d2.cancel_run()
|
|
frames = 0
|
|
while d2.running and frames < 400:
|
|
await process_frame
|
|
frames += 1
|
|
_check(not d2.running and d2.summary.text.begins_with("Cancelled after"), "cancelled run reports it: %s" % d2.summary.text)
|
|
|
|
finish()
|
|
|
|
|
|
func _board() -> TowerBoard:
|
|
var b := TowerBoard.new()
|
|
b.id = "bt"
|
|
b.name = "Tester"
|
|
var start := RollTable.new("start", "Start")
|
|
var e := TableEntry.new("e1", "Begin")
|
|
e.effects = "@gold += nope("
|
|
e.add_pipe("pick")
|
|
e.add_pipe("nowhere")
|
|
e.add_pipe("loop", "2 +")
|
|
start.entries.append(e)
|
|
var pick := RollTable.create(RollTable.KIND_CHOICE, "pick", "Pick", "pick")
|
|
pick.prompt = "Which? {= 1 + }"
|
|
pick.entries[0].text = "Gap"
|
|
pick.entries[0].add_pipe("gap")
|
|
pick.entries[1].text = "Fine"
|
|
pick.entries[1].add_pipe("ok")
|
|
pick.entries[2].text = "Dig in [[Mine]]"
|
|
var gap := RollTable.new("gap", "Gap")
|
|
gap.dice = "1d6"
|
|
for spec in [[1, 2, "Low"], [4, 6, "High"]]:
|
|
var ge := TableEntry.new("g%d" % spec[0], spec[2])
|
|
ge.has_range = true
|
|
ge.range_lo = spec[0]
|
|
ge.range_hi = spec[1]
|
|
gap.entries.append(ge)
|
|
var ok := RollTable.new("ok", "Fine")
|
|
ok.entries.append(TableEntry.new("e1", "All good"))
|
|
var mine := RollTable.new("mine", "Mine")
|
|
mine.entries.append(TableEntry.new("e1", "Dig"))
|
|
var island := RollTable.new("island", "Island")
|
|
island.entries.append(TableEntry.new("e1", "Alone"))
|
|
var bad := RollTable.new("badexpr", "Bad dice")
|
|
bad.dice = "2d6 +"
|
|
var be := TableEntry.new("b1", "X")
|
|
be.has_range = true
|
|
be.range_lo = 2
|
|
be.range_hi = 12
|
|
bad.entries.append(be)
|
|
var loop := RollTable.create(RollTable.KIND_LOOP, "loop", "Repeat", "times")
|
|
loop.loop_expr = "3 *"
|
|
var empty := RollTable.create(RollTable.KIND_CHOICE, "empty", "Empty", "pick")
|
|
empty.entries.clear()
|
|
var ping := RollTable.new("ping", "Ping")
|
|
var pe := TableEntry.new("e1", "to pong")
|
|
pe.add_pipe("pong")
|
|
ping.entries.append(pe)
|
|
var pong := RollTable.new("pong", "Pong")
|
|
var po := TableEntry.new("e1", "to ping")
|
|
po.add_pipe("ping")
|
|
pong.entries.append(po)
|
|
var good := RollTable.new("okrange", "Fine range")
|
|
good.dice = "1d6"
|
|
var gr := TableEntry.new("r1", "All")
|
|
gr.has_range = true
|
|
gr.range_lo = 1
|
|
gr.range_hi = 6
|
|
good.entries.append(gr)
|
|
var gold := RollTable.create(RollTable.KIND_VARIABLE, "v_gold", "Gold", "number")
|
|
gold.var_name = "gold"
|
|
for tb in [start, pick, gap, ok, mine, island, bad, loop, empty, ping, pong, good, gold]:
|
|
b.tables.append(tb)
|
|
return b
|
|
|
|
|
|
func _has(probs: Array, kind: String, tid: String, eid: String) -> bool:
|
|
for p: Dictionary in probs:
|
|
if p["kind"] == kind and p["table_id"] == tid and (eid == "" or p["entry_id"] == eid):
|
|
return true
|
|
return false
|
|
|
|
|
|
func _text_has(probs: Array, s: String) -> bool:
|
|
for p: Dictionary in probs:
|
|
if str(p["text"]).contains(s):
|
|
return true
|
|
return false
|
|
|