mirror of
https://github.com/nushell/nushell.git
synced 2025-02-02 19:50:05 +01:00
std.nu: Rewrite assert method (#8405)
It is a common pattern to add message to assert. Also the error message was not so helpful. (See the difference in the documentation) --------- Co-authored-by: Mate Farkas <Mate.Farkas@oneidentity.com>
This commit is contained in:
parent
24ee381fea
commit
12652f897a
@ -18,88 +18,119 @@ export-env {
|
|||||||
|
|
||||||
# ---------------- builtin std functions --------------------
|
# ---------------- builtin std functions --------------------
|
||||||
|
|
||||||
def _assert [
|
def _assertion-error [start, end, label, message?: string] {
|
||||||
cond: bool
|
error make {
|
||||||
msg: string
|
msg: ($message | default "Assertion failed."),
|
||||||
] {
|
label: {
|
||||||
if not $cond {
|
text: $label,
|
||||||
error make {msg: $msg}
|
start: $start,
|
||||||
|
end: $end
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# ```nushell
|
# ```nushell
|
||||||
# >_ assert ($a == 3)
|
# >_ assert (3 == 3)
|
||||||
# >_ assert ($a != 3)
|
# >_ assert (42 == 3)
|
||||||
# Error:
|
# Error:
|
||||||
# × condition given to `assert` does not hold
|
# × Assertion failed:
|
||||||
# ╭─[entry #12:5:1]
|
# ╭─[myscript.nu:11:1]
|
||||||
# 5 │ if not $cond {
|
# 11 │ assert (3 == 3)
|
||||||
# 6 │ error make {msg: $msg}
|
# 12 │ assert (42 == 3)
|
||||||
# · ─────┬────
|
# · ───┬────
|
||||||
# · ╰── originates from here
|
# · ╰── It is not true.
|
||||||
# 7 │ }
|
# 13 │
|
||||||
# ╰────
|
# ╰────
|
||||||
# ```
|
# ```
|
||||||
export def assert [cond: bool] {
|
export def assert [cond: bool, message?: string] {
|
||||||
_assert $cond "condition given to `assert` does not hold"
|
if $cond { return }
|
||||||
|
let span = (metadata $cond).span
|
||||||
|
_assertion-error $span.start $span.end "It is not true." $message
|
||||||
}
|
}
|
||||||
|
|
||||||
# ```nushell
|
# ```nushell
|
||||||
# >_ assert_eq $a "a string"
|
# ❯ assert eq 3 "a string"
|
||||||
# Error:
|
# Error:
|
||||||
# × left and right operand of `assert eq` should have the same type
|
# × Assertion failed.
|
||||||
# ╭─[entry #12:5:1]
|
# ╭─[entry #13:1:1]
|
||||||
# 5 │ if not $cond {
|
# 1 │ assert eq 3 "a string"
|
||||||
# 6 │ error make {msg: $msg}
|
# · ──────┬─────
|
||||||
# · ─────┬────
|
# · ╰── Different types cannot be equal: int <-> string.
|
||||||
# · ╰── originates from here
|
|
||||||
# 7 │ }
|
|
||||||
# ╰────
|
# ╰────
|
||||||
#
|
#
|
||||||
# >_ assert_eq $a 3
|
#
|
||||||
# >_ assert_eq $a 1
|
# ❯ assert eq 3 3
|
||||||
|
# ❯ assert eq 3 1
|
||||||
# Error:
|
# Error:
|
||||||
# × left is not equal to right
|
# × Assertion failed.
|
||||||
# ╭─[entry #12:5:1]
|
# ╭─[entry #14:1:1]
|
||||||
# 5 │ if not $cond {
|
# 1 │ assert eq 3 1
|
||||||
# 6 │ error make {msg: $msg}
|
# · ─┬─
|
||||||
# · ─────┬────
|
# · ╰── They are not equal: 3 != 1
|
||||||
# · ╰── originates from here
|
|
||||||
# 7 │ }
|
|
||||||
# ╰────
|
# ╰────
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# 👇👇👇 BE CAREFUL! 👇👇👇
|
||||||
|
# ❯ assert ( 1 == 1.0) # passes
|
||||||
|
# ❯ assert eq 1 1.0
|
||||||
|
# Error:
|
||||||
|
# × Assertion failed.
|
||||||
|
# ╭─[entry #16:1:1]
|
||||||
|
# 1 │ assert eq 1 1.0
|
||||||
|
# · ──┬──
|
||||||
|
# · ╰── Different types cannot be equal: int <-> float.
|
||||||
|
# ╰────
|
||||||
|
#
|
||||||
# ```
|
# ```
|
||||||
export def "assert eq" [left: any, right: any] {
|
export def "assert eq" [left: any, right: any, message?: string] {
|
||||||
_assert (($left | describe) == ($right | describe)) $"left and right operand of `assert eq` should have the same type"
|
let left_type = ($left | describe)
|
||||||
_assert ($left == $right) "left is not equal to right"
|
let right_type = ($right | describe)
|
||||||
|
let left_start = (metadata $left).span.start
|
||||||
|
let right_end = (metadata $right).span.end
|
||||||
|
|
||||||
|
if ($left_type != $right_type) {
|
||||||
|
_assertion-error $left_start $right_end $"Different types cannot be equal: ($left_type) <-> ($right_type)." $message
|
||||||
|
}
|
||||||
|
if ($left != $right) {
|
||||||
|
_assertion-error $left_start $right_end $"They are not equal: ($left) != ($right)" $message
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# ```nushell
|
# ```nushell
|
||||||
# >_ assert_ne $a "a string"
|
# ❯ assert ne 1 3
|
||||||
|
# ❯ assert ne 42 42
|
||||||
# Error:
|
# Error:
|
||||||
# × left and right operand of `assert eq` should have the same type
|
# × Assertion failed.
|
||||||
# ╭─[entry #12:5:1]
|
# ╭─[entry #23:1:1]
|
||||||
# 5 │ if not $cond {
|
# 1 │ assert ne 42 42
|
||||||
# 6 │ error make {msg: $msg}
|
# · ──┬──
|
||||||
# · ─────┬────
|
# · ╰── They both are 42
|
||||||
# · ╰── originates from here
|
|
||||||
# 7 │ }
|
|
||||||
# ╰────
|
# ╰────
|
||||||
|
#
|
||||||
#
|
#
|
||||||
# >_ assert_ne $a 1
|
# 👇👇👇 BE CAREFUL! 👇👇👇
|
||||||
# >_ assert_ne $a 3
|
# ❯ assert ( 1 != "a string" ) # passes
|
||||||
|
# ❯ assert ne 1 "a string"
|
||||||
# Error:
|
# Error:
|
||||||
# × left is equal to right
|
# × Assertion failed.
|
||||||
# ╭─[entry #12:5:1]
|
# ╭─[entry #20:1:1]
|
||||||
# 5 │ if not $cond {
|
# 1 │ assert ne 1 "a string"
|
||||||
# 6 │ error make {msg: $msg}
|
# · ──────┬─────
|
||||||
# · ─────┬────
|
# · ╰── They are not equal, although they have different types: int <-> string.
|
||||||
# · ╰── originates from here
|
|
||||||
# 7 │ }
|
|
||||||
# ╰────
|
# ╰────
|
||||||
# ```
|
# ```
|
||||||
export def "assert ne" [left: any, right: any] {
|
export def "assert ne" [left: any, right: any, message?: string] {
|
||||||
_assert (($left | describe) == ($right | describe)) $"left and right operand of `assert eq` should have the same type"
|
let left_type = ($left | describe)
|
||||||
_assert ($left != $right) "left is equal to right"
|
let right_type = ($right | describe)
|
||||||
|
let left_start = (metadata $left).span.start
|
||||||
|
let right_end = (metadata $right).span.end
|
||||||
|
|
||||||
|
if (($left | describe) != ($right | describe)) {
|
||||||
|
_assertion-error $left_start $right_end $"They have different types: ($left_type) <-> ($right_type)." $message
|
||||||
|
}
|
||||||
|
if ($left == $right) {
|
||||||
|
_assertion-error $left_start $right_end $"They both are ($left)" $message
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# ```nushell
|
# ```nushell
|
||||||
|
Loading…
Reference in New Issue
Block a user