Skip to content

Commit

Permalink
Added plugin for e-mail address and a fake class for IRandomizerPlugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Apr 27, 2015
1 parent 5b33624 commit 6d154c3
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,7 @@ UpgradeLog*.htm
*.bim_*.settings

# Microsoft Fakes
FakesAssemblies/
FakesAssemblies/
ObjectFillerNET.v2.ncrunchsolution
ObjectFiller/ObjectFiller.v2.ncrunchproject
ObjectFiller.Test/ObjectFiller.Test.v2.ncrunchproject
124 changes: 124 additions & 0 deletions ObjectFiller.Test/EmailAddressesPluginTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tynamix.ObjectFiller;

namespace ObjectFiller.Test
{
[TestClass]
public class EmailAddressesPluginTests
{
public string StandardAssertMessage = "Given value does not match e-mail address standard.";

/// <summary>
/// Regex for EMail addresses based on RFC 5322. Unfortunately, it does not find whitespace and yes I am to dumb to fix this issue...
/// </summary>
/// <seealso cref="http://www.regular-expressions.info/email.html"/>
private static Regex RFC5322RegEx = new Regex(
@"[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);

[TestMethod]
public void DefaultModeShouldReturnValidEmailAdress()
{
var value = new EmailAddresses().GetValue();

StringAssert.Matches(value, RFC5322RegEx, StandardAssertMessage);
}

[TestMethod]
public void TwoCallsCreateTwoDifferentEMailAddresses()
{
var sut = new EmailAddresses();
var firstValue = sut.GetValue();
var secondValue = sut.GetValue();

Assert.AreNotEqual(firstValue, secondValue);
}

[TestMethod]
public void EMailAddressMustBeValidWithRealNames()
{
var sut = new EmailAddresses();

var value = sut.GetValue();

StringAssert.Matches(value, RFC5322RegEx, StandardAssertMessage);
}

[TestMethod]
public void DomainNamesAreUsedFromRandomData()
{
var referenceValue = "google.com";
var fake = new FakeRandomizerPlugin<string>(referenceValue);

var sut = new EmailAddresses(fake, fake);

var result = sut.GetValue();

StringAssert.EndsWith(result, referenceValue);
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
}

[TestMethod]
public void PluginMustEnsureValidAddressesEvenAnInvalidDomainNameIsProvided()
{
var referenceValue = "googlecom";
var fake = new FakeRandomizerPlugin<string>(referenceValue);

var sut = new EmailAddresses(fake, fake);

var result = sut.GetValue();

StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
}

[TestMethod]
public void LocalPathMustBeUsedFromRandomData()
{
var referenceValue = "karl";
var fake = new FakeRandomizerPlugin<string>(referenceValue);

var sut = new EmailAddresses(fake);

var result = sut.GetValue();

StringAssert.StartsWith(result, referenceValue);
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
}

[TestMethod]
public void PluginMustEnsureValidAddressesEvenAnInvalidLocalPartIsProvided()
{
var referenceValue = "ka rl";
var fake = new FakeRandomizerPlugin<string>(referenceValue);

var sut = new EmailAddresses(fake);

var result = sut.GetValue();

StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
}

[TestMethod]
public void GivenDomainRootIsAttachedToGeneratedEmailAddress()
{
var domainRoot = ".de";
var sut = new EmailAddresses(domainRoot);

var result = sut.GetValue();

StringAssert.EndsWith(result, domainRoot);
StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
}

[TestMethod]
public void EmailAddressesWorksInCombinationWithRealNamesPlugin()
{
var realNames = new RealNames(RealNameStyle.FirstNameLastName);

var sut = new EmailAddresses(realNames);
var result = sut.GetValue();

StringAssert.Matches(result, RFC5322RegEx, StandardAssertMessage);
}
}
}
2 changes: 2 additions & 0 deletions ObjectFiller.Test/ObjectFiller.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="PersonFillingTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ObjectFillerTest.cs" />
<Compile Include="RandomizerPluginFake.cs" />
<Compile Include="RangePluginTest.cs" />
<Compile Include="RealNamePluginTest.cs" />
<Compile Include="TestPoco\Library\Book.cs" />
Expand All @@ -78,6 +79,7 @@
<Compile Include="TestPoco\Person\OrderedPersonProperties.cs" />
<Compile Include="TestPoco\SimpleList.cs" />
<Compile Include="SaveFillerSetupTest.cs" />
<Compile Include="EmailAddressesPluginTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ObjectFiller\ObjectFiller.csproj">
Expand Down
31 changes: 31 additions & 0 deletions ObjectFiller.Test/RandomizerPluginFake.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Tynamix.ObjectFiller;

namespace ObjectFiller.Test
{
public class FakeRandomizerPlugin<T> : IRandomizerPlugin<T>
{
public Func<T> OnGetValue;

public T ReturnValue { get; set; }

public FakeRandomizerPlugin()
{
}

public FakeRandomizerPlugin(T returnValue)
{
ReturnValue = returnValue;
}

public T GetValue()
{
if (OnGetValue != null)
{
return OnGetValue();
}

return ReturnValue;
}
}
}
1 change: 1 addition & 0 deletions ObjectFiller/ObjectFiller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Plugins\String\CityNames.cs" />
<Compile Include="Plugins\String\EmailAddresses.cs" />
<Compile Include="Plugins\String\GermanStreetNames.cs" />
<Compile Include="Plugins\String\Lipsum.cs" />
<Compile Include="HashStack.cs" />
Expand Down
1 change: 0 additions & 1 deletion ObjectFiller/Plugins/RandomListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public RandomListItem(IEnumerable<T> allAvailableValues)

}


public T GetValue()
{
int rndmListIndex = Random.Next(0, _allAvailableValues.Length);
Expand Down
67 changes: 67 additions & 0 deletions ObjectFiller/Plugins/String/EmailAddresses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

namespace Tynamix.ObjectFiller
{
public class EmailAddresses : IRandomizerPlugin<string>
{
private readonly IRandomizerPlugin<string> domainNameSource;

private readonly IRandomizerPlugin<string> localPartSource;

private string domainRoot;

public EmailAddresses()
: this(new MnemonicString(1), new MnemonicString(1), ".com")
{
}

public EmailAddresses(IRandomizerPlugin<string> localPartSource)
: this(localPartSource, new MnemonicString(1), ".com")
{
}

public EmailAddresses(
IRandomizerPlugin<string> localPartSource,
IRandomizerPlugin<string> domainSource,
string domainRoot)
{
this.domainRoot = domainRoot;
this.localPartSource = localPartSource;
this.domainNameSource = domainSource;
}

public EmailAddresses(string domainRoot)
: this(new MnemonicString(1), new MnemonicString(1), domainRoot)
{
}

public EmailAddresses(IRandomizerPlugin<string> localPartSource, IRandomizerPlugin<string> domainSource) : this(localPartSource, domainSource, ".com")
{
}

public string GetValue()
{
var localPart = this.GetLocalPart();
var domain = this.GetDomainName();

return string.Format("{0}@{1}", localPart, domain).ToLower();
}

private string GetDomainName()
{
var domainName = this.domainNameSource.GetValue();

if (domainName.Contains("."))
{
return domainName;
}

return string.Format("{0}{1}", domainName, this.domainRoot);
}

private string GetLocalPart()
{
var originalSample = this.localPartSource.GetValue();
return originalSample.Replace(" ", ".");
}
}
}

0 comments on commit 6d154c3

Please sign in to comment.