incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s9tpep...@apache.org
Subject svn commit: r1237167 - in /incubator/flex/whiteboard/s9tpepper/validators: src-tests/org/apache/flex/validators/StringConfirmationValidatorTests.as src/org/apache/flex/validators/StringConfirmationValidator.as
Date Sat, 28 Jan 2012 21:16:39 GMT
Author: s9tpepper
Date: Sat Jan 28 21:16:39 2012
New Revision: 1237167

URL: http://svn.apache.org/viewvc?rev=1237167&view=rev
Log:
Adding StringConfirmationValidator.
- Common use case is confirming the value of an email or password field.

Added:
    incubator/flex/whiteboard/s9tpepper/validators/src-tests/org/apache/flex/validators/StringConfirmationValidatorTests.as
  (with props)
    incubator/flex/whiteboard/s9tpepper/validators/src/org/apache/flex/validators/StringConfirmationValidator.as
  (with props)

Added: incubator/flex/whiteboard/s9tpepper/validators/src-tests/org/apache/flex/validators/StringConfirmationValidatorTests.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/s9tpepper/validators/src-tests/org/apache/flex/validators/StringConfirmationValidatorTests.as?rev=1237167&view=auto
==============================================================================
--- incubator/flex/whiteboard/s9tpepper/validators/src-tests/org/apache/flex/validators/StringConfirmationValidatorTests.as
(added)
+++ incubator/flex/whiteboard/s9tpepper/validators/src-tests/org/apache/flex/validators/StringConfirmationValidatorTests.as
Sat Jan 28 21:16:39 2012
@@ -0,0 +1,140 @@
+package org.apache.flex.validators
+{
+	import mockolate.runner.MockolateRule;
+	import mockolate.stub;
+
+	import mx.events.ValidationResultEvent;
+	import mx.validators.ValidationResult;
+
+	import org.flexunit.assertThat;
+	import org.flexunit.asserts.assertEquals;
+	import org.flexunit.asserts.assertNotNull;
+	import org.flexunit.asserts.assertNull;
+	import org.flexunit.asserts.assertTrue;
+	import org.flexunit.asserts.fail;
+	import org.hamcrest.number.greaterThan;
+
+	import spark.validators.supportClasses.GlobalizationValidatorBase;
+
+	public class StringConfirmationValidatorTests
+	{
+		[Rule]
+		public var mockolateRule:MockolateRule = new MockolateRule();
+
+		[Mock]
+		public var stringValidator:StringValidator;
+
+		private var stringConfirmationValidator:StringConfirmationValidator;
+
+		[Before]
+		public function setUp():void
+		{
+			stringConfirmationValidator = new StringConfirmationValidator();
+		}
+
+
+		[Test]
+		public function StringConfirmationValidator_onInstantiation_isSubclassOfGlobalizationBaseValidator():void
+		{
+			assertTrue(stringConfirmationValidator is GlobalizationValidatorBase);
+		}
+
+		[Test]
+		public function StringConfirmationValidator_onInstantiation_propertyIsNull():void
+		{
+			assertNull(stringConfirmationValidator.property);
+		}
+
+		[Test]
+		public function stringValidator_onSettingStringValidator_propertyUpdatedToStringValidatorProperty():void
+		{
+			stub(stringValidator).getter("property").returns("aPropertyName");
+
+			stringConfirmationValidator.stringValidator = stringValidator;
+
+			assertEquals("aPropertyName", stringConfirmationValidator.property);
+		}
+
+		[Test]
+		public function stringValidator_onSettingStringValidatorNull_propertyIsNowNull():void
+		{
+			stringConfirmationValidator.property = "aPropertyName";
+
+			stringConfirmationValidator.stringValidator = null;
+
+			assertNull(stringConfirmationValidator.property);
+		}
+
+		[Test]
+		public function validate_sourceDoesNotMatchStringValidatorSource_resultsNotNull():void
+		{
+			stubSourcesDontMatchError();
+
+			const validationResultEvent:ValidationResultEvent = stringConfirmationValidator.validate(null,
false);
+
+			assertNotNull(validationResultEvent.results);
+		}
+
+		[Test]
+		public function validate_sourceDoesNotMatchStringValidatorSource_validationResultMessageIsStringsDontMatchError():void
+		{
+			stubSourcesDontMatchError();
+
+			const validationResultEvent:ValidationResultEvent = stringConfirmationValidator.validate(null,
false);
+			const validationResult:ValidationResult           = validationResultEvent.results.pop()
as ValidationResult;
+
+			assertEquals(stringConfirmationValidator.stringDoesNotMatchError, validationResult.errorMessage);
+		}
+
+		private function stubSourcesDontMatchError():void
+		{
+			stub(stringValidator).getter("property").returns("text");
+			stub(stringValidator).getter("source").returns({text: "some different text",
+															description: "some other object"});
+			stringConfirmationValidator.source = {text: "some text"};
+			stringConfirmationValidator.stringValidator = stringValidator;
+		}
+
+
+		[Test]
+		public function validate_stringValidatorIsNull_resultsNotNull():void
+		{
+			stringConfirmationValidator.stringValidator = null;
+
+			const validationResultEvent:ValidationResultEvent = stringConfirmationValidator.validate(null,
false);
+
+			assertNotNull(validationResultEvent.results);
+		}
+
+		[Test]
+		public function validate_stringValidatorIsNull_validationResultMessageIsNothingToConfirmAgainstError():void
+		{
+			stringConfirmationValidator.stringValidator = null;
+
+			const validationResultEvent:ValidationResultEvent = stringConfirmationValidator.validate(null,
false);
+			const validationResult:ValidationResult           = validationResultEvent.results.pop()
as ValidationResult;
+
+			assertEquals(stringConfirmationValidator.nothingToConfirmAgainstError, validationResult.errorMessage);
+		}
+
+		[Test]
+		public function validate_stringValidatorSourceMatches_resultsIsNull():void
+		{
+			stub(stringValidator).getter("property").returns("text");
+			stub(stringValidator).getter("source").returns({text: "some text", description: "some
other object"});
+			stringConfirmationValidator.source = {text: "some text"};
+			stringConfirmationValidator.stringValidator = stringValidator;
+
+			var validationResultEvent:ValidationResultEvent = stringConfirmationValidator.validate(null,
false);
+
+			assertNull(validationResultEvent.results);
+		}
+
+
+		[After]
+		public function tearDown():void
+		{
+			stringConfirmationValidator = null;
+		}
+	}
+}

Propchange: incubator/flex/whiteboard/s9tpepper/validators/src-tests/org/apache/flex/validators/StringConfirmationValidatorTests.as
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/flex/whiteboard/s9tpepper/validators/src/org/apache/flex/validators/StringConfirmationValidator.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/s9tpepper/validators/src/org/apache/flex/validators/StringConfirmationValidator.as?rev=1237167&view=auto
==============================================================================
--- incubator/flex/whiteboard/s9tpepper/validators/src/org/apache/flex/validators/StringConfirmationValidator.as
(added)
+++ incubator/flex/whiteboard/s9tpepper/validators/src/org/apache/flex/validators/StringConfirmationValidator.as
Sat Jan 28 21:16:39 2012
@@ -0,0 +1,96 @@
+package org.apache.flex.validators
+{
+	import mx.core.mx_internal;
+	import mx.validators.ValidationResult;
+
+	import spark.validators.supportClasses.GlobalizationValidatorBase;
+
+	use namespace mx_internal;
+
+	public class StringConfirmationValidator extends GlobalizationValidatorBase
+	{
+		private var _stringValidator:StringValidator;
+		private var _stringDoesNotMatchError:String      = "Does not match.";
+		private var _nothingToConfirmAgainstError:String = "The confirmation validator does not
have a string to validate against.";
+
+		public function StringConfirmationValidator()
+		{
+			super();
+		}
+
+		public function get nothingToConfirmAgainstError():String
+		{
+			return _nothingToConfirmAgainstError;
+		}
+
+		public function set nothingToConfirmAgainstError(value:String):void
+		{
+			_nothingToConfirmAgainstError = value;
+		}
+
+		public function get stringDoesNotMatchError():String
+		{
+			return _stringDoesNotMatchError;
+		}
+
+		public function set stringDoesNotMatchError(value:String):void
+		{
+			_stringDoesNotMatchError = value;
+		}
+
+		public function get stringValidator():StringValidator
+		{
+			return _stringValidator;
+		}
+
+		public function set stringValidator(value:StringValidator):void
+		{
+			_stringValidator = value;
+
+			property = (_stringValidator) ? _stringValidator.property : null;
+		}
+
+		override protected function doValidation(value:Object):Array
+		{
+			var results:Array = super.doValidation(value);
+
+			if (stringValidator)
+			{
+				if (valuesDontMatch())
+					generateError(results, stringDoesNotMatchError);
+			}
+			else
+			{
+				generateError(results, nothingToConfirmAgainstError);
+			}
+
+			return results;
+		}
+
+		private function generateError(results:Array, errorMessage:String=""):void
+		{
+			results.push(new ValidationResult(true, "", "", errorMessage));
+		}
+
+
+		protected function valuesDontMatch():Boolean
+		{
+			const stringValidatorCurrentValue:String = stringValidator.source[stringValidator.property];
+			const confirmationCurrentValue:String    = source[property];
+			return confirmationCurrentValue != stringValidatorCurrentValue;
+		}
+
+		override mx_internal function createWorkingInstance():void
+		{
+		}
+
+		mx_internal function get g11nWorkingInstance():Object
+		{
+			return null;
+		}
+
+		mx_internal function set g11nWorkingInstance(sparkFormatter:Object):void
+		{
+		}
+	}
+}

Propchange: incubator/flex/whiteboard/s9tpepper/validators/src/org/apache/flex/validators/StringConfirmationValidator.as
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message