|
| 1 | +/* |
| 2 | + * Copyright 2001-2018 Artima, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.scalatestplus.testng |
| 17 | + |
| 18 | +import java.lang.reflect.{Method, Modifier} |
| 19 | +import org.scalatest.{Resources => _, _} |
| 20 | +import org.scalatest.events.{Formatter, IndentedText} |
| 21 | +import org.scalatest.exceptions.NotAllowedException |
| 22 | + |
| 23 | +import scala.collection.immutable.TreeSet |
| 24 | +import scala.reflect.NameTransformer.decode |
| 25 | + |
| 26 | +private[testng] object TestNGHelper { |
| 27 | + |
| 28 | + def formatterForSuiteAborted(suite: Suite, message: String): Option[Formatter] = { |
| 29 | + val suiteClass = suite.getClass |
| 30 | + val actualSuiteName = |
| 31 | + if (suiteClass.getName == "org.scalatest.DeferredAbortedSuite") |
| 32 | + suiteClass.getDeclaredField("suiteClassName").get(suite).asInstanceOf[String] |
| 33 | + else |
| 34 | + suiteClass.getName |
| 35 | + Some(IndentedText(actualSuiteName, message, 0)) |
| 36 | + } |
| 37 | + |
| 38 | + def getIndentedTextForTest(testText: String, level: Int, includeIcon: Boolean) = { |
| 39 | + val decodedTestText = scala.reflect.NameTransformer.decode(testText) |
| 40 | + val formattedText = |
| 41 | + if (includeIcon) { |
| 42 | + val testSucceededIcon = Resources.testSucceededIconChar |
| 43 | + (" " * (if (level == 0) 0 else (level - 1))) + Resources.iconPlusShortName(testSucceededIcon, decodedTestText) |
| 44 | + } |
| 45 | + else { |
| 46 | + (" " * level) + decodedTestText |
| 47 | + } |
| 48 | + IndentedText(formattedText, decodedTestText, level) |
| 49 | + } |
| 50 | + |
| 51 | + def isTestMethodGoodies(m: Method) = { |
| 52 | + |
| 53 | + val isInstanceMethod = !Modifier.isStatic(m.getModifiers()) |
| 54 | + |
| 55 | + // name must have at least 4 chars (minimum is "test") |
| 56 | + val simpleName = m.getName |
| 57 | + val firstFour = if (simpleName.length >= 4) simpleName.substring(0, 4) else "" |
| 58 | + |
| 59 | + val paramTypes = m.getParameterTypes |
| 60 | + val hasNoParams = paramTypes.length == 0 |
| 61 | + |
| 62 | + // Discover testNames(Informer) because if we didn't it might be confusing when someone |
| 63 | + // actually wrote a testNames(Informer) method and it was silently ignored. |
| 64 | + val isTestNames = simpleName == "testNames" |
| 65 | + val isTestTags = simpleName == "testTags" |
| 66 | + val isTestDataFor = (simpleName == "testDataFor" && paramTypes.length == 2 && classOf[String].isAssignableFrom(paramTypes(0)) && classOf[ConfigMap].isAssignableFrom(paramTypes(1))) || |
| 67 | + (simpleName == "testDataFor$default$2" && paramTypes.length == 0) |
| 68 | + |
| 69 | + (isInstanceMethod, simpleName, firstFour, paramTypes, hasNoParams, isTestNames, isTestTags, isTestDataFor) |
| 70 | + } |
| 71 | + |
| 72 | + def takesInformer(m: Method) = { |
| 73 | + val paramTypes = m.getParameterTypes |
| 74 | + paramTypes.length == 1 && classOf[Informer].isAssignableFrom(paramTypes(0)) |
| 75 | + } |
| 76 | + |
| 77 | + val InformerInParens = "(Informer)" |
| 78 | + val FixtureAndInformerInParens = "(FixtureParam, Informer)" |
| 79 | + val FixtureInParens = "(FixtureParam)" |
| 80 | + |
| 81 | + object EncodedOrdering extends Ordering[String] { |
| 82 | + def compare(x: String, y: String): Int = { |
| 83 | + decode(x) compareTo decode(y) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + def yeOldeTestNames(theSuite: Suite): Set[String] = { |
| 88 | + |
| 89 | + def isTestMethod(m: Method) = { |
| 90 | + |
| 91 | + // Factored out to share code with fixture.Suite.testNames |
| 92 | + val (isInstanceMethod, simpleName, firstFour, paramTypes, hasNoParams, isTestNames, isTestTags, isTestDataFor) = isTestMethodGoodies(m) |
| 93 | + |
| 94 | + isInstanceMethod && (firstFour == "test") && !isTestDataFor && ((hasNoParams && !isTestNames && !isTestTags) || takesInformer(m)) |
| 95 | + } |
| 96 | + |
| 97 | + val testNameArray = |
| 98 | + for (m <- theSuite.getClass.getMethods; if isTestMethod(m)) |
| 99 | + yield if (takesInformer(m)) m.getName + InformerInParens else m.getName |
| 100 | + |
| 101 | + val result = TreeSet.empty[String](EncodedOrdering) ++ testNameArray |
| 102 | + if (result.size != testNameArray.length) { |
| 103 | + throw new NotAllowedException("Howdy", 0) |
| 104 | + } |
| 105 | + result |
| 106 | + } |
| 107 | + |
| 108 | + def testMethodTakesAFixtureAndInformer(testName: String) = testName.endsWith(FixtureAndInformerInParens) |
| 109 | + |
| 110 | + def testMethodTakesAnInformer(testName: String): Boolean = testName.endsWith(InformerInParens) |
| 111 | + |
| 112 | + def testMethodTakesAFixture(testName: String) = testName.endsWith(FixtureInParens) |
| 113 | + |
| 114 | + def simpleNameForTest(testName: String) = |
| 115 | + if (testName.endsWith(FixtureAndInformerInParens)) |
| 116 | + testName.substring(0, testName.length - FixtureAndInformerInParens.length) |
| 117 | + else if (testName.endsWith(FixtureInParens)) |
| 118 | + testName.substring(0, testName.length - FixtureInParens.length) |
| 119 | + else if (testName.endsWith(InformerInParens)) |
| 120 | + testName.substring(0, testName.length - InformerInParens.length) |
| 121 | + else |
| 122 | + testName |
| 123 | + |
| 124 | + def getMethodForTestName(theSuite: org.scalatest.Suite, testName: String): Method = { |
| 125 | + val candidateMethods = theSuite.getClass.getMethods.filter(_.getName == simpleNameForTest(testName)) |
| 126 | + val found = |
| 127 | + if (testMethodTakesAFixtureAndInformer(testName)) |
| 128 | + candidateMethods.find( |
| 129 | + candidateMethod => { |
| 130 | + val paramTypes = candidateMethod.getParameterTypes |
| 131 | + paramTypes.length == 2 && paramTypes(1) == classOf[Informer] |
| 132 | + } |
| 133 | + ) |
| 134 | + else if (testMethodTakesAnInformer(testName)) |
| 135 | + candidateMethods.find( |
| 136 | + candidateMethod => { |
| 137 | + val paramTypes = candidateMethod.getParameterTypes |
| 138 | + paramTypes.length == 1 && paramTypes(0) == classOf[Informer] |
| 139 | + } |
| 140 | + ) |
| 141 | + else if (testMethodTakesAFixture(testName)) |
| 142 | + candidateMethods.find( |
| 143 | + candidateMethod => { |
| 144 | + val paramTypes = candidateMethod.getParameterTypes |
| 145 | + paramTypes.length == 1 |
| 146 | + } |
| 147 | + ) |
| 148 | + else |
| 149 | + candidateMethods.find(_.getParameterTypes.length == 0) |
| 150 | + |
| 151 | + found match { |
| 152 | + case Some(method) => method |
| 153 | + case None => |
| 154 | + throw new IllegalArgumentException(Resources.testNotFound(testName)) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + def mergeMap[A, B](ms: List[Map[A, B]])(f: (B, B) => B): Map[A, B] = |
| 159 | + (Map[A, B]() /: (for (m <- ms; kv <- m) yield kv)) { (a, kv) => |
| 160 | + a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) |
| 161 | + } |
| 162 | + |
| 163 | + def autoTagClassAnnotations(tags: Map[String, Set[String]], theSuite: Suite) = { |
| 164 | + val suiteTags = for { |
| 165 | + a <- theSuite.getClass.getAnnotations |
| 166 | + annotationClass = a.annotationType |
| 167 | + if annotationClass.isAnnotationPresent(classOf[TagAnnotation]) |
| 168 | + } yield annotationClass.getName |
| 169 | + |
| 170 | + val autoTestTags = |
| 171 | + if (suiteTags.size > 0) |
| 172 | + Map() ++ theSuite.testNames.map(tn => (tn, suiteTags.toSet)) |
| 173 | + else |
| 174 | + Map.empty[String, Set[String]] |
| 175 | + |
| 176 | + mergeMap[String, Set[String]](List(tags, autoTestTags)) ( _ ++ _ ) |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments