Skip to content

Commit 6c6a030

Browse files
committed
Merge pull request #8 from robdmoore/string-equivalence-classes
String equivalence classes
2 parents 6168638 + e3bb7e6 commit 6c6a030

8 files changed

Lines changed: 105 additions & 5 deletions

File tree

NTestDataBuilder.Tests/EquivalenceClasses/StringEquivalenceClassesTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ public void WhenGettingAnyString_ThenReturnDifferentNonEmptyStringsEveryTime()
2626
s1.ShouldNotBe(s2);
2727
}
2828

29+
[Fact]
30+
public void WhenGettingAnyStringMatchingARegex_ThenReturnDifferentStringMatchingThatRegexEveryTime()
31+
{
32+
var s1 = Any.StringMatching(@"\d{3}\w");
33+
var s2 = Any.StringMatching(@"\d{3}\w");
34+
var s3 = Any.StringMatching(@"\w{4}-\d{2}[a-c]+");
35+
36+
s1.ShouldNotBe(s2);
37+
s1.ShouldNotBe(s3);
38+
s2.ShouldNotBe(s3);
39+
s1.ShouldMatch(@"\d{3}\w");
40+
s2.ShouldMatch(@"\d{3}\w");
41+
s3.ShouldMatch(@"\w{4}-\d{2}[a-c]+");
42+
}
43+
2944
[Fact]
3045
public void WhenGettingAnyStringStartingWithSomething_ThenReturnDifferentStringsStartingWithThatStringEveryTime()
3146
{
@@ -40,5 +55,38 @@ public void WhenGettingAnyStringStartingWithSomething_ThenReturnDifferentStrings
4055
s2.ShouldStartWith("St4rt");
4156
s3.ShouldStartWith("Something Else");
4257
}
58+
59+
[Fact]
60+
public void WhenGettingAnyStringEndingWithSomething_ThenReturnDifferentStringsEndingWithThatStringEveryTime()
61+
{
62+
var s1 = Any.StringEndingWith("3nd");
63+
var s2 = Any.StringEndingWith("3nd");
64+
var s3 = Any.StringEndingWith("Something Else");
65+
66+
s1.ShouldNotBe(s2);
67+
s1.ShouldNotBe(s3);
68+
s2.ShouldNotBe(s3);
69+
s1.ShouldEndWith("3nd");
70+
s2.ShouldEndWith("3nd");
71+
s3.ShouldEndWith("Something Else");
72+
}
73+
74+
[Fact]
75+
public void WhenGettingAnyStringOfASpecificLength_ThenReturnDifferentStringsOfThatLengthEveryTime()
76+
{
77+
var s1 = Any.StringOfLength(5);
78+
var s2 = Any.StringOfLength(5);
79+
var s3 = Any.StringOfLength(1005);
80+
81+
s1.ShouldBeOfType<string>();
82+
s2.ShouldBeOfType<string>();
83+
s3.ShouldBeOfType<string>();
84+
s1.ShouldNotBe(s2);
85+
s1.ShouldNotBe(s3);
86+
s2.ShouldNotBe(s3);
87+
s1.Length.ShouldBe(5);
88+
s2.Length.ShouldBe(5);
89+
s3.Length.ShouldBe(1005);
90+
}
4391
}
4492
}

