diff --git a/src/Nullean.Entities.CsCheck/EntitySpec.cs b/src/Nullean.Entities.CsCheck/EntitySpec.cs index 60b4756..4715fd2 100644 --- a/src/Nullean.Entities.CsCheck/EntitySpec.cs +++ b/src/Nullean.Entities.CsCheck/EntitySpec.cs @@ -377,29 +377,47 @@ public void Snapshot(long expected, int maxStates = 200) /// public SpecFaultsReport Faults(Action? writeLine = null, int maxStates = 100_000, bool? throwOnUncaught = null) { - SpecFaultsReport? last = null; + var reports = new List(); foreach (var start in Starts()) { CheckStart(start); - last = Build(start, autoFaults: true).Spec.Faults(writeLine, maxStates, throwOnUncaught: throwOnUncaught ?? (_faults.Count > 0)); + reports.Add(Build(start, autoFaults: true).Spec.Faults(writeLine, maxStates, throwOnUncaught: false)); + } + ThrowIfUncaughtFromEveryStart(reports, throwOnUncaught); + return reports[^1]; + } + + // A fault only some starts can trigger (a Shipped start never takes Confirm) is uncaught from the others + // by construction; it is a gap only when no start catches it. + private void ThrowIfUncaughtFromEveryStart(List reports, bool? throwOnUncaught) + { + if (!(throwOnUncaught ?? (_faults.Count > 0)) || reports.Count == 0) + { + return; + } + var uncaught = reports[0].Uncaught.ToList(); + foreach (var report in reports.Skip(1)) + { + var here = report.Uncaught.ToHashSet(); + uncaught.RemoveAll(f => !here.Contains(f)); + } + if (uncaught.Count > 0) + { + throw new CsCheckException($"No requirement detects these faults: {string.Join(", ", uncaught)}"); } - return last!; } /// Like , sampled — for a state space cannot close. public SpecFaultsReport SampleFaults(Action? writeLine = null, string? seed = null, long iter = -1, bool? throwOnUncaught = null) { - SpecFaultsReport? last = null; + var reports = new List(); foreach (var start in Starts()) { CheckStart(start); - last = SampleCall.Run( - () => Build(start, autoFaults: true) - .Spec - .SampleFaults(writeLine, seed: seed, iter: iter, throwOnUncaught: throwOnUncaught ?? (_faults.Count > 0)) - ); + reports.Add(SampleCall.Run(() => Build(start, autoFaults: true).Spec.SampleFaults(writeLine, seed: seed, iter: iter, throwOnUncaught: false))); } - return last!; + ThrowIfUncaughtFromEveryStart(reports, throwOnUncaught); + return reports[^1]; } /// diff --git a/tests/Nullean.Entities.CsCheck.Tests/EntitySpecTests.cs b/tests/Nullean.Entities.CsCheck.Tests/EntitySpecTests.cs index ab95777..6e25d51 100644 --- a/tests/Nullean.Entities.CsCheck.Tests/EntitySpecTests.cs +++ b/tests/Nullean.Entities.CsCheck.Tests/EntitySpecTests.cs @@ -129,6 +129,23 @@ public void Faults_throws_for_a_declared_fault_no_constraint_catches() act.Should().Throw(); } + [Test] + public void Faults_with_several_starts_counts_a_fault_caught_from_any_start() + { + // With no Lock/Unlock the Closed start never reaches Locked, so only the Locked start can trigger this fault. + var spec = DoorConstraints + .All + .Spec(Door.New()) + .Action("Open", d => d.Open()) + .Action("Close", d => d.Close()) + .Fault("Close from Locked opens", "Close", (before, after) => before.State == DoorState.Locked ? new Door(DoorState.Open) : after) + .From(new Door(DoorState.Closed), new Door(DoorState.Locked)); + + var act = () => spec.Faults(); + + act.Should().NotThrow(); + } + [Test] public void Commutes_fails_for_actions_whose_order_matters_and_passes_for_ones_that_do_not() {