-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScalaTestClassDescriptor.scala
90 lines (80 loc) · 3.2 KB
/
ScalaTestClassDescriptor.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2001-2023 Artima, Inc.
*
* Licensed 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 org.scalatestplus.junit5
import org.junit.platform.engine.support.descriptor.{AbstractTestDescriptor, ClassSource}
import org.junit.platform.engine.{TestDescriptor, TestSource, TestTag, UniqueId}
import org.scalatest.{Suite, TagAnnotation}
import scala.collection.JavaConverters._
import java.util.Optional
/**
* <code>TestDescriptor</code> for ScalaTest suite.
*
* @param parent The parent descriptor.
* @param theUniqueId The unique ID.
* @param suiteClass The class of the ScalaTest suite.
*/
class ScalaTestClassDescriptor(parent: TestDescriptor, val theUniqueId: UniqueId, val suiteClass: Class[_], autoAddTestChildren: Boolean) extends AbstractTestDescriptor(theUniqueId, suiteClass.getName, ClassSource.from(suiteClass)) {
/**
* Suite instance that will be executed if this class descriptor is selected.
*/
lazy val suite: Suite = {
val canInstantiate = JUnitHelper.checkForPublicNoArgConstructor(suiteClass) && classOf[org.scalatest.Suite].isAssignableFrom(suiteClass)
require(canInstantiate, "Must pass an org.scalatest.Suite with a public no-arg constructor")
suiteClass.newInstance.asInstanceOf[org.scalatest.Suite]
}
// If this descriptor is created from container selectors (class, package, module etc.), children will be added automatically.
if (autoAddTestChildren)
suite.testNames.foreach { tn =>
val testUniqueId = theUniqueId.append("test", tn)
val testDesc = new ScalaTestDescriptor(testUniqueId, tn, None)
addChild(testDesc)
}
/**
* Type of this <code>ScalaTestClassDescriptor</code>.
*
* @return <code>TestDescriptor.Type.CONTAINER</code>
*/
override def getType: TestDescriptor.Type = TestDescriptor.Type.CONTAINER
/**
* Override <code>mayRegisterTests</code> to return true
*
* @return <code>true</code>
*/
override def mayRegisterTests(): Boolean = true
/**
* Return <code>ClassSource</code> for the given suite class.
*
* @return <code>ClassSource</code> created from given suite class
*/
override def getSource: Optional[TestSource] =
Optional.of(ClassSource.from(suiteClass))
/**
* Get tags for this suite.
*
* @return Tags for this suite.
*/
override def getTags: java.util.Set[TestTag] =
suiteClass.getAnnotations.filter((a) => a.annotationType.isAnnotationPresent(classOf[TagAnnotation])).map((a) => TestTag.create(a.annotationType.getName)).toSet.asJava
}
/**
* <code>ScalaTestClassDescriptor</code> companion object.
*/
object ScalaTestClassDescriptor {
/**
* Segment type for <code>ScalaTestClassDescriptor</code>, has the value of <code>class</code>
*/
val segmentType = "class"
}