This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathsuites.scala
More file actions
179 lines (139 loc) · 6.62 KB
/
suites.scala
File metadata and controls
179 lines (139 loc) · 6.62 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package weaver
import cats.effect.Resource
import cats.syntax.all._
import fs2.Stream
import org.junit.runner.RunWith
import org.portablescala.reflect.annotation.EnableReflectiveInstantiation
// Just a non-parameterized marker trait to help SBT's test detection logic.
@EnableReflectiveInstantiation
trait BaseSuiteClass {}
trait Suite[F[_]] extends BaseSuiteClass {
def name: String
def spec(args: List[String]): Stream[F, TestOutcome]
}
// A version of EffectSuite that has a type member instead of a type parameter.
protected[weaver] trait EffectSuiteAux {
type EffectType[A]
implicit protected def effect: CECompat.Effect[EffectType]
}
// format: off
trait EffectSuite[F[_]] extends Suite[F] with EffectSuiteAux with SourceLocation.Here { self =>
final type EffectType[A] = F[A]
implicit protected def effectCompat: EffectCompat[F]
implicit final protected def effect: CECompat.Effect[F] = effectCompat.effect
/**
* Raise an error that leads to the running test being tagged as "cancelled".
*/
def cancel(reason: String)(implicit pos: SourceLocation): F[Nothing] =
effect.raiseError(new CanceledException(Some(reason), pos))
/**
* Raises an error that leads to the running test being tagged as "ignored"
*/
def ignore(reason: String)(implicit pos: SourceLocation): F[Nothing] =
effect.raiseError(new IgnoredException(Some(reason), pos))
override def name : String = self.getClass.getName.replace("$", "")
protected def adaptRunError: PartialFunction[Throwable, Throwable] = PartialFunction.empty
final def run(args : List[String])(report : TestOutcome => F[Unit]) : F[Unit] =
spec(args).evalMap(report).compile.drain.adaptErr(adaptRunError)
}
@RunWith(classOf[weaver.junit.WeaverRunner])
abstract class RunnableSuite[F[_]] extends EffectSuite[F] {
implicit protected def effectCompat: UnsafeRun[EffectType]
private[weaver] def getEffectCompat: UnsafeRun[EffectType] = effectCompat
def plan : List[TestName]
private[weaver] def runUnsafe(args: List[String])(report: TestOutcome => Unit) : Unit =
effectCompat.sync(run(args)(outcome => effectCompat.effect.delay(report(outcome))))
}
abstract class MutableBaseFSuite[F[_]] extends RunnableSuite[F] {
type Res
def maxParallelism : Int = 10000
protected def registerTest(name: TestName)(f: Res => F[TestOutcome]): Unit =
synchronized {
if (isInitialized) throw initError()
testSeq = testSeq :+ (name -> f)
}
def pureTest(name: TestName)(run : => Expectations) : Unit = registerTest(name)(_ => Test(name.name, effectCompat.effect.delay(run)))
def loggedTest(name: TestName)(run: Log[F] => F[Expectations]) : Unit = registerTest(name)(_ => Test(name.name, log => run(log)))
def test(name: TestName) : PartiallyAppliedTest = new PartiallyAppliedTest(name)
class PartiallyAppliedTest(name : TestName) {
def apply(run: => F[Expectations]) : Unit = registerTest(name)(_ => Test(name.name, run))
def apply(run : Res => F[Expectations]) : Unit = registerTest(name)(res => Test(name.name, run(res)))
def apply(run : (Res, Log[F]) => F[Expectations]) : Unit = registerTest(name)(res => Test(name.name, log => run(res, log)))
}
private[this] var testSeq = Seq.empty[(TestName, Res => F[TestOutcome])]
def plan: List[TestName] = testSeq.map(_._1).toList
private[this] var isInitialized = false
private[this] def initError() =
new AssertionError(
"Cannot define new tests after TestSuite was initialized"
)
private[weaver] def getTests(args: List[String]) = {
if (!isInitialized) isInitialized = true
val argsFilter = Filters.filterTests(this.name)(args)
val filteredTests = if (testSeq.exists(_._1.tags(TestName.Tags.only))){
testSeq.filter(_._1.tags(TestName.Tags.only)).map { case (_, test) => (res: Res) => test(res)}
} else testSeq.collect {
case (name, test) if argsFilter(name) => (res : Res) => test(res)
}
val parallism = math.max(1, maxParallelism)
(filteredTests, parallism)
}
}
abstract class MutableFSuite[F[_]] extends MutableBaseFSuite[F]{
def sharedResource : Resource[F, Res]
override def spec(args: List[String]) : Stream[F, TestOutcome] =
synchronized {
val (filteredTests, parallism) = getTests(args)
if (filteredTests.isEmpty) Stream.empty // no need to allocate resources
else for {
resource <- Stream.resource(sharedResource)
tests = filteredTests.map(_.apply(resource))
testStream = Stream.emits(tests).lift[F](effectCompat.effect)
result <- if (parallism > 1 ) testStream.parEvalMap(parallism)(identity)(effectCompat.effect)
else testStream.evalMap(identity)
} yield result
}
}
abstract class MutableForEachSuite[F[_]] extends MutableBaseFSuite[F]{
def uniqueResource : Resource[F, Res]
override def spec(args: List[String]) : Stream[F, TestOutcome] =
synchronized {
val (filteredTests, parallism) = getTests(args)
if (filteredTests.isEmpty) Stream.empty // no need to allocate resources
else {
val testStream = Stream.emits(filteredTests).lift[F](effectCompat.effect)
if (parallism > 1 )
testStream.parEvalMap(parallism)(test => uniqueResource.use(test(_)))(effectCompat.effect)
else testStream.evalMap(test => uniqueResource.use(test(_)))
}
}
}
trait FunSuiteAux {
def test(name: TestName)(run: => Expectations): Unit
}
abstract class FunSuiteF[F[_]] extends RunnableSuite[F] with FunSuiteAux { self =>
override def test(name: TestName)(run: => Expectations): Unit = synchronized {
if(isInitialized) throw initError
testSeq = testSeq :+ (name -> (() => Test.pure(name.name)(() => run)))
}
override def name : String = self.getClass.getName.replace("$", "")
private def pureSpec(args: List[String]) = synchronized {
if(!isInitialized) isInitialized = true
val argsFilter = Filters.filterTests(this.name)(args)
val filteredTests = if (testSeq.exists(_._1.tags(TestName.Tags.only))){
testSeq.filter(_._1.tags(TestName.Tags.only)).map { case (_, test) => test}
} else testSeq.collect {
case (name, test) if argsFilter(name) => test
}
fs2.Stream.emits(filteredTests.map(execute => execute()))
}
override def spec(args: List[String]) = pureSpec(args).covary[F]
override def runUnsafe(args: List[String])(report: TestOutcome => Unit) =
pureSpec(args).compile.toVector.foreach(report)
private[this] var testSeq = Seq.empty[(TestName, () => TestOutcome)]
def plan: List[TestName] = testSeq.map(_._1).toList
private[this] var isInitialized = false
}
private[weaver] object initError extends AssertionError(
"Cannot define new tests after TestSuite was initialized"
)