Skip to content

Commit e8109b5

Browse files
committed
Refactored Person/Get equivalence tests to a more reusable style
1 parent 541c89c commit e8109b5

File tree

6 files changed

+166
-79
lines changed

6 files changed

+166
-79
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Collections.Generic;
2+
using TestStack.Dossier.DataSources;
3+
using TestStack.Dossier.DataSources.Dictionaries;
4+
using Xunit.Extensions;
5+
6+
namespace TestStack.Dossier.Tests.EquivalenceClasses
7+
{
8+
namespace TestStack.Dossier.Tests.EquivalenceClasses
9+
{
10+
public class AddressAusEquivalenceClassesTests : FileDictionaryEquivalenceTests
11+
{
12+
public AnonymousValueFixture Any { get; } = new AnonymousValueFixture();
13+
14+
[Theory]
15+
[ClassData(typeof(AddressAusTestCases))]
16+
public override void WhenGettingAnyData_ThenReturnRandomDataWhichIsReasonablyUnique(DataSource<string> source, List<string> testCases)
17+
{
18+
base.WhenGettingAnyData_ThenReturnRandomDataWhichIsReasonablyUnique(source, testCases);
19+
}
20+
}
21+
22+
public class AddressAusTestCases : FileDictionaryEquivalenceTestCases
23+
{
24+
protected override List<object[]> GetData()
25+
{
26+
return new List<object[]>
27+
{
28+
new object[]
29+
{new Words(FromDictionary.AddressAusCity), GenerateTestCasesForSut(Any.AddressAusCity)},
30+
new object[]
31+
{new Words(FromDictionary.AddressAusCompany), GenerateTestCasesForSut(Any.AddressAusCompany)},
32+
new object[]
33+
{new Words(FromDictionary.AddressAusPhone), GenerateTestCasesForSut(Any.AddressAusPhone)},
34+
new object[]
35+
{new Words(FromDictionary.AddressAusPostCode), GenerateTestCasesForSut(Any.AddressAusPostCode)},
36+
new object[]
37+
{new Words(FromDictionary.AddressAusState), GenerateTestCasesForSut(Any.AddressAusState)},
38+
new object[]
39+
{
40+
new Words(FromDictionary.AddressAusStateAbbreviation),
41+
GenerateTestCasesForSut(Any.AddressAusStateAbbreviation)
42+
},
43+
new object[]
44+
{new Words(FromDictionary.AddressAusStreet), GenerateTestCasesForSut(Any.AddressAusStreet)},
45+
new object[]
46+
{new Words(FromDictionary.AddressAusWebsite), GenerateTestCasesForSut(Any.AddressAusWebsite)},
47+
};
48+
}
49+
}
50+
}
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
namespace TestStack.Dossier.Tests.EquivalenceClasses
6+
{
7+
public abstract class FileDictionaryEquivalenceTestCases : IEnumerable<object[]>
8+
{
9+
protected abstract List<object[]> GetData();
10+
11+
public AnonymousValueFixture Any { get; } = new AnonymousValueFixture();
12+
13+
private List<object[]> _data;
14+
public List<object[]> Data
15+
{
16+
get
17+
{
18+
if (_data == null)
19+
{
20+
_data = GetData();
21+
}
22+
return _data;
23+
}
24+
}
25+
26+
protected List<string> GenerateTestCasesForSut(Func<string> any)
27+
{
28+
var results = new List<string>();
29+
for (int i = 0; i < 10; i++)
30+
{
31+
results.Add(any());
32+
}
33+
return results;
34+
}
35+
36+
public IEnumerator<object[]> GetEnumerator()
37+
{
38+
return Data.GetEnumerator();
39+
}
40+
41+
IEnumerator IEnumerable.GetEnumerator()
42+
{
43+
return GetEnumerator();
44+
}
45+
}
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Shouldly;
4+
using TestStack.Dossier.DataSources;
5+
6+
namespace TestStack.Dossier.Tests.EquivalenceClasses
7+
{
8+
public abstract class FileDictionaryEquivalenceTests
9+
{
10+
public virtual void WhenGettingAnyData_ThenReturnRandomDataWhichIsReasonablyUnique(DataSource<string> source,
11+
List<string> testCases)
12+
{
13+
foreach (var testCase in testCases)
14+
{
15+
testCase.ShouldBeOfType<string>();
16+
testCase.ShouldNotBeNullOrEmpty();
17+
source.Data.ShouldContain(testCase);
18+
}
19+
if (source.Data.Count > 15)
20+
{
21+
var unique = testCases.Distinct().Count();
22+
unique.ShouldBeGreaterThan(5);
23+
}
24+
}
25+
}
26+
}
Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,32 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using Shouldly;
1+
using System.Collections.Generic;
52
using TestStack.Dossier.DataSources;
63
using TestStack.Dossier.DataSources.Dictionaries;
74
using Xunit.Extensions;
85

96
namespace TestStack.Dossier.Tests.EquivalenceClasses
107
{
11-
public class GeoEquivalenceClassesTests
8+
public class GeoEquivalenceClassesTests : FileDictionaryEquivalenceTests
129
{
13-
public static AnonymousValueFixture Any { get; private set; } = new AnonymousValueFixture();
14-
1510
[Theory]
16-
[PropertyData("TestCases")]
17-
public void WhenGettingAnyGeoData_ThenReturnRandomGeoDataWhichIsReasonablyUnique(DataSource<string> source,
18-
List<string> testCases)
11+
[ClassData(typeof(GeoTestCases))]
12+
public override void WhenGettingAnyData_ThenReturnRandomDataWhichIsReasonablyUnique(DataSource<string> source, List<string> testCases)
1913
{
20-
foreach (var testCase in testCases)
21-
{
22-
testCase.ShouldBeOfType<string>();
23-
testCase.ShouldNotBeNullOrEmpty();
24-
source.Data.ShouldContain(testCase);
25-
}
26-
if (source.Data.Count > 15)
27-
{
28-
var unique = testCases.Distinct().Count();
29-
unique.ShouldBeGreaterThan(5);
30-
}
14+
base.WhenGettingAnyData_ThenReturnRandomDataWhichIsReasonablyUnique(source, testCases);
3115
}
16+
}
3217

33-
public static IEnumerable<object[]> TestCases
18+
public class GeoTestCases : FileDictionaryEquivalenceTestCases
19+
{
20+
protected override List<object[]> GetData()
3421
{
35-
get
22+
return new List<object[]>
3623
{
37-
yield return new object[] { new Words(FromDictionary.GeoContinent), GenerateTestCasesForSut(Any.GeoContinent) };
38-
yield return new object[] { new Words(FromDictionary.GeoCountry), GenerateTestCasesForSut(Any.GeoCountry) };
39-
yield return new object[] { new Words(FromDictionary.GeoCountryCode), GenerateTestCasesForSut(Any.GeoCountryCode) };
40-
yield return new object[] { new Words(FromDictionary.GeoLatitude), GenerateTestCasesForSut(Any.GeoLatitude) };
41-
yield return new object[] { new Words(FromDictionary.GeoLongitude), GenerateTestCasesForSut(Any.GeoLongitude) };
42-
}
24+
new object[] {new Words(FromDictionary.GeoContinent), GenerateTestCasesForSut(Any.GeoContinent)},
25+
new object[] {new Words(FromDictionary.GeoCountry), GenerateTestCasesForSut(Any.GeoCountry)},
26+
new object[] {new Words(FromDictionary.GeoCountryCode), GenerateTestCasesForSut(Any.GeoCountryCode)},
27+
new object[] {new Words(FromDictionary.GeoLatitude), GenerateTestCasesForSut(Any.GeoLatitude)},
28+
new object[] {new Words(FromDictionary.GeoLongitude), GenerateTestCasesForSut(Any.GeoLongitude)},
29+
};
4330
}
44-
45-
private static List<string> GenerateTestCasesForSut(Func<string> any)
46-
{
47-
var results = new List<string>();
48-
for (int i = 0; i < 10; i++)
49-
{
50-
results.Add(any());
51-
}
52-
return results;
53-
}
5431
}
5532
}
Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43
using Shouldly;
54
using TestStack.Dossier.DataSources;
@@ -9,26 +8,15 @@
98

109
namespace TestStack.Dossier.Tests.EquivalenceClasses
1110
{
12-
public class PersonEquivalenceClassesTests
11+
public class PersonEquivalenceClassesTests : FileDictionaryEquivalenceTests
1312
{
14-
public static AnonymousValueFixture Any { get; } = new AnonymousValueFixture();
13+
public AnonymousValueFixture Any { get; } = new AnonymousValueFixture();
1514

1615
[Theory]
17-
[PropertyData("TestCases")]
18-
public void WhenGettingAnyPersonData_ThenReturnRandomPersonDataWhichIsReasonablyUnique(DataSource<string> source,
19-
List<string> testCases)
16+
[ClassData(typeof(PersonTestCases))]
17+
public override void WhenGettingAnyData_ThenReturnRandomDataWhichIsReasonablyUnique(DataSource<string> source, List<string> testCases)
2018
{
21-
foreach (var testCase in testCases)
22-
{
23-
testCase.ShouldBeOfType<string>();
24-
testCase.ShouldNotBeNullOrEmpty();
25-
source.Data.ShouldContain(testCase);
26-
}
27-
if (source.Data.Count > 15)
28-
{
29-
var unique = testCases.Distinct().Count();
30-
unique.ShouldBeGreaterThan(5);
31-
}
19+
base.WhenGettingAnyData_ThenReturnRandomDataWhichIsReasonablyUnique(source, testCases);
3220
}
3321

3422
[Fact]
@@ -47,31 +35,27 @@ public void WhenGettingUniqueEmail_ThenReturnUniqueEmailsAcrossFixtureInstances(
4735
generatedValues.Distinct().Count()
4836
.ShouldBe(generatedValues.Count);
4937
}
38+
}
5039

51-
public static IEnumerable<object[]> TestCases
40+
public class PersonTestCases : FileDictionaryEquivalenceTestCases
41+
{
42+
protected override List<object[]> GetData()
5243
{
53-
get
44+
return new List<object[]>
5445
{
55-
yield return new object[] { new Words(FromDictionary.PersonEmailAddress), GenerateTestCasesForSut(Any.PersonEmailAddress) };
56-
yield return new object[] { new Words(FromDictionary.PersonLanguage), GenerateTestCasesForSut(Any.PersonLanguage) };
57-
yield return new object[] { new Words(FromDictionary.PersonNameFirstFemale), GenerateTestCasesForSut(Any.PersonNameFirstFemale) };
58-
yield return new object[] { new Words(FromDictionary.PersonNameFirst), GenerateTestCasesForSut(Any.PersonNameFirst) };
59-
yield return new object[] { new Words(FromDictionary.PersonNameFull), GenerateTestCasesForSut(Any.PersonNameFull) };
60-
yield return new object[] { new Words(FromDictionary.PersonNameLast), GenerateTestCasesForSut(Any.PersonNameLast) };
61-
yield return new object[] { new Words(FromDictionary.PersonNameFirstMale), GenerateTestCasesForSut(Any.PersonNameFirstMale) };
62-
yield return new object[] { new Words(FromDictionary.PersonNameSuffix), GenerateTestCasesForSut(Any.PersonNameSuffix) };
63-
yield return new object[] { new Words(FromDictionary.PersonNameTitle), GenerateTestCasesForSut(Any.PersonNameTitle) };
64-
}
46+
new object[]
47+
{new Words(FromDictionary.PersonEmailAddress), GenerateTestCasesForSut(Any.PersonEmailAddress)},
48+
new object[] {new Words(FromDictionary.PersonLanguage), GenerateTestCasesForSut(Any.PersonLanguage)},
49+
new object[]
50+
{new Words(FromDictionary.PersonNameFirstFemale), GenerateTestCasesForSut(Any.PersonNameFirstFemale)},
51+
new object[] {new Words(FromDictionary.PersonNameFirst), GenerateTestCasesForSut(Any.PersonNameFirst)},
52+
new object[] {new Words(FromDictionary.PersonNameFull), GenerateTestCasesForSut(Any.PersonNameFull)},
53+
new object[] {new Words(FromDictionary.PersonNameLast), GenerateTestCasesForSut(Any.PersonNameLast)},
54+
new object[]
55+
{new Words(FromDictionary.PersonNameFirstMale), GenerateTestCasesForSut(Any.PersonNameFirstMale)},
56+
new object[] {new Words(FromDictionary.PersonNameSuffix), GenerateTestCasesForSut(Any.PersonNameSuffix)},
57+
new object[] {new Words(FromDictionary.PersonNameTitle), GenerateTestCasesForSut(Any.PersonNameTitle)},
58+
};
6559
}
66-
67-
private static List<string> GenerateTestCasesForSut(Func<string> any)
68-
{
69-
var results = new List<string>();
70-
for (int i = 0; i < 10; i++)
71-
{
72-
results.Add(any());
73-
}
74-
return results;
75-
}
7660
}
7761
}

TestStack.Dossier.Tests/TestStack.Dossier.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
<Compile Include="Builder_CreateListTests.cs" />
5959
<Compile Include="Builder_CreateNewTests.cs" />
6060
<Compile Include="Builder_SetUsingBuilderTests.cs" />
61+
<Compile Include="EquivalenceClasses\AddressAusEquivalenceClassesTests.cs" />
62+
<Compile Include="EquivalenceClasses\FileDictionaryEquivalenceTestCases.cs" />
63+
<Compile Include="EquivalenceClasses\FileDictionaryEquivalenceTests.cs" />
6164
<Compile Include="TestHelpers\Builders\AddressViewModelBuilder.cs" />
6265
<Compile Include="TestHelpers\Builders\AutoConstructorCustomerBuilder.cs" />
6366
<Compile Include="ChildBuilderTests.cs" />

0 commit comments

Comments
 (0)