NTestDataBuilder.Tests/ListBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ListBuilderTests
1313
public void WhenBuildingAListOfObjects_ThenThenTheListIsBuiltAndModifiedCorrectly()
1414
{
1515
var i = 0;
16-
var customers = CustomerBuilder.ListOFSize(5)
16+
var customers = CustomerBuilder.ListOfSize(5)
1717
.TheFirst(1).WithFirstName("First")
1818
.TheNext(1).WithLastName("Next Last")
1919
.TheLast(1).WithLastName("Last Last")

NTestDataBuilder/AnonymousValueFixture.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Dynamic;
43
using System.Linq;
54
using System.Linq.Expressions;
65
using NTestDataBuilder.Suppliers;
@@ -32,8 +31,15 @@ public AnonymousValueFixture()
3231
{
3332
LocalValueSuppliers = new List<IAnonymousValueSupplier>();
3433
Fixture = new Fixture();
34+
RegexGenerator = new RegularExpressionGenerator();
3535
}
3636

37+
/// <summary>
38+
/// An AutoFixture RegularExpressionGenerator instance that can be used to generate
39+
/// strings matching a regex pattern.
40+
/// </summary>
41+
public RegularExpressionGenerator RegexGenerator { get; private set; }
42+
3743
/// <summary>
3844
/// An AutoFixture Fixture instance that is scoped to this anonymous value fixture
3945
/// and can be used to generate anonymous values using AutoFixture.

NTestDataBuilder/DummyContext.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Ploeh.AutoFixture.Kernel;
2+
3+
namespace NTestDataBuilder
4+
{
5+
/// <summary>
6+
/// Dummy <see cref="ISpecimenContext"/>.
7+
/// </summary>
8+
public class DummyContext : ISpecimenContext
9+
{
10+
public object Resolve(object request)
11+
{
12+
return null;
13+
}
14+
}
15+
}

NTestDataBuilder/EquivalenceClasses/StringEquivalenceClasses.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Ploeh.AutoFixture;
1+
using System.Text;
2+
using Ploeh.AutoFixture;
3+
using Ploeh.AutoFixture.Kernel;
24

35
namespace NTestDataBuilder.EquivalenceClasses
46
{
@@ -17,6 +19,18 @@ public static string String(this AnonymousValueFixture fixture)
1719
return fixture.Fixture.Create<string>();
1820
}
1921

22+
/// <summary>
23+
/// Generate and return a string matching the given regex.
24+
/// Only a limited subset of regex expressions are supported: http://www.brics.dk/automaton/faq.html.
25+
/// </summary>
26+
/// <param name="fixture">The fixture to generate a string for</param>
27+
/// <param name="regexPattern">The regex pattern to match</param>
28+
/// <returns>The generated string</returns>
29+
public static string StringMatching(this AnonymousValueFixture fixture, string regexPattern)
30+
{
31+
return fixture.RegexGenerator.Create(new RegularExpressionRequest(regexPattern), new DummyContext()).ToString();
32+
}
33+
2034
/// <summary>
2135
/// Generate and return a string starting with the given prefix.
2236
/// </summary>
@@ -38,5 +52,21 @@ public static string StringEndingWith(this AnonymousValueFixture fixture, string
3852
{
3953
return string.Format("{0}{1}", fixture.Fixture.Create<string>(), suffix);
4054
}
55+
56+
/// <summary>
57+
/// Generate and return a string of the given length.
58+
/// </summary>
59+
/// <param name="fixture">The fixture to generate a string for</param>
60+
/// <param name="length">The length of string to generate</param>
61+
/// <returns>The generated string</returns>
62+
public static string StringOfLength(this AnonymousValueFixture fixture, int length)
63+
{
64+
var sb = new StringBuilder();
65+
while (sb.Length < length)
66+
{
67+
sb.Append(String(fixture));
68+
}
69+
return sb.ToString().Substring(0, length);
70+
}
4171
}
4272
}

NTestDataBuilder/Lists/ListBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void MethodsInspected()
178178

179179
public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo)
180180
{
181-
if (new[]{"get_Any", "Build", "AsProxy", "Get", "GetOrDefault", "Set", "Has"}.Contains(memberInfo.Name))
181+
if (new[]{"get_Any", "Build", "AsProxy", "Get", "GetOrDefault", "Set", "Has", "get_ListBuilder", "set_ListBuilder"}.Contains(memberInfo.Name))
182182
return;
183183
throw new InvalidOperationException(string.Format("Tried to build a list with a builder who has non-virtual method. Please make {0} on type {1} virtual.", memberInfo.Name, type.Name));
184184
}

NTestDataBuilder/NTestDataBuilder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="DataSources\Person\PersonNameFirstMaleSource.cs" />
7575
<Compile Include="DataSources\Person\PersonNameSuffixSource.cs" />
7676
<Compile Include="DataSources\Person\PersonNameTitleSource.cs" />
77+
<Compile Include="DummyContext.cs" />
7778
<Compile Include="EquivalenceClasses\GeographyEquivalenceClassescs.cs" />
7879
<Compile Include="EquivalenceClasses\PersonEquivalenceClasses.cs" />
7980
<Compile Include="DataSources\Generators\IGenerator.cs" />

NTestDataBuilder/TestDataBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static IListBuilder<TBuilder> CreateListOfSize(int size)
122122
return Builder<TBuilder>.CreateListOfSize(size);
123123
}
124124

125-
public static NTestDataBuilder.Lists.ListBuilder<TObject, TBuilder> ListOFSize(int size)
125+
public static ListBuilder<TObject, TBuilder> ListOfSize(int size)
126126
{
127127
return new ListBuilder<TObject, TBuilder>(size);
128128
}

0 commit comments

Comments
 (0)