425 lines
20 KiB
GDScript3
425 lines
20 KiB
GDScript3
|
|
extends SceneTree
|
|||
|
|
## Headless checks for conditional pipes and router cards (docs/LOGIC.md §3a
|
|||
|
|
## and §3b): when / otherwise selection, the snapshot rule, expression errors,
|
|||
|
|
## first-match routing, "No case matched", the file format, lint_pipes() and a
|
|||
|
|
## small board where a router replaces a chain-variable workaround.
|
|||
|
|
## Run: godot --headless --path . -s res://tests/test_router.gd
|
|||
|
|
|
|||
|
|
var _failures := 0
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _init() -> void:
|
|||
|
|
_test_when_otherwise()
|
|||
|
|
_test_snapshot()
|
|||
|
|
_test_errors()
|
|||
|
|
_test_router()
|
|||
|
|
_test_no_case()
|
|||
|
|
_test_json()
|
|||
|
|
_test_lint()
|
|||
|
|
_test_cycles()
|
|||
|
|
await _test_mini_board()
|
|||
|
|
_test_ui_helpers()
|
|||
|
|
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
|
|||
|
|
quit(1 if _failures else 0)
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- helpers
|
|||
|
|
|
|||
|
|
func _rng(seed_value: int = 1) -> RandomNumberGenerator:
|
|||
|
|
var r := RandomNumberGenerator.new()
|
|||
|
|
r.seed = seed_value
|
|||
|
|
return r
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _board(tables: Array) -> TowerBoard:
|
|||
|
|
var b := TowerBoard.new()
|
|||
|
|
b.id = "router-test"
|
|||
|
|
for t in tables:
|
|||
|
|
b.tables.append(t)
|
|||
|
|
return b
|
|||
|
|
|
|||
|
|
|
|||
|
|
## A one-entry roll table.
|
|||
|
|
func _one(id: String, text: String, effects: String = "") -> RollTable:
|
|||
|
|
var t := RollTable.new(id, id.capitalize())
|
|||
|
|
var e := TableEntry.new("e1", text)
|
|||
|
|
e.effects = effects
|
|||
|
|
t.entries.append(e)
|
|||
|
|
return t
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _roll(b: TowerBoard, id: String, vars: Dictionary = {}) -> Dictionary:
|
|||
|
|
var ctx := Roller.new_context(b, _rng(), vars)
|
|||
|
|
return Roller.roll(b, id, ctx.rng, 0, {}, ctx)
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Table ids of a tree's steps, in roll order.
|
|||
|
|
func _ids(tree: Dictionary) -> Array:
|
|||
|
|
var out := []
|
|||
|
|
for s: Dictionary in Roller.flatten(tree):
|
|||
|
|
out.append(str(s["table_id"]))
|
|||
|
|
return out
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _var(vars: Dictionary, name: String) -> Variant:
|
|||
|
|
var v: Variant = vars.get(name)
|
|||
|
|
return (v as Dictionary).get("value") if v is Dictionary else null
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _errors(tree: Dictionary) -> Array:
|
|||
|
|
var out := []
|
|||
|
|
for s: Dictionary in Roller.flatten(tree):
|
|||
|
|
for fx: Dictionary in s.get("effects", []):
|
|||
|
|
if fx.get("error", false):
|
|||
|
|
out.append(str(fx["text"]))
|
|||
|
|
return out
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- when / otherwise
|
|||
|
|
|
|||
|
|
## start --if @str >= 8--> strong, --if @str < 4--> weak, --otherwise--> plain,
|
|||
|
|
## --(always)--> after.
|
|||
|
|
func _branch_board(str_value: int) -> TowerBoard:
|
|||
|
|
var start := _one("start", "Begin", "@str = %d" % str_value)
|
|||
|
|
var e := start.entries[0]
|
|||
|
|
e.add_pipe("strong")
|
|||
|
|
e.set_pipe_when("strong", "@str >= 8")
|
|||
|
|
e.add_pipe("weak")
|
|||
|
|
e.set_pipe_when("weak", "@str < 4")
|
|||
|
|
e.add_pipe("plain")
|
|||
|
|
e.set_pipe_otherwise("plain", true)
|
|||
|
|
e.add_pipe("after")
|
|||
|
|
return _board([start, _one("strong", "Strong"), _one("weak", "Weak"), _one("plain", "Plain"), _one("after", "After")])
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _test_when_otherwise() -> void:
|
|||
|
|
_eq(_ids(_roll(_branch_board(10), "start")), ["start", "strong", "after"], "a true condition runs its pipe; otherwise is skipped")
|
|||
|
|
_eq(_ids(_roll(_branch_board(2), "start")), ["start", "weak", "after"], "the second condition can be the true one")
|
|||
|
|
_eq(_ids(_roll(_branch_board(6), "start")), ["start", "plain", "after"], "no condition true: the otherwise pipe runs")
|
|||
|
|
# Two true conditions both run (pipes aren't else-if; use a router for that).
|
|||
|
|
var b := _branch_board(10)
|
|||
|
|
b.get_table("start").entries[0].set_pipe_when("weak", "@str > 1")
|
|||
|
|
_eq(_ids(_roll(b, "start")), ["start", "strong", "weak", "after"], "every true condition runs, in pipe order")
|
|||
|
|
# A condition clears otherwise, and otherwise can't be set while there's a condition.
|
|||
|
|
var e := TableEntry.new("e1", "x")
|
|||
|
|
e.add_pipe("y")
|
|||
|
|
e.set_pipe_otherwise("y", true)
|
|||
|
|
_check(e.is_otherwise("y"), "otherwise is set")
|
|||
|
|
e.set_pipe_when("y", "@a = 1")
|
|||
|
|
_check(not e.is_otherwise("y") and e.when_for("y") == "@a = 1", "a condition replaces otherwise")
|
|||
|
|
e.set_pipe_otherwise("y", true)
|
|||
|
|
_check(not e.is_otherwise("y"), "otherwise is refused while a condition is set")
|
|||
|
|
e.remove_pipe("y")
|
|||
|
|
_check(e.pipe_when.is_empty() and e.pipe_otherwise.is_empty(), "remove_pipe clears the condition")
|
|||
|
|
# Only the pipes taken light up.
|
|||
|
|
var lit_e := {}
|
|||
|
|
var lit_p := {}
|
|||
|
|
Roller.collect_lit(_roll(_branch_board(10), "start"), lit_e, lit_p)
|
|||
|
|
_check(lit_p.has(Roller.pipe_key("start", "e1", "strong")) and lit_p.has(Roller.pipe_key("start", "e1", "after")), "taken pipes light")
|
|||
|
|
_check(not lit_p.has(Roller.pipe_key("start", "e1", "weak")) and not lit_p.has(Roller.pipe_key("start", "e1", "plain")), "skipped pipes stay dark")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- snapshot
|
|||
|
|
|
|||
|
|
func _test_snapshot() -> void:
|
|||
|
|
# The first pipe's subtree changes @x and @n; the later pipes still see the
|
|||
|
|
# values from when the entry was chosen.
|
|||
|
|
var start := _one("start", "Begin", "@x = 0; @n = 1")
|
|||
|
|
var e := start.entries[0]
|
|||
|
|
e.add_pipe("first")
|
|||
|
|
e.set_pipe_when("first", "@x = 0")
|
|||
|
|
e.add_pipe("second")
|
|||
|
|
e.set_pipe_when("second", "@x = 0")
|
|||
|
|
e.add_pipe("third")
|
|||
|
|
e.set_pipe_count("third", "@n")
|
|||
|
|
e.add_pipe("fallback")
|
|||
|
|
e.set_pipe_otherwise("fallback", true)
|
|||
|
|
var first := _one("first", "First", "@x = 1; @n = 4")
|
|||
|
|
var b := _board([start, first, _one("second", "Second"), _one("third", "Third"), _one("fallback", "Fallback")])
|
|||
|
|
var vars := {}
|
|||
|
|
var ids := _ids(_roll(b, "start", vars))
|
|||
|
|
_eq(ids, ["start", "first", "second", "third"], "conditions and counts come from the snapshot, not the changed variables")
|
|||
|
|
_eq(_var(vars, "x"), 1, "the first pipe's subtree did change @x")
|
|||
|
|
# An otherwise pipe doesn't fire because a when pipe's subtree made the
|
|||
|
|
# conditions false afterwards.
|
|||
|
|
_check(not ids.has("fallback"), "otherwise reads the snapshot too")
|
|||
|
|
# A self-pipe with a counting condition still counts up (the condition is
|
|||
|
|
# checked each time the entry is chosen).
|
|||
|
|
var link := _one("link", "Link {@k}", "@k += 1")
|
|||
|
|
link.entries[0].add_pipe("link")
|
|||
|
|
link.entries[0].set_pipe_when("link", "@k < 5")
|
|||
|
|
var v2 := {}
|
|||
|
|
_eq(_ids(_roll(_board([link]), "link", v2)).size(), 5, "a conditional self-pipe stops when its condition turns false")
|
|||
|
|
_eq(_var(v2, "k"), 5, "it ran five times")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- errors
|
|||
|
|
|
|||
|
|
func _test_errors() -> void:
|
|||
|
|
var start := _one("start", "Begin")
|
|||
|
|
var e := start.entries[0]
|
|||
|
|
e.add_pipe("bad")
|
|||
|
|
e.set_pipe_when("bad", "@x + * 2")
|
|||
|
|
e.add_pipe("other")
|
|||
|
|
e.set_pipe_otherwise("other", true)
|
|||
|
|
var tree := _roll(_board([start, _one("bad", "Bad"), _one("other", "Other")]), "start")
|
|||
|
|
_eq(_ids(tree), ["start", "other"], "a condition that errors counts as false (so otherwise runs)")
|
|||
|
|
var errs := _errors(tree)
|
|||
|
|
_check(errs.size() == 1 and str(errs[0]).begins_with("Pipe condition @x + * 2"), "the error shows as an error line on the node: %s" % str(errs))
|
|||
|
|
var step: Dictionary = Roller.flatten(tree)[0]
|
|||
|
|
_check((step.get("effects", []) as Array).size() == 1, "the error line is in the step's effects")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- routers
|
|||
|
|
|
|||
|
|
func _router_board() -> TowerBoard:
|
|||
|
|
var r := RollTable.create(RollTable.KIND_ROUTER, "route", "Route")
|
|||
|
|
r.entries.clear()
|
|||
|
|
var cases := [["@species = \"Aslan\"", "Aslan path", "aslan"], ["@str >= 8", "Strong path", "strong"], ["", "Otherwise", "plain"]]
|
|||
|
|
for i in cases.size():
|
|||
|
|
var c := TableEntry.new("c%d" % (i + 1), cases[i][1])
|
|||
|
|
c.when = cases[i][0]
|
|||
|
|
c.effects = "@took = \"%s\"" % cases[i][2]
|
|||
|
|
c.add_pipe(cases[i][2])
|
|||
|
|
r.entries.append(c)
|
|||
|
|
return _board([r, _one("aslan", "Aslan"), _one("strong", "Strong"), _one("plain", "Plain")])
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _test_router() -> void:
|
|||
|
|
var t := RollTable.create(RollTable.KIND_ROUTER, "r", "Router")
|
|||
|
|
_eq(t.kind, "router", "create makes a router")
|
|||
|
|
_eq(t.entries.size(), 2, "a new router has two cases")
|
|||
|
|
_eq(t.entries[0].text, "if …", "its first case")
|
|||
|
|
_check(t.entries[0].when != "" and Expr.check(t.entries[0].when) == "", "the first case has a valid condition")
|
|||
|
|
_check(t.entries[1].text == "Otherwise" and t.entries[1].when == "", "the second is Otherwise")
|
|||
|
|
_eq(t.otherwise_index(), 1, "otherwise_index")
|
|||
|
|
var b := _router_board()
|
|||
|
|
var vars := {"species": {"value": "Aslan", "source": ""}, "str": {"value": 10, "source": ""}}
|
|||
|
|
var tree := _roll(b, "route", vars)
|
|||
|
|
_eq(_ids(tree), ["route", "aslan"], "the first true case wins, even when a later one is true too")
|
|||
|
|
_eq(str(tree.get("kind", "")), "router", "the node's kind is router")
|
|||
|
|
_eq(tree.get("case"), 0, "the node's case index")
|
|||
|
|
_eq(_var(vars, "took"), "aslan", "the case's effects ran")
|
|||
|
|
_eq(Formatter.strip_tags(str(tree["summary"])), "Aslan path", "the case's text is the summary")
|
|||
|
|
var step: Dictionary = Roller.flatten(tree)[0]
|
|||
|
|
_check(step.get("kind") == "router" and step.get("case") == 0 and str(step.get("num", "")) == "", "the step has kind and case, and no rolled number: %s" % str(step))
|
|||
|
|
var v2 := {"str": {"value": 9, "source": ""}}
|
|||
|
|
_eq(_ids(_roll(b, "route", v2)), ["route", "strong"], "the second case")
|
|||
|
|
_eq(_ids(_roll(b, "route", {})), ["route", "plain"], "nothing true: Otherwise")
|
|||
|
|
# One action per router.
|
|||
|
|
var ctx := Roller.new_context(b, _rng(), {})
|
|||
|
|
Roller.roll(b, "route", ctx.rng, 0, {}, ctx)
|
|||
|
|
_eq(ctx.actions, 2, "a router is one action (plus the table it pipes to)")
|
|||
|
|
# A case condition with an error counts as false and is reported.
|
|||
|
|
b.get_table("route").entries[0].when = "@species = = 3 +"
|
|||
|
|
var t3 := _roll(b, "route", {})
|
|||
|
|
_eq(_ids(t3), ["route", "plain"], "an erroring case is skipped")
|
|||
|
|
var errs := _errors(t3)
|
|||
|
|
_check(errs.size() == 1 and str(errs[0]).begins_with("Case "), "the case error is on the router node: %s" % str(errs))
|
|||
|
|
# Lighting: the matched case is lit.
|
|||
|
|
var le := {}
|
|||
|
|
var lp := {}
|
|||
|
|
Roller.collect_lit(_roll(_router_board(), "route", {}), le, lp)
|
|||
|
|
_check(le.has(Roller.entry_key("route", "c3")) and not le.has(Roller.entry_key("route", "c1")), "only the matched case lights")
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _test_no_case() -> void:
|
|||
|
|
var b := _router_board()
|
|||
|
|
var r := b.get_table("route")
|
|||
|
|
r.entries.remove_at(2) # no Otherwise
|
|||
|
|
var tree := _roll(b, "route", {})
|
|||
|
|
_check(tree.has("stop") and str(tree["summary"]) == Roller.NO_CASE, "nothing matched: the node stops with \"No case matched\"")
|
|||
|
|
_eq(str(tree.get("kind", "")), "router", "the stop node is a router node")
|
|||
|
|
_eq(_ids(tree), ["route"], "nothing follows")
|
|||
|
|
var step: Dictionary = Roller.flatten(tree)[0]
|
|||
|
|
_check(step.get("stop", false) and str(step["text"]) == "No case matched", "the step shows it")
|
|||
|
|
# A router with no cases at all.
|
|||
|
|
r.entries.clear()
|
|||
|
|
_check(_roll(b, "route", {}).has("stop"), "an empty router stops")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- file format
|
|||
|
|
|
|||
|
|
func _test_json() -> void:
|
|||
|
|
var b := _router_board()
|
|||
|
|
var e := b.get_table("route").entries[1]
|
|||
|
|
e.add_pipe("aslan", 2, "who")
|
|||
|
|
e.set_pipe_when("aslan", "@str > 9")
|
|||
|
|
e.add_pipe("plain")
|
|||
|
|
e.set_pipe_otherwise("plain", true)
|
|||
|
|
var d := b.get_table("route").to_dict()
|
|||
|
|
_eq(str(d.get("kind", "")), "router", "kind is written")
|
|||
|
|
var ed: Dictionary = d["entries"][1]
|
|||
|
|
_eq(str(ed.get("when", "")), "@str >= 8", "an entry's when is written")
|
|||
|
|
_check(not (d["entries"][2] as Dictionary).has("when"), "the Otherwise case has no when")
|
|||
|
|
var pipes: Array = ed["pipes"]
|
|||
|
|
_eq(pipes[1], {"table": "aslan", "count": 2, "save_as": "who", "when": "@str > 9"}, "a pipe's when is written with its other options")
|
|||
|
|
_eq(pipes[2], {"table": "plain", "otherwise": true}, "an otherwise pipe")
|
|||
|
|
_eq(pipes[0], "strong", "a plain pipe stays a bare id")
|
|||
|
|
var back := RollTable.from_dict(d)
|
|||
|
|
var be := back.entries[1]
|
|||
|
|
_check(back.kind == "router" and be.when == "@str >= 8" and be.when_for("aslan") == "@str > 9" and be.is_otherwise("plain") and be.count_for("aslan") == 2 and be.save_for("aslan") == "who", "the table round-trips")
|
|||
|
|
_eq(back.to_dict(), d, "to_dict(from_dict(d)) == d")
|
|||
|
|
# Canonical form keeps them, drops bad values, and drops otherwise next to a when.
|
|||
|
|
var raw := {"tables": [{"id": "x", "name": "X", "kind": "router", "entries": [
|
|||
|
|
{"id": "a", "text": "A", "when": " @a = 1 ", "pipes": [{"table": "y", "when": "@b", "otherwise": true}, {"table": "z", "otherwise": true}, {"table": "w", "otherwise": "yes"}, {"table": "v", "when": ""}]},
|
|||
|
|
{"id": "b", "text": "B", "when": ""}]}]}
|
|||
|
|
var c := JsonIO.canonical(raw)
|
|||
|
|
var ce: Dictionary = c["tables"][0]["entries"][0]
|
|||
|
|
_eq(str(c["tables"][0].get("kind", "")), "router", "canonical keeps kind router")
|
|||
|
|
_eq(str(ce.get("when", "")), "@a = 1", "canonical keeps (trimmed) entry when")
|
|||
|
|
_check(not (c["tables"][0]["entries"][1] as Dictionary).has("when"), "canonical drops an empty when")
|
|||
|
|
_eq(ce["pipes"], [{"table": "y", "when": "@b"}, {"table": "z", "otherwise": true}, {"table": "w"}, {"table": "v"}], "canonical pipe conditions")
|
|||
|
|
var text := JsonIO.export_json(b.to_dict())
|
|||
|
|
var imp := JsonIO.import_json(text)
|
|||
|
|
_check(imp["ok"], "the export imports")
|
|||
|
|
var b2 := TowerBoard.new()
|
|||
|
|
b2.load_dict(imp["collection"])
|
|||
|
|
var e2 := b2.get_table("route").entries[1]
|
|||
|
|
_check(e2.when == "@str >= 8" and e2.when_for("aslan") == "@str > 9" and e2.is_otherwise("plain"), "a JSON export round-trips conditions")
|
|||
|
|
_eq(JsonIO.pipe_label(JsonIO.pipe_info({"table": "y", "when": "@str >= 8", "count": 2, "save_as": "w"})), "if @str >= 8 ×2 → w", "edge label for a conditional pipe")
|
|||
|
|
_eq(JsonIO.pipe_label(JsonIO.pipe_info({"table": "y", "otherwise": true})), "otherwise", "edge label for an otherwise pipe")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- lint
|
|||
|
|
|
|||
|
|
func _test_lint() -> void:
|
|||
|
|
_eq(Roller.lint_pipes(_router_board()), [] as Array[Dictionary], "a clean board has no lint")
|
|||
|
|
var b := _router_board()
|
|||
|
|
var r := b.get_table("route")
|
|||
|
|
# Otherwise first: the cases after it can't be chosen.
|
|||
|
|
var other := r.entries[2]
|
|||
|
|
r.entries.remove_at(2)
|
|||
|
|
r.entries.insert(0, other)
|
|||
|
|
r.entries[1].when = "@str >= = 8"
|
|||
|
|
var start := _one("start", "Begin")
|
|||
|
|
var e := start.entries[0]
|
|||
|
|
e.add_pipe("plain")
|
|||
|
|
e.set_pipe_otherwise("plain", true)
|
|||
|
|
e.add_pipe("strong")
|
|||
|
|
e.set_pipe_when("strong", "(((")
|
|||
|
|
var lonely := _one("lonely", "Lonely")
|
|||
|
|
lonely.entries[0].add_pipe("plain")
|
|||
|
|
lonely.entries[0].set_pipe_otherwise("plain", true)
|
|||
|
|
var empty := RollTable.create(RollTable.KIND_ROUTER, "empty", "Empty")
|
|||
|
|
empty.entries.clear()
|
|||
|
|
b.tables.append_array([start, lonely, empty])
|
|||
|
|
var lint := Roller.lint_pipes(b)
|
|||
|
|
var msgs := {}
|
|||
|
|
for item: Dictionary in lint:
|
|||
|
|
_check(item.has("table_id") and item.has("entry_id") and item.has("message"), "lint items have table_id, entry_id, message")
|
|||
|
|
msgs["%s:%s" % [item["table_id"], item["entry_id"]]] = str(msgs.get("%s:%s" % [item["table_id"], item["entry_id"]], "")) + str(item["message"]) + "|"
|
|||
|
|
_check(str(msgs.get("route:c3", "")).contains("Otherwise case isn't last"), "Otherwise case not last: %s" % str(lint))
|
|||
|
|
_check(str(msgs.get("route:c1", "")).begins_with("Case @str >= = 8"), "a bad case condition: %s" % str(lint))
|
|||
|
|
_check(str(msgs.get("start:e1", "")).contains("Pipe condition (((") and str(msgs.get("start:e1", "")).contains("comes before a conditional pipe"), "a bad pipe condition, and otherwise before when: %s" % str(lint))
|
|||
|
|
_check(str(msgs.get("lonely:e1", "")).contains("always runs"), "otherwise with no conditional pipe: %s" % str(lint))
|
|||
|
|
_check(str(msgs.get("empty:", "")).contains("no cases"), "a router with no cases: %s" % str(lint))
|
|||
|
|
_eq(lint.size(), 6, "six lint items")
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _test_cycles() -> void:
|
|||
|
|
# A table whose only pipe back to itself is conditional can finish.
|
|||
|
|
var link := _one("link", "Link", "@k += 1")
|
|||
|
|
link.entries[0].add_pipe("link")
|
|||
|
|
link.entries[0].set_pipe_when("link", "@k < 3")
|
|||
|
|
_eq(Roller.endless_cycles(_board([link])), [] as Array[String], "a conditional self-pipe is not an endless cycle")
|
|||
|
|
var r := RollTable.create(RollTable.KIND_ROUTER, "r", "R")
|
|||
|
|
r.entries[0].add_pipe("r")
|
|||
|
|
_eq(Roller.endless_cycles(_board([r])), [] as Array[String], "a router with a case that finishes is fine")
|
|||
|
|
r.entries[1].add_pipe("r")
|
|||
|
|
_eq(Roller.endless_cycles(_board([r])), ["r"] as Array[String], "a router whose every case loops back is endless")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- mini board
|
|||
|
|
|
|||
|
|
## The workaround a big board used before routers: the Species choice's
|
|||
|
|
## answer sets a chain flag, and two pipes count on it (1 or 0 times). With
|
|||
|
|
## lazy counts, the second count was read after the first branch had run, so
|
|||
|
|
## a branch that changed @species made both fire. The router version checks
|
|||
|
|
## once and picks one case.
|
|||
|
|
func _species_board(use_router: bool) -> TowerBoard:
|
|||
|
|
var species := RollTable.create(RollTable.KIND_CHOICE, "species", "Species", "pick")
|
|||
|
|
species.save_as = "@species"
|
|||
|
|
species.entries[0].text = "Aslan"
|
|||
|
|
species.entries[1].text = "Human"
|
|||
|
|
species.entries.resize(2)
|
|||
|
|
# The Aslan branch rewrites @species (e.g. a clan name), which used to
|
|||
|
|
# confuse the flag on the second pipe.
|
|||
|
|
var aslan := _one("aslan", "Clan training", "@species = \"Aslan (Hkasuao)\"; @path += \"aslan\"")
|
|||
|
|
var human := _one("human", "Human schooling", "@path += \"human\"")
|
|||
|
|
var sheet := RollTable.create(RollTable.KIND_SHEET, "sheet", "Sheet")
|
|||
|
|
sheet.entries[0].text = "{@species}: {@path}"
|
|||
|
|
var tables: Array = [species, aslan, human, sheet]
|
|||
|
|
for e in species.entries:
|
|||
|
|
if use_router:
|
|||
|
|
e.add_pipe("route")
|
|||
|
|
else:
|
|||
|
|
e.effects = "is_aslan = if(@species = \"Aslan\", 1, 0)"
|
|||
|
|
e.add_pipe("aslan", "if(@species = \"Aslan\", 1, 0)")
|
|||
|
|
e.add_pipe("human", "if(@species = \"Aslan\", 0, 1)")
|
|||
|
|
e.add_pipe("sheet")
|
|||
|
|
if use_router:
|
|||
|
|
var route := RollTable.create(RollTable.KIND_ROUTER, "route", "Which upbringing")
|
|||
|
|
route.entries[0].text = "Aslan upbringing"
|
|||
|
|
route.entries[0].when = "@species = \"Aslan\""
|
|||
|
|
route.entries[0].add_pipe("aslan")
|
|||
|
|
route.entries[1].text = "Human upbringing"
|
|||
|
|
route.entries[1].add_pipe("human")
|
|||
|
|
for c in route.entries:
|
|||
|
|
c.add_pipe("sheet")
|
|||
|
|
tables.append(route)
|
|||
|
|
return _board(tables)
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _pick(index: int) -> Callable:
|
|||
|
|
return func(_req: Dictionary) -> Variant: return index
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _test_mini_board() -> void:
|
|||
|
|
for use_router in [false, true]:
|
|||
|
|
for pick in [0, 1]:
|
|||
|
|
var b := _species_board(use_router)
|
|||
|
|
var vars := {}
|
|||
|
|
var ctx := Roller.new_context(b, _rng(), vars)
|
|||
|
|
ctx.prompt = _pick(pick)
|
|||
|
|
var tree: Dictionary = await Roller.roll_async(b, "species", ctx)
|
|||
|
|
var ids := _ids(tree)
|
|||
|
|
var label := "%s, picked %s" % ["router" if use_router else "workaround", "Aslan" if pick == 0 else "Human"]
|
|||
|
|
_check(ids.has("aslan") != ids.has("human"), "%s: exactly one branch runs: %s" % [label, str(ids)])
|
|||
|
|
_check(ids.has("aslan" if pick == 0 else "human"), "%s: the right branch: %s" % [label, str(ids)])
|
|||
|
|
_eq(_var(vars, "path"), "aslan" if pick == 0 else "human", "%s: @path" % label)
|
|||
|
|
_check(_errors(tree).is_empty(), "%s: no errors" % label)
|
|||
|
|
if use_router:
|
|||
|
|
_check(ids.has("route") and ids.has("sheet"), "%s: through the router to the sheet" % label)
|
|||
|
|
# The router version lints clean and has no cycles.
|
|||
|
|
_eq(Roller.lint_pipes(_species_board(true)), [] as Array[Dictionary], "the router board lints clean")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- UI helpers
|
|||
|
|
|
|||
|
|
func _test_ui_helpers() -> void:
|
|||
|
|
_eq(PipePopover.when_error(""), "", "an empty Only if is fine (always)")
|
|||
|
|
_eq(PipePopover.when_error("@str >= 8"), "", "a valid condition")
|
|||
|
|
_check(PipePopover.when_error("@str >= = 8") != "", "a bad condition is caught")
|
|||
|
|
var r := RollTable.create(RollTable.KIND_ROUTER, "r", "R")
|
|||
|
|
_eq(TableCard.case_label(r.entries[0]), "if " + RollTable.ROUTER_DEFAULT_WHEN, "case label")
|
|||
|
|
_eq(TableCard.case_label(r.entries[1]), "Otherwise", "Otherwise label")
|
|||
|
|
var bad := TableSettingsDialog.apply_settings(r, {"name": "R", "cases": ["@a = = 1", ""]})
|
|||
|
|
_check(not bad["ok"] and str(bad["error"]).begins_with("Case 1"), "settings refuse a bad case: %s" % str(bad))
|
|||
|
|
var ok := TableSettingsDialog.apply_settings(r, {"name": "R", "cases": ["@a = 1", ""], "card_mode": true, "ranged": true})
|
|||
|
|
_check(ok["ok"] and r.entries[0].when == "@a = 1" and not r.card_mode and not r.is_ranged(), "settings set case conditions; no card mode or ranges: %s" % str(ok))
|
|||
|
|
_check(TableSettingsDialog.router_hint(["", "@a"]) != "" and TableSettingsDialog.router_hint(["@a", ""]) == "", "router order hint")
|
|||
|
|
_eq(TablePreview.kind_line(r), "Router · if / else if / otherwise", "preview kind line")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------------- checks
|
|||
|
|
|
|||
|
|
func _eq(got: Variant, want: Variant, what: String) -> void:
|
|||
|
|
_check(typeof(got) == typeof(want) and got == want or (got is Array and want is Array and str(got) == str(want)), "%s\n got: %s\n want: %s" % [what, str(got), str(want)])
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _check(ok: bool, what: String) -> void:
|
|||
|
|
if not ok:
|
|||
|
|
_failures += 1
|
|||
|
|
print(" ✗ ", what)
|