This is an automated email from the ASF dual-hosted git repository. joshtynjala pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/royale-asjs.git
commit f1f19edb315b214ae2e2b6fde73c08a111515310 Author: Josh Tynjala <[email protected]> AuthorDate: Mon Dec 19 13:51:50 2022 -0800 RoyaleUnit: assertMatch and assertNotMatch --- .../main/royale/org/apache/royale/test/Assert.as | 42 ++++++++++++++++++++++ .../org/apache/royale/test/asserts/assertMatch.as | 30 ++++++++++++++++ .../apache/royale/test/asserts/assertNotMatch.as | 30 ++++++++++++++++ .../royale/org/apache/royale/test/bdd/IExpect.as | 37 +++++++++++++++++++ .../royale/org/apache/royale/test/bdd/expect.as | 33 +++++++++++++++++ .../src/test/royale/tests/AssertTests.as | 24 +++++++++++++ .../src/test/royale/tests/ExpectBDDTests.as | 24 +++++++++++++ 7 files changed, 220 insertions(+) diff --git a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/Assert.as b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/Assert.as index fb2eb6f67b..44b2c563e5 100644 --- a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/Assert.as +++ b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/Assert.as @@ -221,6 +221,26 @@ package org.apache.royale.test failType(actual, type, message); } + /** + * Asserts that the provided value matches a regular expression. + * Equivalent to testing the value in an + * <code>if(regExp.test(value))</code> statement. + */ + public static function assertMatch(actual:String, regExp:RegExp, message:String = null):void + { + failNotMatch(actual, regExp, message); + } + + /** + * Asserts that the provided value does not match a regular expression. + * Equivalent to testing the value in an + * <code>if(!regExp.test(value))</code> statement. + */ + public static function assertNotMatch(actual:String, regExp:RegExp, message:String = null):void + { + failMatch(actual, regExp, message); + } + /** * Fails. */ @@ -453,6 +473,28 @@ package org.apache.royale.test } } + /** + * Fails if the provided value matches a regular expression. + */ + public static function failMatch(actual:String, regExp:RegExp, message:String = null):void + { + if (regExp.test(actual)) + { + failWithUserMessage("expected <" + actual + "> to match regular expression <" + regExp + ">", message); + } + } + + /** + * Fails if the provided value does not match a regular expression. + */ + public static function failNotMatch(actual:String, regExp:RegExp, message:String = null):void + { + if (!regExp.test(actual)) + { + failWithUserMessage("expected <" + actual + "> not to match regular expression <" + regExp + ">", message); + } + } + /** * @private */ diff --git a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertMatch.as b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertMatch.as new file mode 100644 index 0000000000..299425783d --- /dev/null +++ b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertMatch.as @@ -0,0 +1,30 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.royale.test.asserts +{ + import org.apache.royale.test.Assert; + + /** + * Alias for org.apache.royale.test.Assert assertMatch method + */ + public function assertMatch(value:String, regExp:RegExp, message:String = null):void + { + Assert.assertMatch(value, regExp, message); + } +} \ No newline at end of file diff --git a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotMatch.as b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotMatch.as new file mode 100644 index 0000000000..96c04c667a --- /dev/null +++ b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotMatch.as @@ -0,0 +1,30 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.royale.test.asserts +{ + import org.apache.royale.test.Assert; + + /** + * Alias for org.apache.royale.test.Assert assertNotMatch method + */ + public function assertNotMatch(value:String, regExp:RegExp, message:String = null):void + { + Assert.assertNotMatch(value, regExp, message); + } +} \ No newline at end of file diff --git a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/bdd/IExpect.as b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/bdd/IExpect.as index bf09eeb8d7..3df66d54eb 100644 --- a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/bdd/IExpect.as +++ b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/bdd/IExpect.as @@ -115,6 +115,13 @@ package org.apache.royale.test.bdd */ function lt(other:Number, message:String = null):IExpect; + /** + * Alias for <code>lessThan()</code>. + * + * @see #lessThan + */ + function below(other:Number, message:String = null):IExpect; + /** * Alias for <code>greaterThan()</code>. * @@ -122,6 +129,13 @@ package org.apache.royale.test.bdd */ function gt(other:Number, message:String = null):IExpect; + /** + * Alias for <code>greaterThan()</code>. + * + * @see #greaterThan + */ + function above(other:Number, message:String = null):IExpect; + /** * Alias for <code>lessThanOrEqual()</code>. * @@ -129,6 +143,13 @@ package org.apache.royale.test.bdd */ function lte(other:Number, message:String = null):IExpect; + /** + * Alias for <code>lessThanOrEqual()</code>. + * + * @see #lessThanOrEqual + */ + function most(other:Number, message:String = null):IExpect; + /** * Alias for <code>greaterThanOrEqual()</code>. * @@ -136,6 +157,13 @@ package org.apache.royale.test.bdd */ function gte(other:Number, message:String = null):IExpect; + /** + * Alias for <code>greaterThanOrEqual()</code>. + * + * @see #greaterThanOrEqual + */ + function least(other:Number, message:String = null):IExpect; + /** * Asserts that the provided value is of a type. Equivalent to testing * the value in an <code>if(value is type)</code> statement. @@ -144,6 +172,15 @@ package org.apache.royale.test.bdd */ function type(type:Class, message:String = null):IExpect; + /** + * Asserts that the provided value matches a regular expression. + * Equivalent to testing the value in an + * <code>if(regExp.test(value))</code> statement. + * + * @see Assert#assertMatch + */ + function match(regExp:RegExp, message:String = null):IExpect; + /** * Negates all assertions that follow in the chain. */ diff --git a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/bdd/expect.as b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/bdd/expect.as index 3b13369479..7ce214f377 100644 --- a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/bdd/expect.as +++ b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/bdd/expect.as @@ -151,21 +151,41 @@ class Expect implements IExpect return lessThan(other, message); } + public function below(other:Number, message:String = null):IExpect + { + return lessThan(other, message); + } + public function lte(other:Number, message:String = null):IExpect { return lessThanOrEqual(other, message); } + public function most(other:Number, message:String = null):IExpect + { + return lessThanOrEqual(other, message); + } + public function gt(other:Number, message:String = null):IExpect { return greaterThan(other, message); } + public function above(other:Number, message:String = null):IExpect + { + return greaterThan(other, message); + } + public function gte(other:Number, message:String = null):IExpect { return greaterThanOrEqual(other, message); } + public function least(other:Number, message:String = null):IExpect + { + return greaterThanOrEqual(other, message); + } + public function type(type:Class, message:String = null):IExpect { if (_not) @@ -179,6 +199,19 @@ class Expect implements IExpect return this; } + public function match(regExp:RegExp, message:String = null):IExpect + { + if (_not) + { + Assert.assertNotMatch(_value, regExp, message); + } + else + { + Assert.assertMatch(_value, regExp, message); + } + return this; + } + public function get not():IExpect { _not = !_not; diff --git a/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as b/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as index cb657febad..386a4e76b2 100644 --- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as +++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as @@ -955,6 +955,30 @@ package tests Assert.assertNotType(obj, AssertTypeSubClass); } + [Test] + public function testAssertMatchSuccess():void + { + Assert.assertMatch("abc123", /^[a-z]{3}\d{3}$/); + } + + [Test(expected="org.apache.royale.test.AssertionError")] + public function testAssertMatchFailure():void + { + Assert.assertMatch("123abc", /^[a-z]{3}\d{3}$/); + } + + [Test(expected="org.apache.royale.test.AssertionError")] + public function testAssertNotMatchSuccess():void + { + Assert.assertNotMatch("abc123", /^[a-z]{3}\d{3}$/); + } + + [Test] + public function testAssertNotMatchFailure():void + { + Assert.assertNotMatch("123abc", /^[a-z]{3}\d{3}$/); + } + [Test(expected="org.apache.royale.test.AssertionError")] public function testFail():void { diff --git a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as index 0bf42df96a..80ff0af0b8 100644 --- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as +++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as @@ -1734,6 +1734,30 @@ package tests var obj:ExpectTypeClass = new ExpectTypeClass(); expect(obj).is.not.type(ExpectTypeSubClass); } + + [Test] + public function testExpectMatchSuccess():void + { + expect("abc123").to.match(/^[a-z]{3}\d{3}$/); + } + + [Test(expected="org.apache.royale.test.AssertionError")] + public function testExpectMatchFailure():void + { + expect("123abc").to.match(/^[a-z]{3}\d{3}$/); + } + + [Test(expected="org.apache.royale.test.AssertionError")] + public function testExpectNotMatchSuccess():void + { + expect("abc123").not.to.match(/^[a-z]{3}\d{3}$/); + } + + [Test] + public function testExpectNotMatchFailure():void + { + expect("123abc").not.to.match(/^[a-z]{3}\d{3}$/); + } } }
