|
| 1 | +namespace CSharp.DocSnippets; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using FsCheck; |
| 6 | +using FsCheck.Experimental; |
| 7 | +using FsCheck.Fluent; |
| 8 | +using Microsoft.FSharp.Collections; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +public class StatefulTesting |
| 12 | +{ |
| 13 | + //[Counter] |
| 14 | + public class Counter |
| 15 | + { |
| 16 | + internal int N { get; private set; } |
| 17 | + |
| 18 | + internal Counter(int n) |
| 19 | + { |
| 20 | + N = n; |
| 21 | + } |
| 22 | + |
| 23 | + internal void Inc() |
| 24 | + { |
| 25 | + if (N <= 3) |
| 26 | + N += 1; |
| 27 | + else |
| 28 | + N = -N + 2; |
| 29 | + } |
| 30 | + |
| 31 | + internal void Dec() |
| 32 | + { |
| 33 | + if (N <= 0) |
| 34 | + throw new InvalidOperationException("Precondition fail"); |
| 35 | + |
| 36 | + N -= 1; |
| 37 | + } |
| 38 | + |
| 39 | + internal void Reset() |
| 40 | + { |
| 41 | + N = 0; |
| 42 | + } |
| 43 | + |
| 44 | + public override string ToString() => $"Counter = {N}"; |
| 45 | + } |
| 46 | + //[/Counter] |
| 47 | + |
| 48 | + //[Specification] |
| 49 | + private class CounterSpec |
| 50 | + { |
| 51 | + internal class Inc : Operation<Counter, int> |
| 52 | + { |
| 53 | + public override bool Pre(int m) |
| 54 | + { |
| 55 | + return m > 0; |
| 56 | + } |
| 57 | + |
| 58 | + public override int Run(int m) |
| 59 | + { |
| 60 | + return m + 1; |
| 61 | + } |
| 62 | + |
| 63 | + public override Property Check(Counter c, int m) |
| 64 | + { |
| 65 | + return (m == c.N).Label($"Inc: model = {m}, actual = {c.N}"); |
| 66 | + } |
| 67 | + |
| 68 | + public override string ToString() => "inc"; |
| 69 | + } |
| 70 | + |
| 71 | + internal class Dec : Operation<Counter, int> |
| 72 | + { |
| 73 | + public override bool Pre(int m) |
| 74 | + { |
| 75 | + return m > 0; |
| 76 | + } |
| 77 | + |
| 78 | + public override int Run(int m) |
| 79 | + { |
| 80 | + return m - 1; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + public override Property Check(Counter c, int m) |
| 85 | + { |
| 86 | + c.Dec(); |
| 87 | + return (m == c.N).Label($"Dec: model = {m}, actual = {c.N}"); |
| 88 | + } |
| 89 | + |
| 90 | + public override string ToString() => "dec"; |
| 91 | + } |
| 92 | + |
| 93 | + internal class CounterSetup : Setup<Counter, int> |
| 94 | + { |
| 95 | + private readonly int _initialValue; |
| 96 | + |
| 97 | + internal CounterSetup(int initialValue) |
| 98 | + { |
| 99 | + _initialValue = initialValue; |
| 100 | + } |
| 101 | + |
| 102 | + public override Counter Actual() => new(_initialValue); |
| 103 | + |
| 104 | + public override int Model() => _initialValue; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public class Specification : Machine<Counter, int> |
| 109 | + { |
| 110 | + public Specification(int maxNumberOfCommands) : base(maxNumberOfCommands) { } |
| 111 | + |
| 112 | + public override Arbitrary<Setup<Counter, int>> Setup => |
| 113 | + Arb.From(Gen.Choose(0, 3) |
| 114 | + .Select<int, Setup<Counter, int>>(i => new CounterSpec.CounterSetup(i))); |
| 115 | + |
| 116 | + public override TearDown<Counter> TearDown => new DisposeCall<Counter>(); |
| 117 | + |
| 118 | + public override Gen<Operation<Counter, int>> Next(int _) => |
| 119 | + Gen.Elements(new Operation<Counter, int>[] |
| 120 | + { |
| 121 | + new CounterSpec.Inc(), |
| 122 | + new CounterSpec.Dec() |
| 123 | + }); |
| 124 | + |
| 125 | + public override IEnumerable<FSharpList<Operation<Counter, int>>> ShrinkOperations(FSharpList<Operation<Counter, int>> operation) => |
| 126 | + base.ShrinkOperations(operation); |
| 127 | + } |
| 128 | + //[/Specification] |
| 129 | + |
| 130 | + |
| 131 | + [Fact] |
| 132 | + void counter_tested_with_stateful_testing() |
| 133 | + { |
| 134 | + var property = new Specification(10).ToProperty(); |
| 135 | + |
| 136 | + Check.QuickThrowOnFailure(property); |
| 137 | + } |
| 138 | +} |
0 commit comments