2024-05-13 13:37:53 +00:00
|
|
|
use crate::repl::tests::{fail_test, run_test, TestResult};
|
2021-12-25 19:39:42 +00:00
|
|
|
|
2023-11-01 20:25:35 +00:00
|
|
|
#[test]
|
|
|
|
fn illegal_column_duplication() -> TestResult {
|
|
|
|
fail_test("[[lang, lang]; [nu, 100]]", "column_defined_twice")
|
|
|
|
}
|
|
|
|
|
2021-12-25 19:39:42 +00:00
|
|
|
#[test]
|
|
|
|
fn cell_path_subexpr1() -> TestResult {
|
|
|
|
run_test("([[lang, gems]; [nu, 100]]).lang | get 0", "nu")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_subexpr2() -> TestResult {
|
|
|
|
run_test("([[lang, gems]; [nu, 100]]).lang.0", "nu")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_var1() -> TestResult {
|
|
|
|
run_test("let x = [[lang, gems]; [nu, 100]]; $x.lang | get 0", "nu")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_var2() -> TestResult {
|
|
|
|
run_test("let x = [[lang, gems]; [nu, 100]]; $x.lang.0", "nu")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_simple_list() -> TestResult {
|
2022-02-09 10:58:54 +00:00
|
|
|
run_test(
|
2022-09-11 08:48:27 +00:00
|
|
|
"[[N, u, s, h, e, l, l]] | flatten | str join (char nl)",
|
2022-02-09 10:58:54 +00:00
|
|
|
"N\nu\ns\nh\ne\nl\nl",
|
|
|
|
)
|
2021-12-25 19:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_get_simple_list() -> TestResult {
|
|
|
|
run_test("[[N, u, s, h, e, l, l]] | flatten | get 0", "N")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_table_get() -> TestResult {
|
|
|
|
run_test(
|
2022-05-21 13:32:51 +00:00
|
|
|
"[[origin, people]; [Ecuador, ([[name, meal]; ['Andres', 'arepa']])]] | flatten --all | get meal.0",
|
2021-12-25 19:39:42 +00:00
|
|
|
"arepa",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_table_column_get_last() -> TestResult {
|
|
|
|
run_test(
|
2022-05-21 13:32:51 +00:00
|
|
|
"[[origin, crate, versions]; [World, ([[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten versions --all | last | get versions",
|
2021-12-25 19:39:42 +00:00
|
|
|
"0.22",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-21 13:32:51 +00:00
|
|
|
#[test]
|
|
|
|
fn flatten_should_just_flatten_one_level() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[origin, crate, versions]; [World, ([[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten crate | get crate.name.0",
|
|
|
|
"nu-cli"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_nest_table_when_all_provided() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[origin, crate, versions]; [World, ([[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten crate --all | get name.0",
|
|
|
|
"nu-cli"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-25 19:39:42 +00:00
|
|
|
#[test]
|
|
|
|
fn get_table_columns_1() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[name, age, grade]; [paul,21,a]] | columns | first",
|
|
|
|
"name",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_table_columns_2() -> TestResult {
|
2022-02-09 10:58:54 +00:00
|
|
|
run_test("[[name, age, grade]; [paul,21,a]] | columns | get 1", "age")
|
2021-12-25 19:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flatten_should_flatten_inner_table() -> TestResult {
|
|
|
|
run_test(
|
2022-05-21 13:32:51 +00:00
|
|
|
"[[[name, value]; [abc, 123]]] | flatten --all | get value.0",
|
2021-12-25 19:39:42 +00:00
|
|
|
"123",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn command_filter_reject_1() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[lang, gems]; [nu, 100]] | reject gems | to json",
|
|
|
|
r#"[
|
|
|
|
{
|
|
|
|
"lang": "nu"
|
|
|
|
}
|
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn command_filter_reject_2() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[lang, gems, grade]; [nu, 100, a]] | reject gems grade | to json",
|
|
|
|
r#"[
|
|
|
|
{
|
|
|
|
"lang": "nu"
|
|
|
|
}
|
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn command_filter_reject_3() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[lang, gems, grade]; [nu, 100, a]] | reject grade gems | to json",
|
|
|
|
r#"[
|
|
|
|
{
|
|
|
|
"lang": "nu"
|
|
|
|
}
|
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-29 17:45:16 +00:00
|
|
|
#[test]
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn command_filter_reject_4() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[lang, gems, grade]; [nu, 100, a]] | reject gems | to json -r",
|
2024-03-20 21:14:31 +00:00
|
|
|
r#"[{"lang":"nu","grade":"a"}]"#,
|
2022-01-29 17:45:16 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-25 19:39:42 +00:00
|
|
|
#[test]
|
|
|
|
fn command_drop_column_1() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[lang, gems, grade]; [nu, 100, a]] | drop column 2 | to json",
|
|
|
|
r#"[
|
|
|
|
{
|
|
|
|
"lang": "nu"
|
|
|
|
}
|
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn record_1() -> TestResult {
|
|
|
|
run_test(r#"{'a': 'b'} | get a"#, "b")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn record_2() -> TestResult {
|
|
|
|
run_test(r#"{'b': 'c'}.b"#, "c")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn where_on_ranges() -> TestResult {
|
|
|
|
run_test(r#"1..10 | where $it > 8 | math sum"#, "19")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_on_list() -> TestResult {
|
|
|
|
run_test(r#"[1, 2, 3].1"#, "2")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_cell_path_1() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"[[name, size]; [a, 1.1]] | into int size | get size.0"#,
|
|
|
|
"1",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-01-02 22:45:43 +00:00
|
|
|
fn missing_column_errors() -> TestResult {
|
|
|
|
fail_test(
|
2022-12-22 20:30:10 +00:00
|
|
|
r#"[ { name: ABC, size: 20 }, { name: HIJ } ].size.1 == null"#,
|
2023-01-02 22:45:43 +00:00
|
|
|
"cannot find column",
|
2021-12-25 19:39:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-16 03:50:58 +00:00
|
|
|
#[test]
|
|
|
|
fn missing_optional_column_fills_in_nothing() -> TestResult {
|
2023-09-26 16:49:28 +00:00
|
|
|
// The empty value will be replaced with null because of the ?
|
2023-03-16 03:50:58 +00:00
|
|
|
run_test(
|
2023-09-26 16:49:28 +00:00
|
|
|
r#"[ { name: ABC, size: 20 }, { name: HIJ } ].size?.1 == null"#,
|
2023-03-16 03:50:58 +00:00
|
|
|
"true",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_required_row_fails() -> TestResult {
|
|
|
|
// .3 will fail if there is no 3rd row
|
|
|
|
fail_test(
|
|
|
|
r#"[ { name: ABC, size: 20 }, { name: HIJ } ].3"#,
|
|
|
|
"", // we just care if it errors
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_optional_row_fills_in_nothing() -> TestResult {
|
2023-09-26 16:49:28 +00:00
|
|
|
// ?.3 will return null if there is no 3rd row
|
2023-03-16 03:50:58 +00:00
|
|
|
run_test(
|
2023-09-26 16:49:28 +00:00
|
|
|
r#"[ { name: ABC, size: 20 }, { name: HIJ } ].3? == null"#,
|
2023-03-16 03:50:58 +00:00
|
|
|
"true",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-25 19:39:42 +00:00
|
|
|
#[test]
|
|
|
|
fn string_cell_path() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"let x = "name"; [["name", "score"]; [a, b], [c, d]] | get $x | get 1"#,
|
|
|
|
"c",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_row() -> TestResult {
|
|
|
|
run_test(r#""hello world" | split row " " | get 1"#, "world")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_column() -> TestResult {
|
|
|
|
run_test(
|
2022-02-20 00:26:47 +00:00
|
|
|
r#""hello world" | split column " " | get "column1".0"#,
|
2021-12-25 19:39:42 +00:00
|
|
|
"hello",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wrap() -> TestResult {
|
|
|
|
run_test(r#"([1, 2, 3] | wrap foo).foo.1"#, "2")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"[[name, grade]; [Alice, A], [Betty, B]] | get grade.1"#,
|
|
|
|
"B",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-01-10 21:29:52 +00:00
|
|
|
fn select_1() -> TestResult {
|
2021-12-25 19:39:42 +00:00
|
|
|
run_test(
|
|
|
|
r#"([[name, age]; [a, 1], [b, 2]]) | select name | get 1 | get name"#,
|
|
|
|
"b",
|
|
|
|
)
|
|
|
|
}
|
2021-12-27 23:11:20 +00:00
|
|
|
|
2022-01-10 21:29:52 +00:00
|
|
|
#[test]
|
|
|
|
fn select_2() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"[[name, age]; [a, 1] [b, 2]] | get 1 | select age | get age"#,
|
|
|
|
"2",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-27 23:11:20 +00:00
|
|
|
#[test]
|
|
|
|
fn update_will_insert() -> TestResult {
|
2022-03-17 00:13:34 +00:00
|
|
|
run_test(r#"{} | upsert a b | get a"#, "b")
|
2021-12-27 23:11:20 +00:00
|
|
|
}
|
2022-01-01 04:27:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn length_for_columns() -> TestResult {
|
|
|
|
run_test(
|
2023-08-23 21:03:26 +00:00
|
|
|
r#"[[name,age,grade]; [bill,20,a] [a b c]] | columns | length"#,
|
2022-01-01 04:27:20 +00:00
|
|
|
"3",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn length_for_rows() -> TestResult {
|
|
|
|
run_test(r#"[[name,age,grade]; [bill,20,a] [a b c]] | length"#, "2")
|
|
|
|
}
|
2022-01-02 02:18:39 +00:00
|
|
|
|
2022-02-13 10:38:46 +00:00
|
|
|
#[test]
|
|
|
|
fn length_defaulted_columns() -> TestResult {
|
|
|
|
run_test(
|
2022-10-26 16:36:42 +00:00
|
|
|
r#"[[name, age]; [test, 10]] | default 11 age | get 0 | columns | length"#,
|
2022-02-13 10:38:46 +00:00
|
|
|
"2",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-02 02:18:39 +00:00
|
|
|
#[test]
|
2023-01-02 22:45:43 +00:00
|
|
|
fn nullify_errors() -> TestResult {
|
2023-03-16 03:50:58 +00:00
|
|
|
run_test("([{a:1} {a:2} {a:3}] | get foo? | length) == 3", "true")?;
|
2023-01-02 22:45:43 +00:00
|
|
|
run_test(
|
2023-03-16 03:50:58 +00:00
|
|
|
"([{a:1} {a:2} {a:3}] | get foo? | to nuon) == '[null, null, null]'",
|
2023-01-02 22:45:43 +00:00
|
|
|
"true",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nullify_holes() -> TestResult {
|
|
|
|
run_test(
|
2023-03-16 03:50:58 +00:00
|
|
|
"([{a:1} {b:2} {a:3}] | get a? | to nuon) == '[1, null, 3]'",
|
2023-01-02 22:45:43 +00:00
|
|
|
"true",
|
|
|
|
)
|
2022-01-02 02:18:39 +00:00
|
|
|
}
|
2022-06-01 13:34:42 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_insensitive() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"[[name, age]; [a, 1] [b, 2]] | get NAmE | select 0 | get 0"#,
|
|
|
|
"a",
|
|
|
|
)
|
|
|
|
}
|