From 3f224db990f01b485695cd12dcd46d0db6276e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20FARKAS?= Date: Fri, 17 Mar 2023 20:59:27 +0100 Subject: [PATCH] Make assert eq, assert ne consistent with ==, != operators (#8473) fixes #8418 Co-authored-by: Mate Farkas --- crates/nu-utils/standard_library/std.nu | 43 -------------------- crates/nu-utils/standard_library/test_std.nu | 4 +- 2 files changed, 2 insertions(+), 45 deletions(-) diff --git a/crates/nu-utils/standard_library/std.nu b/crates/nu-utils/standard_library/std.nu index ea75fbf3ac..8c9a3b5b45 100644 --- a/crates/nu-utils/standard_library/std.nu +++ b/crates/nu-utils/standard_library/std.nu @@ -33,16 +33,6 @@ export def assert [cond: bool, message?: string] { } # ```nushell -# ❯ assert eq 3 "a string" -# Error: -# × Assertion failed. -# ╭─[entry #13:1:1] -# 1 │ assert eq 3 "a string" -# · ──────┬───── -# · ╰── Different types cannot be equal: int <-> string. -# ╰──── -# -# # ❯ assert eq 3 3 # ❯ assert eq 3 1 # Error: @@ -54,27 +44,11 @@ export def assert [cond: bool, message?: string] { # ╰──── # # -# 👇👇👇 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, message?: string] { - let left_type = ($left | describe) - 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 } @@ -90,28 +64,11 @@ export def "assert eq" [left: any, right: any, message?: string] { # · ──┬── # · ╰── They both are 42 # ╰──── -# -# -# 👇👇👇 BE CAREFUL! 👇👇👇 -# ❯ assert ( 1 != "a string" ) # passes -# ❯ assert ne 1 "a string" -# Error: -# × Assertion failed. -# ╭─[entry #20:1:1] -# 1 │ assert ne 1 "a string" -# · ──────┬───── -# · ╰── They are not equal, although they have different types: int <-> string. -# ╰──── # ``` export def "assert ne" [left: any, right: any, message?: string] { - let left_type = ($left | describe) - 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 } diff --git a/crates/nu-utils/standard_library/test_std.nu b/crates/nu-utils/standard_library/test_std.nu index 7b231bdc61..1c5c2d4e38 100644 --- a/crates/nu-utils/standard_library/test_std.nu +++ b/crates/nu-utils/standard_library/test_std.nu @@ -19,8 +19,8 @@ export def test_assert [] { test_failing { std assert eq (1 + 2) 4) } std assert ne (1 + 2) 4 - test_failing { std assert ne 1 "foo" } - test_failing { std assert ne (1 + 2) 3) } + std assert ne 1 "foo" + std assert ne (1 + 2) 3) } export def test_match [] {