TableTower/tests/test_range_weights.gd
RobinFiredrake f335faa5c5 Ranged cards: edit by Weights or Ranges, whole-row drag, auto-sort
A ranged card now stores that it is ranged, so an empty card can switch
between Plain and Ranged. Cards made ranged are edited by weight (die faces
per result; ranges follow the row order); a gap becomes an empty result.
Ranges mode keeps typed ranges, rows drag with their range, and auto-sort
(on by default) keeps rows in roll order. Old cards load as Ranges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:29:52 -06:00

98 lines
3.7 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 TestBase
## Headless checks for ranged cards edited by weight or by range (RangeWeights):
## turning gaps into empty results, laying ranges out from weights, the total
## warning, auto-sort, the file format, and an empty card switching to Ranged.
## Run: godot --headless --path . -s res://tests/test_range_weights.gd
func _init() -> void:
_test_to_weights()
_test_total_warning()
_test_auto_sort()
_test_file_format()
_test_empty_card_ranged()
finish()
## The user's example: d100 with 1–5, 10–80, 81–87, 88–100 (a gap at 6–9).
func _workday() -> RollTable:
var t := RollTable.new("work", "Workday Job")
t.dice = "1d100"
for r: Array in [["Result 1", 1, 5], ["Bob", 10, 80], ["Joe", 81, 87], ["Kevin", 88, 100]]:
var e := TableEntry.new("e%d" % t.entries.size(), r[0])
e.has_range = true
e.range_lo = r[1]
e.range_hi = r[2]
t.entries.append(e)
return t
func _texts(t: RollTable) -> Array:
return t.entries.map(func(e: TableEntry) -> String: return e.text)
func _spans(t: RollTable) -> Array:
return t.entries.map(func(e: TableEntry) -> Vector2i: return Vector2i(e.range_lo, e.range_hi))
func _test_to_weights() -> void:
var t := _workday()
RangeWeights.set_mode(t, RangeWeights.WEIGHTS)
_eq(_texts(t), ["Result 1", "", "Bob", "Joe", "Kevin"], "the 6–9 gap becomes an empty result")
_eq(t.entries.map(func(e: TableEntry) -> int: return e.weight), [5, 4, 71, 7, 13], "weights are read off the ranges")
_eq(_spans(t)[1], Vector2i(6, 9), "no roll changes: the empty result covers 6–9")
_eq(t.range_problems(), "", "the weights cover the d100")
t.entries[2].weight = 70
RangeWeights.reflow(t)
_eq(_spans(t)[3], Vector2i(80, 86), "editing a weight moves the ranges after it")
func _test_total_warning() -> void:
var t := _workday()
RangeWeights.set_mode(t, RangeWeights.WEIGHTS)
t.entries[1].weight = 0
RangeWeights.reflow(t)
_eq(t.range_problems(), "Weights add up to 96 of 100: 4 short.", "a short total says so")
_check(not t.entries[1].has_range, "weight 0 gives no range")
t.entries[0].weight = 10
RangeWeights.reflow(t)
_eq(t.range_problems(), "Weights add up to 101 of 100: 1 too many.", "a long total says so")
func _test_auto_sort() -> void:
var t := _workday()
var bob := t.entries[1]
t.entries.remove_at(1)
t.entries.append(bob)
_check(RangeWeights.after_edit(t), "auto-sort reports that it moved rows")
_eq(_texts(t), ["Result 1", "Bob", "Joe", "Kevin"], "auto-sort puts the rows in roll order")
t.auto_sort = false
t.entries.remove_at(1)
t.entries.append(bob)
_check(not RangeWeights.after_edit(t), "with auto-sort off nothing moves")
_eq(_texts(t), ["Result 1", "Joe", "Kevin", "Bob"], "the dragged order stays")
func _test_file_format() -> void:
var t := _workday()
var d := t.to_dict()
_check(not d.has("range_edit"), "Ranges is the default in files, so old cards load unchanged")
RangeWeights.set_mode(t, RangeWeights.WEIGHTS)
t.auto_sort = false
d = t.to_dict()
var back := RollTable.from_dict(d)
_eq(back.range_edit, RangeWeights.WEIGHTS, "Weights is saved")
_check(not back.auto_sort, "auto-sort off is saved")
var empty := RollTable.new("empty", "Empty")
empty.ranged = true
_check(RollTable.from_dict(empty.to_dict()).is_ranged(), "an empty ranged card stays ranged after saving")
func _test_empty_card_ranged() -> void:
var t := RollTable.new("new", "New card")
var res := TableSettingsRules.apply_settings(t, {"ranged": true, "dice": "1d20"})
_check(res["ok"], "the switch is accepted")
_check(t.is_ranged(), "an empty card can switch to Ranged")
_eq(t.range_edit, RangeWeights.WEIGHTS, "a card made ranged is edited by weight")
TableSettingsRules.apply_settings(t, {"ranged": false})
_check(not t.is_ranged(), "and back to Plain")