8000 Reduced Spec Environment types. Add TestOutputTest. Pull printing code out of TestOutput by swoogles · Pull Request #6503 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Reduced Spec Environment types. Add TestOutputTest. Pull printing code out of TestOutput #6503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ class ZTestJUnitRunner(klass: Class[_]) extends Runner with Filterable {
.provide(
Scope.default >>> (ZEnv.live >>> TestEnvironment.live ++ ZLayer.environment[Scope]),
ZIOAppArgs.empty,
spec.layer,
TestLogger.fromConsole
spec.layer
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import zio.test.{
AbstractRunnableSpec,
ExecutionEventSink,
FilteredSpec,
TestOutput,
TestArgs,
TestEnvironment,
TestLogger,
Expand All @@ -25,7 +24,11 @@ abstract class BaseTestTask(
protected def run(
eventHandler: EventHandler,
spec: AbstractRunnableSpec
): ZIO[TestLogger with Clock with TestOutput with ExecutionEventSink with Random, Throwable, Unit] = {
): ZIO[
Clock with ExecutionEventSink with Random,
Throwable,
Unit
] = {
assert(eventHandler != null)
for {
summary <- spec.runSpec(FilteredSpec(spec.spec, args))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ object ExecutionEventSinkSpec extends ZIOSpecDefault {
Console.live,
TestLogger.fromConsole,
ExecutionEventSink.live,
TestOutput.live
TestOutput.live,
ExecutionEventPrinter.live
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ object IntelliJRenderUtils {
IntelliJTestRunner(testEnvironment)
.run(spec)
.provideLayer[Nothing, TestEnvironment with Scope](
TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live
TestClock.default ++ (TestLogger.fromConsole >>> ExecutionEventPrinter.live >>> TestOutput.live >>> ExecutionEventSink.live) ++ Random.live
)
output <- TestConsole.output
} yield output.mkString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object ReportingTestUtils {
TestTestRunner(testEnvironment)
.run(spec)
.provideLayer(
TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live
(TestLogger.fromConsole >>> ExecutionEventPrinter.live >>> TestOutput.live >>> ExecutionEventSink.live) ++ TestClock.default ++ Random.live
)
output <- TestConsole.output
} yield output.mkString
Expand All @@ -61,7 +61,7 @@ object ReportingTestUtils {
TestTestRunner(testEnvironment)
.run(spec)
.provideLayer(
Scope.default >>> (TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live)
Scope.default >>> ((TestLogger.fromConsole >>> ExecutionEventPrinter.live >>> TestOutput.live >>> ExecutionEventSink.live) ++ TestClock.default ++ Random.live)
)
} yield summary.summary

Expand Down
226 changes: 226 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package zio.test
import zio.Scope
import zio._
import zio.test.ExecutionEvent.{SectionEnd, SectionStart}

case class TestEntity(
id: SuiteId,
ancestors: List[SuiteId]
) {
def child(newId: Int): TestEntity =
TestEntity(SuiteId(newId), id :: ancestors)
}

object TestOutputSpec extends ZIOSpecDefault {
/*
1 -> 2 -> 4
-> 5

-> 3 -> 6
-> 7
*/
private val parent = TestEntity(
id = SuiteId(1),
ancestors = List.empty
)

private val child1 =
parent.child(2)

private val child2 =
parent.child(3)

private val child1child1 =
child1.child(4)

private val child1child2 =
child1.child(5)

private val child2child1 =
child2.child(6)

private val child2child2 =
child2.child(7)

val allEntities = List(parent, child1, child2, child1child1, child1child2, child2child1, child2child2)

class ExecutionEventHolder(events: Ref[List[ExecutionEvent]]) extends ExecutionEventPrinter {
override def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] =
events.update(_ :+ event)

def getEvents: ZIO[Any, Nothing, List[ExecutionEvent]] =
events.get
}

val makeFakePrinter: ZIO[Any, Nothing, ExecutionEventHolder] =
for {
output <- Ref.make[List[ExecutionEvent]](List.empty)
} yield new ExecutionEventHolder(output)

val fakePrinterLayer: ZLayer[Any, Nothing, ExecutionEventHolder] = ZLayer.fromZIO(makeFakePrinter)

override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")(
test("nested events without flushing") {
val events =
List(
Start(parent),
Start(child1),
Start(child2),
Test(child1),
Test(child2),
Test(child1),
End(child2),
End(child1)
)
for {
_ <- ZIO.foreach(events)(event => TestOutput.print(event))
outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents)
} yield assertTrue(
outputEvents ==
List(
Start(parent),
Start(child1),
Test(child1),
Test(child1),
End(child1)
)
)
},
test("nested events with flushing") {
val events =
List(
Start(parent),
Start(child1),
Start(child2),
Test(child1),
Test(child2),
Test(child1),
End(child2),
End(child1),
End(parent)
)
for {
_ <- ZIO.foreach(events)(event => TestOutput.print(event))
outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents)
} yield assertTrue(
outputEvents ==
List(
Start(parent),
Start(child1),
Test(child1),
Test(child1),
End(child1),
Start(child2),
Test(child2),
End(child2),
End(parent)
)
)
},
test("mix of suites and individual tests") {
val events =
List(
Start(parent),
Start(child1),
Test(child1, "a"),
Start(child1child1),
Test(child1, "b"),
Test(child1child1),
Test(child1, "c"),
End(child1child1),
End(child1),
End(parent)
)
for {
_ <- ZIO.foreach(events)(event => TestOutput.print(event))
outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents)
} yield assertTrue(
outputEvents ==
List(
Start(parent),
Start(child1),
Test(child1, "a"), // This child1 test came before the child1child1 section, so it was printed immediately
Start(child1child1),
Test(child1child1),
End(child1child1),
Test(child1, "b"), // These other child1 tests were queued until child1child1 ended
Test(child1, "c"),
End(child1),
End(parent)
)
)
},
test("more complex mix of suites and individual tests") {
val events =
List(
Start(parent),
Start(child1),
Start(child2),
Test(child1, "a"),
Start(child2child1),
Test(child2, "j"),
Test(child2child1, "k"),
End(child2child1),
Start(child1child1),
Test(child1, "b"),
Test(child2, "m"),
Test(child1child1),
End(child2),
Test(child1, "c"),
End(child1child1),
End(child1),
End(parent)
)
for {
_ <- ZIO.foreach(events)(event => TestOutput.print(event))
outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents)
} yield assertTrue(
outputEvents ==
List(
Start(parent),
Start(child1),
Test(child1, "a"), // This child1 test came before the child1child1 section, so it was printed immediately
Start(child1child1),
Test(child1child1),
End(child1child1),
Test(child1, "b"), // These other child1 tests were queued until child1child1 ended
Test(child1, "c"),
End(child1),
Start(child2),
Test(child2, "j"),
Start(child2child1),
Test(child2child1, "k"),
End(child2child1),
Test(child2, "m"),
End(child2),
End(parent)
)
)
}
).provide(fakePrinterLayer >+> TestOutput.live)

private def Start(testEntity: TestEntity) =
SectionStart(
List("label"),
testEntity.id,
testEntity.ancestors
)

private def End(testEntity: TestEntity) =
SectionEnd(
List("section: " + testEntity.id.id),
testEntity.id,
testEntity.ancestors
)

private def Test(testEntity: TestEntity, label: String = "label") =
ExecutionEvent.Test(
labelsReversed = List(label),
test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)),
annotations = TestAnnotationMap.empty,
ancestors = testEntity.ancestors,
duration = 0L,
id = testEntity.id
)

}
2 changes: 1 addition & 1 deletion test-tests/shared/src/test/scala/zio/test/TestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object TestUtils {
TestExecutor
.default(testEnvironment)
.run(spec, ExecutionStrategy.Sequential)
.provide(testEnvironment, TestLogger.fromConsole, Scope.default)
.provide(testEnvironment, Scope.default)

def isIgnored[E](spec: ZSpec[TestEnvironment, E]): UIO[Boolean] =
execute(spec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ abstract class AbstractRunnableSpec {
spec: ZSpec[Environment, Failure]
)(implicit
trace: ZTraceElement
): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary] =
): URIO[
Clock with ExecutionEventSink with Random,
Summary
] =
runner.run(aspects.foldLeft(spec)(_ @@ _))

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ abstract class DefaultRunnableSpec extends RunnableSpec[TestEnvironment, Any] {
spec: ZSpec[Environment, Failure]
)(implicit
trace: ZTraceElement
): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary] =
): URIO[
Clock with ExecutionEventSink with Random,
Summary
] =
runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers)

