Skip to content

Commit e3bb7e6

Browse files
committed
Added Any.StringOfLength equivalence class
1 parent 93c87fe commit e3bb7e6

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

NTestDataBuilder.Tests/EquivalenceClasses/StringEquivalenceClassesTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,23 @@ public void WhenGettingAnyStringEndingWithSomething_ThenReturnDifferentStringsEn
7070
s2.ShouldEndWith("3nd");
7171
s3.ShouldEndWith("Something Else");
7272
}
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+
}
7391
}
7492
}

NTestDataBuilder/EquivalenceClasses/StringEquivalenceClasses.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Ploeh.AutoFixture;
1+
using System.Text;
2+
using Ploeh.AutoFixture;
23
using Ploeh.AutoFixture.Kernel;
34

45
namespace NTestDataBuilder.EquivalenceClasses
@@ -19,9 +20,11 @@ public static string String(this AnonymousValueFixture fixture)
1920
}
2021

2122
/// <summary>
22-
/// Generate and return a string.
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.
2325
/// </summary>
2426
/// <param name="fixture">The fixture to generate a string for</param>
27+
/// <param name="regexPattern">The regex pattern to match</param>
2528
/// <returns>The generated string</returns>
2629
public static string StringMatching(this AnonymousValueFixture fixture, string regexPattern)
2730
{
@@ -49,5 +52,21 @@ public static string StringEndingWith(this AnonymousValueFixture fixture, string
4952
{
5053
return string.Format("{0}{1}", fixture.Fixture.Create<string>(), suffix);
5154
}
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+
}
5271
}
5372
}

0 commit comments

Comments
 (0)