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 2019/09/25 18:58:31 UTC
[royale-asjs] branch develop updated (9233a4e -> c6a7047)
This is an automated email from the ASF dual-hosted git repository.
joshtynjala pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git.
from 9233a4e RoyaleUnit: [BeforeClass] and [AfterClass] metadata must be added to static method
new 0fe44a5 RoyaleUnit: stricter detection of [Before], [After], [BeforeClass], [AfterClass], and [Test] that checks if static is correct
new c6a7047 RoyaleUnit: supports expected attribute in [Test] metadata to detect thrown exceptions
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../apache/royale/test/runners/MetadataRunner.as | 93 +++++++++++++--
.../royale/tests/BeforeClassAndAfterClassTests.as | 99 ++++++++++++++++
.../src/test/royale/tests/ExpectedTests.as | 128 +++++++++++++++++++++
.../src/test/royale/tests/RoyaleUnitSuite.as | 1 +
.../RoyaleUnit/src/test/royale/tests/ScopeTests.as | 33 +++---
5 files changed, 328 insertions(+), 26 deletions(-)
create mode 100644 frameworks/projects/RoyaleUnit/src/test/royale/tests/ExpectedTests.as
[royale-asjs] 02/02: RoyaleUnit: supports expected attribute in
[Test] metadata to detect thrown exceptions
Posted by jo...@apache.org.
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 <jo...@apache.org>
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
[royale-asjs] 01/02: RoyaleUnit: stricter detection of [Before],
[After], [BeforeClass], [AfterClass],
and [Test] that checks if static is correct
Posted by jo...@apache.org.
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 0fe44a58e8b6a6ef705781f95b29829257590922
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Wed Sep 25 11:53:33 2019 -0700
RoyaleUnit: stricter detection of [Before], [After], [BeforeClass], [AfterClass], and [Test] that checks if static is correct
---
.../apache/royale/test/runners/MetadataRunner.as | 62 ++++++++++++--
.../royale/tests/BeforeClassAndAfterClassTests.as | 99 ++++++++++++++++++++++
.../RoyaleUnit/src/test/royale/tests/ScopeTests.as | 33 ++++----
3 files changed, 171 insertions(+), 23 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 9c776c6..bbc3a86 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
@@ -23,7 +23,9 @@ package org.apache.royale.test.runners
import org.apache.royale.reflection.MethodDefinition;
import org.apache.royale.reflection.TypeDefinition;
import org.apache.royale.reflection.describeType;
+ import org.apache.royale.reflection.getDefinitionByName;
import org.apache.royale.reflection.getQualifiedClassName;
+ import org.apache.royale.test.Assert;
import org.apache.royale.test.AssertionError;
import org.apache.royale.test.async.AsyncLocator;
import org.apache.royale.test.async.IAsyncHandler;
@@ -291,8 +293,34 @@ package org.apache.royale.test.runners
*/
protected function readStaticMetadataTags():void
{
- _beforeClass = collectMethodWithMetadataTag(TestMetadata.BEFORE_CLASS, true);
- _afterClass = collectMethodWithMetadataTag(TestMetadata.AFTER_CLASS, true);
+ var beforeClassDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.BEFORE_CLASS, true);
+ if(beforeClassDefinition != null)
+ {
+ _beforeClass = _testClass[beforeClassDefinition.name];
+ }
+ var afterClassDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.AFTER_CLASS, true);
+ if(afterClassDefinition != null)
+ {
+ _afterClass = _testClass[afterClassDefinition.name];
+ }
+ var beforeDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.BEFORE, true);
+ if(beforeDefinition != null)
+ {
+ _failures = true;
+ _notifier.fireTestFailure(new Failure(description + ".initializationError", new Error("Unexpected [Before] metadata on static method <" + beforeDefinition.name + "> defined on type <" + beforeDefinition.declaredBy.qualifiedName + ">. [Before] may be used on instance methods only.")));
+ }
+ var afterDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.AFTER, true);
+ if(afterDefinition != null)
+ {
+ _failures = true;
+ _notifier.fireTestFailure(new Failure(description + ".initializationError", new Error("Unexpected [After] metadata on static method <" + afterDefinition.name + "> defined on type <" + afterDefinition.declaredBy.qualifiedName + ">. [After] may be used on instance methods only.")));
+ }
+ var testDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.TEST, true);
+ if(testDefinition != null)
+ {
+ _failures = true;
+ _notifier.fireTestFailure(new Failure(description + ".initializationError", new Error("Unexpected [Test] metadata on static method <" + testDefinition.name + "> defined on type <" + testDefinition.declaredBy.qualifiedName + ">. [Test] may be used on instance methods only.")));
+ }
}
/**
@@ -304,16 +332,36 @@ package org.apache.royale.test.runners
if(_collectedTests.length === 0)
{
//at least one test is required
- throw new Error("No methods with [Test] metadata found on instance of type: <" + getQualifiedClassName(_testClass) + ">. Did you forget to include the -keep-as3-metadata compiler option?")
+ throw new Error("No methods with [Test] metadata found on instance of type <" + getQualifiedClassName(_testClass) + ">. Did you forget to include the -keep-as3-metadata compiler option?")
+ }
+ var beforeDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.BEFORE);
+ if(beforeDefinition != null)
+ {
+ _before = _target[beforeDefinition.name];
+ }
+ var afterDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.AFTER);
+ if(afterDefinition != null)
+ {
+ _after = _target[afterDefinition.name];
+ }
+ var beforeClassDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.BEFORE_CLASS);
+ if(beforeClassDefinition != null)
+ {
+ _failures = true;
+ _notifier.fireTestFailure(new Failure(description + ".initializationError", new Error("Unexpected [BeforeClass] metadata on instance method <" + beforeClassDefinition.name + "> defined on type <" + beforeClassDefinition.declaredBy.qualifiedName + ">. [BeforeClass] may be used on static methods only.")));
+ }
+ var afterClassDefinition:MethodDefinition = collectMethodWithMetadataTag(TestMetadata.AFTER_CLASS);
+ if(afterClassDefinition != null)
+ {
+ _failures = true;
+ _notifier.fireTestFailure(new Failure(description + ".initializationError", new Error("Unexpected [AfterClass] metadata on instance method <" + afterClassDefinition.name + "> defined on type <" + afterClassDefinition.declaredBy.qualifiedName + ">. [AfterClass] may be used on static methods only.")));
}
- _before = collectMethodWithMetadataTag(TestMetadata.BEFORE);
- _after = collectMethodWithMetadataTag(TestMetadata.AFTER);
}
/**
* @private
*/
- protected function collectMethodWithMetadataTag(tagName:String, isStatic:Boolean = false):Function
+ protected function collectMethodWithMetadataTag(tagName:String, isStatic:Boolean = false):MethodDefinition
{
var described:Object = isStatic ? _testClass : _target;
var typeDefinition:TypeDefinition = describeType(described);
@@ -329,7 +377,7 @@ package org.apache.royale.test.runners
var metadata:Array = method.retrieveMetaDataByName(tagName);
if(metadata.length > 0)
{
- return described[method.name];
+ return method;
}
}
return null;
diff --git a/frameworks/projects/RoyaleUnit/src/test/royale/tests/BeforeClassAndAfterClassTests.as b/frameworks/projects/RoyaleUnit/src/test/royale/tests/BeforeClassAndAfterClassTests.as
index 6a8e69e..0952547 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/BeforeClassAndAfterClassTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/BeforeClassAndAfterClassTests.as
@@ -98,10 +98,49 @@ package tests
Assert.assertTrue(test1Ran);
Assert.assertTrue(test2Ran);
}
+
+ [Test]
+ public function testBeforeClassOnInstanceMethod():void
+ {
+ _runner = new MetadataRunner(BeforeClassOnInstanceMethodFixture);
+
+ Assert.assertFalse(beforeClassRan);
+
+ var notifier:RunNotifier = new RunNotifier();
+ var listener:RunListener = new RunListener();
+ notifier.addListener(listener);
+ _runner.run(notifier);
+
+ Assert.assertFalse(beforeClassRan);
+
+ Assert.assertFalse(listener.result.successful);
+ Assert.assertStrictlyEquals(listener.result.failures.length, 1);
+ }
+
+ [Test]
+ public function testAfterClassOnInstanceMethod():void
+ {
+ _runner = new MetadataRunner(AfterClassOnInstanceMethodFixture);
+
+ Assert.assertFalse(afterClassRan);
+
+ var notifier:RunNotifier = new RunNotifier();
+ var listener:RunListener = new RunListener();
+ notifier.addListener(listener);
+ _runner.run(notifier);
+
+ Assert.assertFalse(afterClassRan);
+
+ Assert.assertFalse(listener.result.successful);
+ Assert.assertStrictlyEquals(listener.result.failures.length, 1);
+ }
}
}
import org.apache.royale.test.Assert;
+import org.apache.royale.test.runners.notification.Failure;
+import org.apache.royale.test.runners.notification.Result;
+import org.apache.royale.test.runners.notification.IRunListener;
var beforeClassRan:Boolean = false;
var afterClassRan:Boolean = false;
@@ -209,4 +248,64 @@ class BeforeClassAndAfterClassFixture
Assert.assertTrue(beforeClassRan);
Assert.assertFalse(afterClassRan);
}
+}
+
+class BeforeClassOnInstanceMethodFixture
+{
+ [BeforeClass]
+ public function beforeClassOnInstanceMethod():void
+ {
+ beforeClassRan = true;
+ }
+
+ [Test]
+ public function test1():void
+ {
+ //at least one test is always required
+ }
+}
+
+class AfterClassOnInstanceMethodFixture
+{
+ [AfterClass]
+ public function afterClassOnInstanceMethod():void
+ {
+ afterClassRan = true;
+ }
+
+ [Test]
+ public function test1():void
+ {
+ //at least one test is always required
+ }
+}
+
+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/ScopeTests.as b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ScopeTests.as
index 2f45131..19a39ae 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ScopeTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ScopeTests.as
@@ -23,21 +23,6 @@ package tests
public class ScopeTests
{
private static var _staticValue:String = "ScopeTests static";
- private var _value:String = "ScopeTests non-static";
-
- [Before]
- public function before():void
- {
- Assert.assertStrictlyEquals(this._value, "ScopeTests non-static",
- "Function marked with [Before] metadata called with incorrect scope.");
- }
-
- [After]
- public function after():void
- {
- Assert.assertStrictlyEquals(this._value, "ScopeTests non-static",
- "Function marked with [After] metadata called with incorrect scope.");
- }
[BeforeClass]
public static function beforeClass():void
@@ -52,11 +37,27 @@ package tests
Assert.assertStrictlyEquals(ScopeTests._staticValue, "ScopeTests static",
"Function marked with [AfterClass] metadata called with incorrect scope.");
}
+
+ private var _value:String = "ScopeTests instance";
+
+ [Before]
+ public function before():void
+ {
+ Assert.assertStrictlyEquals(this._value, "ScopeTests instance",
+ "Function marked with [Before] metadata called with incorrect scope.");
+ }
+
+ [After]
+ public function after():void
+ {
+ Assert.assertStrictlyEquals(this._value, "ScopeTests instance",
+ "Function marked with [After] metadata called with incorrect scope.");
+ }
[Test]
public function testScope():void
{
- Assert.assertStrictlyEquals(this._value, "ScopeTests non-static",
+ Assert.assertStrictlyEquals(this._value, "ScopeTests instance",
"Function marked with [Test] metadata called with incorrect scope.");
}
}