/**
Expand Down
9 changes: 5 additions & 4 deletions test/shared/src/main/scala/zio/test/ExecutionEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ object ExecutionEvent {
duration: Long = 0L,
id: SuiteId
) extends ExecutionEvent {
def labels: List[String] = labelsReversed.reverse
val labels: List[String] = labelsReversed.reverse
}

final case class SectionStart(
labelsReversed: List[String],
id: SuiteId,
ancestors: List[SuiteId]
) extends ExecutionEvent {
def labels: List[String] = labelsReversed.reverse
val labels: List[String] = labelsReversed.reverse
}

final case class SectionEnd(
labelsReversed: List[String],
id: SuiteId,
ancestors: List[SuiteId]
) extends ExecutionEvent {
def labels: List[String] = labelsReversed.reverse
val labels: List[String] = labelsReversed.reverse
}

final case class RuntimeFailure[+E](
Expand All @@ -35,12 +35,13 @@ object ExecutionEvent {
failure: TestFailure[E],
ancestors: List[SuiteId]
) extends ExecutionEvent {
def labels: List[String] = labelsReversed.reverse
val labels: List[String] = labelsReversed.reverse
}

}

sealed trait ExecutionEvent {
val id: SuiteId
val ancestors: List[SuiteId]
val labels: List[String]
}
25 changes: 25 additions & 0 deletions test/shared/src/main/scala/zio/test/ExecutionEventPrinter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package zio.test

import zio.{ZIO, ZLayer}

trait ExecutionEventPrinter {
def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit]
}

object ExecutionEventPrinter {
val live: ZLayer[TestLogger, Nothing, ExecutionEventPrinter] = {
ZLayer.fromZIO(for {
testLogger <- ZIO.service[TestLogger]
} yield new Live(testLogger))
}

def print(event: ExecutionEvent): ZIO[ExecutionEventPrinter, Nothing, Unit] =
ZIO.serviceWithZIO[ExecutionEventPrinter](_.print(event))

class Live(logger: TestLogger) extends ExecutionEventPrinter {
override def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] =
logger.logLine(
ReporterEventRenderer.render(event).mkString("\n")
)
}
}
Loading
0