Skip to content

Commit

Permalink
Merge pull request #62 from unoplatform/dev/jela/validation-helper
Browse files Browse the repository at this point in the history
Add existing tests for ValidationHelper
  • Loading branch information
jeromelaban authored Jan 7, 2022
2 parents 8a7744d + ab69a90 commit 382d459
Showing 1 changed file with 252 additions and 0 deletions.
252 changes: 252 additions & 0 deletions src/Uno.Core.Tests/Validation/ValidationHelperFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Extensions;
using System.Globalization;
using Uno.Validation;

namespace Uno.Core.Validation
{
[TestClass]
public class ValidationHelperFixture
{
[TestMethod]
public void EmailValidation()
{
string[] validEmails =
{
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
//some gTLD
"[email protected]",
"[email protected]",
"[email protected]",
// special characters and accent are not yet supported
//"[email protected]ów.pl",
//"a@épingle.ca",
};

validEmails.ForEach(input => Assert.IsTrue(ValidationHelper.IsEmail(input)));

string[] invalidEmails =
{
"Abc.example.com", // At least one '@'
"A@b@[email protected]", // No more than one '@'
"[email protected]", // No consecutive dots for local part
"[email protected]", // No consecutive dots for domain part
"[email protected]", // Dot character should not be first
"[email protected]", // Dot character should not be last
" @example.com", // Must have local part
"@example.com", // Must have local part
"john@ .com", // Must have domain part
"john@test. ", // Must have domain part
"jo [email protected]", // No un-quoted spaces in local part
"john@exa mple.com", // No un-quoted spaces in domain part
null,
string.Empty,
"a@a",
"a.ca"
};

invalidEmails.ForEach(input => Assert.IsFalse(ValidationHelper.IsEmail(input)));
}

[TestMethod]
public void CanadianPostalCodeValidation()
{
string[] invalidPostalCodes =
{
"",
"10H 0H0",
"H01 0H0",
"H*H 0H0",
"H*H?0H0",
"H*H@0H0",
"H0H@0H0",
"H010H0",
"6L6",
"@8K5",
"14J4",
"J7C6",
"9D5",
" 8I3",
" AF9HJ",
"H7E7H7Q",
" 9I1",
"QH70G9",
"Z1A 1A9", //Cannot start with DFIOQU WZ
"W1A 1A9", //Cannot start with DFIOQU WZ
"A1Q 1A9", //DFIOQU cannot be used at any letter position
"90210", // US Zip code
};

Assert.IsTrue(ValidationHelper.IsCanadianPostalCode("A1A 1A9"));
Assert.IsTrue(ValidationHelper.IsCanadianPostalCode("a1a 1a9"));
Assert.IsTrue(ValidationHelper.IsCanadianPostalCode("a1a1a9"));

Assert.IsFalse(ValidationHelper.IsCanadianPostalCode(null));
Assert.IsFalse(ValidationHelper.IsCanadianPostalCode(string.Empty));
invalidPostalCodes.ForEach(pc => Assert.IsFalse(ValidationHelper.IsCanadianPostalCode(pc)));
}

[TestMethod]
public void ZipCodeValidation()
{
Assert.IsTrue(ValidationHelper.IsZipCode("90210"));
Assert.IsTrue(ValidationHelper.IsZipCode("90210-1234"));

Assert.IsFalse(ValidationHelper.IsZipCode(null));
Assert.IsFalse(ValidationHelper.IsZipCode(string.Empty));
Assert.IsFalse(ValidationHelper.IsZipCode("9021"));
Assert.IsFalse(ValidationHelper.IsZipCode("90210-123"));
Assert.IsFalse(ValidationHelper.IsZipCode("G1Q 1Q9"));
}

[TestMethod]
public void UsStatesValidation()
{
//Lower case test
Assert.IsTrue(ValidationHelper.IsUSState("al"));

//All us states from https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations USPS column
var usStates = new string[] {
"AL",
"AK",
"AZ",
"AR",
"CA",
"CO",
"CT",
"DE",
"DC",
"FL",
"GA",
"HI",
"ID",
"IL",
"IN",
"IA",
"KS",
"KY",
"LA",
"ME",
"MD",
"MA",
"MI",
"MN",
"MS",
"MO",
"MT",
"NE",
"NV",
"NH",
"NJ",
"NM",
"NY",
"NC",
"ND",
"OH",
"OK",
"OR",
"PA",
"RI",
"SC",
"SD",
"TN",
"TX",
"UT",
"VT",
"VA",
"WA",
"WV",
"WI",
"WY"};
usStates.ForEach(s => Assert.IsTrue(ValidationHelper.IsUSState(s)));

//See above reference
var usTerritories = new string[] {
"AS",
"GU",
"MP",
"PR",
"VI"
};
usTerritories.ForEach(s => Assert.IsTrue(ValidationHelper.IsUSState(s, includeTerritories: true)));

// See above reference
var usMillitaryMailCodes = new string[] {
"AA",
"AE",
"AP"
};
usMillitaryMailCodes.ForEach(s => Assert.IsTrue(ValidationHelper.IsUSState(s, includeMilitary: true)));

Assert.IsFalse(ValidationHelper.IsUSState(null));
Assert.IsFalse(ValidationHelper.IsUSState(string.Empty));
Assert.IsFalse(ValidationHelper.IsUSState("ZZ"));
}

[TestMethod]
public void USCAnadaPhoneValidation()
{
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("555-1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("5551234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("1115551234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("111555-1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("111-555-1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("111 555-1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("111 555 1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("111 555.1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("111.555.1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("(111)555.1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("1(111)555.1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("+1(111)555.1234"));
Assert.IsTrue(ValidationHelper.IsUSCanadaPhone("1-123-555-1234"));

Assert.IsFalse(ValidationHelper.IsUSCanadaPhone(null));
Assert.IsFalse(ValidationHelper.IsUSCanadaPhone(string.Empty));
Assert.IsFalse(ValidationHelper.IsUSCanadaPhone("(11)555.1234"));
Assert.IsFalse(ValidationHelper.IsUSCanadaPhone("11555.1234"));
Assert.IsFalse(ValidationHelper.IsUSCanadaPhone("11 555 1234"));
Assert.IsFalse(ValidationHelper.IsUSCanadaPhone("1_123-555-1234"));
}

[TestMethod]
public void CurrencyValidation()
{
var culture = new CultureInfo("us-en");
culture.NumberFormat.CurrencySymbol = "$";

Assert.IsTrue(ValidationHelper.IsCurrency("$1.2", culture));
Assert.IsTrue(ValidationHelper.IsCurrency("1,222.20$", culture));
Assert.IsTrue(ValidationHelper.IsCurrency("$12", culture));
Assert.IsTrue(ValidationHelper.IsCurrency("1.2", culture));

Assert.IsTrue(ValidationHelper.IsCurrency("1,2", culture)); // ',' considered thousands seperator
Assert.IsFalse(ValidationHelper.IsCurrency("1 2", culture)); // wrong thousands seperator

culture = new CultureInfo("fr-fr");
culture.NumberFormat.CurrencySymbol = "£";
Assert.IsTrue(ValidationHelper.IsCurrency("£1,2", culture));
Assert.IsTrue(ValidationHelper.IsCurrency("1 121,2", culture));

Assert.IsFalse(ValidationHelper.IsCurrency(null));
Assert.IsFalse(ValidationHelper.IsCurrency(string.Empty));
Assert.IsFalse(ValidationHelper.IsCurrency("$1,2", culture));
Assert.IsFalse(ValidationHelper.IsCurrency("1.2", culture)); //wrong decimal seperator
}
}
}

0 comments on commit 382d459

Please sign in to comment.