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 3938fcc4020a3897c4de3ed41b59c34388d9d6ad Author: Josh Tynjala <[email protected]> AuthorDate: Mon Dec 19 14:16:34 2022 -0800 RoyaleUnit: assertThrows --- .../main/royale/org/apache/royale/test/Assert.as | 42 ++++++++++++++++++++++ .../org/apache/royale/test/asserts/assertThrows.as | 30 ++++++++++++++++ .../royale/org/apache/royale/test/bdd/IExpect.as | 14 ++++++++ .../royale/org/apache/royale/test/bdd/expect.as | 18 ++++++++++ .../src/test/royale/tests/AssertTests.as | 35 ++++++++++++++++++ .../src/test/royale/tests/ExpectBDDTests.as | 35 ++++++++++++++++++ 6 files changed, 174 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 44b2c563e5..0a59c54c14 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 @@ -241,6 +241,28 @@ package org.apache.royale.test failMatch(actual, regExp, message); } + /** + * Asserts that the provided function throws. + * + * <p>Equivalent to <code>[Test(expected="RangeError")]</code></p> + */ + public static function assertThrows(fn:Function, type:Class = null, message:String = null):void + { + try + { + fn(); + } + catch(e:*) + { + if (type != null) + { + assertType(e, type, message); + } + return; + } + fail("expected function to throw"); + } + /** * Fails. */ @@ -495,6 +517,26 @@ package org.apache.royale.test } } + /** + * Fails if the provided function throws. + */ + public static function failThrows(fn:Function, type:Class = null, message:String = null):void + { + try + { + fn(); + } + catch(e:*) + { + if (type != null && e is type) + { + fail("expected function not to throw type <" + type + ">"); + return; + } + fail("expected function not to throw"); + } + } + /** * @private */ diff --git a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertThrows.as b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertThrows.as new file mode 100644 index 0000000000..0d94e0893e --- /dev/null +++ b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/asserts/assertThrows.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 assertThrows method + */ + public function assertThrows(actual:Function, type:Class = null, message:String = null):void + { + Assert.assertThrows(actual, 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 3df66d54eb..3ccc327057 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 @@ -181,6 +181,20 @@ package org.apache.royale.test.bdd */ function match(regExp:RegExp, message:String = null):IExpect; + /** + * Asserts that the provided function throws. + * + * @see Assert#assertThrows + */ + function throw(type:Class = null, message:String = null):IExpect; + + /** + * Alias for <code>throw()</code>. + * + * @see #throw + */ + function throws(type:Class = null, 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 7ce214f377..9312ab0bc4 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 @@ -212,6 +212,24 @@ class Expect implements IExpect return this; } + public function throw(type:Class = null, message:String = null):IExpect + { + if (_not) + { + Assert.failThrows(_value, type, message); + } + else + { + Assert.assertThrows(_value, type, message); + } + return this; + } + + public function throws(type:Class = null, message:String = null):IExpect + { + return this.throw(type, message); + } + 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 386a4e76b2..72286d4aaa 100644 --- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as +++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/AssertTests.as @@ -979,6 +979,41 @@ package tests Assert.assertNotMatch("123abc", /^[a-z]{3}\d{3}$/); } + [Test] + public function testAssertThrowsNoType():void + { + Assert.assertThrows(function():void + { + throw new Error("I'm an error") + }); + } + + [Test] + public function testAssertThrowsMatchingType():void + { + Assert.assertThrows(function():void + { + throw new RangeError("I'm a RangeError") + }, RangeError); + } + + [Test(expected="org.apache.royale.test.AssertionError")] + public function testAssertThrowsWrongType():void + { + Assert.assertThrows(function():void + { + throw new Error("I'm an error") + }, RangeError); + } + + [Test(expected="org.apache.royale.test.AssertionError")] + public function testAssertThrowsWithoutThrow():void + { + Assert.assertThrows(function():void + { + }); + } + [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 80ff0af0b8..b8b00ed5d1 100644 --- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as +++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectBDDTests.as @@ -1758,6 +1758,41 @@ package tests { expect("123abc").not.to.match(/^[a-z]{3}\d{3}$/); } + + [Test] + public function testExpectThrowsNoType():void + { + expect(function():void + { + throw new Error("I'm an error") + }).throws(); + } + + [Test] + public function testAssertThrowsMatchingType():void + { + expect(function():void + { + throw new RangeError("I'm a RangeError") + }).throws(RangeError); + } + + [Test(expected="org.apache.royale.test.AssertionError")] + public function testAssertThrowsWrongType():void + { + expect(function():void + { + throw new Error("I'm an error") + }).throws(RangeError); + } + + [Test(expected="org.apache.royale.test.AssertionError")] + public function testAssertThrowsWithoutThrow():void + { + expect(function():void + { + }).throws(); + } } }
