Move command changes. Refactorings. (#2683)

Continuing on anchoring and improvements on Nu's overall internal commands (#2635).
`move column` sub command has been turned into the command `move` since
we use it to move exclusively columns. Examples added as well.

Fixed it to carry along any anchor locations that might be in place if
table to be moved originates from other sources.
This commit is contained in:
Andrés N. Robalino
2020-10-20 04:07:13 -05:00
committed by GitHub
parent bc6c884a14
commit b6d19cc9fa
49 changed files with 516 additions and 625 deletions

View File

@ -191,7 +191,7 @@ mod tests {
#[test]
fn gets_matching_field_from_nested_rows_inside_a_row() -> Result<(), ShellError> {
let field_path = column_path("package.version");
let field_path = column_path("package.version").as_column_path()?;
let (version, tag) = string("0.4.0").into_parts();
@ -205,7 +205,7 @@ mod tests {
assert_eq!(
*value.into_value(tag).get_data_by_column_path(
&field_path?.item,
&field_path.item,
Box::new(error_callback("package.version"))
)?,
version
@ -217,7 +217,7 @@ mod tests {
#[test]
fn gets_first_matching_field_from_rows_with_same_field_inside_a_table() -> Result<(), ShellError>
{
let field_path = column_path("package.authors.name");
let field_path = column_path("package.authors.name").as_column_path()?;
let (_, tag) = string("Andrés N. Robalino").into_parts();
@ -235,7 +235,7 @@ mod tests {
assert_eq!(
value.into_value(tag).get_data_by_column_path(
&field_path?.item,
&field_path.item,
Box::new(error_callback("package.authors.name"))
)?,
table(&[
@ -250,7 +250,7 @@ mod tests {
#[test]
fn column_path_that_contains_just_a_number_gets_a_row_from_a_table() -> Result<(), ShellError> {
let field_path = column_path("package.authors.0");
let field_path = column_path("package.authors.0").as_column_path()?;
let (_, tag) = string("Andrés N. Robalino").into_parts();
@ -268,7 +268,7 @@ mod tests {
assert_eq!(
*value.into_value(tag).get_data_by_column_path(
&field_path?.item,
&field_path.item,
Box::new(error_callback("package.authors.0"))
)?,
UntaggedValue::row(indexmap! {
@ -281,7 +281,7 @@ mod tests {
#[test]
fn column_path_that_contains_just_a_number_gets_a_row_from_a_row() -> Result<(), ShellError> {
let field_path = column_path(r#"package.authors."0""#);
let field_path = column_path(r#"package.authors."0""#).as_column_path()?;
let (_, tag) = string("Andrés N. Robalino").into_parts();
@ -299,7 +299,7 @@ mod tests {
assert_eq!(
*value.into_value(tag).get_data_by_column_path(
&field_path?.item,
&field_path.item,
Box::new(error_callback("package.authors.\"0\""))
)?,
UntaggedValue::row(indexmap! {
@ -312,7 +312,7 @@ mod tests {
#[test]
fn replaces_matching_field_from_a_row() -> Result<(), ShellError> {
let field_path = column_path("amigos");
let field_path = column_path("amigos").as_column_path()?;
let sample = UntaggedValue::row(indexmap! {
"amigos".into() => table(&[
@ -326,7 +326,7 @@ mod tests {
let actual = sample
.into_untagged_value()
.replace_data_at_column_path(&field_path?.item, replacement)
.replace_data_at_column_path(&field_path.item, replacement)
.ok_or_else(|| ShellError::untagged_runtime_error("Could not replace column"))?;
assert_eq!(actual, row(indexmap! {"amigos".into() => string("jonas")}));
@ -336,7 +336,7 @@ mod tests {
#[test]
fn replaces_matching_field_from_nested_rows_inside_a_row() -> Result<(), ShellError> {
let field_path = column_path(r#"package.authors."los.3.caballeros""#);
let field_path = column_path(r#"package.authors."los.3.caballeros""#).as_column_path()?;
let sample = UntaggedValue::row(indexmap! {
"package".into() => row(indexmap! {
@ -353,7 +353,7 @@ mod tests {
let actual = sample
.into_value(&tag)
.replace_data_at_column_path(&field_path?.item, replacement.clone())
.replace_data_at_column_path(&field_path.item, replacement.clone())
.ok_or_else(|| {
ShellError::labeled_error(
"Could not replace column",
@ -377,7 +377,8 @@ mod tests {
}
#[test]
fn replaces_matching_field_from_rows_inside_a_table() -> Result<(), ShellError> {
let field_path = column_path(r#"shell_policy.releases."nu.version.arepa""#);
let field_path =
column_path(r#"shell_policy.releases."nu.version.arepa""#).as_column_path()?;
let sample = UntaggedValue::row(indexmap! {
"shell_policy".into() => row(indexmap! {
@ -409,7 +410,7 @@ mod tests {
let actual = sample
.into_value(tag.clone())
.replace_data_at_column_path(&field_path?.item, replacement.clone())
.replace_data_at_column_path(&field_path.item, replacement.clone())
.ok_or_else(|| {
ShellError::labeled_error(
"Could not replace column",

View File

@ -97,99 +97,68 @@ pub fn report(
}
pub mod helpers {
use super::Model;
use indexmap::indexmap;
use nu_errors::ShellError;
use nu_protocol::{UntaggedValue, Value};
use nu_source::{Span, Tag, TaggedItem};
use nu_protocol::{row, Value};
use nu_source::{Tag, TaggedItem};
use nu_test_support::value::{date, int, string, table};
use nu_value_ext::ValueExt;
use num_bigint::BigInt;
use indexmap::IndexMap;
pub fn int(s: impl Into<BigInt>) -> Value {
UntaggedValue::int(s).into_untagged_value()
}
pub fn decimal_from_float(f: f64, span: Span) -> Value {
UntaggedValue::decimal_from_float(f, span).into_untagged_value()
}
pub fn string(input: impl Into<String>) -> Value {
UntaggedValue::string(input.into()).into_untagged_value()
}
pub fn row(entries: IndexMap<String, Value>) -> Value {
UntaggedValue::row(entries).into_untagged_value()
}
pub fn table(list: &[Value]) -> Value {
UntaggedValue::table(list).into_untagged_value()
}
pub fn date(input: impl Into<String>) -> Value {
let key = input.into().tagged_unknown();
crate::value::Date::naive_from_str(key.borrow_tagged())
.expect("date from string failed")
.into_untagged_value()
}
pub fn committers() -> Vec<Value> {
vec![
row(indexmap! {
row! {
"date".into() => date("2019-07-23"),
"name".into() => string("AR"),
"country".into() => string("EC"),
"chickens".into() => int(10),
}),
row(indexmap! {
"chickens".into() => int(10)
},
row! {
"date".into() => date("2019-07-23"),
"name".into() => string("JT"),
"country".into() => string("NZ"),
"chickens".into() => int(5),
}),
row(indexmap! {
"chickens".into() => int(5)
},
row! {
"date".into() => date("2019-10-10"),
"name".into() => string("YK"),
"country".into() => string("US"),
"chickens".into() => int(6),
}),
row(indexmap! {
"chickens".into() => int(6)
},
row! {
"date".into() => date("2019-09-24"),
"name".into() => string("AR"),
"country".into() => string("EC"),
"chickens".into() => int(20),
}),
row(indexmap! {
"chickens".into() => int(20)
},
row! {
"date".into() => date("2019-10-10"),
"name".into() => string("JT"),
"country".into() => string("NZ"),
"chickens".into() => int(15),
}),
row(indexmap! {
"chickens".into() => int(15)
},
row! {
"date".into() => date("2019-09-24"),
"name".into() => string("YK"),
"country".into() => string("US"),
"chickens".into() => int(4),
}),
row(indexmap! {
"chickens".into() => int(4)
},
row! {
"date".into() => date("2019-10-10"),
"name".into() => string("AR"),
"country".into() => string("EC"),
"chickens".into() => int(30),
}),
row(indexmap! {
"chickens".into() => int(30)
},
row! {
"date".into() => date("2019-09-24"),
"name".into() => string("JT"),
"country".into() => string("NZ"),
"chickens".into() => int(10),
}),
row(indexmap! {
"chickens".into() => int(10)
},
row! {
"date".into() => date("2019-07-23"),
"name".into() => string("YK"),
"country".into() => string("US"),
"chickens".into() => int(2),
}),
"chickens".into() => int(2)
},
]
}
@ -217,6 +186,17 @@ pub mod helpers {
date.format(&fmt)
})
}
}
#[cfg(test)]
mod tests {
use super::helpers::{committers, date_formatter};
use super::{report, Labels, Model, Operation, Range, Reduction};
use nu_errors::ShellError;
use nu_protocol::Value;
use nu_source::{Tag, TaggedItem};
use nu_test_support::value::{decimal_from_float, int, table};
use nu_value_ext::ValueExt;
pub fn assert_without_checking_percentages(report_a: Model, report_b: Model) {
assert_eq!(report_a.labels.x, report_b.labels.x);
@ -224,19 +204,6 @@ pub mod helpers {
assert_eq!(report_a.ranges, report_b.ranges);
assert_eq!(report_a.data, report_b.data);
}
}
#[cfg(test)]
mod tests {
use super::helpers::{
assert_without_checking_percentages, committers, date_formatter, decimal_from_float, int,
table,
};
use super::{report, Labels, Model, Operation, Range, Reduction};
use nu_errors::ShellError;
use nu_protocol::Value;
use nu_source::{Span, Tag, TaggedItem};
use nu_value_ext::ValueExt;
#[test]
fn prepares_report_using_counting_value() {
@ -304,19 +271,19 @@ mod tests {
]),
percentages: table(&[
table(&[
decimal_from_float(33.33, Span::unknown()),
decimal_from_float(66.66, Span::unknown()),
decimal_from_float(99.99, Span::unknown()),
decimal_from_float(33.33),
decimal_from_float(66.66),
decimal_from_float(99.99),
]),
table(&[
decimal_from_float(16.66, Span::unknown()),
decimal_from_float(33.33, Span::unknown()),
decimal_from_float(49.99, Span::unknown()),
decimal_from_float(16.66),
decimal_from_float(33.33),
decimal_from_float(49.99),
]),
table(&[
decimal_from_float(6.66, Span::unknown()),
decimal_from_float(13.33, Span::unknown()),
decimal_from_float(19.99, Span::unknown()),
decimal_from_float(6.66),
decimal_from_float(13.33),
decimal_from_float(19.99),
]),
]),
},