mirror of
https://github.com/nushell/nushell.git
synced 2024-12-02 21:34:48 +01:00
9ef1203ef9
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description Test runner now uses annotations instead of magic function names to pick up code to run. Additionally skipping tests is now done on annotation level so skipping and unskipping a test no longer requires changes to the test code In order for a function to be picked up by the test runner it needs to meet following criteria: * Needs to be private (all exported functions are ignored) * Needs to contain one of valid annotations (and only the annotation) directly above the definition, all other comments are ignored Following are considered valid annotations: * \# test * \# test-skip * \# before-all * \# before-each * \# after-each * \# after-all # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
256 lines
6.8 KiB
Plaintext
256 lines
6.8 KiB
Plaintext
##################################################################################
|
||
#
|
||
# Assert commands.
|
||
#
|
||
##################################################################################
|
||
|
||
# Universal assert command
|
||
#
|
||
# If the condition is not true, it generates an error.
|
||
#
|
||
# # Example
|
||
#
|
||
# ```nushell
|
||
# >_ assert (3 == 3)
|
||
# >_ assert (42 == 3)
|
||
# Error:
|
||
# × Assertion failed:
|
||
# ╭─[myscript.nu:11:1]
|
||
# 11 │ assert (3 == 3)
|
||
# 12 │ assert (42 == 3)
|
||
# · ───┬────
|
||
# · ╰── It is not true.
|
||
# 13 │
|
||
# ╰────
|
||
# ```
|
||
#
|
||
# The --error-label flag can be used if you want to create a custom assert command:
|
||
# ```
|
||
# def "assert even" [number: int] {
|
||
# assert ($number mod 2 == 0) --error-label {
|
||
# start: (metadata $number).span.start,
|
||
# end: (metadata $number).span.end,
|
||
# text: $"($number) is not an even number",
|
||
# }
|
||
# }
|
||
# ```
|
||
export def main [
|
||
condition: bool, # Condition, which should be true
|
||
message?: string, # Optional error message
|
||
--error-label: record # Label for `error make` if you want to create a custom assert
|
||
] {
|
||
if $condition { return }
|
||
let span = (metadata $condition).span
|
||
error make {
|
||
msg: ($message | default "Assertion failed."),
|
||
label: ($error_label | default {
|
||
text: "It is not true.",
|
||
start: $span.start,
|
||
end: $span.end
|
||
})
|
||
}
|
||
}
|
||
|
||
|
||
# Negative assertion
|
||
#
|
||
# If the condition is not false, it generates an error.
|
||
#
|
||
# # Examples
|
||
#
|
||
# >_ assert (42 == 3)
|
||
# >_ assert (3 == 3)
|
||
# Error:
|
||
# × Assertion failed:
|
||
# ╭─[myscript.nu:11:1]
|
||
# 11 │ assert (42 == 3)
|
||
# 12 │ assert (3 == 3)
|
||
# · ───┬────
|
||
# · ╰── It is not false.
|
||
# 13 │
|
||
# ╰────
|
||
#
|
||
#
|
||
# The --error-label flag can be used if you want to create a custom assert command:
|
||
# ```
|
||
# def "assert not even" [number: int] {
|
||
# assert not ($number mod 2 == 0) --error-label {
|
||
# start: (metadata $number).span.start,
|
||
# end: (metadata $number).span.end,
|
||
# text: $"($number) is an even number",
|
||
# }
|
||
# }
|
||
# ```
|
||
#
|
||
export def not [
|
||
condition: bool, # Condition, which should be false
|
||
message?: string, # Optional error message
|
||
--error-label: record # Label for `error make` if you want to create a custom assert
|
||
] {
|
||
if $condition {
|
||
let span = (metadata $condition).span
|
||
error make {
|
||
msg: ($message | default "Assertion failed."),
|
||
label: ($error_label | default {
|
||
text: "It is not false.",
|
||
start: $span.start,
|
||
end: $span.end
|
||
})
|
||
}
|
||
}
|
||
}
|
||
|
||
# Assert that executing the code generates an error
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert error {|| missing_command} # passes
|
||
# > assert error {|| 12} # fails
|
||
export def error [
|
||
code: closure,
|
||
message?: string
|
||
] {
|
||
let error_raised = (try { do $code; false } catch { true })
|
||
main ($error_raised) $message --error-label {
|
||
start: (metadata $code).span.start
|
||
end: (metadata $code).span.end
|
||
text: $"There were no error during code execution: (view source $code)"
|
||
}
|
||
}
|
||
|
||
# Assert $left == $right
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert equal 1 1 # passes
|
||
# > assert equal (0.1 + 0.2) 0.3
|
||
# > assert equal 1 2 # fails
|
||
export def equal [left: any, right: any, message?: string] {
|
||
main ($left == $right) $message --error-label {
|
||
start: (metadata $left).span.start
|
||
end: (metadata $right).span.end
|
||
text: $"They are not equal. Left = '($left)'. Right = '($right)'."
|
||
}
|
||
}
|
||
|
||
# Assert $left != $right
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert not equal 1 2 # passes
|
||
# > assert not equal 1 "apple" # passes
|
||
# > assert not equal 7 7 # fails
|
||
export def "not equal" [left: any, right: any, message?: string] {
|
||
main ($left != $right) $message --error-label {
|
||
start: (metadata $left).span.start
|
||
end: (metadata $right).span.end
|
||
text: $"They both are '($left)'."
|
||
}
|
||
}
|
||
|
||
# Assert $left <= $right
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert less or equal 1 2 # passes
|
||
# > assert less or equal 1 1 # passes
|
||
# > assert less or equal 1 0 # fails
|
||
export def "less or equal" [left: any, right: any, message?: string] {
|
||
main ($left <= $right) $message --error-label {
|
||
start: (metadata $left).span.start
|
||
end: (metadata $right).span.end
|
||
text: $"Left: '($left)', Right: '($right)'"
|
||
}
|
||
}
|
||
|
||
# Assert $left < $right
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert less 1 2 # passes
|
||
# > assert less 1 1 # fails
|
||
export def less [left: any, right: any, message?: string] {
|
||
main ($left < $right) $message --error-label {
|
||
start: (metadata $left).span.start
|
||
end: (metadata $right).span.end
|
||
text: $"Left: '($left)', Right: '($right)'"
|
||
}
|
||
}
|
||
|
||
# Assert $left > $right
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert greater 2 1 # passes
|
||
# > assert greater 2 2 # fails
|
||
export def greater [left: any, right: any, message?: string] {
|
||
main ($left > $right) $message --error-label {
|
||
start: (metadata $left).span.start
|
||
end: (metadata $right).span.end
|
||
text: $"Left: '($left)', Right: '($right)'"
|
||
}
|
||
}
|
||
|
||
# Assert $left >= $right
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert greater or equal 2 1 # passes
|
||
# > assert greater or equal 2 2 # passes
|
||
# > assert greater or equal 1 2 # fails
|
||
export def "greater or equal" [left: any, right: any, message?: string] {
|
||
main ($left >= $right) $message --error-label {
|
||
start: (metadata $left).span.start
|
||
end: (metadata $right).span.end
|
||
text: $"Left: '($left)', Right: '($right)'"
|
||
}
|
||
}
|
||
|
||
alias "core length" = length
|
||
# Assert length of $left is $right
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert length [0, 0] 2 # passes
|
||
# > assert length [0] 3 # fails
|
||
export def length [left: list, right: int, message?: string] {
|
||
main (($left | core length) == $right) $message --error-label {
|
||
start: (metadata $left).span.start
|
||
end: (metadata $right).span.end
|
||
text: $"Length of ($left) is ($left | core length), not ($right)"
|
||
}
|
||
}
|
||
|
||
alias "core str contains" = str contains
|
||
# Assert that ($left | str contains $right)
|
||
#
|
||
# For more documentation see the assert command
|
||
#
|
||
# # Examples
|
||
#
|
||
# > assert str contains "arst" "rs" # passes
|
||
# > assert str contains "arst" "k" # fails
|
||
export def "str contains" [left: string, right: string, message?: string] {
|
||
main ($left | core str contains $right) $message --error-label {
|
||
start: (metadata $left).span.start
|
||
end: (metadata $right).span.end
|
||
text: $"'($left)' does not contain '($right)'."
|
||
}
|
||
}
|