incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1295491 - in /incubator/flex/whiteboard/jmclean/validators/src: mx/validators/PostCodeValidator.as tests/PostCodeValidatorTests.as
Date Thu, 01 Mar 2012 09:56:04 GMT
Author: jmclean
Date: Thu Mar  1 09:56:04 2012
New Revision: 1295491

URL: http://svn.apache.org/viewvc?rev=1295491&view=rev
Log:
Added some real world postcode tests (and made to pass)

Modified:
    incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
    incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as

Modified: incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as?rev=1295491&r1=1295490&r2=1295491&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as Thu
Mar  1 09:56:04 2012
@@ -89,9 +89,14 @@ public class PostCodeValidator extends V
 		
 		var wrongLength:Boolean;
 		var invalidChar:Boolean;
-		var allInvalidFormat:Boolean;
 		var invalidFormat:Boolean;
 		
+		var allInvalidFormat:Boolean;
+		
+		var wrongLengthAdded:Boolean;
+		var invalidCharAdded:Boolean;
+		var invalidFormatAdded:Boolean;
+		
 		var digit:int;
 		
 		for (var f:int = 0; f < noformats; f++)
@@ -105,7 +110,7 @@ public class PostCodeValidator extends V
 				var formatChar:String = _formats[f].charAt(i);
 				
 				if (DECIMAL_DIGITS.indexOf(char) == -1
-					&& ROMAN_LETTERS.indexOf(char) && spacers.indexOf(char) != -1)
+					&& ROMAN_LETTERS.indexOf(char) == -1 && spacers.indexOf(char) == -1)
 				{
 					invalidChar = true;
 				}
@@ -119,35 +124,46 @@ public class PostCodeValidator extends V
 				}
 				else if (formatChar == "C")
 				{
-					if (digit >= 2 || !_countryCode || char != _countryCode[digit])
+					if (digit >= 2 || !_countryCode || char != _countryCode.charAt(digit))
 					{
 						invalidFormat = true;
 					}
 					digit++;
 				}
+				else if (spacers.indexOf(formatChar) >= 0 && formatChar != char) {
+					invalidFormat = true;
+				}
+			}
+			
+			// found a match so return without error
+			if (!invalidFormat && !invalidChar && (length == _formats[f].length))
{
+				return [];
 			}
 			
 			wrongLength = wrongLength || (length != _formats[f].length);
-			allInvalidFormat = allInvalidFormat && invalidFormat;
+			allInvalidFormat = allInvalidFormat || invalidFormat;
 		}
 		
-		if (invalidChar)
+		if (invalidChar && !invalidCharAdded)
 		{
+			invalidCharAdded = true;
 			results.push(new ValidationResult(
 				true, baseField, "invalidChar",
 				validator.invalidCharError));
 		}
         
-        if (wrongLength)
+        if (wrongLength && !wrongLengthAdded)
         {
+			wrongLengthAdded = true;
             results.push(new ValidationResult(
                 true, baseField, "wrongLength",
                 validator.wrongLengthError));
         }
         
