Skip to content

Commit 93c87fe

Browse files
committed
Added equivalence class for Any.StringMatching
1 parent f8c7103 commit 93c87fe

5 files changed

Lines changed: 64 additions & 1 deletion

File tree

NTestDataBuilder.Tests/EquivalenceClasses/StringEquivalenceClassesTests.cs

Lines changed: 30 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,20 @@ 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+
}
4373
}
4474
}

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Ploeh.AutoFixture;
2+
using Ploeh.AutoFixture.Kernel;
23

34
namespace NTestDataBuilder.EquivalenceClasses
45
{
@@ -17,6 +18,16 @@ public static string String(this AnonymousValueFixture fixture)
1718
return fixture.Fixture.Create<string>();
1819
}
1920

21+
/// <summary>
22+
/// Generate and return a string.
23+
/// </summary>
24+
/// <param name="fixture">The fixture to generate a string for</param>
25+
/// <returns>The generated string</returns>
26+
public static string StringMatching(this AnonymousValueFixture fixture, string regexPattern)
27+
{
28+
return fixture.RegexGenerator.Create(new RegularExpressionRequest(regexPattern), new DummyContext()).ToString();
29+
}
30+
2031
/// <summary>
2132
/// Generate and return a string starting with the given prefix.
2233
/// </summary>

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" />

0 commit comments

Comments
 (0)