Error on bad row in column path (#2964)

* Error on bad row in column path

* Add more pathing tests
This commit is contained in:
Jonathan Turner 2021-01-23 12:14:13 +13:00 committed by GitHub
parent 42b1287759
commit 52dc04a35a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 3 deletions

View File

@ -172,8 +172,8 @@ pub async fn evaluate_baseline_expr(
let next = item.get_data_by_member(member);
match next {
Err(err) => {
if let UnspannedPathMember::String(_name) = &member.unspanned {
Err(err) => match &member.unspanned {
UnspannedPathMember::String(_name) => {
let possible_matches = did_you_mean(&item, member.as_string());
match possible_matches {
@ -187,7 +187,14 @@ pub async fn evaluate_baseline_expr(
None => return Err(err),
}
}
}
UnspannedPathMember::Int(_row) => {
return Err(ShellError::labeled_error(
"Unknown row",
"unknown row",
&member.span,
));
}
},
Ok(next) => {
item = next.clone().value.into_value(&tag);
}

View File

@ -558,6 +558,54 @@ fn can_process_one_row_from_internal_and_pipes_it_to_stdin_of_external() {
assert_eq!(actual.out, "nushell");
}
#[test]
fn index_out_of_bounds() {
let actual = nu!(
cwd: ".",
r#"
let foo = [1, 2, 3]; echo $foo.5
"#
);
assert!(actual.err.contains("unknown row"));
}
#[test]
fn index_row() {
let actual = nu!(
cwd: ".",
r#"
let foo = [[name]; [joe] [bob]]; echo $foo.1 | to json
"#
);
assert_eq!(actual.out, r#"{"name":"bob"}"#);
}
#[test]
fn index_cell() {
let actual = nu!(
cwd: ".",
r#"
let foo = [[name]; [joe] [bob]]; echo $foo.name.1
"#
);
assert_eq!(actual.out, "bob");
}
#[test]
fn index_cell_alt() {
let actual = nu!(
cwd: ".",
r#"
let foo = [[name]; [joe] [bob]]; echo $foo.1.name
"#
);
assert_eq!(actual.out, "bob");
}
#[test]
fn echoing_ranges() {
let actual = nu!(