Skip to content
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

update testing doc #1422

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
minor fixes
- `use std assert` at the start
- consistently call `assert ...` instead of `std assert ...`
- split "command" and output blocks
- merge `span` and `end` into `span` for the last example
  • Loading branch information
amtoine committed May 29, 2024
commit c212f398bb840e0147d80ca3b5801cffe1a5db11
79 changes: 51 additions & 28 deletions book/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,43 @@

## Assert commands

The foundation for every assertion is the `std assert` command. If the condition is not true, it makes an error. For example:
Nushell provides a set of "assertion" commands in the standard library.
One could use built-in equality / order tests such as `==` or `<=` or more complex commands and throw errors manually when an expected condition fails, but using what the standard library has to offer is arguably easier!

In the following, it will be assumed that the `std assert` module has been imported inside the current scope
```nushell
use std assert
```

The foundation for every assertion is the `std assert` command. If the condition is not true, it makes an error.

```nu
❯ use std assert
❯ std assert (1 == 2)
assert (1 == 2)
```
```
Error:
× Assertion failed.
╭─[entry #13:1:1]
1 │ std assert (1 == 2)
· ───┬──
· ╰── It is not true.
1 │ assert (1 == 2)
· ───┬──
· ╰── It is not true.
╰────
```

Optionally, a message can be set to show the intention of the assert command, what went wrong or what was expected:

```nu
❯ std assert ($a == 19) $"The lockout code is wrong, received: ($a)"
let a = 0
assert ($a == 19) $"The lockout code is wrong, received: ($a)"
```
```
Error:
× The lockout code is wrong, received: 13
╭─[entry #25:1:1]
1 │ std assert ($a == 19) $"The lockout code is wrong, received: ($a)"
· ────┬───
· ╰── It is not true.
1 │ let a = 0
2 │ assert ($a == 19) $"The lockout code is wrong, received: ($a)"
· ────┬───
· ╰── It is not true.
╰────
```

Expand All @@ -34,46 +47,56 @@ There are many assert commands, which behave exactly as the base one with the pr
For example this is not so helpful without additional message:

```nu
❯ std assert ($b | str contains $a)
Error:
× Assertion failed.
╭─[entry #35:1:1]
1 │ assert ($b | str contains $a)
· ──────┬─────
· ╰── It is not true.
let a = "foo"
let b = "bar"
assert ($b | str contains $a)
```
```
Error: × Assertion failed.
╭─[entry #5:3:8]
2 │ let b = "bar"
3 │ assert ($b | str contains $a)
· ───────────┬──────────
· ╰── It is not true.
╰────
```

While with using `assert str contains`:

```nu
❯ std assert str contains $b $a
Error:
× Assertion failed.
╭─[entry #34:1:1]
1 │ assert str contains $b $a
let a = "a needle"
let b = "haystack"
assert str contains $b $a
```
```
Error: × Assertion failed.
╭─[entry #7:3:21]
2 │ let b = "bar"
3 │ assert str contains $b $a
· ──┬──
· ╰── 'haystack' does not contain 'a needle'.
· ╰─┤ This does not contain 'a needle'.
· │ value: "haystack"
╰────
```

In general for base `assert` command it is encouraged to always provide the additional message to show what went wrong. If you cannot use any built-in assert command, you can create a custom one with passing the label for [`error make`](/commands/docs/error_make.md) for the `assert` command:

```nu
def "assert even" [number: int] {
std assert ($number mod 2 == 0) --error-label {
start: (metadata $number).span.start,
end: (metadata $number).span.end,
assert ($number mod 2 == 0) --error-label {
text: $"($number) is not an even number",
span: (metadata $number).span,
}
}
```

Then you'll have your detailed custom error message:

```nu
❯ let $a = 13
❯ assert even $a
let $a = 13
assert even $a
```
```
Error:
× Assertion failed.
╭─[entry #37:1:1]
Expand Down