mirror of
https://github.com/nushell/nushell.git
synced 2024-12-23 07:30:13 +01:00
Add some cell path tests (#7563)
This commit is contained in:
parent
757d7479af
commit
05e07ddf5c
@ -2,6 +2,18 @@ use nu_test_support::fs::Stub::FileWithContent;
|
|||||||
use nu_test_support::playground::Playground;
|
use nu_test_support::playground::Playground;
|
||||||
use nu_test_support::{nu, pipeline};
|
use nu_test_support::{nu, pipeline};
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fetches_a_row() {
|
fn fetches_a_row() {
|
||||||
Playground::setup("get_test_1", |dirs, sandbox| {
|
Playground::setup("get_test_1", |dirs, sandbox| {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
mod test_cell_path;
|
||||||
mod test_conditionals;
|
mod test_conditionals;
|
||||||
mod test_config_path;
|
mod test_config_path;
|
||||||
mod test_converters;
|
mod test_converters;
|
||||||
|
88
src/tests/test_cell_path.rs
Normal file
88
src/tests/test_cell_path.rs
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
use crate::tests::{fail_test, run_test, TestResult};
|
||||||
|
|
||||||
|
// Tests for $nothing / null / Value::Nothing
|
||||||
|
#[test]
|
||||||
|
fn nothing_fails_string() -> TestResult {
|
||||||
|
fail_test("$nothing.foo", "doesn't support cell paths")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nothing_fails_int() -> TestResult {
|
||||||
|
fail_test("$nothing.3", "Can't access")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests for records
|
||||||
|
#[test]
|
||||||
|
fn record_single_field_success() -> TestResult {
|
||||||
|
run_test("{foo: 'bar'}.foo == 'bar'", "true")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn record_single_field_failure() -> TestResult {
|
||||||
|
fail_test("{foo: 'bar'}.foobar", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn record_int_failure() -> TestResult {
|
||||||
|
fail_test("{foo: 'bar'}.3", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nested_record_field_success() -> TestResult {
|
||||||
|
run_test("{foo: {bar: 'baz'} }.foo.bar == 'baz'", "true")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nested_record_field_failure() -> TestResult {
|
||||||
|
fail_test("{foo: {bar: 'baz'} }.foo.asdf", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn record_with_nested_list_success() -> TestResult {
|
||||||
|
run_test("{foo: [{bar: 'baz'}]}.foo.0.bar == 'baz'", "true")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn record_with_nested_list_int_failure() -> TestResult {
|
||||||
|
fail_test("{foo: [{bar: 'baz'}]}.foo.3.bar", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn record_with_nested_list_column_failure() -> TestResult {
|
||||||
|
fail_test("{foo: [{bar: 'baz'}]}.foo.0.asdf", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests for lists
|
||||||
|
#[test]
|
||||||
|
fn list_single_field_success() -> TestResult {
|
||||||
|
run_test("[{foo: 'bar'}].foo.0 == 'bar'", "true")?;
|
||||||
|
// test field access both ways
|
||||||
|
run_test("[{foo: 'bar'}].0.foo == 'bar'", "true")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_single_field_failure() -> TestResult {
|
||||||
|
fail_test("[{foo: 'bar'}].asdf", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test the scenario where the requested column is not present in all rows
|
||||||
|
#[test]
|
||||||
|
fn jagged_list_access_succeeds() -> TestResult {
|
||||||
|
run_test("[{foo: 'bar'}, {}].foo == ['bar', $nothing]", "true")?;
|
||||||
|
run_test("[{}, {foo: 'bar'}].foo == [$nothing, 'bar']", "true")
|
||||||
|
}
|
||||||
|
|
||||||
|
// test that accessing a nonexistent row fails
|
||||||
|
#[test]
|
||||||
|
fn list_row_access_failure() -> TestResult {
|
||||||
|
fail_test("[{foo: 'bar'}, {foo: 'baz'}].2", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// regression test for an old bug
|
||||||
|
#[test]
|
||||||
|
fn do_not_delve_too_deep_in_nested_lists() -> TestResult {
|
||||||
|
fail_test(
|
||||||
|
"[[{foo: bar}]].foo",
|
||||||
|
"did not find anything under this name",
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user