|
| 1 | +def _assert [ |
| 2 | + cond: bool |
| 3 | + msg: string |
| 4 | +] { |
| 5 | + if not $cond { |
| 6 | + error make {msg: $msg} |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +# ```nushell |
| 11 | +# >_ assert ($a == 3) |
| 12 | +# >_ assert ($a != 3) |
| 13 | +# Error: |
| 14 | +# × condition given to `assert` does not hold |
| 15 | +# ╭─[entry #12:5:1] |
| 16 | +# 5 │ if not $cond { |
| 17 | +# 6 │ error make {msg: $msg} |
| 18 | +# · ─────┬──── |
| 19 | +# · ╰── originates from here |
| 20 | +# 7 │ } |
| 21 | +# ╰──── |
| 22 | +# ``` |
| 23 | +export def assert [cond: bool] { |
| 24 | + _assert $cond "condition given to `assert` does not hold" |
| 25 | +} |
| 26 | + |
| 27 | +# ```nushell |
| 28 | +# >_ assert_eq $a "a string" |
| 29 | +# Error: |
| 30 | +# × left and right operand of `assert_eq` should have the same type |
| 31 | +# ╭─[entry #12:5:1] |
| 32 | +# 5 │ if not $cond { |
| 33 | +# 6 │ error make {msg: $msg} |
| 34 | +# · ─────┬──── |
| 35 | +# · ╰── originates from here |
| 36 | +# 7 │ } |
| 37 | +# ╰──── |
| 38 | +# |
| 39 | +# >_ assert_eq $a 3 |
| 40 | +# >_ assert_eq $a 1 |
| 41 | +# Error: |
| 42 | +# × left is not equal to right |
| 43 | +# ╭─[entry #12:5:1] |
| 44 | +# 5 │ if not $cond { |
| 45 | +# 6 │ error make {msg: $msg} |
| 46 | +# · ─────┬──── |
| 47 | +# · ╰── originates from here |
| 48 | +# 7 │ } |
| 49 | +# ╰──── |
| 50 | +# ``` |
| 51 | +export def assert_eq [left: any, right: any] { |
| 52 | + _assert (($left | describe) == ($right | describe)) $"left and right operand of `assert_eq` should have the same type" |
| 53 | + _assert ($left == $right) "left is not equal to right" |
| 54 | +} |
| 55 | + |
| 56 | +# ```nushell |
| 57 | +# >_ assert_ne $a "a string" |
| 58 | +# Error: |
| 59 | +# × left and right operand of `assert_eq` should have the same type |
| 60 | +# ╭─[entry #12:5:1] |
| 61 | +# 5 │ if not $cond { |
| 62 | +# 6 │ error make {msg: $msg} |
| 63 | +# · ─────┬──── |
| 64 | +# · ╰── originates from here |
| 65 | +# 7 │ } |
| 66 | +# ╰──── |
| 67 | +# |
| 68 | +# >_ assert_ne $a 1 |
| 69 | +# >_ assert_ne $a 3 |
| 70 | +# Error: |
| 71 | +# × left is equal to right |
| 72 | +# ╭─[entry #12:5:1] |
| 73 | +# 5 │ if not $cond { |
| 74 | +# 6 │ error make {msg: $msg} |
| 75 | +# · ─────┬──── |
| 76 | +# · ╰── originates from here |
| 77 | +# 7 │ } |
| 78 | +# ╰──── |
| 79 | +# ``` |
| 80 | +export def assert_ne [left: any, right: any] { |
| 81 | + _assert (($left | describe) == ($right | describe)) $"left and right operand of `assert_eq` should have the same type" |
| 82 | + _assert ($left != $right) "left is equal to right" |
| 83 | +} |
| 84 | + |
| 85 | +# ```nushell |
| 86 | +# >_ let branches = { |
| 87 | +# ))) 1: { print "this is the 1st branch"} |
| 88 | +# ))) 2: { print "this is the 2nd branch" } |
| 89 | +# ))) 3: { print "this is the 3rd branch" } |
| 90 | +# ))) 4: { print "this is the 4th branch" } |
| 91 | +# ))) } |
| 92 | +# |
| 93 | +# >_ match 1 $branches |
| 94 | +# ))) match 2 $branches |
| 95 | +# ))) match 3 $branches |
| 96 | +# ))) match 4 $branches |
| 97 | +# ))) match 5 $branches |
| 98 | +# this is the 1st branch |
| 99 | +# this is the 2nd branch |
| 100 | +# this is the 3rd branch |
| 101 | +# this is the 4th branch |
| 102 | +# |
| 103 | +# >_ match 1 $branches { "this is the default branch" } |
| 104 | +# ))) match 2 $branches { "this is the default branch" } |
| 105 | +# ))) match 3 $branches { "this is the default branch" } |
| 106 | +# ))) match 4 $branches { "this is the default branch" } |
| 107 | +# ))) match 5 $branches { "this is the default branch" } |
| 108 | +# this is the 1st branch |
| 109 | +# this is the 2nd branch |
| 110 | +# this is the 3rd branch |
| 111 | +# this is the 4th branch |
| 112 | +# this is the default branch |
| 113 | +# ``` |
| 114 | +export def match [ |
| 115 | + input:string |
| 116 | + matchers:record |
| 117 | + default?: block |
| 118 | +] { |
| 119 | + if (($matchers | get -i $input) != null) { |
| 120 | + $matchers | get $input | do $in |
| 121 | + } else if ($default != null) { |
| 122 | + do $default |
| 123 | + } |
| 124 | +} |
0 commit comments