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 c6a7047dfe1c9449ac0faeadb42b6ac2198a98aa
Author: Josh Tynjala <[email protected]>
AuthorDate: Wed Sep 25 11:55:51 2019 -0700

    RoyaleUnit: supports expected attribute in [Test] metadata to detect thrown 
exceptions
---
 .../apache/royale/test/runners/MetadataRunner.as   |  31 ++++-
 .../src/test/royale/tests/ExpectedTests.as         | 128 +++++++++++++++++++++
 .../src/test/royale/tests/RoyaleUnitSuite.as       |   1 +
 3 files changed, 157 insertions(+), 3 deletions(-)

diff --git 
a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/runners/MetadataRunner.as
 
b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/runners/MetadataRunner.as
index bbc3a86..7b39957 100644
--- 
a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/runners/MetadataRunner.as
+++ 
b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/runners/MetadataRunner.as
@@ -251,7 +251,24 @@ package org.apache.royale.test.runners
                                        
AsyncLocator.setAsyncHandlerForTest(_target, asyncHandler);
                                        asyncHandler.bodyExecuting = true;
                                }
-                               test.reference.apply(_target);
+                               if(test.expected != null)
+                               {
+                                       var expectedType:Class = 
getDefinitionByName(test.expected) as Class;
+                                       var throwsType:Class = null;
+                                       try
+                                       {
+                                               test.reference.apply(_target);
+                                       }
+                                       catch(exception:Object)
+                                       {
+                                               throwsType = 
exception.constructor;
+                                       }
+                                       Assert.assertStrictlyEquals(throwsType, 
expectedType);
+                               }
+                               else
+                               {
+                                       test.reference.apply(_target);
+                               }
                                if(asyncHandler)
                                {
                                        asyncHandler.bodyExecuting = false;
@@ -407,6 +424,7 @@ package org.apache.royale.test.runners
                                var ignore:Boolean = false;
                                var async:Boolean = false;
                                var asyncTimeout:int = 0;
+                               var expected:String = null;
 
                                var testMetadata:Array = 
method.retrieveMetaDataByName(TestMetadata.TEST);
                                if(testMetadata.length > 0)
@@ -435,6 +453,11 @@ package org.apache.royale.test.runners
                                                        asyncTimeout = 
DEFAULT_ASYNC_TIMEOUT;
                                                }
                                        }
+                                       var expectedArgs:Array = 
testTag.getArgsByKey("expected");
+                                       if(expectedArgs.length > 0)
+                                       {
+                                               expected = 
expectedArgs[0].value;
+                                       }
                                }
                                var ignoreMetadata:Array = 
method.retrieveMetaDataByName(TestMetadata.IGNORE);
                                if(ignoreMetadata.length > 0)
@@ -443,7 +466,7 @@ package org.apache.royale.test.runners
                                }
                                if(testName !== null)
                                {
-                                       _collectedTests.push(new 
TestInfo(testName, testFunction, ignore, asyncTimeout));
+                                       _collectedTests.push(new 
TestInfo(testName, testFunction, ignore, asyncTimeout, expected));
                                }
                        }
                }
@@ -492,18 +515,20 @@ import org.apache.royale.utils.Timer;
 
 class TestInfo
 {
-       public function TestInfo(name:String, reference:Function, 
ignore:Boolean, asyncTimeout:int)
+       public function TestInfo(name:String, reference:Function, 
ignore:Boolean, asyncTimeout:int, expected:String)
        {
                this.description = name;
                this.reference = reference;
                this.ignore = ignore;
                this.asyncTimeout = asyncTimeout;
+               this.expected = expected;
        }
 
        public var description:String;
        public var reference:Function;
        public var ignore:Boolean;
        public var asyncTimeout:int;
+       public var expected:String;
 }
 
 class AsyncHandler implements IAsyncHandler
diff --git 
a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectedTests.as 
b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectedTests.as
new file mode 100644
index 0000000..a1a5b84
--- /dev/null
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectedTests.as
@@ -0,0 +1,128 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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 tests
+{
+       import org.apache.royale.test.Assert;
+       import org.apache.royale.test.runners.MetadataRunner;
+       import org.apache.royale.test.runners.notification.RunNotifier;
+
+       public class ExpectedTests
+       {
+               private var _runner:MetadataRunner;
+
+               [Test]
+               public function testExpectedWithCorrectException():void
+               {
+                       _runner = new MetadataRunner(CorrectExceptionFixture);
+                       var notifier:RunNotifier = new RunNotifier();
+                       var listener:RunListener = new RunListener();
+                       notifier.addListener(listener);
+                       _runner.run(notifier);
+                       Assert.assertTrue(listener.result.successful);
+                       
Assert.assertStrictlyEquals(listener.result.failures.length, 0);
+               }
+
+               [Test]
+               public function testExpectedWithIncorrectException():void
+               {
+                       _runner = new MetadataRunner(IncorrectExceptionFixture);
+                       var notifier:RunNotifier = new RunNotifier();
+                       var listener:RunListener = new RunListener();
+                       notifier.addListener(listener);
+                       _runner.run(notifier);
+                       Assert.assertFalse(listener.result.successful);
+                       
Assert.assertStrictlyEquals(listener.result.failures.length, 1);
+               }
+
+               [Test]
+               public function testExpectedWithNoException():void
+               {
+                       _runner = new MetadataRunner(NoExceptionFixture);
+                       var notifier:RunNotifier = new RunNotifier();
+                       var listener:RunListener = new RunListener();
+                       notifier.addListener(listener);
+                       _runner.run(notifier);
+                       Assert.assertFalse(listener.result.successful);
+                       
Assert.assertStrictlyEquals(listener.result.failures.length, 1);
+               }
+       }
+}
+
+var test1Ran:Boolean = false;
+var test2Ran:Boolean = false;
+
+class CorrectExceptionFixture
+{
+       [Test(expected="RangeError")]
+       public function test1():void
+       {
+               throw new RangeError();
+       }
+}
+
+class IncorrectExceptionFixture
+{
+       [Test(expected="RangeError")]
+       public function test1():void
+       {
+               throw new ReferenceError();
+       }
+}
+
+class NoExceptionFixture
+{
+       [Test(expected="RangeError")]
+       public function test1():void
+       {
+       }
+}
+
+import org.apache.royale.test.runners.notification.IRunListener;
+import org.apache.royale.test.runners.notification.Failure;
+import org.apache.royale.test.runners.notification.Result;
+
+class RunListener implements IRunListener
+{
+       public var result:Result = null;
+
+       public function testStarted(description:String):void
+       {
+       }
+
+       public function testFinished(description:String):void
+       {
+       }
+
+       public function testFailure(failure:Failure):void
+       {
+       }
+
+       public function testIgnored(description:String):void
+       {
+       }
+
+       public function testRunStarted(description:String):void
+       {
+       }
+
+       public function testRunFinished(result:Result):void
+       {
+               this.result = result;
+       }
+}
\ No newline at end of file
diff --git 
a/frameworks/projects/RoyaleUnit/src/test/royale/tests/RoyaleUnitSuite.as 
b/frameworks/projects/RoyaleUnit/src/test/royale/tests/RoyaleUnitSuite.as
index 2168c3c..636f5a7 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/RoyaleUnitSuite.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/RoyaleUnitSuite.as
@@ -28,5 +28,6 @@ package tests
                public var scopeTests:ScopeTests;
                public var runNotifierTests:RunNotifierTests;
                public var asyncTests:AsyncTests;
+               public var expectedTests:ExpectedTests;
        }
 }
\ No newline at end of file

Reply via email to