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 87fc0b8a3e407454eb936e41cbc9e70910d6ed6a
Author: Josh Tynjala <[email protected]>
AuthorDate: Mon Dec 19 12:19:00 2022 -0800

    RoyaleUnit: assertWithin and assertNotWithin
---
 .../main/royale/org/apache/royale/test/Assert.as   | 46 ++++++++++++++++++++++
 .../apache/royale/test/asserts/assertNotWithin.as  | 30 ++++++++++++++
 .../org/apache/royale/test/asserts/assertWithin.as | 30 ++++++++++++++
 .../royale/org/apache/royale/test/bdd/IExpect.as   | 10 +++++
 .../royale/org/apache/royale/test/bdd/expect.as    | 13 ++++++
 .../src/test/royale/tests/AssertTests.as           | 30 ++++++++++++++
 .../src/test/royale/tests/ExpectBDDTests.as        | 32 ++++++++++++++-
 7 files changed, 190 insertions(+), 1 deletion(-)

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 1e71760497..d10b92fc15 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
@@ -141,6 +141,28 @@ package org.apache.royale.test
                        }
                }
 
+               /**
+                * Asserts that the provided value is between a minimum and 
maximum
+                * value (inclusive). Equivalent to testing the
+                * value in an <code>if(value >= minimum && value <= 
maximum)</code>
+                * statement.
+                */
+               public static function assertWithin(actual:Number, 
minimum:Number, maximum:Number, message:String = null):void
+               {
+                       failNotWithin(actual, minimum, maximum, message);
+               }
+
+               /**
+                * Asserts that the provided value is not between a minimum and 
maximum
+                * value (inclusive). Equivalent to testing the
+                * value in an <code>if(value < minimum || value > 
maximum)</code>
+                * statement.
+                */
+               public static function assertNotWithin(actual:Number, 
minimum:Number, maximum:Number, message:String = null):void
+               {
+                       failWithin(actual, minimum, maximum, message);
+               }
+
                /**
                 * Fails.
                 */
@@ -282,6 +304,30 @@ package org.apache.royale.test
                        }
                }
 
+               /**
+                * Fails if the provided value is between a minimum and maximum 
value
+                * (inclusive).
+                */
+               public static function failWithin(actual:Number, 
minimum:Number, maximum:Number, message:String = null):void
+               {
+                       if (actual >= minimum && actual <= maximum)
+                       {
+                               failWithUserMessage("expected not between <" + 
minimum + "> and <" + maximum + "> but was <" + actual + ">", message);
+                       }
+               }
+
+               /**
+                * Fails if the provided value is not between a minimum and 
maximum
+                * value (inclusive).
+                */
+               public static function failNotWithin(actual:Number, 
minimum:Number, maximum:Number, message:String = null):void
+               {
+                       if (actual < minimum || actual > maximum)
+                       {
+                               failWithUserMessage("expected between <" + 
minimum + "> and <" + maximum + "> but was <" + actual + ">", message);
+                       }
+               }
+
                /**
                 * @private
                 */
diff --git 
a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotWithin.as
 
b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotWithin.as
new file mode 100644
index 0000000000..bd2311ab55
--- /dev/null
+++ 
b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotWithin.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 assertNotWithin method
+        */
+       public function assertNotWithin(actual:Number, minimum:Number, 
maximum:Number, message:String = null):void
+       {
+               Assert.assertNotWithin(actual, minimum, maximum, message);
+       }
+}
\ No newline at end of file
diff --git 
a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertWithin.as
 
b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertWithin.as
new file mode 100644
index 0000000000..9b7a10f907
--- /dev/null
+++ 
b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertWithin.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 assertWithin method
+        */
+       public function assertWithin(actual:Number, minimum:Number, 
maximum:Number, message:String = null):void
+       {
+               Assert.assertWithin(actual, minimum, maximum, 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 76b338d507..9d49aec9bf 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
@@ -55,6 +55,16 @@ package org.apache.royale.test.bdd
                 */
                function strictlyEquals(value:*, message:String = null):IExpect;
 
+               /**
+                * Asserts that the provided value is between a minimum and 
maximum
+                * value (inclusive). Equivalent to testing the
+                * value in an <code>if(value >= minimum && value <= 
maximum)</code>
+                * statement.
+                * 
+                * @see Assert#assertWithin
+                */
+               function within(minimum:Number, maximum:Number, 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 fd7ff805df..b21258f4ff 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
@@ -76,6 +76,19 @@ class Expect implements IExpect
                return strictlyEqual(value, message);
        }
 
+       public function within(minimum:Number, maximum:Number, message:String = 
null):IExpect
+       {
+               if (_not)
+               {
+                       Assert.assertNotWithin(_value, minimum, maximum, 
message);
+               }
+               else
+               {
+                       Assert.assertWithin(_value, minimum, maximum, 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 914d3cccf9..7c89441578 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as
@@ -811,6 +811,36 @@ package tests
                        Assert.assertNaN(value);
                }
 
+               [Test]
+               public function testAssertWithinBetween():void
+               {
+                       Assert.assertWithin(0, -1, 1);
+               }
+
+               [Test]
+               public function testAssertWithinMinimum():void
+               {
+                       Assert.assertWithin(-1, -1, 1);
+               }
+
+               [Test]
+               public function testAssertWithinMaximum():void
+               {
+                       Assert.assertWithin(-1, -1, 1);
+               }
+
+               [Test(expected="org.apache.royale.test.AssertionError")]
+               public function testAssertWithinOutsideMinimum():void
+               {
+                       Assert.assertWithin(-1.0001, -1, 1);
+               }
+
+               [Test(expected="org.apache.royale.test.AssertionError")]
+               public function testAssertWithinOutsideMaximum():void
+               {
+                       Assert.assertWithin(1.0001, -1, 1);
+               }
+
                [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 c4c9bf99e7..461741bd11 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as
@@ -1175,7 +1175,7 @@ package tests
                }
 
                [Test]
-               
+
                public function testExpectStringWithNaNGetter():void
                {
                        expect("royale").is.NaN;
@@ -1590,5 +1590,35 @@ package tests
                {
                        expect({}).is.not.null;
                }
+
+               [Test]
+               public function testExpectWithinBetween():void
+               {
+                       expect(0).is.within(-1, 1);
+               }
+
+               [Test]
+               public function testExpectWithinMinimum():void
+               {
+                       expect(-1).is.within(-1, 1);
+               }
+
+               [Test]
+               public function testExpectWithinMaximum():void
+               {
+                       expect(1).is.within(-1, 1);
+               }
+
+               [Test(expected="org.apache.royale.test.AssertionError")]
+               public function testExpectWithinOutsideMinimum():void
+               {
+                       expect(-1.0001).is.within(-1, 1);
+               }
+
+               [Test(expected="org.apache.royale.test.AssertionError")]
+               public function testExpectWithinOutsideMaximum():void
+               {
+                       expect(-1.0001).is.within(-1, 1);
+               }
        }
 }
\ No newline at end of file

Reply via email to