forked from extern/nushell
Convert more examples and tests to record!
macro (#10840)
# Description Use `record!` macro instead of defining two separate `vec!` for `cols` and `vals` when appropriate. This visually aligns the key with the value. Further more you don't have to deal with the construction of `Record { cols, vals }` so we can hide the implementation details in the future. ## State Not covering all possible commands yet, also some tests/examples are better expressed by creating cols and vals separately. # User/Developer-Facing Changes The examples and tests should read more natural. No relevant functional change # Bycatch Where I noticed it I replaced usage of `Value` constructors with `Span::test_data()` or `Span::unknown()` to the `Value::test_...` constructors. This should make things more readable and also simplify changes to the `Span` system in the future.
This commit is contained in:
committed by
GitHub
parent
7d67ca3652
commit
4b301710d3
@ -227,137 +227,119 @@ pub fn compare(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use nu_protocol::{Record, Span, Value};
|
||||
use nu_protocol::{record, Value};
|
||||
|
||||
#[test]
|
||||
fn test_sort_value() {
|
||||
let val = Value::list(
|
||||
vec![
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("pear"), Value::test_int(3)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("orange"), Value::test_int(7)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("apple"), Value::test_int(9)],
|
||||
}),
|
||||
],
|
||||
Span::test_data(),
|
||||
);
|
||||
let val = Value::test_list(vec![
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("pear"),
|
||||
"count" => Value::test_int(3),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("orange"),
|
||||
"count" => Value::test_int(7),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("apple"),
|
||||
"count" => Value::test_int(9),
|
||||
}),
|
||||
]);
|
||||
|
||||
let sorted_alphabetically =
|
||||
sort_value(&val, vec!["fruit".to_string()], true, false, false).unwrap();
|
||||
assert_eq!(
|
||||
sorted_alphabetically,
|
||||
Value::list(
|
||||
vec![
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("apple"), Value::test_int(9)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("orange"), Value::test_int(7)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("pear"), Value::test_int(3)],
|
||||
}),
|
||||
],
|
||||
Span::test_data(),
|
||||
)
|
||||
Value::test_list(vec![
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("apple"),
|
||||
"count" => Value::test_int(9),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("orange"),
|
||||
"count" => Value::test_int(7),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("pear"),
|
||||
"count" => Value::test_int(3),
|
||||
}),
|
||||
],)
|
||||
);
|
||||
|
||||
let sorted_by_count_desc =
|
||||
sort_value(&val, vec!["count".to_string()], false, false, false).unwrap();
|
||||
assert_eq!(
|
||||
sorted_by_count_desc,
|
||||
Value::list(
|
||||
vec![
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("apple"), Value::test_int(9)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("orange"), Value::test_int(7)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("pear"), Value::test_int(3)],
|
||||
}),
|
||||
],
|
||||
Span::test_data(),
|
||||
)
|
||||
Value::test_list(vec![
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("apple"),
|
||||
"count" => Value::test_int(9),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("orange"),
|
||||
"count" => Value::test_int(7),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("pear"),
|
||||
"count" => Value::test_int(3),
|
||||
}),
|
||||
],)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sort_value_in_place() {
|
||||
let mut val = Value::list(
|
||||
vec![
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("pear"), Value::test_int(3)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("orange"), Value::test_int(7)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("apple"), Value::test_int(9)],
|
||||
}),
|
||||
],
|
||||
Span::test_data(),
|
||||
);
|
||||
let mut val = Value::test_list(vec![
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("pear"),
|
||||
"count" => Value::test_int(3),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("orange"),
|
||||
"count" => Value::test_int(7),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("apple"),
|
||||
"count" => Value::test_int(9),
|
||||
}),
|
||||
]);
|
||||
|
||||
sort_value_in_place(&mut val, vec!["fruit".to_string()], true, false, false).unwrap();
|
||||
assert_eq!(
|
||||
val,
|
||||
Value::list(
|
||||
vec![
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("apple"), Value::test_int(9)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("orange"), Value::test_int(7)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("pear"), Value::test_int(3)],
|
||||
}),
|
||||
],
|
||||
Span::test_data(),
|
||||
)
|
||||
Value::test_list(vec![
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("apple"),
|
||||
"count" => Value::test_int(9),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("orange"),
|
||||
"count" => Value::test_int(7),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("pear"),
|
||||
"count" => Value::test_int(3),
|
||||
}),
|
||||
],)
|
||||
);
|
||||
|
||||
sort_value_in_place(&mut val, vec!["count".to_string()], false, false, false).unwrap();
|
||||
assert_eq!(
|
||||
val,
|
||||
Value::list(
|
||||
vec![
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("apple"), Value::test_int(9)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("orange"), Value::test_int(7)],
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["fruit".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("pear"), Value::test_int(3)],
|
||||
}),
|
||||
],
|
||||
Span::test_data(),
|
||||
)
|
||||
Value::test_list(vec![
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("apple"),
|
||||
"count" => Value::test_int(9),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("orange"),
|
||||
"count" => Value::test_int(7),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"fruit" => Value::test_string("pear"),
|
||||
"count" => Value::test_int(3),
|
||||
}),
|
||||
],)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user