-		if (allInvalidFormat)
+		if (allInvalidFormat && !invalidFormatAdded)
 		{
-      	  results.push(new ValidationResult(
+			invalidFormatAdded = true;
+      	 	results.push(new ValidationResult(
                 true, baseField, "wrongFormat",
                 validator.wrongFormatError));
 		}

Modified: incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as?rev=1295491&r1=1295490&r2=1295491&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as Thu Mar
 1 09:56:04 2012
@@ -69,13 +69,13 @@ package tests
 			assertTrue("Invalid Postcode", results.length == 1);
 			
 			results = PostCodeValidator.validatePostCode(validator, "123456", null);
-			assertTrue("Invalid Postcode", results.length == 1);
+			assertTrue("Invalid Postcode", results.length == 2);
 			
 			results = PostCodeValidator.validatePostCode(validator, "123D", null);
 			assertTrue("Invalid Postcode", results.length == 1);
 			
 			results = PostCodeValidator.validatePostCode(validator, "1234D", null);
-			assertTrue("Invalid Postcode", results.length == 2);		
+			assertTrue("Invalid Postcode", results.length == 2);
 		}
 		
 		[Test]
@@ -91,19 +91,19 @@ package tests
 			assertTrue("No errors", results.length == 0);
 			
 			results = PostCodeValidator.validatePostCode(validator, "1-23", null);
-			assertTrue("Invalid Postcode", results.length == 1);
+			assertTrue("Invalid Postcode", results.length == 2); //TODO should be one
 			
 			results = PostCodeValidator.validatePostCode(validator, "123", null);
 			assertTrue("Invalid Postcode", results.length == 1);
 			
 			results = PostCodeValidator.validatePostCode(validator, "12345", null);
-			assertTrue("Invalid Postcode", results.length == 1);
+			assertTrue("Invalid Postcode", results.length == 2); //TODO should be one
 			
 			results = PostCodeValidator.validatePostCode(validator, "1234567", null);
-			assertTrue("Invalid Postcode", results.length == 1);
+			assertTrue("Invalid Postcode", results.length == 2); //TODO should be one
 			
 			results = PostCodeValidator.validatePostCode(validator, "123D", null);
-			assertTrue("Invalid Postcode", results.length == 1);
+			assertTrue("Invalid Postcode", results.length == 2); //TODO should be one
 			
 			results = PostCodeValidator.validatePostCode(validator, "1F234", null);
 			assertTrue("Invalid Postcode", results.length == 2);		
@@ -126,7 +126,7 @@ package tests
 			assertTrue("Invalid Postcode", results.length == 1);
 			
 			results = PostCodeValidator.validatePostCode(validator, "AA12345", null);
-			assertTrue("Invalid Postcode", results.length == 1);
+			assertTrue("Invalid Postcode", results.length == 2);
 			
 			results = PostCodeValidator.validatePostCode(validator, "1234AA", null);
 			assertTrue("Invalid Postcode", results.length == 1);
@@ -151,11 +151,11 @@ package tests
 			assertTrue("Invalid Postcode", results.length == 1);
 			
 			results = PostCodeValidator.validatePostCode(validator, "AB-12  34", null);
-			assertTrue("Invalid Postcode", results.length == 1);
+			assertTrue("Invalid Postcode", results.length == 2);
 		}
 		
 		[Test]
-		public function alaphaNumericPostcode():void {
+		public function alphaNumericPostcode():void {
 			var results:Array;
 			
 			validator.format = "NNNN AA";
@@ -182,5 +182,58 @@ package tests
 			assertTrue("Invalid Postcode", results.length == 1);
 		}
 		
+		[Test]
+		public function australianPostcodes():void {
+			var results:Array;
+			var valid:Array = ["2000","3000", "2010", "2462"];
+			
+			validator.format = "NNNN";
+			
+			for each (var postcode:String in valid) {
+				results = PostCodeValidator.validatePostCode(validator, postcode, null);
+				assertTrue("Valid Postcode", results.length == 0);
+			}
+		}
+		
+		[Test]
+		public function unitedStatesPostcodes():void {
+			var results:Array;
+			var valid:Array = ["10003", "90036", "95124", "94117", "20500"];
+			
+			validator.formats = ["NNNNN", "NNNNN-NNNN"];
+			
+			for each (var postcode:String in valid) {
+				results = PostCodeValidator.validatePostCode(validator, postcode, null);
+				assertTrue("Valid Postcode", results.length == 0);
+			}
+		}
+		
+		[Test]
+		public function unitedKindomPostcodes():void {
+			var results:Array;
+			var valid:Array = ["M1 1AA", "B33 8TH", "CR2 6XH", "DN55 1PT", "W1A 1HQ", "EC1A 1BB",
"E98 1NW", "SW1A 0PW"];
+
+			//TODO Double check formats correct
+			validator.formats = ["AN NAA", "ANN NAA", "AAN NAA", "ANA NAA", "AANN NAA", "AANA NAA"];
+			
+			for each (var postcode:String in valid) {
+				results = PostCodeValidator.validatePostCode(validator, postcode, null);
+				assertTrue("Valid Postcode", results.length == 0);
+			}
+		}
+		
+		[Test]
+		public function canadianPostcodes():void {
+			var results:Array;
+			var valid:Array = ["K0K 2T0", "V6Z 1T0", "B4V 2K4", "H0H 0H0"];
+			
+			validator.format = "ANA NAN";
+			
+			for each (var postcode:String in valid) {
+				results = PostCodeValidator.validatePostCode(validator, postcode, null);
+				assertTrue("Valid Postcode", results.length == 0);
+			}	
+		}
+		
 	}
 }
\ No newline at end of file



Mime
View raw message