forked from extern/nushell
Add tests for get_data_by_key (#2658)
* Add test for get_data_by_key * Apply same order for ValuExt impl * Nothing helper for tests * Use get_data_by_key from ValueExt
This commit is contained in:
parent
95e61773a5
commit
38bdb053d2
@ -6,7 +6,7 @@ use nu_data::command::signature_dict;
|
|||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value};
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value};
|
||||||
use nu_source::{SpannedItem, Tagged};
|
use nu_source::{SpannedItem, Tagged};
|
||||||
use nu_value_ext::get_data_by_key;
|
use nu_value_ext::ValueExt;
|
||||||
|
|
||||||
pub struct Help;
|
pub struct Help;
|
||||||
|
|
||||||
@ -96,7 +96,8 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
|
|||||||
dict.insert_untagged("name", cmd_name);
|
dict.insert_untagged("name", cmd_name);
|
||||||
dict.insert_untagged(
|
dict.insert_untagged(
|
||||||
"description",
|
"description",
|
||||||
get_data_by_key(&value, "usage".spanned_unknown())
|
value
|
||||||
|
.get_data_by_key("usage".spanned_unknown())
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
ShellError::labeled_error(
|
ShellError::labeled_error(
|
||||||
"Expected a usage key",
|
"Expected a usage key",
|
||||||
|
@ -5,7 +5,7 @@ use nu_protocol::{
|
|||||||
merge_descriptors, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue,
|
merge_descriptors, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue,
|
||||||
};
|
};
|
||||||
use nu_source::{SpannedItem, Tagged};
|
use nu_source::{SpannedItem, Tagged};
|
||||||
use nu_value_ext::get_data_by_key;
|
use nu_value_ext::ValueExt;
|
||||||
|
|
||||||
pub struct Pivot;
|
pub struct Pivot;
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ pub async fn pivot(
|
|||||||
if args.header_row {
|
if args.header_row {
|
||||||
for i in input.clone() {
|
for i in input.clone() {
|
||||||
if let Some(desc) = descs.get(0) {
|
if let Some(desc) = descs.get(0) {
|
||||||
match get_data_by_key(&i, desc[..].spanned_unknown()) {
|
match &i.get_data_by_key(desc[..].spanned_unknown()) {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
if let Ok(s) = x.as_string() {
|
if let Ok(s) = x.as_string() {
|
||||||
headers.push(s.to_string());
|
headers.push(s.to_string());
|
||||||
@ -136,7 +136,7 @@ pub async fn pivot(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i in input.clone() {
|
for i in input.clone() {
|
||||||
match get_data_by_key(&i, desc[..].spanned_unknown()) {
|
match &i.get_data_by_key(desc[..].spanned_unknown()) {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
dict.insert_value(headers[column_num].clone(), x.clone());
|
dict.insert_value(headers[column_num].clone(), x.clone());
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ use nu_data::base::coerce_compare;
|
|||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
|
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
use nu_value_ext::get_data_by_key;
|
use nu_value_ext::ValueExt;
|
||||||
|
|
||||||
pub struct SortBy;
|
pub struct SortBy;
|
||||||
|
|
||||||
@ -118,8 +118,8 @@ pub fn sort(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for sort_arg in keys.iter() {
|
for sort_arg in keys.iter() {
|
||||||
let match_test = get_data_by_key(&vec[0], sort_arg.borrow_spanned());
|
let match_test = &vec[0].get_data_by_key(sort_arg.borrow_spanned());
|
||||||
if match_test == None {
|
if match_test.is_none() {
|
||||||
return Err(ShellError::labeled_error(
|
return Err(ShellError::labeled_error(
|
||||||
"Can not find column to sort by",
|
"Can not find column to sort by",
|
||||||
"invalid column",
|
"invalid column",
|
||||||
@ -168,7 +168,7 @@ pub fn sort(
|
|||||||
let calc_key = |item: &Value| {
|
let calc_key = |item: &Value| {
|
||||||
keys.iter()
|
keys.iter()
|
||||||
.map(|f| {
|
.map(|f| {
|
||||||
let mut value_option = get_data_by_key(item, f.borrow_spanned());
|
let mut value_option = item.get_data_by_key(f.borrow_spanned());
|
||||||
|
|
||||||
if insensitive {
|
if insensitive {
|
||||||
if let Some(value) = &value_option {
|
if let Some(value) = &value_option {
|
||||||
|
@ -4,7 +4,7 @@ use indexmap::{indexset, IndexSet};
|
|||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue, Value};
|
use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue, Value};
|
||||||
use nu_source::Spanned;
|
use nu_source::Spanned;
|
||||||
use nu_value_ext::{as_string, get_data_by_key};
|
use nu_value_ext::{as_string, ValueExt};
|
||||||
|
|
||||||
fn from_value_to_delimited_string(
|
fn from_value_to_delimited_string(
|
||||||
tagged_value: &Value,
|
tagged_value: &Value,
|
||||||
@ -66,7 +66,7 @@ fn from_value_to_delimited_string(
|
|||||||
for l in list {
|
for l in list {
|
||||||
let mut row = vec![];
|
let mut row = vec![];
|
||||||
for desc in &merged_descriptors {
|
for desc in &merged_descriptors {
|
||||||
row.push(match get_data_by_key(l, desc.borrow_spanned()) {
|
row.push(match l.get_data_by_key(desc.borrow_spanned()) {
|
||||||
Some(s) => to_string_tagged_value(&s)?,
|
Some(s) => to_string_tagged_value(&s)?,
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
});
|
});
|
||||||
|
@ -22,6 +22,10 @@ pub fn row(entries: IndexMap<String, Value>) -> Value {
|
|||||||
UntaggedValue::row(entries).into_untagged_value()
|
UntaggedValue::row(entries).into_untagged_value()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn nothing() -> Value {
|
||||||
|
UntaggedValue::nothing().into_untagged_value()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn table(list: &[Value]) -> Value {
|
pub fn table(list: &[Value]) -> Value {
|
||||||
UntaggedValue::table(list).into_untagged_value()
|
UntaggedValue::table(list).into_untagged_value()
|
||||||
}
|
}
|
||||||
|
@ -97,14 +97,6 @@ impl ValueExt for Value {
|
|||||||
insert_data_at_member(self, member, new_value)
|
insert_data_at_member(self, member, new_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_data_at_column_path(
|
|
||||||
&self,
|
|
||||||
split_path: &ColumnPath,
|
|
||||||
new_value: Value,
|
|
||||||
) -> Result<Value, ShellError> {
|
|
||||||
insert_data_at_column_path(self, split_path, new_value)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn forgiving_insert_data_at_column_path(
|
fn forgiving_insert_data_at_column_path(
|
||||||
&self,
|
&self,
|
||||||
split_path: &ColumnPath,
|
split_path: &ColumnPath,
|
||||||
@ -113,6 +105,14 @@ impl ValueExt for Value {
|
|||||||
forgiving_insert_data_at_column_path(self, split_path, new_value)
|
forgiving_insert_data_at_column_path(self, split_path, new_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn insert_data_at_column_path(
|
||||||
|
&self,
|
||||||
|
split_path: &ColumnPath,
|
||||||
|
new_value: Value,
|
||||||
|
) -> Result<Value, ShellError> {
|
||||||
|
insert_data_at_column_path(self, split_path, new_value)
|
||||||
|
}
|
||||||
|
|
||||||
fn replace_data_at_column_path(
|
fn replace_data_at_column_path(
|
||||||
&self,
|
&self,
|
||||||
split_path: &ColumnPath,
|
split_path: &ColumnPath,
|
||||||
@ -911,4 +911,43 @@ mod tests {
|
|||||||
*string("Arepas de Yehuda")
|
*string("Arepas de Yehuda")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_row_data_by_key() {
|
||||||
|
let row = row(indexmap! {
|
||||||
|
"lines".to_string() => int(0),
|
||||||
|
"words".to_string() => int(7),
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
row.get_data_by_key("lines".spanned_unknown()).unwrap(),
|
||||||
|
int(0)
|
||||||
|
);
|
||||||
|
assert!(row.get_data_by_key("chars".spanned_unknown()).is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_table_data_by_key() {
|
||||||
|
let row1 = row(indexmap! {
|
||||||
|
"lines".to_string() => int(0),
|
||||||
|
"files".to_string() => int(10),
|
||||||
|
});
|
||||||
|
|
||||||
|
let row2 = row(indexmap! {
|
||||||
|
"files".to_string() => int(1)
|
||||||
|
});
|
||||||
|
|
||||||
|
let table_value = table(&[row1, row2]);
|
||||||
|
assert_eq!(
|
||||||
|
table_value
|
||||||
|
.get_data_by_key("files".spanned_unknown())
|
||||||
|
.unwrap(),
|
||||||
|
table(&[int(10), int(1)])
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
table_value
|
||||||
|
.get_data_by_key("chars".spanned_unknown())
|
||||||
|
.unwrap(),
|
||||||
|
table(&[nothing(), nothing()])
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user