Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adpi2 committed Nov 16, 2023
1 parent 3cc5a42 commit 045d8ec
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 66 deletions.
2 changes: 1 addition & 1 deletion frontend/src/test/scala/bloop/TestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class BaseTestSpec(val projectName: String, buildName: String)
build.state.test(project)
try assert(logger.errors.size == 0)
catch { case _: AssertionError => logger.dump() }
assertNoDiff(
assertEndsWith(
logger.renderTimeInsensitiveTestInfos,
expectedFullTestsOutput
)
Expand Down
11 changes: 5 additions & 6 deletions frontend/src/test/scala/bloop/dap/DebugProtocolSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
} yield output
}

assertNoDiff(output, "Hello, World!\n")
assertNoDiff(output.linesIterator.toSeq.last, "Hello, World!")
}
}
}
Expand Down Expand Up @@ -76,8 +76,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
previousSessionOutput <- previousSession.takeCurrentOutput
} yield previousSessionOutput
}

assertNoDiff(output, "")
assert(output.linesIterator.size == 5)
}
}
}
Expand Down Expand Up @@ -107,7 +106,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
val project = TestProject(workspace, "p", List(main))

loadBspState(workspace, List(project), logger) { state =>
// start debug session and the immediately disconnect from it
// start debug session and then immediately disconnect from it
val blockingSessionOutput = state.withDebugSession(project, mainClassParams("Main")) {
client =>
for {
Expand All @@ -120,7 +119,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
} yield output
}

assertNoDiff(blockingSessionOutput, "Blocking Hello!")
assertNoDiff(blockingSessionOutput.linesIterator.toSeq.last, "Blocking Hello!")

// fix the main class
val sources = state.toTestState.getProjectFor(project).sources
Expand All @@ -139,7 +138,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
} yield output
}

