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 16:46:07 UTC

[royale-asjs] branch develop updated: RoyaleUnit: [BeforeClass] and [AfterClass] metadata must be added to static method

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


The following commit(s) were added to refs/heads/develop by this push:
     new 9233a4e  RoyaleUnit: [BeforeClass] and [AfterClass] metadata must be added to static method
9233a4e is described below

commit 9233a4ed3146c75e9cd42750e55cadf4c55324db
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Wed Sep 25 09:45:53 2019 -0700

    RoyaleUnit: [BeforeClass] and [AfterClass] metadata must be added to static method
---
 .../apache/royale/test/runners/MetadataRunner.as   | 38 ++++++++++++++--------
 .../royale/tests/BeforeClassAndAfterClassTests.as  |  8 ++---
 .../RoyaleUnit/src/test/royale/tests/ScopeTests.as | 17 +++++-----
 3 files changed, 37 insertions(+), 26 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 8bb3d19..9c776c6 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
@@ -173,8 +173,13 @@ package org.apache.royale.test.runners
 			_notifier.fireTestRunStarted(description);
 			if(_testClass)
 			{
+				readStaticMetadataTags();
+				if(_beforeClass !== null)
+				{
+					_beforeClass.apply(_testClass);
+				}
 				_target = new _testClass();
-				readMetadataTags();
+				readInstanceMetadataTags();
 				continueAll();
 			}
 			else
@@ -199,7 +204,7 @@ package org.apache.royale.test.runners
 			}
 			if(_afterClass !== null)
 			{
-				_afterClass.apply(_target);
+				_afterClass.apply(_testClass);
 			}
 			_notifier.removeListener(_listener);
 			_notifier.fireTestRunFinished(_result);
@@ -223,10 +228,6 @@ package org.apache.royale.test.runners
 		 */
 		protected function continueNext():Boolean
 		{
-			if(_currentIndex == 0 && _beforeClass !== null)
-			{
-				_beforeClass.apply(_target);
-			}
 			var test:TestInfo = _collectedTests[_currentIndex];
 			try
 			{
@@ -288,15 +289,23 @@ package org.apache.royale.test.runners
 		/**
 		 * @private
 		 */
-		protected function readMetadataTags():void
+		protected function readStaticMetadataTags():void
+		{
+			_beforeClass = collectMethodWithMetadataTag(TestMetadata.BEFORE_CLASS, true);
+			_afterClass = collectMethodWithMetadataTag(TestMetadata.AFTER_CLASS, true);
+		}
+
+		/**
+		 * @private
+		 */
+		protected function readInstanceMetadataTags():void
 		{
 			collectTests();
 			if(_collectedTests.length === 0)
 			{
-				throw new Error("No methods found with [Test] metadata. Did you forget to include the -keep-as3-metadata compiler option?")
+				//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?")
 			}
-			_beforeClass = collectMethodWithMetadataTag(TestMetadata.BEFORE_CLASS);
-			_afterClass = collectMethodWithMetadataTag(TestMetadata.AFTER_CLASS);
 			_before = collectMethodWithMetadataTag(TestMetadata.BEFORE);
 			_after = collectMethodWithMetadataTag(TestMetadata.AFTER);
 		}
@@ -304,14 +313,15 @@ package org.apache.royale.test.runners
 		/**
 		 * @private
 		 */
-		protected function collectMethodWithMetadataTag(tagName:String):Function
+		protected function collectMethodWithMetadataTag(tagName:String, isStatic:Boolean = false):Function
 		{
-			var typeDefinition:TypeDefinition = describeType(_target);
+			var described:Object = isStatic ? _testClass : _target;
+			var typeDefinition:TypeDefinition = describeType(described);
 			if(!typeDefinition)
 			{
 				return null;
 			}
-			var methods:Array = typeDefinition.methods;
+			var methods:Array = isStatic ? typeDefinition.staticMethods : typeDefinition.methods;
 			var length:int = methods.length;
 			for(var i:int = 0; i < length; i++)
 			{
@@ -319,7 +329,7 @@ package org.apache.royale.test.runners
 				var metadata:Array = method.retrieveMetaDataByName(tagName);
 				if(metadata.length > 0)
 				{
-					return _target[method.name];
+					return described[method.name];
 				}
 			}
 			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 e963f18..6a8e69e 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/BeforeClassAndAfterClassTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/BeforeClassAndAfterClassTests.as
@@ -111,7 +111,7 @@ var test2Ran:Boolean = false;
 class BeforeClassFixture
 {
 	[BeforeClass]
-	public function beforeClass():void
+	public static function beforeClass():void
 	{
 		Assert.assertFalse(beforeClassRan);
 		beforeClassRan = true;
@@ -142,7 +142,7 @@ class BeforeClassFixture
 class AfterClassFixture
 {
 	[AfterClass]
-	public function afterClass():void
+	public static function afterClass():void
 	{
 		Assert.assertFalse(afterClassRan);
 		afterClassRan = true;
@@ -173,7 +173,7 @@ class AfterClassFixture
 class BeforeClassAndAfterClassFixture
 {
 	[BeforeClass]
-	public function beforeClass():void
+	public static function beforeClass():void
 	{
 		Assert.assertFalse(beforeClassRan);
 		beforeClassRan = true;
@@ -183,7 +183,7 @@ class BeforeClassAndAfterClassFixture
 	}
 
 	[AfterClass]
-	public function afterClass():void
+	public static function afterClass():void
 	{
 		Assert.assertFalse(afterClassRan);
 		afterClassRan = true;
diff --git a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ScopeTests.as b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ScopeTests.as
index 9a21c3a..2f45131 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/ScopeTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/ScopeTests.as
@@ -22,40 +22,41 @@ package tests
 
 	public class ScopeTests
 	{
-		private var _value:String = "ScopeTests hello";
+		private static var _staticValue:String = "ScopeTests static";
+		private var _value:String = "ScopeTests non-static";
 
 		[Before]
 		public function before():void
 		{
-			Assert.assertStrictlyEquals(this._value, "ScopeTests hello",
+			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 hello",
+			Assert.assertStrictlyEquals(this._value, "ScopeTests non-static",
 				"Function marked with [After] metadata called with incorrect scope.");
 		}
 
 		[BeforeClass]
-		public function beforeClass():void
+		public static function beforeClass():void
 		{
-			Assert.assertStrictlyEquals(this._value, "ScopeTests hello",
+			Assert.assertStrictlyEquals(ScopeTests._staticValue, "ScopeTests static",
 				"Function marked with [BeforeClass] metadata called with incorrect scope.");
 		}
 
 		[AfterClass]
-		public function afterClass():void
+		public static function afterClass():void
 		{
-			Assert.assertStrictlyEquals(this._value, "ScopeTests hello",
+			Assert.assertStrictlyEquals(ScopeTests._staticValue, "ScopeTests static",
 				"Function marked with [AfterClass] metadata called with incorrect scope.");
 		}
 
 		[Test]
 		public function testScope():void
 		{
-			Assert.assertStrictlyEquals(this._value, "ScopeTests hello",
+			Assert.assertStrictlyEquals(this._value, "ScopeTests non-static",
 				"Function marked with [Test] metadata called with incorrect scope.");
 		}
 	}