2019-12-17 18:54:39 +00:00
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
use nu_test_support::playground::Playground;
|
2020-05-07 11:03:43 +00:00
|
|
|
use nu_test_support::{nu, pipeline};
|
2019-10-31 09:42:18 +00:00
|
|
|
|
2022-12-21 22:54:39 +00: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 09:36:08 +00:00
|
|
|
#[test]
|
2019-12-15 16:15:06 +00:00
|
|
|
fn fetches_a_row() {
|
2019-10-31 09:36:08 +00:00
|
|
|
Playground::setup("get_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
nu_party_venue = "zion"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-04-02 15:25:05 +00:00
|
|
|
let actual = nu!( cwd: dirs.test(), "open sample.toml | get nu_party_venue");
|
2019-10-31 09:36:08 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "zion");
|
2019-10-31 09:36:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-11-01 21:19:46 +00:00
|
|
|
fn fetches_by_index() {
|
2019-10-31 09:36:08 +00:00
|
|
|
Playground::setup("get_test_2", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "nu"
|
|
|
|
version = "0.4.1"
|
2023-03-15 05:54:55 +00:00
|
|
|
authors = ["Yehuda Katz <wycats@gmail.com>", "JT Turner <547158+jntrnr@users.noreply.github.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
2019-10-31 09:36:08 +00:00
|
|
|
description = "When arepas shells are tasty and fun."
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-04-02 15:25:05 +00:00
|
|
|
let actual = nu!( cwd: dirs.test(), "open sample.toml | get package.authors.2");
|
2019-10-31 09:36:08 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "Andrés N. Robalino <andres@androbtech.com>");
|
2019-10-31 09:36:08 +00:00
|
|
|
})
|
|
|
|
}
|
2022-02-16 01:48:32 +00:00
|
|
|
|
2019-10-31 19:20:22 +00:00
|
|
|
#[test]
|
2019-11-01 21:19:46 +00:00
|
|
|
fn fetches_by_column_path() {
|
2019-10-31 19:20:22 +00:00
|
|
|
Playground::setup("get_test_3", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
2019-11-01 21:19:46 +00:00
|
|
|
name = "nu"
|
2019-10-31 19:20:22 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-04-02 15:25:05 +00:00
|
|
|
let actual = nu!( cwd: dirs.test(), "open sample.toml | get package.name");
|
2019-10-31 19:20:22 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "nu");
|
2019-10-31 19:20:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-11-01 21:19:46 +00:00
|
|
|
fn column_paths_are_either_double_quoted_or_regular_unquoted_words_separated_by_dot() {
|
2019-10-31 19:20:22 +00:00
|
|
|
Playground::setup("get_test_4", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
2023-03-15 05:54:55 +00:00
|
|
|
9999 = ["Yehuda Katz <wycats@gmail.com>", "JT Turner <jtd.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
2019-10-31 19:20:22 +00:00
|
|
|
description = "When arepas shells are tasty and fun."
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-04-02 15:25:05 +00:00
|
|
|
let actual = nu!( cwd: dirs.test(), r#"open sample.toml | get package."9999" | length"#);
|
2019-10-31 19:20:22 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "3");
|
2019-10-31 19:20:22 +00:00
|
|
|
})
|
|
|
|
}
|
2019-10-31 09:36:08 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-11-01 21:19:46 +00:00
|
|
|
fn fetches_more_than_one_column_path() {
|
2019-10-31 19:20:22 +00:00
|
|
|
Playground::setup("get_test_5", |dirs, sandbox| {
|
2019-10-31 09:36:08 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[[fortune_tellers]]
|
|
|
|
name = "Andrés N. Robalino"
|
|
|
|
arepas = 1
|
|
|
|
|
|
|
|
[[fortune_tellers]]
|
2022-02-09 14:59:40 +00:00
|
|
|
name = "JT"
|
2019-10-31 09:36:08 +00:00
|
|
|
arepas = 1
|
|
|
|
|
|
|
|
[[fortune_tellers]]
|
|
|
|
name = "Yehuda Katz"
|
|
|
|
arepas = 1
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
2019-12-15 16:15:06 +00:00
|
|
|
cwd: dirs.test(), pipeline(
|
2023-04-02 15:25:05 +00:00
|
|
|
"
|
2019-10-31 09:36:08 +00:00
|
|
|
open sample.toml
|
|
|
|
| get fortune_tellers.2.name fortune_tellers.0.name fortune_tellers.1.name
|
2022-02-09 10:58:54 +00:00
|
|
|
| get 2
|
2023-04-02 15:25:05 +00:00
|
|
|
"
|
2019-10-31 09:36:08 +00:00
|
|
|
));
|
|
|
|
|
2022-02-09 14:59:40 +00:00
|
|
|
assert_eq!(actual.out, "JT");
|
2019-10-31 09:36:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:20:22 +00:00
|
|
|
#[test]
|
|
|
|
fn errors_fetching_by_column_not_present() {
|
|
|
|
Playground::setup("get_test_6", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
2022-09-21 00:46:01 +00:00
|
|
|
[tacos]
|
|
|
|
sentence_words = ["Yo", "quiero", "tacos"]
|
2020-09-22 02:14:11 +00:00
|
|
|
[pizzanushell]
|
|
|
|
sentence-words = ["I", "want", "pizza"]
|
2019-10-31 19:20:22 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-04-02 15:25:05 +00:00
|
|
|
let actual = nu!( cwd: dirs.test(), "open sample.toml | get taco");
|
2019-10-31 19:20:22 +00:00
|
|
|
|
2022-02-16 01:48:32 +00:00
|
|
|
assert!(actual.err.contains("Name not found"),);
|
2022-09-21 00:46:01 +00:00
|
|
|
assert!(actual.err.contains("did you mean 'tacos'"),);
|
2019-10-31 19:20:22 +00:00
|
|
|
})
|
|
|
|
}
|
2019-11-01 21:19:46 +00:00
|
|
|
|
2019-10-31 09:36:08 +00:00
|
|
|
#[test]
|
2019-11-01 21:19:46 +00:00
|
|
|
fn errors_fetching_by_column_using_a_number() {
|
2019-10-31 19:20:22 +00:00
|
|
|
Playground::setup("get_test_7", |dirs, sandbox| {
|
2019-11-01 21:19:46 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[spanish_lesson]
|
|
|
|
0 = "can only be fetched with 0 double quoted."
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-04-02 15:25:05 +00:00
|
|
|
let actual = nu!( cwd: dirs.test(), "open sample.toml | get spanish_lesson.0");
|
2019-11-01 21:19:46 +00:00
|
|
|
|
2022-10-25 01:22:57 +00:00
|
|
|
assert!(actual.err.contains("Type mismatch"),);
|
2019-11-01 21:19:46 +00:00
|
|
|
})
|
|
|
|
}
|
2022-02-04 02:01:45 +00:00
|
|
|
|
2019-11-01 21:19:46 +00:00
|
|
|
#[test]
|
|
|
|
fn errors_fetching_by_index_out_of_bounds() {
|
|
|
|
Playground::setup("get_test_8", |dirs, sandbox| {
|
2019-10-31 09:36:08 +00:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
[spanish_lesson]
|
|
|
|
sentence_words = ["Yo", "quiero", "taconushell"]
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
let actual = nu!(
|
2023-04-02 15:25:05 +00:00
|
|
|
cwd: dirs.test(), " open sample.toml | get spanish_lesson.sentence_words.3 ");
|
2019-10-31 09:36:08 +00:00
|
|
|
|
2022-10-29 17:47:50 +00:00
|
|
|
assert!(actual.err.contains("Row number too large (max: 2)"),);
|
2022-02-16 01:48:32 +00:00
|
|
|
assert!(actual.err.contains("too large"),);
|
2019-10-31 09:36:08 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-26 06:01:55 +00:00
|
|
|
|
2022-10-29 17:47:50 +00: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)"),);
|
|
|
|
}
|
|
|
|
|
2020-04-26 06:01:55 +00:00
|
|
|
#[test]
|
|
|
|
fn quoted_column_access() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats",
|
2022-10-26 16:36:42 +00:00
|
|
|
r#"'[{"foo bar": {"baz": 4}}]' | from json | get "foo bar".baz.0 "#
|
2020-04-26 06:01:55 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "4");
|
2020-04-26 06:01:55 +00:00
|
|
|
}
|
2022-12-16 06:03:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_does_not_delve_too_deep_in_nested_lists() {
|
2023-04-02 15:25:05 +00:00
|
|
|
let actual = nu!(r#"[[{foo: bar}]] | get foo"#);
|
2022-12-16 06:03:38 +00:00
|
|
|
|
2023-01-02 22:45:43 +00:00
|
|
|
assert!(actual.err.contains("cannot find column"));
|
2022-12-16 06:03:38 +00:00
|
|
|
}
|
2023-03-16 18:50:04 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ignore_errors_works() {
|
2023-04-02 15:25:05 +00:00
|
|
|
let actual = nu!(r#" let path = "foo"; {} | get -i $path | to nuon "#);
|
2023-03-16 18:50:04 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "null");
|
|
|
|
}
|