assertNoDiff(output, "Non-blocking Hello!")
assertNoDiff(output.linesIterator.toSeq.last, "Non-blocking Hello!")
}
}
}
Expand Down
23 changes: 7 additions & 16 deletions frontend/src/test/scala/bloop/dap/DebugServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
} yield {
assert(client.socket.isClosed)
assertNoDiff(
output.linesIterator
.filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment"))
.filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV"))
.mkString(lineSeparator),
output.linesIterator.toSeq.last,
"Hello, World!"
)
}
Expand Down Expand Up @@ -190,10 +187,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
} yield {
assert(client.socket.isClosed)
assertNoDiff(
output.linesIterator
.filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment"))
.filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV"))
.mkString(lineSeparator),
output.linesIterator.toSeq.takeRight(2).mkString(lineSeparator),
"hello\nworld!"
)
}
Expand Down Expand Up @@ -291,7 +285,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
} yield {
assert(client.socket.isClosed)
assertNoDiff(
finalOutput,
finalOutput.linesIterator.toSeq.takeRight(7).mkString(lineSeparator),
"""|Breakpoint in main method
|Breakpoint in hello class
|Breakpoint in hello inner class
Expand Down Expand Up @@ -545,6 +539,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
for {
port <- startRemoteProcess(buildProject, testState)
client <- server.startConnection
initOutput <- client.takeCurrentOutput
_ <- client.initialize()
_ <- client.attach("localhost", port)
breakpoints <- client.setBreakpoints(breakpoints)
Expand All @@ -560,12 +555,8 @@ object DebugServerSpec extends DebugBspBaseSuite {
} yield {
assert(client.socket.isClosed)

assertNoDiff(outputOnBreakpoint, "")

assertNoDiff(
finalOutput,
""
)
assertNoDiff(outputOnBreakpoint, initOutput)
assertNoDiff(finalOutput, initOutput)
}
}
}
Expand Down Expand Up @@ -667,7 +658,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
_ <- Task.fromFuture(client.closedPromise.future)
} yield {
assert(client.socket.isClosed)
assertNoDiff(finalOutput, workspace.toString)
assertNoDiff(finalOutput.linesIterator.toSeq.last, workspace.toString)
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions frontend/src/test/scala/bloop/testing/BaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,11 @@ abstract class BaseSuite extends TestSuite with BloopHelpers {
expectedTitle: String
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
colored {
DiffAssertions.assertNoDiffOrPrintExpected(
DiffAssertions.assertNoDiffOrPrintObtained(
obtained,
expected,
obtainedTitle,
expectedTitle,
true
expectedTitle
)
()
}
Expand All @@ -455,11 +454,20 @@ abstract class BaseSuite extends TestSuite with BloopHelpers {
print: Boolean = true
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
colored {
DiffAssertions.assertNoDiffOrPrintExpected(obtained, expected, title, title, print)
if (print) DiffAssertions.assertNoDiffOrPrintObtained(obtained, expected, title, title)
else DiffAssertions.assertNoDiff(obtained, expected, title, title)
()
}
}

def assertEndsWith(
obtained: String,
expected: String,
title: String = ""
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
colored { DiffAssertions.assertEndsWithOrPrintObtained(obtained, expected, title, title) }
}

def colored[T](
thunk: => T
)(implicit filename: sourcecode.File, line: sourcecode.Line): T = {
Expand Down
64 changes: 25 additions & 39 deletions frontend/src/test/scala/bloop/testing/DiffAssertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,19 @@ object DiffAssertions {
obtainedTitle: String,
expectedTitle: String
)(implicit source: sourcecode.Line): Unit = {
orPrintObtained(
() => { assertNoDiff(obtained, expected, obtainedTitle, expectedTitle); () },
obtained
)
orPrintObtained(obtained) { () =>
assertNoDiff(obtained, expected, obtainedTitle, expectedTitle); ()
}
}

def assertNoDiffOrPrintExpected(
def assertEndsWithOrPrintObtained(
obtained: String,
expected: String,
obtainedTitle: String,
expectedTitle: String,
print: Boolean = true
)(implicit
source: sourcecode.Line
): Boolean = {
try assertNoDiff(obtained, expected, obtainedTitle, expectedTitle)
catch {
case ex: Exception =>
if (print) {
obtained.linesIterator.toList match {
case head +: tail =>
val b = new StringBuilder()
b.++=(" \"\"\"|" + head).++=(System.lineSeparator())
tail.foreach { line =>
b.++=(" |")
.++=(line)
.++=(System.lineSeparator())
}
b.++=(" |\"\"\".stripMargin").++=(System.lineSeparator())
println(b.mkString)
case head +: Nil =>
println(head)
case Nil =>
println("obtained is empty")
}
}
throw ex
expectedTitle: String
)(implicit source: sourcecode.Line): Unit = {
orPrintObtained(obtained) { () =>
assertEndsWith(obtained, expected, obtainedTitle, expectedTitle); ()
}
}

Expand All @@ -66,12 +42,22 @@ object DiffAssertions {
if (result.isEmpty) true
else {
throw new TestFailedException(
error2message(
obtained,
expected,
obtainedTitle,
expectedTitle
)
error2message(obtained, expected, obtainedTitle, expectedTitle)
)
}
}

def assertEndsWith(
obtained: String,
expected: String,
obtainedTitle: String,
expectedTitle: String
)(implicit source: sourcecode.Line): Boolean = colored {
if (obtained.isEmpty && !expected.isEmpty) fail("Obtained empty output!")
if (obtained.endsWith(expected)) true
else {
throw new TestFailedException(
error2message(obtained, expected, obtainedTitle, expectedTitle)
)
}
}
Expand Down Expand Up @@ -126,7 +112,7 @@ object DiffAssertions {
}
}

def orPrintObtained(thunk: () => Unit, obtained: String): Unit = {
def orPrintObtained(obtained: String)(thunk: () => Unit): Unit = {
try thunk()
catch {
case ex: Exception =>
Expand Down

0 comments on commit 045d8ec

Please sign in to comment.