Better error messages.

This commit is contained in:
Andrés N. Robalino 2019-10-31 04:36:08 -05:00
parent 7614ce4b49
commit b54ce921dd
4 changed files with 149 additions and 10 deletions

View File

@ -56,6 +56,26 @@ pub fn get_column_path(
obj.tag(), obj.tag(),
path, path,
Box::new(move |(obj_source, column_path_tried)| { Box::new(move |(obj_source, column_path_tried)| {
match obj_source {
Value::Table(rows) => {
let total = rows.len();
let end_tag = match fields.iter().nth_back(if fields.len() > 2 { 1 } else { 0 })
{
Some(last_field) => last_field.tag(),
None => column_path_tried.tag(),
};
return ShellError::labeled_error_with_secondary(
"Row not found",
format!("There isn't a row indexed at '{}'", **column_path_tried),
column_path_tried.tag(),
format!("The table only has {} rows (0..{})", total, total - 1),
end_tag,
);
}
_ => {}
}
match did_you_mean(&obj_source, &column_path_tried) { match did_you_mean(&obj_source, &column_path_tried) {
Some(suggestions) => { Some(suggestions) => {
return ShellError::labeled_error( return ShellError::labeled_error(

View File

@ -459,11 +459,10 @@ impl Value {
} }
} }
// TODO: This is basically a legacy construct, I think
pub fn data_descriptors(&self) -> Vec<String> { pub fn data_descriptors(&self) -> Vec<String> {
match self { match self {
Value::Primitive(_) => vec![], Value::Primitive(_) => vec![],
Value::Row(o) => o Value::Row(columns) => columns
.entries .entries
.keys() .keys()
.into_iter() .into_iter()
@ -534,12 +533,9 @@ impl Value {
) -> Result<Option<Tagged<&Value>>, ShellError> { ) -> Result<Option<Tagged<&Value>>, ShellError> {
let mut current = self; let mut current = self;
for p in path { for p in path {
let value = if p.chars().all(char::is_numeric) { let value = match p.item().parse::<usize>() {
current.get_data_by_index(p.chars().fold(0 as usize, |acc, c| { Ok(number) => current.get_data_by_index(number),
c.to_digit(10).unwrap_or(0) as usize + acc Err(_) => current.get_data_by_key(p),
}))
} else {
current.get_data_by_key(p)
}; };
match value { match value {

View File

@ -98,8 +98,6 @@ impl Str {
value.tag(), value.tag(),
&f, &f,
Box::new(move |(obj_source, column_path_tried)| { Box::new(move |(obj_source, column_path_tried)| {
//let fields = f.clone();
match did_you_mean(&obj_source, &column_path_tried) { match did_you_mean(&obj_source, &column_path_tried) {
Some(suggestions) => { Some(suggestions) => {
return ShellError::labeled_error( return ShellError::labeled_error(

125
tests/command_get_tests.rs Normal file
View File

@ -0,0 +1,125 @@
mod helpers;
use helpers as h;
use helpers::{Playground, Stub::*};
#[test]
fn get() {
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(), h::pipeline(
r#"
open sample.toml
| get nu_party_venue
| echo $it
"#
));
assert_eq!(actual, "zion");
})
}
#[test]
fn fetches_by_index_from_a_given_table() {
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(), h::pipeline(
r#"
open sample.toml
| get package.authors.2
| echo $it
"#
));
assert_eq!(actual, "Andrés N. Robalino <andres@androbtech.com>");
})
}
#[test]
fn fetches_more_than_one_column_member_path() {
Playground::setup("get_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[[fortune_tellers]]
name = "Andrés N. Robalino"
arepas = 1
[[fortune_tellers]]
name = "Jonathan Turner"
arepas = 1
[[fortune_tellers]]
name = "Yehuda Katz"
arepas = 1
"#,
)]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
open sample.toml
| get fortune_tellers.2.name fortune_tellers.0.name fortune_tellers.1.name
| nth 2
| echo $it
"#
));
assert_eq!(actual, "Jonathan Turner");
})
}
#[test]
fn errors_fetching_by_index_out_of_bounds_from_table() {
Playground::setup("get_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[spanish_lesson]
sentence_words = ["Yo", "quiero", "taconushell"]
"#,
)]);
let actual = nu_error!(
cwd: dirs.test(), h::pipeline(
r#"
open sample.toml
| get spanish_lesson.sentence_words.3
"#
));
assert!(actual.contains("Row not found"));
assert!(actual.contains("There isn't a row indexed at '3'"));
assert!(actual.contains("The table only has 3 rows (0..2)"))
})
}
#[test]
fn requires_at_least_one_column_member_path() {
Playground::setup("first_test_4", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("andres.txt")]);
let actual = nu_error!(
cwd: dirs.test(), "ls | get"
);
assert!(actual.contains("requires member parameter"));
})
}