/
F_Parameterized_Test_With_Better_Generators.cs
184 lines (163 loc) · 6.66 KB
/
F_Parameterized_Test_With_Better_Generators.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using FsCheck;
namespace Sheepish.CSharp
{
[TestFixture]
[TestFixtureSource(nameof(SheepishExamples))]
[TestFixtureSource(nameof(SheepishExamples2))]
public class F_Parameterized_Test_With_Better_Generators
{
readonly SheepishTestCase testCase;
readonly bool isSheepBleat;
string Text => testCase.Text;
public F_Parameterized_Test_With_Better_Generators(SheepishTestCase testCase)
{
this.testCase = testCase;
isSheepBleat = Sheepish.IsSheepBleat(testCase.Text);
}
[Test]
public void SheepishIsAtLeast3CharactersLong()
{
if (isSheepBleat)
Assert.GreaterOrEqual(testCase.Text.Length, 3,
"Sheep bleat is at least 3 characters");
}
[Test]
public void SheepishStartsWith_b()
{
if (isSheepBleat)
StringAssert.StartsWith("b", Text);
}
[Test]
public void SheepishEndsWith_aa()
{
if (isSheepBleat)
StringAssert.EndsWith("aa", Text);
}
[Test]
public void SheepishOnlyContains_ab()
{
var chars = new HashSet<char>(Text);
var ab = new HashSet<char>("ab");
if (isSheepBleat)
CollectionAssert.AreEquivalent(ab, chars);
if (!ab.SetEquals(chars))
Assert.IsFalse(isSheepBleat, "isSheepBleat");
}
[Test]
public void Sheepish_HasCorrectStructure()
{
if (Text.Length < 3)
Assert.IsFalse(isSheepBleat, "Too short");
else
{
var correctSheepishForStringLength =
"b" + new string('a', Text.Length - 1);
Assert.AreEqual(
correctSheepishForStringLength == Text,
isSheepBleat,
correctSheepishForStringLength + "<->" + Text);
}
}
static IEnumerable<int> ExampleOfGenerators(int exampleCount)
{
Gen<int> generator = Gen.Choose(int.MinValue, int.MaxValue);
IEnumerable<int> examples = generator.Sample(exampleCount);
return examples;
}
static Gen<string> UsuallyEmptyString =>
Gen.Frequency(
Tuple.Create(9, Gen.Constant("")),
Tuple.Create(1, Arb.Default.NonEmptyString().Generator.Select(s => s.Item)));
static Gen<string> StringOfA =>
Gen.Choose(-3, 3).Select(n => new string('a', Math.Max(n, 0)));
static Gen<string> StringOfB =>
Gen.Choose(0, 3).Select(n => new string('b', Math.Max(n, 0)));
static IEnumerable<SheepishTestCase> SheepishExamples =>
Gen.zip(
Gen.zip3(UsuallyEmptyString,
StringOfB,
UsuallyEmptyString),
Gen.zip3(StringOfA,
UsuallyEmptyString,
StringOfA))
.Select(t => t.Item1.Item1 + t.Item1.Item2 + t.Item1.Item3
+ t.Item2.Item1 + t.Item2.Item2 + t.Item2.Item3) // That's confusing!
.Select(text => new SheepishTestCase(text, "random")) // To test case
.Sample(100); // take 100 examples
/// <summary>
/// A class to represent a "better" sheepish generation.
/// </summary>
public class SheepishParts
{
// FsCheck can use reflection to find such a constructor,
// https://github.com/fscheck/FsCheck/blob/8fd6dd7/src/FsCheck/Reflect.fs#L21-L27
public SheepishParts(UsuallyEmptyString S0, StringOfBs B1, UsuallyEmptyString S2, StringOfAs A3, UsuallyEmptyString S4, StringOfAs A5)
{
this.S0 = S0;
this.B1 = B1;
this.S2 = S2;
this.A3 = A3;
this.S4 = S4;
this.A5 = A5;
}
public UsuallyEmptyString S0 { get; }
public StringOfBs B1 { get; }
public UsuallyEmptyString S2 { get; }
public StringOfAs A3 { get; }
public UsuallyEmptyString S4 { get; }
public StringOfAs A5 { get; }
public override string ToString() => "" + S0 + B1 + S2 + A3 + S4 + A5;
public class UsuallyEmptyString : Wrapper<string> { }
public class StringOfAs : Wrapper<string> { }
public class StringOfBs : Wrapper<string> { }
}
public class SheepishPartsGenerators
{
public static Arbitrary<T> FromStringGen<T>(Gen<string> gen)
where T : Wrapper<string>, new() =>
gen.Select(s => new T{ Value = s }).ToArbitrary();
public static Arbitrary<SheepishParts.UsuallyEmptyString> UsuallyEmptyString() => FromStringGen<SheepishParts.UsuallyEmptyString>(F_Parameterized_Test_With_Better_Generators.UsuallyEmptyString);
public static Arbitrary<SheepishParts.StringOfAs> StringOfAs() => FromStringGen<SheepishParts.StringOfAs>(F_Parameterized_Test_With_Better_Generators.StringOfA);
public static Arbitrary<SheepishParts.StringOfBs> StringOfBs() => FromStringGen<SheepishParts.StringOfBs>(F_Parameterized_Test_With_Better_Generators.StringOfB);
}
public class Wrapper<T>
{
public Wrapper() => Value = default(T);
public Wrapper(T value) => Value = value;
public T Value;
public override string ToString() => Value?.ToString();
}
static F_Parameterized_Test_With_Better_Generators()
{
try
{
Arb.Register<SheepishPartsGenerators>();
}
catch(Exception e)
{
e.ToString();
}
}
static IEnumerable<SheepishTestCase> SheepishExamples2 =>
Arb.Default.Derive<SheepishParts>().Generator
.Select(parts => parts.ToString())
.Select(text => new SheepishTestCase(text, "random")) // To test case
.Sample(5); // take 5 examples
public class SheepishTestCase
{
public SheepishTestCase(string text, string reason)
{
Text = text;
Reason = reason;
}
public string Text;
public string Reason;
// nUnit requires a unique ToString() for a name.
public override string ToString() => Text + "<-" + Reason;
}
}
}