-
Couldn't load subscription status.
- Fork 1.4k
MutableRunnableSpec #4523
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
MutableRunnableSpec #4523
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b9be948
MutableRunnableSpec
fokot ff4b47d
scala fix
fokot c0c52a1
no more existential types
fokot 79638ec
scalafmt
fokot 148fc37
typo
fokot 86958e2
scalafix
fokot dd720b3
Merge remote-tracking branch 'upstream/master' into adding_MutableRun…
fokot 51e0d65
nested suites
fokot cd6bd78
nested suites
fokot d598f8c
environment not working
fokot 8ef7783
environments
fokot 4d1dc31
Merge remote-tracking branch 'upstream/master' into adding_MutableRun…
fokot 075cc95
fixes after merging master
fokot b5896d1
Merge remote-tracking branch 'upstream/master' into adding_MutableRun…
fokot d6ab762
some code review fixes
fokot e5da621
some code review fixes
fokot 2dfe470
some code review fixes
fokot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
test-tests/shared/src/test/scala/zio/test/DefaultMutableRunnableSpecSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package zio.test | ||
|
|
||
| import zio.ZIO | ||
| import zio.test.Assertion.equalTo | ||
| import zio.test.TestAspect.ignore | ||
|
|
||
| object DefaultMutableRunnableSpecSpec extends DefaultMutableRunnableSpec { | ||
|
|
||
| test("top level test") { | ||
| assert(0)(equalTo(0)) | ||
| } | ||
|
|
||
| suite("first") { | ||
|
|
||
| test("simple") { | ||
| assert(1)(equalTo(1)) | ||
| } | ||
|
|
||
| testM("effect") { | ||
| for { | ||
| res <- ZIO.succeed(10) | ||
| } yield assert(res)(equalTo(10)) | ||
| } | ||
| } | ||
|
|
||
| suite("second") { | ||
|
|
||
| test("simple 2") { | ||
| assert(2)(equalTo(2)) | ||
| } | ||
|
|
||
| test("ignoring this test") { | ||
| assert(1)(equalTo(123)) | ||
| } @@ ignore | ||
|
|
||
| suite("nested suite") { | ||
| test("test in nested suite") { | ||
| assert(3)(equalTo(3)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| suite("ignored suite") { | ||
|
|
||
| test("failing test 2") { | ||
| assert(2)(equalTo(123)) | ||
| } | ||
| } @@ ignore | ||
|
|
||
| test("last") { | ||
| // test("test in test must be commented not to fail") { | ||
| // assert(0)(equalTo(0)) | ||
| // } | ||
| assert(0)(equalTo(0)) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
test/shared/src/main/scala/zio/test/DefaultMutableRunnableSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package zio.test | ||
|
|
||
| import zio.ZLayer | ||
| import zio.test.environment.TestEnvironment | ||
|
|
||
| /** | ||
| * Syntax for writing test like | ||
| * {{{ | ||
| * object MySpec extends DefaultMutableRunnableSpec { | ||
| * suite("foo") { | ||
| * testM("name") { | ||
| * } @@ ignore | ||
| * | ||
| * test("name 2") | ||
| * } | ||
| * suite("another suite") { | ||
| * test("name 3") | ||
| * } | ||
| * } | ||
| * }}} | ||
| */ | ||
| class DefaultMutableRunnableSpec extends MutableRunnableSpec(ZLayer.identity[TestEnvironment]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package zio.test | ||
|
|
||
| import zio.clock.Clock | ||
| import zio.duration._ | ||
| import zio.test.environment.TestEnvironment | ||
| import zio.{Chunk, Has, URIO, ZIO, ZLayer} | ||
|
|
||
| import scala.util.control.NoStackTrace | ||
|
|
||
| /** | ||
| * Syntax for writing test like | ||
| * {{{ | ||
| * object MySpec extends MutableRunnableSpec(layer) { | ||
| * suite("foo") { | ||
| * testM("name") { | ||
| * } @@ ignore | ||
| * | ||
| * test("name 2") | ||
| * } | ||
| * suite("another suite") { | ||
| * test("name 3") | ||
| * } | ||
| * } | ||
| * }}} | ||
| */ | ||
| class MutableRunnableSpec[R <: Has[_]](layer: ZLayer[TestEnvironment, Throwable, R]) | ||
| extends RunnableSpec[TestEnvironment, Any] { | ||
| self => | ||
|
|
||
| private class InAnotherTestException(`type`: String, label: String) | ||
| extends Exception(s"${`type`} `${label}` is in another test") | ||
| with NoStackTrace | ||
|
|
||
| sealed trait SpecBuilder { | ||
| def toSpec: ZSpec[R, Any] | ||
| def label: String | ||
| } | ||
|
|
||
| sealed case class SuiteBuilder(label: String) extends SpecBuilder { | ||
|
|
||
| private[test] var nested: Chunk[SpecBuilder] = Chunk.empty | ||
| private var aspects: Chunk[TestAspect[R, R, Failure, Failure]] = Chunk.empty | ||
|
|
||
| /** | ||
| * Syntax for adding aspects. | ||
| * {{{ | ||
| * test("foo") { assert(42, equalTo(42)) } @@ ignore | ||
| * }}} | ||
| */ | ||
| final def @@( | ||
| aspect: TestAspect[R, R, Failure, Failure] | ||
| ): SpecBuilder = { | ||
| aspects = aspects :+ aspect | ||
| this | ||
| } | ||
|
|
||
| def toSpec: ZSpec[R, Any] = | ||
| aspects.foldLeft( | ||
| zio.test.suite(label)( | ||
| nested.map(_.toSpec): _* | ||
| ) | ||
| )((spec, aspect) => spec @@ aspect) | ||
| } | ||
|
|
||
| sealed case class TestBuilder(label: String, var toSpec: ZSpec[R, Any]) extends SpecBuilder { | ||
|
|
||
| /** | ||
| * Syntax for adding aspects. | ||
| * {{{ | ||
| * test("foo") { assert(42, equalTo(42)) } @@ ignore | ||
| * }}} | ||
| */ | ||
| final def @@( | ||
| aspect: TestAspect[R, R, Failure, Failure] | ||
| ): TestBuilder = { | ||
| toSpec = toSpec @@ aspect | ||
| this | ||
| } | ||
| } | ||
|
|
||
| // init SpecBuilder for this test class | ||
| private var stack: List[SuiteBuilder] = SuiteBuilder(self.getClass.getSimpleName.stripSuffix("$")) :: Nil | ||
| // to prevent calling tests inside tests | ||
| // there should be no tests constructions after spec was built | ||
| private var specBuilt = false | ||
|
|
||
| /** | ||
| * Builds a suite containing a number of other specs. | ||
| */ | ||
| final def suite(label: String)(specs: => SpecBuilder): SuiteBuilder = { | ||
| if (specBuilt) | ||
| throw new InAnotherTestException("Suite", label) | ||
| val _oldStack = stack | ||
| val builder = SuiteBuilder(label) | ||
| stack.head.nested = stack.head.nested :+ builder | ||
| stack = builder :: stack | ||
| specs | ||
| stack = _oldStack | ||
| builder | ||
| } | ||
|
|
||
| /** | ||
| * Builds a spec with a single pure test. | ||
| */ | ||
| final def test(label: String)(assertion: => TestResult): TestBuilder = { | ||
| if (specBuilt) | ||
| throw new InAnotherTestException("Test", label) | ||
| val test = zio.test.test(label)(assertion) | ||
| val builder = TestBuilder(label, test) | ||
| stack.head.nested = stack.head.nested :+ builder | ||
| builder | ||
| } | ||
|
|
||
| /** | ||
| * Builds a spec with a single effectful test. | ||
| */ | ||
| final def testM(label: String)(assertion: => ZIO[R, Failure, TestResult]): TestBuilder = { | ||
| if (specBuilt) | ||
| throw new InAnotherTestException("Test", label) | ||
| val test = zio.test.testM(label)(assertion) | ||
| val builder = TestBuilder(label, test) | ||
| stack.head.nested = stack.head.nested :+ builder | ||
| builder | ||
| } | ||
|
|
||
| final override def spec: ZSpec[Environment, Failure] = { | ||
| specBuilt = true | ||
| stack.head.toSpec.provideLayerShared(layer.mapError(TestFailure.fail)) | ||
| } | ||
|
|
||
| override def aspects: List[TestAspect[Nothing, TestEnvironment, Nothing, Any]] = | ||
| List(TestAspect.timeoutWarning(60.seconds)) | ||
|
|
||
| override def runner: TestRunner[TestEnvironment, Any] = | ||
| defaultTestRunner | ||
|
|
||
| /** | ||
| * Returns an effect that executes a given spec, producing the results of the execution. | ||
| */ | ||
| private[zio] override def runSpec( | ||
| spec: ZSpec[Environment, Failure] | ||
| ): URIO[TestLogger with Clock, ExecutedSpec[Failure]] = | ||
| runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is some overlap here with what Balasz is doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will speak with him. Maybe renaming would be good to call this
DefaultMutableRunnableSpec -> MutableRunnableSpec
DefaultMutableRunnableSpec -> CustomMutableRunnableSpec
to have it consistent. What do you think? @jdegoes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe so.