nushell/crates/nu-std/tests/test_iter.nu
Yethal 9ef1203ef9
Implement annotations support in test runner (#9406)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
Test runner now uses annotations instead of magic function names to pick
up code to run. Additionally skipping tests is now done on annotation
level so skipping and unskipping a test no longer requires changes to
the test code

In order for a function to be picked up by the test runner it needs to
meet following criteria:
* Needs to be private (all exported functions are ignored)
* Needs to contain one of valid annotations (and only the annotation)
directly above the definition, all other comments are ignored

Following are considered valid annotations:
* \# test
* \# test-skip
* \# before-all
* \# before-each
* \# after-each
* \# after-all

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-02 10:41:33 +02:00

135 lines
3.2 KiB
Plaintext

use std *
#[test]
def iter_find [] {
let hastack1 = [1 2 3 4 5 6 7]
let hastack2 = [nushell rust shell iter std]
let hastack3 = [nu 69 2023-04-20 "std"]
let res = ($hastack1 | iter find {|it| $it mod 2 == 0})
assert equal $res 2
let res = ($hastack2 | iter find {|it| $it starts-with 's'})
assert equal $res 'shell'
let res = ($hastack2 | iter find {|it| ($it | length) == 50})
assert equal $res null
let res = ($hastack3 | iter find {|it| (it | describe) == filesize})
assert equal $res null
}
#[test]
def iter_intersperse [] {
let res = ([1 2 3 4] | iter intersperse 0)
assert equal $res [1 0 2 0 3 0 4]
let res = ([] | iter intersperse x)
assert equal $res []
let res = ([1] | iter intersperse 5)
assert equal $res [1]
let res = ([a b c d e] | iter intersperse 5)
assert equal $res [a 5 b 5 c 5 d 5 e]
let res = (1..4 | iter intersperse 0)
assert equal $res [1 0 2 0 3 0 4]
let res = (4 | iter intersperse 1)
assert equal $res [4]
}
#[test]
def iter_scan [] {
let scanned = ([1 2 3] | iter scan 0 {|x, y| $x + $y} -n)
assert equal $scanned [1, 3, 6]
let scanned = ([1 2 3] | iter scan 0 {|x, y| $x + $y})
assert equal $scanned [0, 1, 3, 6]
let scanned = ([a b c d] | iter scan "" {|x, y| [$x, $y] | str join} -n)
assert equal $scanned ["a" "ab" "abc" "abcd"]
}
#[test]
def iter_filter_map [] {
let res = ([2 5 "4" 7] | iter filter-map {|it| $it ** 2})
assert equal $res [4 25 49]
let res = (
["3" "42" "69" "n" "x" ""]
| iter filter-map {|it| $it | into int}
)
assert equal $res [3 42 69]
}
#[test]
def iter_find_index [] {
let res = (
["iter", "abc", "shell", "around", "nushell", "std"]
| iter find-index {|x| $x starts-with 's'}
)
assert equal $res 2
let is_even = {|x| $x mod 2 == 0}
let res = ([3 5 13 91] | iter find-index $is_even)
assert equal $res (-1)
let res = (42 | iter find-index {|x| $x == 42})
assert equal $res 0
}
#[test]
def iter_zip_with [] {
let res = (
[1 2 3] | iter zip-with [2 3 4] {|a, b| $a + $b }
)
assert equal $res [3 5 7]
let res = (42 | iter zip-with [1 2 3] {|a, b| $a // $b})
assert equal $res [42]
let res = (2..5 | iter zip-with 4 {|a, b| $a * $b})
assert equal $res [8]
let res = (
[[name repo]; [rust github] [haskell gitlab]]
| iter zip-with 1.. {|data, num|
{ name: $data.name, repo: $data.repo position: $num }
}
)
assert equal $res [
[name repo position];
[rust github 1]
[haskell gitlab 2]
]
}
#[test]
def iter_flat_map [] {
let res = (
[[1 2 3] [2 3 4] [5 6 7]] | iter flat-map {|it| $it | math sum}
)
assert equal $res [6 9 18]
let res = ([1 2 3] | iter flat-map {|it| $it + ($it * 10)})
assert equal $res [11 22 33]
}
#[test]
def iter_zip_into_record [] {
let headers = [name repo position]
let values = [rust github 1]
let res = (
$headers | iter zip-into-record $values
)
assert equal $res [
[name repo position];
[rust github 1]
]
}