You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2022/12/19 21:07:13 UTC
[royale-asjs] 05/05: RoyaleUnit: assertType and assertNotType
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 0df828ab44752f04a5bad0a36492f5107f058189
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Mon Dec 19 13:04:51 2022 -0800
RoyaleUnit: assertType and assertNotType
---
.../main/royale/org/apache/royale/test/Assert.as | 40 ++++++++++++++++++
.../apache/royale/test/asserts/assertNotType.as | 30 ++++++++++++++
.../org/apache/royale/test/asserts/assertType.as | 30 ++++++++++++++
.../royale/org/apache/royale/test/bdd/IExpect.as | 8 ++++
.../royale/org/apache/royale/test/bdd/expect.as | 13 ++++++
.../src/test/royale/tests/AssertTests.as | 47 ++++++++++++++++++++-
.../src/test/royale/tests/ExpectBDDTests.as | 48 +++++++++++++++++++++-
7 files changed, 214 insertions(+), 2 deletions(-)
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 d5f9071df2..fb2eb6f67b 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
@@ -203,6 +203,24 @@ package org.apache.royale.test
failLessThan(actual, other, message);
}
+ /**
+ * Asserts that the provided value is of a type. Equivalent to testing
+ * the value in an <code>if(value is type)</code> statement.
+ */
+ public static function assertType(actual:*, type:Class, message:String = null):void
+ {
+ failNotType(actual, type, message);
+ }
+
+ /**
+ * Asserts that the provided value is not of a type. Equivalent to
+ * testing the value in an <code>if(!(value is type))</code> statement.
+ */
+ public static function assertNotType(actual:*, type:Class, message:String = null):void
+ {
+ failType(actual, type, message);
+ }
+
/**
* Fails.
*/
@@ -413,6 +431,28 @@ package org.apache.royale.test
}
}
+ /**
+ * Fails if the provided value is of a type.
+ */
+ public static function failType(actual:*, type:Class, message:String = null):void
+ {
+ if (actual is type)
+ {
+ failWithUserMessage("expected <" + actual + "> to not be of type <" + type + ">", message);
+ }
+ }
+
+ /**
+ * Fails if the provided value is not of a type.
+ */
+ public static function failNotType(actual:*, type:Class, message:String = null):void
+ {
+ if (!(actual is type))
+ {
+ failWithUserMessage("expected <" + actual + "> to be of type <" + type + ">", message);
+ }
+ }
+
/**
* @private
*/
diff --git a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotType.as b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotType.as
new file mode 100644
index 0000000000..d34851bf53
--- /dev/null
+++ b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertNotType.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 assertNotType method
+ */
+ public function assertNotType(value:*, type:Class, message:String = null):void
+ {
+ Assert.assertNotType(value, type, message);
+ }
+}
\ No newline at end of file
diff --git a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertType.as b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertType.as
new file mode 100644
index 0000000000..f2b73075cb
--- /dev/null
+++ b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertType.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 assertType method
+ */
+ public function assertType(value:*, type:Class, message:String = null):void
+ {
+ Assert.assertType(value, type, 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 91afba0ad1..bf09eeb8d7 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
@@ -136,6 +136,14 @@ package org.apache.royale.test.bdd
*/
function gte(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.
+ *
+ * @see Assert#assertType
+ */
+ function type(type:Class, 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 6adb0d54a7..3b13369479 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
@@ -166,6 +166,19 @@ class Expect implements IExpect
return greaterThanOrEqual(other, message);
}
+ public function type(type:Class, message:String = null):IExpect
+ {
+ if (_not)
+ {
+ Assert.assertNotType(_value, type, message);
+ }
+ else
+ {
+ Assert.assertType(_value, type, 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 28cb3b2ceb..cb657febad 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as
@@ -913,6 +913,48 @@ package tests
Assert.assertGreaterThanOrEqual(1, 0);
}
+ [Test]
+ public function testAssertTypeMatch():void
+ {
+ var obj:AssertTypeClass = new AssertTypeClass();
+ Assert.assertType(obj, AssertTypeClass);
+ }
+
+ [Test]
+ public function testAssertTypeMatchSubClass():void
+ {
+ var obj:AssertTypeSubClass = new AssertTypeSubClass();
+ Assert.assertType(obj, AssertTypeClass);
+ }
+
+ [Test(expected="org.apache.royale.test.AssertionError")]
+ public function testAssertTypeMismatch():void
+ {
+ var obj:AssertTypeClass = new AssertTypeClass();
+ Assert.assertType(obj, AssertTypeSubClass);
+ }
+
+ [Test(expected="org.apache.royale.test.AssertionError")]
+ public function testAssertNotTypeMatch():void
+ {
+ var obj:AssertTypeClass = new AssertTypeClass();
+ Assert.assertNotType(obj, AssertTypeClass);
+ }
+
+ [Test(expected="org.apache.royale.test.AssertionError")]
+ public function testAssertNotTypeMatchSubClass():void
+ {
+ var obj:AssertTypeSubClass = new AssertTypeSubClass();
+ Assert.assertNotType(obj, AssertTypeClass);
+ }
+
+ [Test]
+ public function testAssertNotTypeMismatch():void
+ {
+ var obj:AssertTypeClass = new AssertTypeClass();
+ Assert.assertNotType(obj, AssertTypeSubClass);
+ }
+
[Test(expected="org.apache.royale.test.AssertionError")]
public function testFail():void
{
@@ -1224,4 +1266,7 @@ package tests
Assert.failNaN(value);
}
}
-}
\ No newline at end of file
+}
+
+class AssertTypeClass {}
+class AssertTypeSubClass extends AssertTypeClass {}
\ No newline at end of file
diff --git a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as
index eac3e66c88..0bf42df96a 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as
@@ -1692,5 +1692,51 @@ package tests
{
expect(1).greaterThanOrEqual(0);
}
+
+ [Test]
+ public function testExpectTypeMatch():void
+ {
+ var obj:ExpectTypeClass = new ExpectTypeClass();
+ expect(obj).is.type(ExpectTypeClass);
+ }
+
+ [Test]
+ public function testExpectTypeMatchSubClass():void
+ {
+ var obj:ExpectTypeSubClass = new ExpectTypeSubClass();
+ expect(obj).is.type(ExpectTypeClass);
+ }
+
+ [Test(expected="org.apache.royale.test.AssertionError")]
+ public function testExpectTypeMismatch():void
+ {
+ var obj:ExpectTypeClass = new ExpectTypeClass();
+ expect(obj).is.type(ExpectTypeSubClass);
+ }
+
+ [Test(expected="org.apache.royale.test.AssertionError")]
+ public function testAssertNotTypeMatch():void
+ {
+ var obj:ExpectTypeClass = new ExpectTypeClass();
+ expect(obj).is.not.type(ExpectTypeClass);
+ }
+
+ [Test(expected="org.apache.royale.test.AssertionError")]
+ public function testAssertNotTypeMatchSubClass():void
+ {
+ var obj:ExpectTypeSubClass = new ExpectTypeSubClass();
+ expect(obj).is.not.type(ExpectTypeClass);
+ }
+
+ [Test]
+ public function testExpectNotTypeMismatch():void
+ {
+ var obj:ExpectTypeClass = new ExpectTypeClass();
+ expect(obj).is.not.type(ExpectTypeSubClass);
+ }
}
-}
\ No newline at end of file
+}
+
+
+class ExpectTypeClass {}
+class ExpectTypeSubClass extends ExpectTypeClass {}
\ No newline at end of file