343 lines
18 KiB
GDScript
343 lines
18 KiB
GDScript
extends TestBase
|
||
## Headless checks for Describe (docs/PLAIN.md §1 and §3): plain phrases for
|
||
## every effect op, the condition forms, dice, dynamic options, pipes, check
|
||
## cards and why facts, with labels from variable cards, collections and card
|
||
## inputs, and the fallbacks to the original text.
|
||
## Run: godot --headless --path . -s res://tests/test_describe.gd
|
||
|
||
|
||
func _init() -> void:
|
||
_test_labels()
|
||
_test_effects()
|
||
_test_conditions()
|
||
_test_dice()
|
||
_test_options()
|
||
_test_pipes()
|
||
_test_check()
|
||
_test_why_facts()
|
||
_test_why_from_rolls()
|
||
_test_fallbacks()
|
||
_test_rules_view()
|
||
finish()
|
||
|
||
|
||
# ---------------------------------------------------------------- helpers
|
||
|
||
|
||
|
||
func _var_card(id: String, name: String, var_name: String, type: String = "number") -> RollTable:
|
||
var t := RollTable.create(RollTable.KIND_VARIABLE, id, name, type)
|
||
t.var_name = var_name
|
||
return t
|
||
|
||
|
||
func _one(id: String, name: String = "") -> RollTable:
|
||
var t := RollTable.new(id, name if name != "" else id.capitalize())
|
||
t.entries.append(TableEntry.new("e1", "x"))
|
||
return t
|
||
|
||
|
||
## A board with labelled values: Strength (@str), Social Standing (@soc), Luck
|
||
## (@luck), Town (@town, text), Traits (@traits, list), the Skills collection
|
||
## (@skill_*), and Tier 3 / Weapons tables. Tier 3 declares the input tier
|
||
## ("Tier").
|
||
func _board() -> TowerBoard:
|
||
var b := TowerBoard.new()
|
||
b.id = "describe"
|
||
b.tables.append(_var_card("v_str", "Strength", "str"))
|
||
b.tables.append(_var_card("v_soc", "Social Standing", "soc"))
|
||
b.tables.append(_var_card("v_luck", "Luck", "luck"))
|
||
b.tables.append(_var_card("v_town", "Town", "town", "text"))
|
||
b.tables.append(_var_card("v_traits", "Traits", "traits", "list"))
|
||
b.tables.append(RollTable.create(RollTable.KIND_VARIABLE, "v_skills", "Skills", "collection"))
|
||
var tier := _one("tier3", "Tier 3")
|
||
tier.inputs = [{"name": "tier", "label": "Tier", "default": "1"}]
|
||
b.tables.append(tier)
|
||
b.tables.append(_one("weapons", "[b]Weapons[/b]"))
|
||
return b
|
||
|
||
|
||
# ---------------------------------------------------------------- labels
|
||
|
||
func _test_labels() -> void:
|
||
var b := _board()
|
||
_eq(b.get_table("v_skills").var_name, "skill", "a spread's stem drops the final s")
|
||
_eq(Describe.label("@str", b), "Strength", "variable card label")
|
||
_eq(Describe.label("@STR", b), "Strength", "labels ignore case")
|
||
_eq(Describe.label("@skill_gun_combat", b), "Gun Combat", "spread member label")
|
||
_eq(Describe.label("@skill_*", b), "Skills", "spread label")
|
||
_eq(Describe.label("@contact_*", b), "Contact values", "a prefix without a spread")
|
||
_eq(Describe.label("@soc_x", b), "Soc X", "no spread: pretty name")
|
||
_eq(Describe.label("@dex", b), "Dex", "unlabelled value")
|
||
_eq(Describe.label("tier", b), "Tier", "input label")
|
||
_eq(Describe.label("answer", b), "Answer", "chain label")
|
||
_eq(Describe.label("nat", b), "Natural roll", "nat label")
|
||
_eq(Describe.label("@[option]", b), "Chosen value", "indirect label")
|
||
_eq(Describe.label("@str", null), "Str", "no spread")
|
||
# The variable card wins over a collection.
|
||
b.tables.append(_var_card("v_pilot", "Piloting", "skill_pilot"))
|
||
_eq(Describe.label("@skill_pilot", b), "Piloting", "card label before spread")
|
||
# A collection doesn't declare its stem as a variable.
|
||
var ctx := Roller.new_context(b, RandomNumberGenerator.new(), {})
|
||
_check(ctx.declared("skill") == null, "a spread isn't a declared variable")
|
||
_check(ctx.declared("str") != null, "a number card is")
|
||
|
||
|
||
# ---------------------------------------------------------------- effects
|
||
|
||
func _test_effects() -> void:
|
||
var b := _board()
|
||
_eq(Describe.effect("@luck -= 1", b), "Luck −1", "subtract")
|
||
_eq(Describe.effect("@skill_pilot += 1", b), "Pilot +1", "add to a spread member")
|
||
_eq(Describe.effect("@luck += -2", b), "Luck −2", "add a negative")
|
||
_eq(Describe.effect("@luck -= -2", b), "Luck +2", "subtract a negative")
|
||
_eq(Describe.effect("@town = \"Millbrook\"", b), "Town = Millbrook", "set text")
|
||
_eq(Describe.effect("@str = 2d6", b), "Strength = 2d6", "set dice")
|
||
_eq(Describe.effect("@str = 2d6 + dm(@soc)", b), "Strength = 2d6 + Social Standing modifier", "set expression")
|
||
_eq(Describe.effect("@credits += 1d6 * 1000", b), "Credits + d6 × 1000", "add expression")
|
||
_eq(Describe.effect("@age *= 2", b), "Age ×2", "multiply")
|
||
_eq(Describe.effect("@age /= 2", b), "Age ÷2", "divide")
|
||
_eq(Describe.effect("@traits += \"Psionic\"", b), "Add Psionic to Traits", "append")
|
||
_eq(Describe.effect("@traits -= \"Psionic\"", b), "Remove Psionic from Traits", "remove")
|
||
_eq(Describe.effect("reset @skill_*", b), "Reset all Skills", "reset a spread")
|
||
_eq(Describe.effect("reset @contact_*", b), "Reset all Contact values", "reset a prefix")
|
||
_eq(Describe.effect("reset @luck", b), "Reset Luck", "reset a declared value")
|
||
_eq(Describe.effect("reset @dex", b), "Clear Dex", "reset an undeclared value")
|
||
_eq(Describe.effect("@[option] += 1", b), "Chosen value +1", "indirect, no card")
|
||
var pick := RollTable.create(RollTable.KIND_CHOICE, "raise", "Raise a skill", "pick")
|
||
pick.options_from = "@skill_*"
|
||
_eq(Describe.effect("@[option] += 1", b, pick), "Chosen skill +1", "indirect on a skills card")
|
||
pick.options_from = "@contacts"
|
||
_eq(Describe.effect("@[option] += 1", b, pick), "Chosen value +1", "indirect outside a spread")
|
||
_eq(Describe.effect("hero = \"Bob\"", b), "Hero = Bob", "chain target")
|
||
_eq(Describe.effects("@luck -= 1; @str = 7\nreset @skill_*", b), ["Luck −1", "Strength = 7", "Reset all Skills"] as Array[String], "effects field")
|
||
|
||
|
||
# ---------------------------------------------------------------- conditions
|
||
|
||
func _test_conditions() -> void:
|
||
var b := _board()
|
||
_eq(Describe.condition("@str >= 8", b), "Strength is 8 or more", "ge number")
|
||
_eq(Describe.condition("@str >= @soc", b), "Strength is at least Social Standing", "ge value")
|
||
_eq(Describe.condition("@str <= 3", b), "Strength is at most 3", "le")
|
||
_eq(Describe.condition("@str > 3", b), "Strength is more than 3", "gt")
|
||
_eq(Describe.condition("@str < 3", b), "Strength is less than 3", "lt")
|
||
_eq(Describe.condition("@str != 0", b), "Strength is not 0", "ne")
|
||
_eq(Describe.condition("@town == \"Aslan\"", b), "Town is Aslan", "eq text")
|
||
_eq(Describe.condition("@x = 0 and @soc >= 9", b), "X is 0 and Social Standing is 9 or more", "and")
|
||
_eq(Describe.condition("@x = 0 or @luck < -1", b), "X is 0 or Luck is less than −1", "or, negative")
|
||
_eq(Describe.condition("has(@traits, \"Psionic\")", b), "Traits include Psionic", "has")
|
||
_eq(Describe.condition("@commissioned", b), "Commissioned is set", "set")
|
||
_eq(Describe.condition("not @commissioned", b), "Commissioned is not set", "unset")
|
||
_eq(Describe.condition("option_value >= 1", b), "Chosen value is 1 or more", "chain value")
|
||
_eq(Describe.condition("tier = 3", b), "Tier is 3", "input")
|
||
_eq(Describe.condition("@skill_pilot >= 1", b), "Pilot is 1 or more", "spread member")
|
||
_eq(Describe.condition("", b), "", "empty")
|
||
|
||
|
||
# ---------------------------------------------------------------- dice
|
||
|
||
func _test_dice() -> void:
|
||
var b := _board()
|
||
_eq(Describe.dice("2d6 + dm(@str)", b), "2d6 + Strength modifier", "2d6 + modifier")
|
||
_eq(Describe.dice("1d6 + tier", b), "d6 + Tier", "input in dice")
|
||
_eq(Describe.dice("d20", b), "d20", "d20")
|
||
_eq(Describe.dice("3d8 - 2", b), "3d8 − 2", "3d8 − 2")
|
||
_eq(Describe.dice("2D", b), "2d6", "2D input reads as 2d6")
|
||
_eq(Describe.dice("@terms", b), "Terms", "a value")
|
||
_eq(Describe.dice("-1", b), "−1", "negative number")
|
||
_eq(Describe.dice("max(@str, 3)", b), "max(Strength, 3)", "function call")
|
||
_eq(Describe.dice("(1d6 + 1) * 10", b), "(d6 + 1) × 10", "parentheses")
|
||
_eq(Describe.dice("count(@skill_*)", b), "count(Skills)", "prefix argument")
|
||
|
||
|
||
# ---------------------------------------------------------------- options
|
||
|
||
func _test_options() -> void:
|
||
var b := _board()
|
||
var t := RollTable.create(RollTable.KIND_CHOICE, "raise", "Raise", "pick")
|
||
_eq(Describe.options(t, b), "", "fixed card")
|
||
t.options_from = "@skill_*"
|
||
_eq(Describe.options(t, b), "your Skills", "spread source")
|
||
t.options_where = "option_value >= 1"
|
||
_eq(Describe.options(t, b), "your Skills, level 1 or more", "level filter")
|
||
t.options_where = "option != @last"
|
||
_eq(Describe.options(t, b), "your Skills, where Chosen option is not Last", "other filter")
|
||
t.options_where = ""
|
||
t.options_from = "@contact_*"
|
||
_eq(Describe.options(t, b), "your Contact values", "prefix without spread")
|
||
t.options_from = "@traits"
|
||
_eq(Describe.options(t, b), "your Traits", "list source")
|
||
t.options_from = "skills"
|
||
_eq(Describe.options(t, b), "skills", "bad pattern falls back")
|
||
|
||
|
||
# ---------------------------------------------------------------- pipes
|
||
|
||
func _test_pipes() -> void:
|
||
var b := _board()
|
||
var e := TableEntry.new("e1", "x")
|
||
e.add_pipe("tier3")
|
||
_eq(Describe.pipe(e, "tier3", b), "", "plain thread")
|
||
e.set_pipe_with("tier3", {"tier": "3"})
|
||
e.set_pipe_when("tier3", "@luck >= 2")
|
||
_eq(Describe.pipe(e, "tier3", b), "Tier 3 · only if Luck is 2 or more", "input and condition")
|
||
e.add_pipe("weapons", 2, "@weapon")
|
||
_eq(Describe.pipe(e, "weapons", b), "×2 · saves Weapon", "count and save")
|
||
e.set_pipe_otherwise("weapons", true)
|
||
_eq(Describe.pipe(e, "weapons", b), "otherwise · ×2 · saves Weapon", "otherwise")
|
||
e.set_pipe_count("weapons", "@terms")
|
||
_eq(Describe.pipe(e, "weapons", b), "otherwise · ×Terms · saves Weapon", "expression count")
|
||
|
||
|
||
# ---------------------------------------------------------------- check
|
||
|
||
func _test_check() -> void:
|
||
var b := _board()
|
||
var t := RollTable.create(RollTable.KIND_CHECK, "c", "Check")
|
||
_eq(Describe.check(t, b), "Roll 2d6, need 8 or more", "default check")
|
||
t.check_mod = "dm(@str)"
|
||
_eq(Describe.check(t, b), "Roll 2d6 + Strength modifier, need 8 or more", "with modifier")
|
||
t.check_cmp = "le"
|
||
t.check_target = "@soc"
|
||
t.check_mod = "-1"
|
||
_eq(Describe.check(t, b), "Roll 2d6 − 1, need at most Social Standing", "roll-under, value target")
|
||
_eq(Describe.check(_one("r"), b), "", "not a check")
|
||
|
||
|
||
# ---------------------------------------------------------------- why
|
||
|
||
func _test_why_facts() -> void:
|
||
var b := _board()
|
||
var W := func(f: Dictionary, plain: bool = true) -> String: return Describe.why(f, b, plain)
|
||
_eq(W.call({"type": "when", "to": "tier3", "expr": "@luck >= 2", "values": {"@luck": 1}, "result": false}),
|
||
"Skipped Tier 3 because Luck was 1 (needs 2 or more)", "when false")
|
||
_eq(W.call({"type": "when", "to": "tier3", "expr": "@luck >= 2", "values": {"@luck": 3}, "result": true}),
|
||
"Took Tier 3 because Luck was 3 (needs 2 or more)", "when true")
|
||
_eq(W.call({"type": "when", "to": "tier3", "expr": "@luck >= 2", "values": {"@luck": null}, "result": false}),
|
||
"Skipped Tier 3 because Luck wasn't set (needs 2 or more)", "when unset")
|
||
_eq(W.call({"type": "when", "to": "tier3", "expr": "@x = 0 and @soc >= 9", "values": {"@x": 0, "@soc": 5}, "result": false}),
|
||
"Skipped Tier 3 because X was 0 and Social Standing was 5 (rule: X is 0 and Social Standing is 9 or more)", "when, two values")
|
||
_eq(W.call({"type": "when", "to": "tier3", "expr": "1d6 >= 4", "values": {}, "result": true}),
|
||
"Took Tier 3: 1d6 >= 4 was true", "when without values")
|
||
_eq(W.call({"type": "when", "to": "tier3", "expr": "@x >", "values": {}, "result": false, "error": "Expected value"}),
|
||
"Skipped Tier 3: its condition couldn't be worked out (Expected value)", "when error")
|
||
_eq(W.call({"type": "when", "to": "tier3", "expr": "@luck >= 2", "values": {"@luck": 1}, "result": false}, false),
|
||
"Skipped tier3: when @luck >= 2 was false (@luck = 1)", "when, text mode")
|
||
_eq(W.call({"type": "otherwise", "to": "weapons", "result": true}), "Took Weapons because no condition was true", "otherwise ran")
|
||
_eq(W.call({"type": "otherwise", "to": "weapons", "result": false}), "Skipped Weapons because a condition was true", "otherwise skipped")
|
||
_eq(W.call({"type": "case", "index": 0, "expr": "@str >= 10", "values": {"@str": 7}, "result": false}),
|
||
"Case 1 didn't match because Strength was 7 (needs 10 or more)", "case false")
|
||
_eq(W.call({"type": "case", "index": 1, "expr": "has(@traits, \"Psionic\")", "values": {"@traits": "Psionic"}, "result": true}),
|
||
"Case 2 matched because Traits was Psionic (needs to include Psionic)", "case true")
|
||
_eq(W.call({"type": "case", "index": 2, "expr": "", "values": {}, "result": true}),
|
||
"Otherwise (case 3): no case before it matched", "otherwise case")
|
||
_eq(W.call({"type": "roll", "pick": "dice", "dice": "2d6", "faces": [3, 4], "nat": 7, "mod": 0, "total": 7}),
|
||
"Rolled 3 + 4 = 7", "roll")
|
||
_eq(W.call({"type": "roll", "pick": "dice", "dice": "2d6 + 1", "faces": [3.0, 4.0], "nat": 7.0, "mod": 1.0, "total": 8.0}),
|
||
"Rolled 3 + 4 = 7, +1 = 8", "roll with modifier (JSON floats)")
|
||
_eq(W.call({"type": "roll", "pick": "dice", "dice": "@terms", "faces": [], "total": 3}), "Worked out 3", "expression roll")
|
||
_eq(W.call({"type": "roll", "pick": "weighted", "dice": "d6", "total": 4}), "Rolled 4 on d6", "weighted")
|
||
_eq(W.call({"type": "roll", "pick": "card", "dice": "d6", "total": 4}), "Drew card 4", "card")
|
||
_eq(W.call({"type": "check", "dice": "2d6", "mod_expr": "dm(@str)", "target_expr": "8", "faces": [5, 1], "nat": 6, "mod": 1,
|
||
"total": 7, "target": 8, "cmp": "ge", "success": false}), "Rolled 5 + 1 = 6, +1 = 7: needed 8 or more, failure", "check")
|
||
_eq(W.call({"type": "check", "dice": "2d6", "mod_expr": "", "target_expr": "8", "faces": [6, 6], "nat": 12, "mod": 0,
|
||
"total": 12, "target": 8, "cmp": "le", "success": false}, false), "check 2d6 vs 8 (le): 12 +0 = 12 → failure", "check text mode")
|
||
_eq(W.call({"type": "effect", "var": "@luck", "op": "-=", "before": 2, "after": 1}), "Luck went from 2 to 1", "effect")
|
||
_eq(W.call({"type": "effect", "var": "@skill_pilot", "op": "+=", "before": null, "after": 1}), "Pilot became 1", "effect from unset")
|
||
_eq(W.call({"type": "effect", "var": "@dex", "op": "reset", "before": 4, "after": null}), "Dex was cleared", "reset effect")
|
||
_eq(W.call({"type": "effect", "var": "@skill_*", "op": "reset", "count": 3}), "Reset all Skills (3)", "prefix reset")
|
||
_eq(W.call({"type": "effect", "var": "@luck", "op": "-=", "before": 2, "after": 1}, false), "@luck -=: 2 → 1", "effect text mode")
|
||
_eq(W.call({"type": "input", "name": "tier", "value": 3, "from": "pipe"}), "Tier was 3 (from the thread)", "input")
|
||
_eq(W.call({"type": "input", "name": "tier", "value": 1, "from": "default"}), "Tier was 1 (default)", "input default")
|
||
_eq(W.call({"type": "show_if", "hidden": 2, "chosen_hidden": false}), "2 options hidden by their conditions", "show_if")
|
||
_eq(W.call({"type": "options_where", "from": "@skill_*", "expr": "option_value >= 1", "kept": 3, "total": 5}),
|
||
"Offered 3 of 5 Skills (level 1 or more)", "options_where")
|
||
_check(W.call({"type": "mystery", "x": 1}).contains("mystery"), "unknown facts give a dump")
|
||
|
||
|
||
## Facts recorded by a real roll come out as sentences, and survive JSON.
|
||
func _test_why_from_rolls() -> void:
|
||
var b := _board()
|
||
var start := RollTable.new("start", "Start")
|
||
var e := TableEntry.new("e1", "go")
|
||
e.effects = "@luck -= 1"
|
||
e.add_pipe("tier3")
|
||
e.set_pipe_when("tier3", "@luck >= 2")
|
||
e.add_pipe("weapons")
|
||
e.set_pipe_otherwise("weapons", true)
|
||
start.entries.append(e)
|
||
b.tables.append(start)
|
||
var vars := {"luck": {"value": 2, "source": ""}}
|
||
var ctx := Roller.new_context(b, RandomNumberGenerator.new(), vars)
|
||
var tree := Roller.roll(b, "start", ctx.rng, 0, {}, ctx)
|
||
var steps := Roller.flatten(tree)
|
||
var why: Array = steps[0].get("why", [])
|
||
var sentences: Array = []
|
||
for f: Dictionary in why:
|
||
sentences.append(Describe.why(f, b))
|
||
_check(sentences.has("Luck went from 2 to 1"), "effect fact from a roll: %s" % [sentences])
|
||
_check(sentences.has("Skipped Tier 3 because Luck was 1 (needs 2 or more)"), "when fact from a roll: %s" % [sentences])
|
||
_check(sentences.has("Took Weapons because no condition was true"), "otherwise fact from a roll: %s" % [sentences])
|
||
var back: Variant = JSON.parse_string(JSON.stringify(steps))
|
||
var why2: Array = (back as Array)[0]["why"]
|
||
var again: Array = []
|
||
for f: Dictionary in why2:
|
||
again.append(Describe.why(f, b))
|
||
_eq(again, sentences, "facts survive JSON")
|
||
|
||
|
||
# ---------------------------------------------------------------- fallbacks
|
||
|
||
func _test_fallbacks() -> void:
|
||
var b := _board()
|
||
_eq(Describe.effect("this is not a statement", b), "this is not a statement", "bad effect")
|
||
_eq(Describe.effect("@x += (", b), "@x += (", "bad amount")
|
||
_eq(Describe.condition("count(@skill_*) > 0", b), "count(@skill_*) > 0", "function condition")
|
||
_eq(Describe.condition("@a = 1 and @b = 2 or @c = 3", b), "@a = 1 and @b = 2 or @c = 3", "mixed and/or")
|
||
_eq(Describe.condition("(@a = 1)", b), "(@a = 1)", "parentheses")
|
||
_eq(Describe.condition("1d6 >= 4", b), "1d6 >= 4", "dice condition")
|
||
_eq(Describe.dice("2d6 +* \"", b), "2d6 +* \"", "unclosed quote")
|
||
_eq(Describe.dice("", b), "", "empty dice")
|
||
var long := "@a = 1 and @bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 2 and @cccccccccccccccccccccccccccccccccccccccc = 3"
|
||
_check(Describe.condition(long, b).length() <= Describe.MAX_LEN + 20, "long phrases stay short")
|
||
var huge := ""
|
||
for k in 30:
|
||
huge += "@v%d = %d and " % [k, k]
|
||
huge += "@z = 1"
|
||
_check(Describe.condition(huge, b).length() <= Describe.MAX_LEN, "very long phrases are cut")
|
||
_check(Describe.why({}, b) != null, "empty fact")
|
||
_eq(Describe.pipe(null, "x", b), "", "no entry")
|
||
_eq(Describe.options(null, b), "", "no table")
|
||
# Odd input never crashes and never comes back empty.
|
||
for junk: String in ["@", "@[", "@[]", "\"", "((((", "1d", "d", "2x", "@a +", "- - 1", "@a = = 1", "has(", "not",
|
||
"and", "@a and", "@[x] = 1 and @[", "€ = 1", "@skill_* += 1", "reset", "reset @", "@a /= 0", ",,,", "1..2", "]["]:
|
||
_check(Describe.effect(junk, b) != "", "effect junk: %s" % junk)
|
||
_check(Describe.condition(junk, b) != "", "condition junk: %s" % junk)
|
||
_check(Describe.dice(junk, b) != "", "dice junk: %s" % junk)
|
||
Builder.parse_effect(junk)
|
||
Builder.parse_condition(junk)
|
||
Describe.why({"type": "when", "to": junk, "expr": junk, "values": {junk: junk}, "result": true}, b)
|
||
Describe.why({"type": "case", "index": junk, "expr": junk, "values": junk}, b, false)
|
||
Describe.why({"type": "roll", "faces": junk, "total": junk, "mod": junk}, b)
|
||
Describe.why({"type": "check", "faces": [junk], "mod": junk}, b)
|
||
Describe.why({"type": "effect", "var": junk, "count": junk}, b)
|
||
|
||
|
||
# ---------------------------------------------------------------- display preference
|
||
|
||
func _test_rules_view() -> void:
|
||
var dir := "user://test_describe_state"
|
||
var st := StateStore.new(dir)
|
||
st.reload()
|
||
st.set_setting(StateStore.RULES_VIEW, null)
|
||
_eq(st.rules_view(), "plain", "plain by default")
|
||
_check(st.rules_plain(), "rules_plain")
|
||
st.set_rules_view("text")
|
||
_eq(st.rules_view(), "text", "text")
|
||
st.set_rules_view("bogus")
|
||
_eq(st.rules_view(), "text", "bad values are ignored")
|
||
st.save()
|
||
_eq(StateStore.new(dir).rules_view(), "text", "kept in the state file")
|
||
st.set_rules_view("plain")
|
||
st.save()
|