-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added plugin for e-mail address and a fake class for IRandomizerPlugin.
- Loading branch information
unknown
committed
Apr 27, 2015
1 parent
5b33624
commit 6d154c3
Showing
7 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ", "."); | ||
} | ||
} | ||
} |