TableTower/tests/test_builder.gd

251 lines
16 KiB
GDScript3
Raw Normal View History

extends TestBase
## Headless checks for Builder (docs/PLAIN.md §2): effect and condition models
## that round-trip exactly with the stored text, the cases that don't fit a
## builder (ok: false), and known_values().
## Run: tools/run_tests.sh builder
func run() -> void:
_test_parse_effect()
_test_effect_round_trips()
_test_effect_not_ok()
_test_parse_condition()
_test_condition_round_trips()
_test_condition_not_ok()
_test_known_values()
_test_scales()
finish()
# ---------------------------------------------------------------- effects
func _test_parse_effect() -> void:
_eq(Builder.parse_effect("@luck -= 1"), {"ok": true, "op": "subtract", "var": "@luck", "amount": "1"}, "subtract")
_eq(Builder.parse_effect("@Luck+=1"), {"ok": true, "op": "add", "var": "@luck", "amount": "1"}, "add, tight, lowercased")
_eq(Builder.parse_effect("@str = 2d6"), {"ok": true, "op": "set", "var": "@str", "amount": "2d6"}, "set dice")
_eq(Builder.parse_effect("@town = \"Millbrook\""), {"ok": true, "op": "set", "var": "@town", "amount": "\"Millbrook\""}, "set text")
_eq(Builder.parse_effect("@age *= 2"), {"ok": true, "op": "multiply", "var": "@age", "amount": "2"}, "multiply")
_eq(Builder.parse_effect("@age /= 2"), {"ok": true, "op": "divide", "var": "@age", "amount": "2"}, "divide")
_eq(Builder.parse_effect("reset @skill_*"), {"ok": true, "op": "reset", "var": "@skill_*", "amount": ""}, "reset prefix")
_eq(Builder.parse_effect("reset @luck"), {"ok": true, "op": "reset", "var": "@luck", "amount": ""}, "reset")
_eq(Builder.parse_effect("@traits += \"Psionic\""), {"ok": true, "op": "append", "var": "@traits", "amount": "Psionic"}, "append")
_eq(Builder.parse_effect("@traits -= 'Psionic'"), {"ok": true, "op": "remove", "var": "@traits", "amount": "Psionic"}, "remove")
_eq(Builder.parse_effect("@contacts += option_label"), {"ok": true, "op": "add", "var": "@contacts", "amount": "option_label"}, "add a chain value")
_eq(Builder.parse_effect("@[option] += 1"), {"ok": true, "op": "add", "var": "@[option]", "amount": "1"}, "indirect")
_eq(Builder.parse_effect("reset @[ option ]"), {"ok": true, "op": "reset", "var": "@[option]", "amount": ""}, "indirect reset")
_eq(Builder.parse_effect("@skill_vacc = max(@skill_vacc, 0)"),
{"ok": true, "op": "set", "var": "@skill_vacc", "amount": "max(@skill_vacc, 0)"}, "set expression")
_eq(Builder.parse_effect("hero = \"Bob\""), {"ok": true, "op": "set", "var": "hero", "amount": "\"Bob\""}, "chain target")
_eq(Builder.effect_text({"op": "add", "var": "@luck", "amount": "1"}), "@luck += 1", "effect_text add")
_eq(Builder.effect_text({"op": "append", "var": "@traits", "amount": "Psionic"}), "@traits += \"Psionic\"", "effect_text append")
_eq(Builder.effect_text({"op": "reset", "var": "@skill_*"}), "reset @skill_*", "effect_text reset")
_eq(Builder.effect_text({"op": "bogus", "var": "@x", "amount": "1"}), "", "effect_text bad op")
_eq(Builder.effect_text({"op": "set", "var": "@x", "amount": ""}), "", "effect_text no amount")
var list := Builder.parse_effects("@a += 1; @b = \"x;y\"\nnonsense")
_eq(list.size(), 3, "parse_effects splits")
_eq(str(list[1]["amount"]), "\"x;y\"", "a ; inside quotes stays")
_check(not list[2]["ok"] and list[2]["src"] == "nonsense", "bad statement kept with src")
func _test_effect_round_trips() -> void:
var models := [
{"ok": true, "op": "set", "var": "@str", "amount": "2d6"},
{"ok": true, "op": "set", "var": "@town", "amount": "\"Millbrook\""},
{"ok": true, "op": "set", "var": "@x", "amount": "-1"},
{"ok": true, "op": "set", "var": "@x", "amount": "if(@a > 1, 2, 3)"},
{"ok": true, "op": "add", "var": "@luck", "amount": "1"},
{"ok": true, "op": "add", "var": "@credits", "amount": "1d6 * 1000"},
{"ok": true, "op": "subtract", "var": "@luck", "amount": "1"},
{"ok": true, "op": "multiply", "var": "@age", "amount": "2"},
{"ok": true, "op": "divide", "var": "@age", "amount": "3"},
{"ok": true, "op": "reset", "var": "@luck", "amount": ""},
{"ok": true, "op": "reset", "var": "@skill_*", "amount": ""},
{"ok": true, "op": "reset", "var": "@[option]", "amount": ""},
{"ok": true, "op": "append", "var": "@traits", "amount": "Psionic"},
{"ok": true, "op": "append", "var": "@traits", "amount": "Said \"hi\""},
{"ok": true, "op": "append", "var": "@traits", "amount": "a\\b"},
{"ok": true, "op": "remove", "var": "@traits", "amount": "Old Bill"},
{"ok": true, "op": "add", "var": "@[option]", "amount": "1"},
{"ok": true, "op": "set", "var": "@[\"skill_\" + option]", "amount": "max(@[\"skill_\" + option], 0)"},
{"ok": true, "op": "add", "var": "count", "amount": "1"},
{"ok": true, "op": "add", "var": "@contacts", "amount": "option_label"},
]
for m: Dictionary in models:
var text := Builder.effect_text(m)
_eq(Builder.parse_effect(text), m, "round trip %s" % text)
# Text -> model -> text normalizes spacing only.
for src: String in ["@luck-=1", "@str=2d6", "reset @skill_*", "@traits+=\"X\""]:
var m := Builder.parse_effect(src)
var again := Builder.parse_effect(Builder.effect_text(m))
_eq(again, m, "text round trip %s" % src)
var r1 := Effects.run(src, RollContext.new({}), "")
_check(r1.size() == 1, "runs: %s" % src)
func _test_effect_not_ok() -> void:
for src: String in ["", "hello", "@x += (", "@x ==", "reset luck", "reset @x @y", "@[option += 1", "@x += ", "3 = @x", "@[ ] = 1"]:
var m := Builder.parse_effect(src)
_check(not m["ok"], "not ok: \"%s\" gave %s" % [src, m])
_check(str(m.get("error", "")) != "", "error text for \"%s\"" % src)
# ---------------------------------------------------------------- conditions
func _test_parse_condition() -> void:
_eq(Builder.parse_condition("@str >= 8"), {"ok": true, "join": "and", "clauses": [{"var": "@str", "cmp": "ge", "value": "8"}]}, "ge")
_eq(Builder.parse_condition("@x = 0 and @soc >= 9"), {"ok": true, "join": "and", "clauses": [
{"var": "@x", "cmp": "eq", "value": "0"}, {"var": "@soc", "cmp": "ge", "value": "9"}]}, "and")
_eq(Builder.parse_condition("@a == 1 || @b != @c"), {"ok": true, "join": "or", "clauses": [
{"var": "@a", "cmp": "eq", "value": "1"}, {"var": "@b", "cmp": "ne", "value": "@c"}]}, "|| and ==")
_eq(Builder.parse_condition("has(@traits, \"Psionic\")"), {"ok": true, "join": "and", "clauses": [
{"var": "@traits", "cmp": "has", "value": "\"Psionic\""}]}, "has")
_eq(Builder.parse_condition("@commissioned"), {"ok": true, "join": "and", "clauses": [{"var": "@commissioned", "cmp": "set", "value": ""}]}, "set")
_eq(Builder.parse_condition("not @x"), {"ok": true, "join": "and", "clauses": [{"var": "@x", "cmp": "unset", "value": ""}]}, "unset")
_eq(Builder.parse_condition("!@x"), {"ok": true, "join": "and", "clauses": [{"var": "@x", "cmp": "unset", "value": ""}]}, "! unset")
_eq(Builder.parse_condition("@luck < -1"), {"ok": true, "join": "and", "clauses": [{"var": "@luck", "cmp": "lt", "value": "-1"}]}, "negative")
_eq(Builder.parse_condition("option_value > 2.5"), {"ok": true, "join": "and", "clauses": [{"var": "option_value", "cmp": "gt", "value": "2.5"}]}, "chain var")
_eq(Builder.parse_condition("@[option] <= tier"), {"ok": true, "join": "and", "clauses": [{"var": "@[option]", "cmp": "le", "value": "tier"}]}, "indirect and chain value")
_eq(Builder.parse_condition(" "), {"ok": true, "join": "and", "clauses": []}, "empty")
_eq(Builder.condition_text({"join": "or", "clauses": [{"var": "@a", "cmp": "eq", "value": "1"}, {"var": "@b", "cmp": "set", "value": ""}]}),
"@a = 1 or @b", "condition_text")
_eq(Builder.condition_text({"join": "and", "clauses": []}), "", "condition_text empty")
_eq(Builder.condition_text({"join": "and", "clauses": [{"var": "@a", "cmp": "zz", "value": "1"}]}), "", "condition_text bad cmp")
func _test_condition_round_trips() -> void:
var models := [
{"ok": true, "join": "and", "clauses": []},
{"ok": true, "join": "and", "clauses": [{"var": "@str", "cmp": "ge", "value": "8"}]},
{"ok": true, "join": "and", "clauses": [{"var": "@str", "cmp": "le", "value": "-3"}]},
{"ok": true, "join": "and", "clauses": [{"var": "@str", "cmp": "gt", "value": "@dex"}]},
{"ok": true, "join": "and", "clauses": [{"var": "@str", "cmp": "lt", "value": "2.5"}]},
{"ok": true, "join": "and", "clauses": [{"var": "@species", "cmp": "eq", "value": "\"Aslan\""}]},
{"ok": true, "join": "and", "clauses": [{"var": "@species", "cmp": "ne", "value": "'Vargr'"}]},
{"ok": true, "join": "and", "clauses": [{"var": "@traits", "cmp": "has", "value": "\"Psionic\""}]},
{"ok": true, "join": "and", "clauses": [{"var": "@traits", "cmp": "has", "value": "option"}]},
{"ok": true, "join": "and", "clauses": [{"var": "@commissioned", "cmp": "set", "value": ""}]},
{"ok": true, "join": "and", "clauses": [{"var": "@commissioned", "cmp": "unset", "value": ""}]},
{"ok": true, "join": "and", "clauses": [{"var": "@x", "cmp": "eq", "value": "true"}]},
{"ok": true, "join": "or", "clauses": [{"var": "@a", "cmp": "eq", "value": "1"}, {"var": "@b", "cmp": "ne", "value": "2"},
{"var": "tier", "cmp": "ge", "value": "3"}]},
{"ok": true, "join": "and", "clauses": [{"var": "@[option]", "cmp": "ge", "value": "1"}, {"var": "option_value", "cmp": "lt", "value": "@cap"}]},
]
for m: Dictionary in models:
var text := Builder.condition_text(m)
_eq(Builder.parse_condition(text), m, "round trip \"%s\"" % text)
if text != "":
_eq(Expr.check(text), "", "parses as an expression: %s" % text)
for src: String in ["@str>=8", "@a==1 and @b=2", "has( @t ,\"x\" )", "!@x", "@a = 1 && @b = 2"]:
var m := Builder.parse_condition(src)
_check(m["ok"], "ok: %s" % src)
_eq(Builder.parse_condition(Builder.condition_text(m)), m, "text round trip %s" % src)
func _test_condition_not_ok() -> void:
for src: String in ["@a = 1 and @b = 2 or @c = 3", "(@a = 1)", "count(@skill_*) > 0", "1d6 >= 4", "@a + 1 > 2",
"8 <= @str", "@x = \"open", "max(@a, 1) > 2", "@a >", "@a = 1 and", "not has(@t, \"x\")", "@a = 2d6", "has(@t)"]:
var m := Builder.parse_condition(src)
_check(not m["ok"], "not ok: \"%s\" gave %s" % [src, m])
# ---------------------------------------------------------------- known_values
func _test_known_values() -> void:
var b := TowerBoard.new()
var luck := RollTable.create(RollTable.KIND_VARIABLE, "v_luck", "Luck")
luck.var_name = "luck"
b.tables.append(luck)
var town := RollTable.create(RollTable.KIND_VARIABLE, "v_town", "Home Town", "text")
town.var_name = "town"
b.tables.append(town)
b.tables.append(RollTable.create(RollTable.KIND_VARIABLE, "v_skills", "Skills", "collection"))
var start := RollTable.new("start", "Start")
var e := TableEntry.new("e1", "[[Names -> hero]]")
e.effects = "@skill_pilot += 1; @traits += \"Psionic\"; @credits = 100; @luck -= 1; @[option] += 1; reset @x_*"
e.add_pipe("tier", 1, "@weapon")
e.set_pipe_when("tier", "@str >= 8")
e.set_pipe_with("tier", {"tier": "@level + 1"})
start.entries.append(e)
b.tables.append(start)
var tier := RollTable.create(RollTable.KIND_CHECK, "tier", "Tier")
tier.inputs = [{"name": "tier", "label": "Tier", "default": "1"}]
b.tables.append(tier)
var ask := RollTable.create(RollTable.KIND_CHOICE, "ask", "How many", "number")
ask.save_as = "@terms"
b.tables.append(ask)
var vals := Builder.known_values(b)
var by := {}
var order: Array = []
for v: Dictionary in vals:
by[v["name"]] = v
order.append(v["name"])
for k in ["name", "label", "kind", "source"]:
_check(v.has(k), "%s has %s" % [v.get("name"), k])
_eq(order.slice(0, 3), ["@luck", "@town", "@skill_*"], "variable cards first, then spreads")
_eq(by["@luck"], {"name": "@luck", "label": "Luck", "kind": "number", "source": "variable", "table_id": "v_luck"}, "a variable card")
_eq(by["@town"]["kind"], "text", "text variable")
_eq(by["@town"]["label"], "Home Town", "label from the card")
_eq(by["@skill_*"]["kind"], "collection", "spread kind")
_eq(by["@skill_*"]["label"], "Skills", "spread label")
_eq(by["@skill_pilot"]["label"], "Pilot", "member label")
_eq(by["@skill_pilot"]["collection"], "@skill_*", "member knows its spread")
_eq(by["@skill_pilot"]["source"], "effect", "effect target")
_eq(by["@traits"]["kind"], "list", "append makes a list")
_eq(by["@credits"]["kind"], "number", "number effect")
_eq(by["@weapon"]["source"], "save_as", "thread save_as")
_eq(by["hero"]["source"], "save_as", "[[ -> hero]] save")
_eq(by["@terms"]["kind"], "number", "number choice save_as")
_eq(by["tier"]["source"], "input", "card input")
_eq(by["tier"]["label"], "Tier", "input label")
_eq(by["@str"]["source"], "read", "value only read")
_eq(by["@level"]["source"], "read", "value read by a with")
_check(by.has("answer") and by["answer"]["source"] == "chain", "answer from a number choice")
_check(by.has("nat") and by.has("total") and by.has("effect"), "check chain names")
_check(not by.has("i") and not by.has("option"), "no loop, no dynamic pick")
_check(not by.has("@[option]") and not by.has("@x_*"), "no indirect or prefix targets")
_eq(Builder.known_values(null), [] as Array[Dictionary], "null spread")
# ---------------------------------------------------------------- scales
func _scale_board() -> TowerBoard:
var b := TowerBoard.new()
var c := RollTable.create(RollTable.KIND_VARIABLE, "v_climate", "Climate", RollTable.VAR_SCALE)
c.steps.assign(["Supercold", "Cold", "Temperate", "Hot", "Superhot"])
b.tables.append(c)
return b
func _test_scales() -> void:
var b := _scale_board()
_eq(Builder.parse_effect("@climate += 1", b), {"ok": true, "op": "step_up", "var": "@climate", "amount": "1"}, "step up on a scale")
_eq(Builder.parse_effect("@climate -= 2", b), {"ok": true, "op": "step_down", "var": "@climate", "amount": "2"}, "step down")
_eq(Builder.parse_effect("@climate += 1"), {"ok": true, "op": "add", "var": "@climate", "amount": "1"}, "without the board it's add")
_eq(Builder.parse_effect("@luck += 1", b), {"ok": true, "op": "add", "var": "@luck", "amount": "1"}, "not a scale: add")
_eq(Builder.parse_effect("@climate = \"Hot\"", b), {"ok": true, "op": "set", "var": "@climate", "amount": "\"Hot\""}, "set a step")
_eq(Builder.parse_effect("@climate = toward(@climate, \"Temperate\")"),
{"ok": true, "op": "toward", "var": "@climate", "amount": "Temperate"}, "toward")
_eq(Builder.parse_effect("@atmo = max(@atmo, \"Medium\")"), {"ok": true, "op": "at_least", "var": "@atmo", "amount": "Medium"}, "at least")
_eq(Builder.parse_effect("@atmo = min(@atmo, 'Thin')"), {"ok": true, "op": "at_most", "var": "@atmo", "amount": "Thin"}, "at most")
_eq(Builder.parse_effect("@atmo = max(@other, \"Medium\")")["op"], "set", "max of another value stays set")
_eq(Builder.parse_effect("@atmo = max(@atmo, 0)")["op"], "set", "max with a number stays set")
_eq(Builder.effect_text({"op": "toward", "var": "@climate", "amount": "Temperate"}), "@climate = toward(@climate, \"Temperate\")", "toward text")
_eq(Builder.effect_text({"op": "step_down", "var": "@climate", "amount": "1"}), "@climate -= 1", "step down text")
_eq(Builder.effect_text({"op": "at_most", "var": "@climate", "amount": ""}), "", "no step: no text")
for m: Dictionary in [
{"ok": true, "op": "step_up", "var": "@climate", "amount": "1"},
{"ok": true, "op": "step_down", "var": "@climate", "amount": "3"},
{"ok": true, "op": "toward", "var": "@climate", "amount": "Temperate"},
{"ok": true, "op": "at_least", "var": "@climate", "amount": "Cold"},
{"ok": true, "op": "at_most", "var": "@climate", "amount": "Semi-arid"},
{"ok": true, "op": "set", "var": "@climate", "amount": "\"Hot\""}]:
_eq(Builder.parse_effect(Builder.effect_text(m), b), m, "scale round trip " + str(m["op"]))
var kinds := {}
for k: Dictionary in Builder.known_values(b):
kinds[k["name"]] = k["kind"]
_eq(kinds.get("@climate"), "scale", "known_values: a scale card's kind is scale")
var fresh := RollTable.create(RollTable.KIND_VARIABLE, "s", "Mood", RollTable.VAR_SCALE)
_eq(fresh.steps, ["Low", "Medium", "High"], "a new scale gets default steps")
_eq(fresh.initial, "Low", "and starts at the first")