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:32 UTC

[royale-asjs] 01/02: RoyaleUnit: stricter detection of [Before], [After], [BeforeClass], [AfterClass], and [Test] that checks if static is correct

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.");
 		}
 	}