nushell/crates/nu-command/tests/commands/get.rs

244 lines
6.2 KiB
Rust
Raw Normal View History

2019-12-17 19:54:39 +01:00
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
2019-10-31 10:42:18 +01:00
2022-12-21 23:54:39 +01:00
#[test]
fn simple_get_record() {
let actual = nu!(r#"({foo: 'bar'} | get foo) == "bar""#);
assert_eq!(actual.out, "true");
}
#[test]
fn simple_get_list() {
let actual = nu!(r#"([{foo: 'bar'}] | get foo) == [bar]"#);
assert_eq!(actual.out, "true");
}
2019-10-31 10:36:08 +01:00
#[test]
fn fetches_a_row() {
2019-10-31 10:36:08 +01:00
Playground::setup("get_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
nu_party_venue = "zion"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
2019-10-31 10:36:08 +01:00
r#"
open sample.toml
| get nu_party_venue
"#
));
assert_eq!(actual.out, "zion");
2019-10-31 10:36:08 +01:00
})
}
#[test]
fn fetches_by_index() {
2019-10-31 10:36:08 +01:00
Playground::setup("get_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nu"
version = "0.4.1"
authors = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
description = "When arepas shells are tasty and fun."
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
2019-10-31 10:36:08 +01:00
r#"
open sample.toml
| get package.authors.2
"#
));
assert_eq!(actual.out, "Andrés N. Robalino <andres@androbtech.com>");
2019-10-31 10:36:08 +01:00
})
}
2022-02-16 02:48:32 +01:00
#[test]
fn fetches_by_column_path() {
Playground::setup("get_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nu"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| get package.name
"#
));
assert_eq!(actual.out, "nu");
})
}
#[test]
fn column_paths_are_either_double_quoted_or_regular_unquoted_words_separated_by_dot() {
Playground::setup("get_test_4", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
9999 = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
description = "When arepas shells are tasty and fun."
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| get package."9999"
| length
"#
));
assert_eq!(actual.out, "3");
})
}
2019-10-31 10:36:08 +01:00
#[test]
fn fetches_more_than_one_column_path() {
Playground::setup("get_test_5", |dirs, sandbox| {
2019-10-31 10:36:08 +01:00
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[[fortune_tellers]]
name = "Andrés N. Robalino"
arepas = 1
[[fortune_tellers]]
2022-02-09 15:59:40 +01:00
name = "JT"
2019-10-31 10:36:08 +01:00
arepas = 1
[[fortune_tellers]]
name = "Yehuda Katz"
arepas = 1
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
2019-10-31 10:36:08 +01:00
r#"
open sample.toml
| get fortune_tellers.2.name fortune_tellers.0.name fortune_tellers.1.name
| get 2
2019-10-31 10:36:08 +01:00
"#
));
2022-02-09 15:59:40 +01:00
assert_eq!(actual.out, "JT");
2019-10-31 10:36:08 +01:00
})
}
#[test]
fn errors_fetching_by_column_not_present() {
Playground::setup("get_test_6", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[tacos]
sentence_words = ["Yo", "quiero", "tacos"]
[pizzanushell]
sentence-words = ["I", "want", "pizza"]
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| get taco
"#
));
2022-02-16 02:48:32 +01:00
assert!(actual.err.contains("Name not found"),);
assert!(actual.err.contains("did you mean 'tacos'"),);
})
}
2019-10-31 10:36:08 +01:00
#[test]
fn errors_fetching_by_column_using_a_number() {
Playground::setup("get_test_7", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[spanish_lesson]
0 = "can only be fetched with 0 double quoted."
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| get spanish_lesson.0
"#
));
assert!(actual.err.contains("Type mismatch"),);
})
}
#[test]
fn errors_fetching_by_index_out_of_bounds() {
Playground::setup("get_test_8", |dirs, sandbox| {
2019-10-31 10:36:08 +01:00
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[spanish_lesson]
sentence_words = ["Yo", "quiero", "taconushell"]
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
2019-10-31 10:36:08 +01:00
r#"
open sample.toml
| get spanish_lesson.sentence_words.3
"#
));
assert!(actual.err.contains("Row number too large (max: 2)"),);
2022-02-16 02:48:32 +01:00
assert!(actual.err.contains("too large"),);
2019-10-31 10:36:08 +01:00
})
}
#[test]
fn errors_fetching_by_accessing_empty_list() {
let actual = nu!(cwd: ".", pipeline(r#"[] | get 3"#));
assert!(actual.err.contains("Row number too large (empty content)"),);
}
#[test]
fn quoted_column_access() {
let actual = nu!(
cwd: "tests/fixtures/formats",
2022-10-26 18:36:42 +02:00
r#"'[{"foo bar": {"baz": 4}}]' | from json | get "foo bar".baz.0 "#
);
assert_eq!(actual.out, "4");
}
#[test]
fn get_does_not_delve_too_deep_in_nested_lists() {
let actual = nu!(
cwd: ".",
r#"[[{foo: bar}]] | get foo"#
);
Make `get` hole errors and cell path hole errors identical (improvement on #7002) (#7647) # Description This closes #7498, as well as fixes an issue reported in https://github.com/nushell/nushell/pull/7002#issuecomment-1368340773 BEFORE: ``` 〉[{foo: 'bar'} {}] | get foo Error: nu::shell::column_not_found (link) × Cannot find column ╭─[entry #5:1:1] 1 │ [{foo: 'bar'} {}] | get foo · ────────┬──────── ─┬─ · │ ╰── value originates here · ╰── cannot find column 'Empty cell' ╰──── 〉[{foo: 'bar'} {}].foo ╭───┬─────╮ │ 0 │ bar │ │ 1 │ │ ╰───┴─────╯ ``` AFTER: ``` 〉[{foo: 'bar'} {}] | get foo Error: nu::shell::column_not_found (link) × Cannot find column ╭─[entry #1:1:1] 1 │ [{foo: 'bar'} {}] | get foo · ─┬ ─┬─ · │ ╰── cannot find column 'foo' · ╰── value originates here ╰──── 〉[{foo: 'bar'} {}].foo Error: nu::shell::column_not_found (link) × Cannot find column ╭─[entry #3:1:1] 1 │ [{foo: 'bar'} {}].foo · ─┬ ─┬─ · │ ╰── cannot find column 'foo' · ╰── value originates here ╰──── ``` EDIT: This also changes the semantics of `get`/`select` `-i` somewhat. I've decided to leave it like this because it works more intuitively with `default` and `compact`. BEFORE: ``` 〉[{a:1} {b:2} {a:3}] | select -i foo | to nuon null ``` AFTER: ``` 〉[{a:1} {b:2} {a:3}] | select -i foo | to nuon [[foo]; [null], [null], [null]] ``` # User-Facing Changes See above. EDIT: the issue with holes in cases like ` [{foo: 'bar'} {}].foo.0` versus ` [{foo: 'bar'} {}].0.foo` has been resolved. # 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` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # 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-01-02 23:45:43 +01:00
assert!(actual.err.contains("cannot find column"));
}