A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
fix(sim): validate anchor and status values (I2)
eyalgolanfaf17d359ac8
diff --git a/sim/Statecraft.Sim.Tests/ValidationTests.cs b/sim/Statecraft.Sim.Tests/ValidationTests.csindex c7644f59a..a7dc41b1f 100644--- a/sim/Statecraft.Sim.Tests/ValidationTests.cs+++ b/sim/Statecraft.Sim.Tests/ValidationTests.cs@@ -62,4 +62,35 @@ status: draftAssert.Equal(field, ex.Field);Assert.EndsWith(message, ex.Message);}++ // I2: enum-like fields.+ [Theory]+ [InlineData("events/drought.yaml", "anchor: soft", "anchor: hrad", "events[drought].anchor")]+ [InlineData("events/drought.yaml", "anchor: soft", "anchor: Hard", "events[drought].anchor")]+ [InlineData("events/drought.yaml", "status: draft", "status: done", "events[drought].status")]+ [InlineData("people.yaml", " loyalty: 9\n", " loyalty: 9\n status: checked\n", "people[pm_one].status")]+ [InlineData("legislatures.yaml", " size: 10\n", " size: 10\n status: final\n", "legislatures[house].status")]+ [InlineData("governments.yaml", " coalition: [alpha]\n", " coalition: [alpha]\n status: Fact-Checked\n", "governments[gov1].status")]+ [InlineData("aliyah.yaml", " origins: { europe: 230, asia: 0 }\n", " origins: { europe: 230, asia: 0 }\n status: ok\n", "series[0].status")]+ [InlineData("population.yaml", "anchors: []", "anchors:\n - date: 1948-11-08\n jews: 700\n status: ''\n", "anchors[0].status")]+ public void EnumLikeFieldOutsideItsValuesIsAContentError(string file, string from, string to, string field)+ {+ var ex = Bad(file, from, to);+ Assert.Equal(file, ex.File);+ Assert.Equal(field, ex.Field);+ }++ [Theory]+ [InlineData("hard")]+ [InlineData("soft")]+ public void BothAnchorsLoad(string anchor) =>+ ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["drought.yaml"] = Event.Replace("anchor: soft", "anchor: " + anchor) }));++ [Theory]+ [InlineData("draft")]+ [InlineData("fact-checked")]+ [InlineData("historian-reviewed")]+ [InlineData("sensitivity-reviewed")]+ public void EveryStatusLoads(string status) =>+ ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["drought.yaml"] = Event.Replace("status: draft", "status: " + status) }));}diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 5eeb3471f..23381b1cc 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -115,6 +115,14 @@ namespace Statecraft.Simif (!seen.Add(id)) throw new ContentException(file, $"{list}[{id}].id", "duplicate id");}+ static readonly HashSet<string> Statuses = new HashSet<string> { "draft", "fact-checked", "historian-reviewed", "sensitivity-reviewed" };++ internal static void Status(string file, string field, string status)+ {+ if (!Statuses.Contains(status))+ throw new ContentException(file, field, $"unknown status '{status}' (must be draft, fact-checked, historian-reviewed or sensitivity-reviewed)");+ }+static void Validate(Content c){var offices = new HashSet<string>(c.Offices.Select(o => o.Id));@@ -137,6 +145,12 @@ namespace Statecraft.Simforeach (var p in c.Policies)if (indicators.Contains(p.Id)) throw new ContentException("model.yaml", $"policies[{p.Id}].id", "same id as an indicator (effect sources would be ambiguous)");+ foreach (var p in c.People) Status("people.yaml", $"people[{p.Id}].status", p.Status);+ foreach (var l in c.Legislatures) Status("legislatures.yaml", $"legislatures[{l.Id}].status", l.Status);+ foreach (var g in c.Governments) Status("governments.yaml", $"governments[{g.Id}].status", g.Status);+ for (int i = 0; i < c.Aliyah.Count; i++) Status("aliyah.yaml", FormattableString.Invariant($"series[{i}].status"), c.Aliyah[i].Status);+ for (int i = 0; i < c.PopulationAnchors.Count; i++) Status("population.yaml", FormattableString.Invariant($"anchors[{i}].status"), c.PopulationAnchors[i].Status);+foreach (var p in c.People){if (p.Party != "" && !parties.Contains(p.Party)) throw new ContentException("people.yaml", $"people[{p.Id}].party", $"unknown party '{p.Party}'");diff --git a/sim/Statecraft.Sim/EventRules.cs b/sim/Statecraft.Sim/EventRules.csindex ccd37d1d8..f1700b7a7 100644--- a/sim/Statecraft.Sim/EventRules.cs+++ b/sim/Statecraft.Sim/EventRules.cs@@ -18,6 +18,8 @@ namespace Statecraft.Sim{string F(string field) => $"events[{e.Id}].{field}";if (!ids.Add(e.Id)) throw new ContentException(e.File, F("id"), "duplicate event id");+ if (e.Anchor != "hard" && e.Anchor != "soft") throw new ContentException(e.File, F("anchor"), $"unknown anchor '{e.Anchor}' (must be hard or soft)");+ ContentLoader.Status(e.File, F("status"), e.Status);if (e.Office != "" && !offices.Contains(e.Office)) throw new ContentException(e.File, F("office"), $"unknown office '{e.Office}'");CheckPlaceholders(c, e, F("title"), e.Title, offices);CheckPlaceholders(c, e, F("text"), e.Text, offices);