diff --git a/crates/nu-utils/standard_library/README.md b/crates/nu-utils/standard_library/README.md index f2fd9fc666..5d303ffac4 100644 --- a/crates/nu-utils/standard_library/README.md +++ b/crates/nu-utils/standard_library/README.md @@ -52,11 +52,10 @@ use /path/to/standard_library/std.nu ## :pencil2: contribute to the standard library ### :wrench: add new commands -- add new standard commands to [`std.nu`](std.nu) -- add associated tests to [`tests.nu`](tests.nu) - - define a new `test_` before the `main` +- add new standard commands to [`std.nu`](std.nu), or preferably create a new submodule. +- add associated tests to [`test_std.nu`](tests_std.nu) or preferably to `test_.nu`. + - define a new exported (!) `test_` command - import the `assert` functions you need at the top of the functions, e.g. `use std.nu "assert eq"` - - add a call to `test_` at the bottom of the `main` ### :test_tube: run the tests the following call should return no errors diff --git a/crates/nu-utils/standard_library/test_dirs.nu b/crates/nu-utils/standard_library/test_dirs.nu new file mode 100644 index 0000000000..bb1f568a83 --- /dev/null +++ b/crates/nu-utils/standard_library/test_dirs.nu @@ -0,0 +1,63 @@ +use std.nu + +def "myassert" [ + predicate: bool + msg?:string = "..." + --verbose = false (-v) # enable to see successful tests +] { + if not $predicate { + let span = (metadata $predicate).span + error make {msg: $"Assertion failed checking ($msg)", + label: {text: "Condition not true" start: $span.start end: $span.end}} + } else { + if $verbose { + echo $"check succeeded: ($msg)" + } + } +} + +export def test_dirs_command [] { + # need some directories to play with + let base_path = (($nu.temp-path) | path join $"test_dirs_(random uuid)" | path expand ) + let path_a = ($base_path | path join "a") + let path_b = ($base_path | path join "b") + + try { + mkdir $base_path $path_a $path_b + cd $base_path + use dirs.nu + + myassert (1 == ($env.DIRS_LIST | length)) "list is just pwd after initialization" + myassert ($base_path == $env.DIRS_LIST.0) "list is just pwd after initialization" + + dirs next + myassert ($base_path == $env.DIRS_LIST.0) "next wraps at end of list" + + dirs prev + myassert ($base_path == $env.DIRS_LIST.0) "prev wraps at top of list" + + dirs add $path_b $path_a + myassert ($path_b == $env.PWD) "add changes PWD to first added dir" + myassert (3 == ($env.DIRS_LIST | length)) "add in fact adds to list" + myassert ($path_a == $env.DIRS_LIST.2) "add in fact adds to list" + + dirs next 2 + myassert ($base_path == $env.PWD) "next wraps at end of list" + + dirs prev 1 + myassert ($path_a == $env.PWD) "prev wraps at start of list" + + dirs drop + myassert (2 == ($env.DIRS_LIST | length)) "drop removes from list" + myassert ($base_path == $env.PWD) "drop changes PWD to next in list (after dropped element)" + + myassert ((dirs show) == [[active path]; [true $base_path] [false $path_b]]) "show table contains expected information" + } catch { |error| + $error | debug + true + } + + cd $base_path + cd .. + rm -r $base_path +} \ No newline at end of file diff --git a/crates/nu-utils/standard_library/test_std.nu b/crates/nu-utils/standard_library/test_std.nu new file mode 100644 index 0000000000..802728f751 --- /dev/null +++ b/crates/nu-utils/standard_library/test_std.nu @@ -0,0 +1,64 @@ +use std.nu + +export def test_assert [] { + def test_failing [code: closure] { + let code_did_run = (try { do $code; true } catch { false }) + + if $code_did_run { + error make {msg: (view source $code)} + } + } + + std assert true + std assert (1 + 2 == 3) + test_failing { std assert false } + test_failing { std assert (1 + 2 == 4) } + + std assert eq (1 + 2) 3 + test_failing { std assert eq 1 "foo" } + 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) } +} + +export def test_match [] { + use std.nu assert + + let branches = { + 1: { -1 } + 2: { -2 } + } + + assert ((std match 1 $branches) == -1) + assert ((std match 2 $branches) == -2) + assert ((std match 3 $branches) == $nothing) + + assert ((std match 1 $branches { 0 }) == -1) + assert ((std match 2 $branches { 0 }) == -2) + assert ((std match 3 $branches { 0 }) == 0) +} + +export def test_path_add [] { + use std.nu "assert eq" + + with-env [PATH []] { + assert eq $env.PATH [] + + std path add "/foo/" + assert eq $env.PATH ["/foo/"] + + std path add "/bar/" "/baz/" + assert eq $env.PATH ["/bar/", "/baz/", "/foo/"] + + let-env PATH = [] + + std path add "foo" + std path add "bar" "baz" --append + assert eq $env.PATH ["foo", "bar", "baz"] + + assert eq (std path add "fooooo" --ret) ["fooooo", "foo", "bar", "baz"] + assert eq $env.PATH ["fooooo", "foo", "bar", "baz"] + } +} diff --git a/crates/nu-utils/standard_library/tests.nu b/crates/nu-utils/standard_library/tests.nu index 6437a14c2f..e023471978 100644 --- a/crates/nu-utils/standard_library/tests.nu +++ b/crates/nu-utils/standard_library/tests.nu @@ -1,135 +1,19 @@ -use std.nu - -def test_assert [] { - def test_failing [code: closure] { - let code_did_run = (try { do $code; true } catch { false }) - - if $code_did_run { - error make {msg: (view source $code)} - } - } - - std assert true - std assert (1 + 2 == 3) - test_failing { std assert false } - test_failing { std assert (1 + 2 == 4) } - - std assert eq (1 + 2) 3 - test_failing { std assert eq 1 "foo" } - 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) } -} - -def tests [] { - use std.nu assert - - let branches = { - 1: { -1 } - 2: { -2 } - } - - assert ((std match 1 $branches) == -1) - assert ((std match 2 $branches) == -2) - assert ((std match 3 $branches) == $nothing) - - assert ((std match 1 $branches { 0 }) == -1) - assert ((std match 2 $branches { 0 }) == -2) - assert ((std match 3 $branches { 0 }) == 0) -} - -def test_path_add [] { - use std.nu "assert eq" - - with-env [PATH []] { - assert eq $env.PATH [] - - std path add "/foo/" - assert eq $env.PATH ["/foo/"] - - std path add "/bar/" "/baz/" - assert eq $env.PATH ["/bar/", "/baz/", "/foo/"] - - let-env PATH = [] - - std path add "foo" - std path add "bar" "baz" --append - assert eq $env.PATH ["foo", "bar", "baz"] - - assert eq (std path add "fooooo" --ret) ["fooooo", "foo", "bar", "baz"] - assert eq $env.PATH ["fooooo", "foo", "bar", "baz"] - } -} - - -def test_dirs [] { - - def "myassert" [ - predicate: bool - msg?:string = "..." - --verbose = false (-v) # enable to see successful tests - ] { - if not $predicate { - let span = (metadata $predicate).span - error make {msg: $"Assertion failed checking ($msg)", - label: {text: "Condition not true" start: $span.start end: $span.end}} - } else { - if $verbose { - echo $"check succeeded: ($msg)" - } - } - } - - # need some directories to play with - let base_path = (($nu.temp-path) | path join $"test_dirs_(random uuid)" | path expand ) - let path_a = ($base_path | path join "a") - let path_b = ($base_path | path join "b") - - try { - mkdir $base_path $path_a $path_b - cd $base_path - use dirs.nu - - myassert (1 == ($env.DIRS_LIST | length)) "list is just pwd after initialization" - myassert ($base_path == $env.DIRS_LIST.0) "list is just pwd after initialization" - - dirs next - myassert ($base_path == $env.DIRS_LIST.0) "next wraps at end of list" - - dirs prev - myassert ($base_path == $env.DIRS_LIST.0) "prev wraps at top of list" - - dirs add $path_b $path_a - myassert ($path_b == $env.PWD) "add changes PWD to first added dir" - myassert (3 == ($env.DIRS_LIST | length)) "add in fact adds to list" - myassert ($path_a == $env.DIRS_LIST.2) "add in fact adds to list" - - dirs next 2 - myassert ($base_path == $env.PWD) "next wraps at end of list" - - dirs prev 1 - myassert ($path_a == $env.PWD) "prev wraps at start of list" - - dirs drop - myassert (2 == ($env.DIRS_LIST | length)) "drop removes from list" - myassert ($base_path == $env.PWD) "drop changes PWD to next in list (after dropped element)" - - myassert ((dirs show) == [[active path]; [true $base_path] [false $path_b]]) "show table contains expected information" - } catch { |error| - $error | debug - true - } - - cd $base_path - cd .. - rm -r $base_path -} - def main [] { - test_assert - tests - test_path_add - test_dirs + for test_file in (ls ($env.FILE_PWD | path join "test_*.nu") -f | get name) { + let $module_name = ($test_file | path parse).stem + + echo $"(ansi default)INFO Run tests in ($module_name)(ansi reset)" + let tests = ( + nu -c $'use ($test_file) *; $nu.scope.commands | to nuon' + | from nuon + | where module_name == $module_name + | where ($it.name | str starts-with "test_") + | get name + ) + + for test_case in $tests { + echo $"(ansi default_dimmed)DEBUG Run test ($module_name)/($test_case)(ansi reset)" + nu -c $'use ($test_file) ($test_case); ($test_case)' + } + } }