Skip to content

Commit

Permalink
Make baseline tests more assertive given true defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
jultty committed Nov 14, 2023
1 parent c5a8686 commit 3f7f605
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ Types of fixtures:
> Functional test-local fixtures are desirable since they are easy to reason about. Try to use functional test-local fixtures when possible, and only resort to reusable or ad-hoc fixtures when necessary.

```scala
import java.nio.file._

Expand All @@ -196,7 +195,7 @@ class FunFixture extends munit.FunSuite {
}
```

This can now be used to compose multiple fixtures into a single one:
> Use `FunFixture.map2` to compose multiple fixtures into a single fixture.
```scala
// Fixture with access to two temporary files
Expand All @@ -211,9 +210,7 @@ files2.test("two") {
}
```

#### Reusable test-local fixtures
#### Reusable suite-local fixtures
#### Ad-hoc suite-local fixtures
See [MUnit docs](https://scalameta.org/munit/docs/fixtures.html#reusable-test-local-fixtures) for Reusable test-local fixtures, Reusable suite-local fixtures and Ad-hoc suite-local fixtures.

### Avoiding stateful operations in the constructor
```scala
Expand All @@ -231,6 +228,34 @@ class MySuite extends munit.FunSuite {

> For example, IDEs like IntelliJ may load the class to discover the names of the test cases that are available.[^6]
### Clues

> Use `clue()` to include optional clues in the boolean condition based on values in the expression.
```scala
assert(clue(a) > clue(b))
```

Add this to `build.sbt` to prevent errors where the name of the variable is not shown in the clue:

```scala
scalacOptions += "-Yrangepos"
```

### Assertions

- `assert(bool)`
- `assertEquals(any, any)`
- > Comparing two values of different types is a compile error.
- > It's a compile error even if the comparison is true at runtime.
- > It's OK to compare two types as long as one argument is a subtype of the other type.
- `assertNotEquals(any, any)`
- `assertNoDiff(string, string)` compare two multiline strings
- `intercept(exception)` expect a particular exception to be thrown
- `interceptMessage(msg)` expect thrown exception to have a specific error message
- `fail()` fail the test immediately
- `compileErrors(string)` assert code snippet fails with a specific compile-time error message

## Tooling
### Suppress a WartRemover warning
```scala
Expand All @@ -255,6 +280,7 @@ Suppress `info` level logging when running and watching runs:
## See also
- <https://scalac.io/blog/scala-isnt-hard-how-to-master-scala-step-by-step/>
- <https://scalameta.org/munit/docs/getting-started.html>
- <https://scalameta.org/munit/docs/assertions.html>

## References
[^1]: <https://scalac.io/blog/why-use-scala/>
Expand All @@ -263,3 +289,4 @@ Suppress `info` level logging when running and watching runs:
[^4]: <https://docs.scala-lang.org/scala3/book/taste-vars-data-types.html>
[^5]: <https://docs.scala-lang.org/scala3/book/taste-control-structures.html>
[^6]: <https://scalameta.org/munit/docs/fixtures.html>

1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ lazy val root = project
run / watchLogLevel := Level.Warn,
test / watchLogLevel := Level.Warn,
Global / onChangedBuildSource := ReloadOnSourceChanges,
test / watchBeforeCommand := Watch.clearScreen,

libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test,

Expand Down
16 changes: 8 additions & 8 deletions src/test/scala/Suite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ import scala.concurrent.duration.Duration

abstract class BaseSuite extends munit.FunSuite {
override val munitTimeout = Duration(10, "sec")
val base: Boolean = true
val on_base: String = "on base"
}

class FunFixture extends BaseSuite {

var check: Boolean = _
var on_setup: String = ""

val fixture = FunFixture[Boolean](
setup = { _ => check = true; base },
teardown = { (c: Boolean) => var c = false },
val fixture = FunFixture[String](
setup = { _ => on_setup = "on setup"; on_setup },
teardown = { _ => on_setup = "" },
)

test("base suite is extended") {
assert(base)
assertEquals(clue(on_base), "on base")
}

test(".fail test fails".fail) {
assertEquals(1, 0)
}

fixture.test("fixture check is set during setup") { (check: Boolean) =>
assert(check)
fixture.test("fixture check is set during setup") { _ =>
assertEquals(clue(on_setup), "on setup")
}
}

0 comments on commit 3f7f605

Please sign in to comment.