diff --git a/crates/nu-std/lib/testing.nu b/crates/nu-std/lib/testing.nu index d175c0b627..eb3e9576ca 100644 --- a/crates/nu-std/lib/testing.nu +++ b/crates/nu-std/lib/testing.nu @@ -48,12 +48,61 @@ export def assert [ msg: ($message | default "Assertion failed."), label: ($error_label | default { text: "It is not true.", - start: (metadata $condition).span.start, - end: (metadata $condition).span.end + 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 "assert 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 diff --git a/crates/nu-std/tests/test_asserts.nu b/crates/nu-std/tests/test_asserts.nu index 353c21f945..e2c6f6dd81 100644 --- a/crates/nu-std/tests/test_asserts.nu +++ b/crates/nu-std/tests/test_asserts.nu @@ -7,6 +7,13 @@ export def test_assert [] { assert error { assert (1 + 2 == 4) } } +export def test_assert_not [] { + assert not false + assert not (1 + 2 == 4) + assert error { assert not true } + assert error { assert not (1 + 2 == 3) } +} + export def test_assert_equal [] { assert equal (1 + 2) 3 assert equal (0.1 + 0.2 | into string | into decimal) 0.3 # 0.30000000000000004 == 0.3 diff --git a/crates/nu-std/tests/test_logger.nu b/crates/nu-std/tests/test_logger.nu index 6a9c34cb87..2cb2b79f5d 100644 --- a/crates/nu-std/tests/test_logger.nu +++ b/crates/nu-std/tests/test_logger.nu @@ -114,8 +114,8 @@ def "assert custom message contains" [ tested_str ] { let output = (run custom $system_level $format $message_level) - assert ($output | str contains $tested_str) - assert ($output | str contains "test message") + assert str contains $output $tested_str + assert str contains $output "test message" } def "assert custom message not contains" [ @@ -125,8 +125,8 @@ def "assert custom message not contains" [ tested_str ] { let output = (run custom $system_level $format $message_level) - assert (not ($output | str contains $tested_str)) - assert ($output | str contains "test message") + assert not ($output | str contains $tested_str) + assert str contains $output "test message" } def "assert no custom message" [