Skip to content

Commit ddea6c1

Browse files
committed
Merge pull request #41 from HerrLoesch/master
Added plugin for city and street names.
2 parents c7d9341 + 6d154c3 commit ddea6c1

File tree

11 files changed

+2984
-2
lines changed

11 files changed

+2984
-2
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,7 @@ UpgradeLog*.htm
172172
*.bim_*.settings
173173

174174
# Microsoft Fakes
175-
FakesAssemblies/
175+
FakesAssemblies/
176+
ObjectFillerNET.v2.ncrunchsolution
177+
ObjectFiller/ObjectFiller.v2.ncrunchproject
178+
ObjectFiller.Test/ObjectFiller.Test.v2.ncrunchproject
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace ObjectFiller.Test
2+
{
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
using Tynamix.ObjectFiller.Plugins.String;
6+
7+
[TestClass]
8+
public class CityNamesPluginTest
9+
{
10+
[TestMethod]
11+
public void RandomNameIsReturned()
12+
{
13+
var sut = new CityNames();
14+
var value = sut.GetValue();
15+
16+
Assert.IsFalse(string.IsNullOrEmpty(value));
17+
}
18+
}
19+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System.Text.RegularExpressions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Tynamix.ObjectFiller;
4+
5+
namespace ObjectFiller.Test
6+
{
7+
[TestClass]
8+
public class EmailAddressesPluginTests
9+
{
10+
public string StandardAssertMessage = "Given value does not match e-mail address standard.";
11+
12+
/// <summary>
13+
/// Regex for EMail addresses based on RFC 5322. Unfortunately, it does not find whitespace and yes I am to dumb to fix this issue...
14+
/// </summary>
15+
/// <seealso cref="http://www.regular-expressions.info/email.html"/>
16+
private static Regex RFC5322RegEx = new Regex(
17+
@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", RegexOptions.IgnoreCase);
18+
19+
[TestMethod]
20+
public void DefaultModeShouldReturnValidEmailAdress()
21+
{
22+
var value = new EmailAddresses().GetValue();
23+
24+
StringAssert.Matches(value, RFC5322RegEx, StandardAssertMessage);
25+
}
26+
27+
[TestMethod]
28+
public void TwoCallsCreateTwoDifferentEMailAddresses()
29+
{
30+
var sut = new EmailAddresses();
31+
var firstValue = sut.GetValue();
32+
var secondValue = sut.GetValue();
33+
34+
Assert.AreNotEqual(firstValue, secondValue);
35+
}
36+
37+
[TestMethod]
38+
public void EMailAddressMustBeValidWithRealNames()
39+
{
40+
var sut = new EmailAddresses();
41+
42+
var value = sut.GetValue();
43+
44+
StringAssert.Matches(value, RFC5322RegEx, StandardAssertMessage);
45+
}
46+
47+
[TestMethod]
48+
public void DomainNamesAreUsedFromRandomData()
49+
{
50+
var referenceValue = "google.com";
51+
var fake = new FakeRandomizerPlugin<string>(referenceValue);
52+
53+
var sut = new EmailAddresses(fake, fake);
54+
55+
var result = sut.GetValue();
56+
57+
StringAssert.EndsWith(result, referenceValue);
58+
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
59+
}
60+
61+
[TestMethod]
62+
public void PluginMustEnsureValidAddressesEvenAnInvalidDomainNameIsProvided()
63+
{
64+
var referenceValue = "googlecom";
65+
var fake = new FakeRandomizerPlugin<string>(referenceValue);
66+
67+
var sut = new EmailAddresses(fake, fake);
68+
69+
var result = sut.GetValue();
70+
71+
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
72+
}
73+
74+
[TestMethod]
75+
public void LocalPathMustBeUsedFromRandomData()
76+
{
77+
var referenceValue = "karl";
78+
var fake = new FakeRandomizerPlugin<string>(referenceValue);
79+
80+
var sut = new EmailAddresses(fake);
81+
82+
var result = sut.GetValue();
83+
84+
StringAssert.StartsWith(result, referenceValue);
85+
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
86+
}
87+
88+
[TestMethod]
89+
public void PluginMustEnsureValidAddressesEvenAnInvalidLocalPartIsProvided()
90+
{
91+
var referenceValue = "ka rl";
92+
var fake = new FakeRandomizerPlugin<string>(referenceValue);
93+
94+
var sut = new EmailAddresses(fake);
95+
96+
var result = sut.GetValue();
97+
98+
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
99+
}
100+
101+
[TestMethod]
102+
public void GivenDomainRootIsAttachedToGeneratedEmailAddress()
103+
{
104+
var domainRoot = ".de";
105+
var sut = new EmailAddresses(domainRoot);
106+
107+
var result = sut.GetValue();
108+
109+
StringAssert.EndsWith(result, domainRoot);
110+
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
111+
}
112+
113+
[TestMethod]
114+
public void EmailAddressesWorksInCombinationWithRealNamesPlugin()
115+
{
116+
var realNames = new RealNames(RealNameStyle.FirstNameLastName);
117+
118+
var sut = new EmailAddresses(realNames);
119+
var result = sut.GetValue();
120+
121+
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
122+
}
123+
}
124+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Tynamix.ObjectFiller.Plugins.String;
3+
4+
namespace ObjectFiller.Test
5+
{
6+
[TestClass]
7+
public class GermanStreetNamesPluginTest
8+
{
9+
[TestMethod]
10+
public void RandomNameIsReturned()
11+
{
12+
var sut = new GermanStreetNames();
13+
var value = sut.GetValue();
14+
15+
Assert.IsFalse(string.IsNullOrEmpty(value));
16+
}
17+
}
18+
}

ObjectFiller.Test/ObjectFiller.Test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@
4646
</ItemGroup>
4747
<ItemGroup>
4848
<Compile Include="AddressFillingTest.cs" />
49+
<Compile Include="CityNamesPluginTest.cs" />
4950
<Compile Include="CreateInstanceTest.cs" />
5051
<Compile Include="DefaultDatatypeMappingsTest.cs" />
5152
<Compile Include="EnumTest.cs" />
53+
<Compile Include="GermanStreetNamesPluginTest.cs" />
5254
<Compile Include="HashStackTests.cs" />
5355
<Compile Include="LibraryFillingTest.cs" />
5456
<Compile Include="ListFillingTest.cs" />
@@ -57,6 +59,7 @@
5759
<Compile Include="PersonFillingTest.cs" />
5860
<Compile Include="Properties\AssemblyInfo.cs" />
5961
<Compile Include="ObjectFillerTest.cs" />
62+
<Compile Include="RandomizerPluginFake.cs" />
6063
<Compile Include="RangePluginTest.cs" />
6164
<Compile Include="RealNamePluginTest.cs" />
6265
<Compile Include="TestPoco\Library\Book.cs" />
@@ -76,6 +79,7 @@
7679
<Compile Include="TestPoco\Person\OrderedPersonProperties.cs" />
7780
<Compile Include="TestPoco\SimpleList.cs" />
7881
<Compile Include="SaveFillerSetupTest.cs" />
82+
<Compile Include="EmailAddressesPluginTest.cs" />
7983
</ItemGroup>
8084
<ItemGroup>
8185
<ProjectReference Include="..\ObjectFiller\ObjectFiller.csproj">
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using Tynamix.ObjectFiller;
3+
4+
namespace ObjectFiller.Test
5+
{
6+
public class FakeRandomizerPlugin<T> : IRandomizerPlugin<T>
7+
{
8+
public Func<T> OnGetValue;
9+
10+
public T ReturnValue { get; set; }
11+
12+
public FakeRandomizerPlugin()
13+
{
14+
}
15+
16+
public FakeRandomizerPlugin(T returnValue)
17+
{
18+
ReturnValue = returnValue;
19+
}
20+
21+
public T GetValue()
22+
{
23+
if (OnGetValue != null)
24+
{
25+
return OnGetValue();
26+
}
27+
28+
return ReturnValue;
29+
}
30+
}
31+
}

ObjectFiller/ObjectFiller.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<Reference Include="System.Core" />
3737
</ItemGroup>
3838
<ItemGroup>
39+
<Compile Include="Plugins\String\CityNames.cs" />
40+
<Compile Include="Plugins\String\EmailAddresses.cs" />
41+
<Compile Include="Plugins\String\GermanStreetNames.cs" />
3942
<Compile Include="Plugins\String\Lipsum.cs" />
4043
<Compile Include="HashStack.cs" />
4144
<Compile Include="Setup\FillerSetup.cs" />

ObjectFiller/Plugins/RandomListItem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public RandomListItem(IEnumerable<T> allAvailableValues)
2626

2727
}
2828

29-
3029
public T GetValue()
3130
{
3231
int rndmListIndex = Random.Next(0, _allAvailableValues.Length);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Tynamix.ObjectFiller.Plugins.String
2+
{
3+
using System.Collections.Generic;
4+
5+
public class CityNames : IRandomizerPlugin<string>
6+
{
7+
protected readonly List<string> _names = new List<string>
8+
{
9+
"Lodon",
10+
"Paris",
11+
"Dresden",
12+
"Berlin",
13+
"Rom",
14+
"New York"
15+
};
16+
17+
public string GetValue()
18+
{
19+
var index = Random.Next(_names.Count - 1);
20+
return _names[index];
21+
}
22+
}
23+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
namespace Tynamix.ObjectFiller
3+
{
4+
public class EmailAddresses : IRandomizerPlugin<string>
5+
{
6+
private readonly IRandomizerPlugin<string> domainNameSource;
7+
8+
private readonly IRandomizerPlugin<string> localPartSource;
9+
10+
private string domainRoot;
11+
12+
public EmailAddresses()
13+
: this(new MnemonicString(1), new MnemonicString(1), ".com")
14+
{
15+
}
16+
17+
public EmailAddresses(IRandomizerPlugin<string> localPartSource)
18+
: this(localPartSource, new MnemonicString(1), ".com")
19+
{
20+
}
21+
22+
public EmailAddresses(
23+
IRandomizerPlugin<string> localPartSource,
24+
IRandomizerPlugin<string> domainSource,
25+
string domainRoot)
26+
{
27+
this.domainRoot = domainRoot;
28+
this.localPartSource = localPartSource;
29+
this.domainNameSource = domainSource;
30+
}
31+
32+
public EmailAddresses(string domainRoot)
33+
: this(new MnemonicString(1), new MnemonicString(1), domainRoot)
34+
{
35+
}
36+
37+
public EmailAddresses(IRandomizerPlugin<string> localPartSource, IRandomizerPlugin<string> domainSource) : this(localPartSource, domainSource, ".com")
38+
{
39+
}
40+
41+
public string GetValue()
42+
{
43+
var localPart = this.GetLocalPart();
44+
var domain = this.GetDomainName();
45+
46+
return string.Format("{0}@{1}", localPart, domain).ToLower();
47+
}
48+
49+
private string GetDomainName()
50+
{
51+
var domainName = this.domainNameSource.GetValue();
52+
53+
if (domainName.Contains("."))
54+
{
55+
return domainName;
56+
}
57+
58+
return string.Format("{0}{1}", domainName, this.domainRoot);
59+
}
60+
61+
private string GetLocalPart()
62+
{
63+
var originalSample = this.localPartSource.GetValue();
64+
return originalSample.Replace(" ", ".");
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)