Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Scenario tests: refuse fractional whole numbers, catch orphan snapshots (refs #296) (#361)

agamrafaeli-instincta58f9942c268
diff --git a/sim/Statecraft.Sim.Tests/GoldenManifestTests.cs b/sim/Statecraft.Sim.Tests/GoldenManifestTests.cs
index af66ea17c..01a7e44e8 100644
--- a/sim/Statecraft.Sim.Tests/GoldenManifestTests.cs
+++ b/sim/Statecraft.Sim.Tests/GoldenManifestTests.cs
@@ -20,6 +20,19 @@ public class GoldenManifestTests
Assert.All(m.Values, h => Assert.Matches("^[0-9a-f]{64}$", h));
}
+ [Fact]
+ public void EveryScenarioSnapshotHasItsScenarioFile()
+ {
+ var names = System.IO.Directory.GetFiles(Scenario.Dir(), "*.yaml").Select(f => System.IO.Path.GetFileNameWithoutExtension(f)).ToHashSet();
+ var snapshots = System.IO.Directory.GetFiles(System.IO.Path.Combine(Repo, "sim", "Statecraft.Sim.Tests", "golden"), "scenario-*.txt");
+ Assert.NotEmpty(snapshots);
+ foreach (var f in snapshots)
+ {
+ var n = System.IO.Path.GetFileNameWithoutExtension(f).Substring("scenario-".Length);
+ Assert.True(names.Contains(n), $"golden/{System.IO.Path.GetFileName(f)} has no scenarios/{n}.yaml: delete the snapshot with its scenario");
+ }
+ }
+
[Fact]
public void TheLibraryConstantsUnityComparesAgainstEqualTheManifest()
{
diff --git a/sim/Statecraft.Sim.Tests/Scenario.cs b/sim/Statecraft.Sim.Tests/Scenario.cs
index 202ebc91c..38588454a 100644
--- a/sim/Statecraft.Sim.Tests/Scenario.cs
+++ b/sim/Statecraft.Sim.Tests/Scenario.cs
@@ -78,12 +78,20 @@ public sealed class Scenario
return new AllocateBudget(map.ToDictionary(kv => Convert.ToString(kv.Key, CultureInfo.InvariantCulture)!, kv => double.Parse(Convert.ToString(kv.Value, CultureInfo.InvariantCulture)!, CultureInfo.InvariantCulture)));
case "choose_event_option": return new ChooseEventOption(Str(c, "event"), Str(c, "option"));
case "set_mobilization": return new SetMobilization(Num(c, "level"));
- case "security_order": return new SecurityOrder((int)Num(c, "delta"));
+ case "security_order": return new SecurityOrder(Whole(c, "delta"));
case "appoint": return new Appoint(Str(c, "office"), Str(c, "person"));
- default: return new MapAction(Str(c, "action"), (int)Num(c, "q"), (int)Num(c, "r"));
+ default: return new MapAction(Str(c, "action"), Whole(c, "q"), Whole(c, "r"));
}
}
+ /// <summary>A whole number: 1.5 is refused instead of silently becoming 1.</summary>
+ static int Whole(Dictionary<string, object> c, string key)
+ {
+ var v = Num(c, key);
+ if (v != Math.Floor(v)) throw new InvalidDataException($"'{key}' must be a whole number, got {v.ToString(CultureInfo.InvariantCulture)}");
+ return (int)v;
+ }
+
/// <summary>Plays the scenario and returns the final state. Fails with the date, the order and the sim's reason when a command is rejected.</summary>
public GameState Play(Content c)
{
diff --git a/sim/Statecraft.Sim.Tests/ScenarioTests.cs b/sim/Statecraft.Sim.Tests/ScenarioTests.cs
index 6e818401b..b45b95dd7 100644
--- a/sim/Statecraft.Sim.Tests/ScenarioTests.cs
+++ b/sim/Statecraft.Sim.Tests/ScenarioTests.cs
@@ -58,6 +58,13 @@ public class ScenarioTests
Assert.Contains("never ran", Assert.Throws<InvalidOperationException>(() => sc.Play(C)).Message);
}
+ [Theory]
+ [InlineData(" - { kind: security_order, delta: 1.5 }\n", "'delta' must be a whole number")]
+ [InlineData(" - { kind: map_action, action: housing, q: 2.5, r: 1 }\n", "'q' must be a whole number")]
+ [InlineData(" - { kind: map_action, action: housing, q: 2, r: 1.1 }\n", "'r' must be a whole number")]
+ public void AFractionalWholeNumberIsRefusedNotTruncated(string command, string expected) =>
+ Assert.Contains(expected, Assert.Throws<InvalidDataException>(() => Scenario.Parse(Head + command)).Message);
+
[Theory]
[InlineData(" - { kind: fly_to_moon }\n", "unknown command kind 'fly_to_moon'")]
[InlineData(" - { kind: set_policy, policy: rationing }\n", "needs 'level'")]