Skip to content

Commit a850c43

Browse files
committed
Merge pull request #18 from robdmoore/release2tweaks
Release2tweaks
2 parents 95dc44a + 592558c commit a850c43

18 files changed

+383
-70
lines changed

BREAKING_CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Simply add the following to the files that generate lists of builders and change
5050
using NTestDataBuilder.Lists;
5151
```
5252

53+
Assuming you aren't using NBuilder for anything other than generating lists of entities with NTestDataBuilder 1.0 you should be abke to do a global find and replace against `using FizzWare.NBuilder;`.
54+
5355
If you uninstall the NBuilder package then you will need to remove the using statements for that library too.
5456

5557
Also, remove any `BuildList` extension methods you created.

NTestDataBuilder.Tests/BuildListTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ namespace NTestDataBuilder.Tests
1111
{
1212
public class BuildListTests
1313
{
14+
[Fact]
15+
public void GivenANormalBuilderInstance_WhenCallingIsListBuilderProxy_ThenReturnFalse()
16+
{
17+
var builder = new BasicCustomerBuilder();
18+
19+
builder.IsListBuilderProxy().ShouldBe(false);
20+
}
21+
22+
[Fact]
23+
public void GivenAListBuilderProxyInstance_WhenCallingIsListBuilderProxy_ThenReturnTrue()
24+
{
25+
var builder = BasicCustomerBuilder.CreateListOfSize(1).TheFirst(1);
26+
27+
builder.IsListBuilderProxy().ShouldBe(true);
28+
}
29+
1430
[Fact]
1531
public void GivenListOfBuilders_WhenCallingBuildList_ThenAListOfEntitiesOfTheRightSizeShouldBeReturned()
1632
{
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace NTestDataBuilder.Tests
6+
{
7+
public class ChildBuilderTests
8+
{
9+
[Fact]
10+
public void WhenNotUsingChildBuilder_ThenTheAnonymousValueFixtureIsNotReused()
11+
{
12+
var parent = new ParentBuilder().Build();
13+
14+
parent.ParentEnum.ShouldBe(parent.Child.ChildEnum);
15+
}
16+
17+
[Fact]
18+
public void WhenUsingChildBuilder_ThenTheAnonymousValueFixtureIsReused()
19+
{
20+
var parent = new ParentBuilder().WithChildBuilder().Build();
21+
22+
parent.ParentEnum.ShouldNotBe(parent.Child.ChildEnum);
23+
}
24+
25+
[Fact]
26+
public void WhenUsingChildBuilderIncludingModifier_ThenTheModifierGetsApplied()
27+
{
28+
const int number = 2;
29+
var parent = new ParentBuilder()
30+
.WithChildBuilder(b => b.WithANumber(number))
31+
.Build();
32+
33+
parent.Child.Number.ShouldBe(number);
34+
}
35+
}
36+
37+
public enum AnEnum
38+
{
39+
One,
40+
Two
41+
}
42+
43+
public class ParentObject
44+
{
45+
public ParentObject(AnEnum parentEnum, ChildObject child)
46+
{
47+
ParentEnum = parentEnum;
48+
Child = child;
49+
}
50+
51+
public AnEnum ParentEnum { get; private set; }
52+
public ChildObject Child { get; private set; }
53+
}
54+
55+
public class ChildObject
56+
{
57+
public ChildObject(AnEnum childEnum, int number)
58+
{
59+
ChildEnum = childEnum;
60+
Number = number;
61+
}
62+
63+
public AnEnum ChildEnum { get; private set; }
64+
public int Number { get; private set; }
65+
}
66+
67+
public class ParentBuilder : TestDataBuilder<ParentObject, ParentBuilder>
68+
{
69+
public ParentBuilder()
70+
{
71+
Set(x => x.Child, new ChildBuilder().Build());
72+
}
73+
74+
public ParentBuilder WithChildBuilder(Func<ChildBuilder, ChildBuilder> modifier = null)
75+
{
76+
return Set(x => x.Child, GetChildBuilder<ChildObject, ChildBuilder>(modifier).Build());
77+
}
78+
79+
protected override ParentObject BuildObject()
80+
{
81+
return new ParentObject(Get(x => x.ParentEnum), Get(x => x.Child));
82+
}
83+
}
84+
85+
public class ChildBuilder : TestDataBuilder<ChildObject, ChildBuilder>
86+
{
87+
public ChildBuilder()
88+
{
89+
WithANumber(1);
90+
}
91+
92+
public ChildBuilder WithANumber(int number)
93+
{
94+
return Set(x => x.Number, number);
95+
}
96+
97+
protected override ChildObject BuildObject()
98+
{
99+
return new ChildObject(Get(x => x.ChildEnum), Get(x => x.Number));
100+
}
101+
}
102+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NTestDataBuilder.DataSources;
5+
using NTestDataBuilder.DataSources.Geography;
6+
using NTestDataBuilder.EquivalenceClasses.Geo;
7+
using Shouldly;
8+
using Xunit.Extensions;
9+
10+
namespace NTestDataBuilder.Tests.EquivalenceClasses
11+
{
12+
public class GeoEquivalenceClassesTests
13+
{
14+
public static AnonymousValueFixture Any { get; private set; }
15+
16+
public GeoEquivalenceClassesTests()
17+
{
18+
Any = new AnonymousValueFixture();
19+
}
20+
21+
[Theory]
22+
[PropertyData("TestCases")]
23+
public void WhenGettingAnyGeoData_ThenReturnRandomGeoDataWhichIsReasonablyUnique(DataSource<string> source,
24+
List<string> testCases)
25+
{
26+
foreach (var testCase in testCases)
27+
{
28+
testCase.ShouldBeOfType<string>();
29+
testCase.ShouldNotBeNullOrEmpty();
30+
source.Data.ShouldContain(testCase);
31+
}
32+
if (source.Data.Count > 15)
33+
{
34+
var unique = testCases.Distinct().Count();
35+
unique.ShouldBeGreaterThan(5);
36+
}
37+
}
38+
39+
public static IEnumerable<object[]> TestCases
40+
{
41+
get
42+
{
43+
yield return new object[] { new GeoContinentSource(), GenerateTestCasesForSut(Any.Continent) };
44+
yield return new object[] { new GeoCountrySource(), GenerateTestCasesForSut(Any.Country) };
45+
yield return new object[] { new GeoCountryCodeSource(), GenerateTestCasesForSut(Any.CountryCode) };
46+
yield return new object[] { new GeoLatitudeSource(), GenerateTestCasesForSut(Any.Latitude) };
47+
yield return new object[] { new GeoLongitudeSource(), GenerateTestCasesForSut(Any.Longitude) };
48+
}
49+
}
50+
51+
private static List<string> GenerateTestCasesForSut(Func<string> any)
52+
{
53+
var results = new List<string>();
54+
for (int i = 0; i < 10; i++)
55+
{
56+
results.Add(any());
57+
}
58+
return results;
59+
}
60+
}
61+
}

NTestDataBuilder.Tests/EquivalenceClasses/PersonEquivalenceClassesTests.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using NTestDataBuilder.DataSources;
5-
using NTestDataBuilder.DataSources.Geography;
65
using NTestDataBuilder.DataSources.Person;
7-
using NTestDataBuilder.EquivalenceClasses;
6+
using NTestDataBuilder.EquivalenceClasses.Person;
87
using Shouldly;
8+
using Xunit;
99
using Xunit.Extensions;
1010

1111
namespace NTestDataBuilder.Tests.EquivalenceClasses
@@ -37,25 +37,34 @@ public void WhenGettingAnyPersonData_ThenReturnRandomPersonDataWhichIsReasonably
3737
}
3838
}
3939

40+
[Fact]
41+
public void WhenGettingUniqueEmail_ThenReturnUniqueEmails()
42+
{
43+
var source = new PersonEmailAddressSource();
44+
var generatedValues = new List<string>();
45+
46+
for (var i = 0; i < source.Data.Count; i++)
47+
{
48+
generatedValues.Add(Any.UniqueEmailAddress());
49+
}
50+
51+
generatedValues.Distinct().Count()
52+
.ShouldBe(generatedValues.Count);
53+
}
54+
4055
public static IEnumerable<object[]> TestCases
4156
{
4257
get
4358
{
44-
yield return new object[] { new PersonEmailAddressSource(), GenerateTestCasesForSut(Any.PersonEmailAddress) };
45-
yield return new object[] { new PersonLanguageSource(), GenerateTestCasesForSut(Any.PersonLanguage) };
46-
yield return new object[] { new PersonNameFirstFemaleSource(), GenerateTestCasesForSut(Any.PersonNameFirstFemale) };
47-
yield return new object[] { new PersonNameFirstSource(), GenerateTestCasesForSut(Any.PersonNameFirst) };
48-
yield return new object[] { new PersonNameFullSource(), GenerateTestCasesForSut(Any.PersonNameFull) };
49-
yield return new object[] { new PersonNameLastSource(), GenerateTestCasesForSut(Any.PersonNameLast) };
50-
yield return new object[] { new PersonNameFirstMaleSource(), GenerateTestCasesForSut(Any.PersonNameFirstMale) };
51-
yield return new object[] { new PersonNameSuffixSource(), GenerateTestCasesForSut(Any.PersonNameSuffix) };
52-
yield return new object[] { new PersonNameTitleSource(), GenerateTestCasesForSut(Any.PersonNameTitle) };
53-
54-
yield return new object[] { new GeoContinentSource(), GenerateTestCasesForSut(Any.GeoContinent) };
55-
yield return new object[] { new GeoCountrySource(), GenerateTestCasesForSut(Any.GeoCountry) };
56-
yield return new object[] { new GeoCountryCodeSource(), GenerateTestCasesForSut(Any.GeoCountryCode) };
57-
yield return new object[] { new GeoLatitudeSource(), GenerateTestCasesForSut(Any.GeoLatitude) };
58-
yield return new object[] { new GeoLongitudeSource(), GenerateTestCasesForSut(Any.GeoLongitude) };
59+
yield return new object[] { new PersonEmailAddressSource(), GenerateTestCasesForSut(Any.EmailAddress) };
60+
yield return new object[] { new PersonLanguageSource(), GenerateTestCasesForSut(Any.Language) };
61+
yield return new object[] { new PersonNameFirstFemaleSource(), GenerateTestCasesForSut(Any.FemaleFirstName) };
62+
yield return new object[] { new PersonNameFirstSource(), GenerateTestCasesForSut(Any.FirstName) };
63+
yield return new object[] { new PersonNameFullSource(), GenerateTestCasesForSut(Any.FullName) };
64+
yield return new object[] { new PersonNameLastSource(), GenerateTestCasesForSut(Any.LastName) };
65+
yield return new object[] { new PersonNameFirstMaleSource(), GenerateTestCasesForSut(Any.MaleFirstName) };
66+
yield return new object[] { new PersonNameSuffixSource(), GenerateTestCasesForSut(Any.Suffix) };
67+
yield return new object[] { new PersonNameTitleSource(), GenerateTestCasesForSut(Any.Title) };
5968
}
6069
}
6170

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
</Reference>
5050
</ItemGroup>
5151
<ItemGroup>
52+
<Compile Include="ChildBuilderTests.cs" />
5253
<Compile Include="Entities\CustomerClass.cs" />
5354
<Compile Include="DataSources\DataSourceTests.cs" />
5455
<Compile Include="DataSources\Dictionaries\CacheTests.cs" />
@@ -58,6 +59,7 @@
5859
<Compile Include="DataSources\Generators\RandomGeneratorTests.cs" />
5960
<Compile Include="DataSources\Generators\SequentiaGeneratorTests.cs" />
6061
<Compile Include="DataSources\DataSourceConventionTests.cs" />
62+
<Compile Include="EquivalenceClasses\GeoEquivalenceClassesTests.cs" />
6163
<Compile Include="EquivalenceClasses\PersonEquivalenceClassesTests.cs" />
6264
<Compile Include="EquivalenceClasses\IntegerEquivalenceClassesTests.cs" />
6365
<Compile Include="EquivalenceClasses\EnumEquivalenceClassesTests.cs" />

NTestDataBuilder/AnonymousValueFixture.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static AnonymousValueFixture()
1818
GlobalValueSuppliers = new List<IAnonymousValueSupplier>();
1919
DefaultValueSuppliers = new IAnonymousValueSupplier[]
2020
{
21+
new DefaultEmailValueSupplier(),
2122
new DefaultFirstNameValueSupplier(),
2223
new DefaultLastNameValueSupplier(),
2324
new DefaultStringValueSupplier(),
@@ -33,6 +34,7 @@ public AnonymousValueFixture()
3334
{
3435
LocalValueSuppliers = new List<IAnonymousValueSupplier>();
3536
Fixture = new Fixture();
37+
Bag = new NullingExpandoObject();
3638
RegexGenerator = new RegularExpressionGenerator();
3739
}
3840

@@ -48,6 +50,11 @@ public AnonymousValueFixture()
4850
/// </summary>
4951
public Fixture Fixture { get; private set; }
5052

53+
/// <summary>
54+
/// Dynamic bag of objects that can be used by equivalence classes / anonymous value suppliers to store state.
55+
/// </summary>
56+
public dynamic Bag { get; private set; }
57+
5158
/// <summary>
5259
/// Ordered, immutable collection of default anonymous value suppliers to interrogate when automatically generating an anonymous value.
5360
/// These have the lowest priority and are a fallback if there are no local or global value suppliers that apply.

NTestDataBuilder/DataSources/Generators/RandomGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NTestDataBuilder.DataSources.Generators
77
/// </summary>
88
public class RandomGenerator : IGenerator
99
{
10-
private readonly Random _random;
10+
private static readonly Random Random = new Random();
1111
/// <inheritdoc />
1212
public int StartIndex { get; set; }
1313
/// <inheritdoc />
@@ -32,13 +32,12 @@ public RandomGenerator(int startIndex, int listSize)
3232

3333
StartIndex = startIndex;
3434
ListSize = listSize;
35-
_random = new Random();
3635
}
3736

3837
/// <inerhitdoc />
3938
public int Generate()
4039
{
41-
return _random.Next(StartIndex, ListSize);
40+
return Random.Next(StartIndex, ListSize);
4241
}
4342
}
4443
}

NTestDataBuilder/DataSources/Person/PersonEmailAddressSource.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NTestDataBuilder.DataSources.Dictionaries;
2+
using NTestDataBuilder.DataSources.Generators;
23

34
namespace NTestDataBuilder.DataSources.Person
45
{
@@ -7,5 +8,17 @@ namespace NTestDataBuilder.DataSources.Person
78
/// </summary>
89
public class PersonEmailAddressSource : FileDictionarySource
910
{
11+
/// <summary>
12+
/// Create a person email address source with random generation.
13+
/// </summary>
14+
public PersonEmailAddressSource() {}
15+
16+
/// <summary>
17+
/// Create a person email address source with custom generation.
18+
/// </summary>
19+
/// <param name="generator">The generator to use</param>
20+
public PersonEmailAddressSource(IGenerator generator)
21+
: base(generator, new CachedFileDictionaryRepository())
22+
{}
1023
}
1124
}

0 commit comments

Comments
 (0)