Skip to content

Commit

Permalink
Make basic testing working
Browse files Browse the repository at this point in the history
  • Loading branch information
Olafur Pall Geirsson committed Jan 3, 2020
1 parent dfeaecb commit d77c3b0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ website/i18n/*
!website/i18n/en.json

project/metals.sbt
out/
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ inThisBuild(
List(
scalaVersion := scala213,
crossScalaVersions := List(scala213, scala212),
fork := true
fork := true,
testFrameworks := List(
new TestFramework("com.geirsson.junit.JUnitFramework")
)
)
)

Expand Down
26 changes: 15 additions & 11 deletions munit/src/main/scala/munit/Runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,23 @@ final class Runner(cls: Class[_ <: Suite]) extends org.junit.runner.Runner {

def runAll(notifier: RunNotifier): Unit = {
try {
val canStart = runBeforeAll(notifier)
if (canStart) {
val isContinue = runBeforeAll(notifier)
if (isContinue) {
suite.tests.foreach { test =>
val description = Description.createTestDescription(cls, test.name)
notifier.fireTestStarted(description)
try {
StackMarker.dropOutside(test.body())
} catch {
case ex: Throwable =>
StackMarker.trimStackTrace(ex)
notifier.fireTestFailure(new Failure(description, ex))
} finally {
notifier.fireTestFinished(description)
var isContinue = runBeforeEach(notifier, test)
if (isContinue) {
notifier.fireTestStarted(description)
try {
StackMarker.dropOutside(test.body())
} catch {
case ex: Throwable =>
StackMarker.trimStackTrace(ex)
notifier.fireTestFailure(new Failure(description, ex))
} finally {
notifier.fireTestFinished(description)
runAfterEach(notifier, test)
}
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions munit/src/main/scala/munit/internal/StackMarker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.{util => ju}
* Wrapper-functions that can be used to mark parts of the callstack that are
* meant to be filtered out later.
*/
class StackMarker
object StackMarker {
// Ask Scalac/Scala.js nicely to try and avoid inlining these two marker methods,
// to make sure they don't disappear from the stack traces
Expand All @@ -19,24 +20,27 @@ object StackMarker {
new ju.IdentityHashMap[Throwable, java.lang.Boolean]()
)
def loop(e: Throwable): Unit = {
if (!isVisited.contains(e)) {
isVisited.add(e)
e.setStackTrace(filterCallStack(e.getStackTrace()))
if (e != null && isVisited.add(e)) {
val stack = e.getStackTrace()
if (stack != null) {
e.setStackTrace(filterCallStack(stack))
}
loop(e.getCause())
}
}
loop(ex)
}
val className = classOf[StackMarker].getCanonicalName() + "$"
def filterCallStack(
stack: Array[StackTraceElement]
): Array[StackTraceElement] = {
val droppedInside = stack.indexWhere(x =>
x.getClassName == "munit.internal.StackMarker$" &&
x.getClassName == className &&
x.getMethodName == "dropInside"
)

val droppedOutside = stack.indexWhere(x =>
x.getClassName == "munit.internal.StackMarker$" &&
x.getClassName == className &&
x.getMethodName == "dropOutside"
)

Expand Down
30 changes: 30 additions & 0 deletions munit/src/test/scala/munit/BasicSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package munit

import org.junit.runner.RunWith

@RunWith(classOf[Runner])
class BasicSuite extends Suite {
override def beforeAll(context: BeforeAll): Unit = {
println("beforeAll")
}
override def afterAll(context: AfterAll): Unit = {
println("afterAll")
}
override def beforeEach(context: BeforeEach): Unit = {
println("beforeEach: " + context.test.name)
}
override def afterEach(context: AfterEach): Unit = {
println("afterEach: " + context.test.name)
}
test("not-implemented") {
println("not-implemented")
Thread.sleep(200)
???
}
test("pass") {
Thread.sleep(400)
println("pass")
println("pass")
assert(1 == 1)
}
}

0 comments on commit d77c3b0

Please sign in to comment.