|
| 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 | +} |
0 commit comments