This commit is contained in:
Forrest 2026-10-04 12:07:36 -04:00
parent 276d43613d
commit 4298caf5cb
552 changed files with 42990 additions and 776 deletions

View file

@ -1,6 +1,7 @@
extends RefCounted
## FarmState: creation from config, exact to_dict/from_dict round trips (also through JSON),
## rejection of invalid data, snapshot independence, and night loss records.
## FarmState: creation from config (all three asset categories and the pen's gate), exact
## to_dict/from_dict round trips (also through JSON), rejection of invalid data, snapshot
## independence, and night loss records for every category.
func run(r: GameTestRunner) -> void:
@ -10,7 +11,11 @@ func run(r: GameTestRunner) -> void:
r.check(s.phase == CampaignController.Phase.MORNING, "a new farm starts in the morning")
r.check(s.scarecrow_hp == cfg.scarecrow_max_hp and s.scarecrow_max_hp == cfg.scarecrow_max_hp, "scarecrow starts at full HP")
r.check(s.crops == cfg.start_crops and s.crops_total == cfg.start_crops, "crops start at the configured count")
r.check(s.animals_total == 0 and s.supplies_total == 0, "animals and supplies are not in play until Step 8")
r.check(s.animals == cfg.start_animals and s.animals_total == cfg.start_animals, "animals start at the configured count")
var stock := cfg.start_wood + cfg.start_scrap + cfg.start_straw
r.check(s.supplies == stock and s.supplies_total == stock and not s.shed_raided, "supplies are the shed's starting stock (%d)" % stock)
r.check(s.structures.size() == cfg.start_structures.size() and s.structures[0]["type"] == "gate"
and s.structures[0]["hp"] == cfg.gate_hp and s.next_structure_id == 2, "the pen's gate stands from the start")
r.check(s.is_valid(), "a new farm is valid")
# A state with every field away from its default.
@ -20,13 +25,15 @@ func run(r: GameTestRunner) -> void:
s.phase_time_left = 12.5
s.scarecrow_hp = 6
s.crops_total = 40
s.crops = 31
s.animals_total = 8
s.animals = 5
for id in [3, 7, 8, 12, 19, 20, 21, 33, 40]:
s.lose_crop(id)
s.night_events = {"crops_stolen": 2, "crops_recovered": 1, "animals_strayed": 1, "supplies_lost": 5}
for id in [2, 5, 7]:
s.lose_animal(id)
s.supplies_total = 120
s.supplies = 77
s.night_log.append({"night": 1, "crops_lost": 4, "animals_lost": 0, "supplies_lost": 0})
s.night_log.append({"night": 2, "crops_lost": 5, "animals_lost": 3, "supplies_lost": 43})
s.shed_raided = true
s.night_log.append(_entry(1, 4, 0, 0))
s.night_log.append(_entry(2, 5, 3, 43))
var d := s.to_dict()
var copy := FarmState.from_dict(d)
@ -43,17 +50,35 @@ func run(r: GameTestRunner) -> void:
_night_records(r, cfg)
func _entry(night: int, crops: int, animals: int, supplies: int) -> Dictionary:
var e := {"night": night}
for key in FarmState.LOG_KEYS:
if key != "night":
e[key] = 0
e["crops_lost"] = crops
e["animals_lost"] = animals
e["supplies_lost"] = supplies
e["crops_stolen"] = crops + 1
e["animals_stolen"] = animals
e["supplies_stolen"] = supplies + 5
return e
func _rejects(r: GameTestRunner, good: Dictionary) -> void:
var cases := {
"another version": func(d: Dictionary) -> void: d["version"] = FarmState.VERSION + 1,
"a missing key": func(d: Dictionary) -> void: d.erase("crops"),
"a missing key": func(d: Dictionary) -> void: d.erase("lost_crops"),
"an unknown phase": func(d: Dictionary) -> void: d["phase"] = 42,
"more crops than the total": func(d: Dictionary) -> void: d["crops"] = d["crops_total"] + 1,
"negative animals": func(d: Dictionary) -> void: d["animals"] = -1,
"a lost crop id beyond the total": func(d: Dictionary) -> void: d["lost_crops"] = [d["crops_total"] + 1],
"a crop lost twice": func(d: Dictionary) -> void: d["lost_crops"] = [3, 3],
"an unknown night event": func(d: Dictionary) -> void: d["night_events"] = {"cows_tipped": 1},
"a lost animal id beyond the total": func(d: Dictionary) -> void: d["lost_animals"] = [d["animals_total"] + 1],
"an animal lost twice": func(d: Dictionary) -> void: d["lost_animals"] = [2, 2],
"negative supplies in play": func(d: Dictionary) -> void: d["supplies_total"] = -1,
"HP above max": func(d: Dictionary) -> void: d["scarecrow_hp"] = d["scarecrow_max_hp"] + 1,
"night 0": func(d: Dictionary) -> void: d["night"] = 0,
"more nights survived than played": func(d: Dictionary) -> void: d["nights_survived"] = d["night"] + 1,
"a malformed night log": func(d: Dictionary) -> void: d["night_log"] = [{"night": 1}],
"a malformed night log": func(d: Dictionary) -> void: d["night_log"] = [{"night": 1, "crops_lost": 0, "animals_lost": 0, "supplies_lost": 0}],
}
for name: String in cases:
var d := good.duplicate(true)
@ -64,8 +89,33 @@ func _rejects(r: GameTestRunner, good: Dictionary) -> void:
func _night_records(r: GameTestRunner, cfg: GameConfig) -> void:
var s := FarmState.create(cfg)
var start := s.asset_counts()
s.crops -= 7
for id in range(1, 8):
s.lose_crop(id)
s.lose_animal(3)
s.record_event("crops_stolen")
s.record_event("crops_stolen")
s.record_event("crops_recovered")
s.record_event("supplies_stolen", 5)
s.record_event("supplies_lost", 5)
s.resources.shed["wood"] = 99 # stored and spent materials don't count as losses
s.log_night(start)
var e: Dictionary = s.night_log[0]
r.check(s.night_log.size() == 1 and e["night"] == 1 and e["crops_lost"] == 7 and e["animals_lost"] == 0,
"log_night records the losses since the night started")
r.check(s.night_log.size() == 1 and e["night"] == 1 and e["crops_lost"] == 7 and e["animals_lost"] == 1,
"log_night records the crops and animals lost since the night started")
r.check(e["supplies_lost"] == 5 and e["supplies_stolen"] == 5, "supplies lost are the units carried off (%d)" % e["supplies_lost"])
r.check(e["crops_stolen"] == 2 and e["crops_recovered"] == 1 and s.night_events.is_empty(),
"log_night copies the night's theft events and clears them")
s = FarmState.create(cfg)
r.check(s.lose_crop(5) and not s.lose_crop(5) and not s.lose_crop(0) and not s.lose_crop(s.crops_total + 1),
"lose_crop accepts each known unit once")
r.check(s.crops == s.crops_total - 1 and s.is_crop_lost(5) and not s.is_crop_lost(6), "crops is derived from the lost units")
r.check(s.lose_animal(1) and not s.lose_animal(1) and not s.lose_animal(0) and s.animals == s.animals_total - 1,
"lose_animal accepts each known animal once; animals is derived")
s.supplies_in_transit = 4
r.check(s.supplies == s.resources.shed_total() + 4, "supplies count the shed's stock and the sacks still on the farm")
s.supplies_in_transit = 0
s.animals_total = 0
s.supplies_total = 0
s.lose_all_crops()
r.check(s.crops == 0 and s.is_stripped(), "losing every crop unit strips a crops-only farm")