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:
Stefan Holderbach 2023-10-28 14:52:31 +02:00 committed by GitHub
parent 7d67ca3652
commit 4b301710d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
99 changed files with 1592 additions and 2540 deletions

View File

@ -976,13 +976,15 @@ fn extract_char(value: &Value, config: &Config) -> Result<char, ShellError> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use nu_protocol::record;
use super::*; use super::*;
#[test] #[test]
fn test_send_event() { fn test_send_event() {
let cols = vec!["send".to_string()]; let event = record! {
let vals = vec![Value::test_string("Enter")]; "send" => Value::test_string("Enter"),
let event = Record { vals, cols }; };
let span = Span::test_data(); let span = Span::test_data();
let b = EventType::try_from_record(&event, span).unwrap(); let b = EventType::try_from_record(&event, span).unwrap();
@ -997,9 +999,9 @@ mod test {
#[test] #[test]
fn test_edit_event() { fn test_edit_event() {
let cols = vec!["edit".to_string()]; let event = record! {
let vals = vec![Value::test_string("Clear")]; "edit" => Value::test_string("Clear"),
let event = Record { vals, cols }; };
let span = Span::test_data(); let span = Span::test_data();
let b = EventType::try_from_record(&event, span).unwrap(); let b = EventType::try_from_record(&event, span).unwrap();
@ -1017,12 +1019,10 @@ mod test {
#[test] #[test]
fn test_send_menu() { fn test_send_menu() {
let cols = vec!["send".to_string(), "name".to_string()]; let event = record! {
let vals = vec![ "send" => Value::test_string("Menu"),
Value::test_string("Menu"), "name" => Value::test_string("history_menu"),
Value::test_string("history_menu"), };
];
let event = Record { vals, cols };
let span = Span::test_data(); let span = Span::test_data();
let b = EventType::try_from_record(&event, span).unwrap(); let b = EventType::try_from_record(&event, span).unwrap();
@ -1040,28 +1040,19 @@ mod test {
#[test] #[test]
fn test_until_event() { fn test_until_event() {
// Menu event let menu_event = Value::test_record(record! {
let cols = vec!["send".to_string(), "name".to_string()]; "send" => Value::test_string("Menu"),
let vals = vec![ "name" => Value::test_string("history_menu"),
Value::test_string("Menu"), });
Value::test_string("history_menu"), let enter_event = Value::test_record(record! {
]; "send" => Value::test_string("Enter"),
});
let menu_event = Value::test_record(Record { cols, vals }); let event = record! {
"until" => Value::list(
// Enter event
let cols = vec!["send".to_string()];
let vals = vec![Value::test_string("Enter")];
let enter_event = Value::test_record(Record { cols, vals });
// Until event
let cols = vec!["until".to_string()];
let vals = vec![Value::list(
vec![menu_event, enter_event], vec![menu_event, enter_event],
Span::test_data(), Span::test_data(),
)]; ),
let event = Record { cols, vals }; };
let span = Span::test_data(); let span = Span::test_data();
let b = EventType::try_from_record(&event, span).unwrap(); let b = EventType::try_from_record(&event, span).unwrap();
@ -1082,22 +1073,13 @@ mod test {
#[test] #[test]
fn test_multiple_event() { fn test_multiple_event() {
// Menu event let menu_event = Value::test_record(record! {
let cols = vec!["send".to_string(), "name".to_string()]; "send" => Value::test_string("Menu"),
let vals = vec![ "name" => Value::test_string("history_menu"),
Value::test_string("Menu"), });
Value::test_string("history_menu"), let enter_event = Value::test_record(record! {
]; "send" => Value::test_string("Enter"),
});
let menu_event = Value::test_record(Record { cols, vals });
// Enter event
let cols = vec!["send".to_string()];
let vals = vec![Value::test_string("Enter")];
let enter_event = Value::test_record(Record { cols, vals });
// Multiple event
let event = Value::list(vec![menu_event, enter_event], Span::test_data()); let event = Value::list(vec![menu_event, enter_event], Span::test_data());
let config = Config::default(); let config = Config::default();
@ -1113,9 +1095,9 @@ mod test {
#[test] #[test]
fn test_error() { fn test_error() {
let cols = vec!["not_exist".to_string()]; let event = record! {
let vals = vec![Value::test_string("Enter")]; "not_exist" => Value::test_string("Enter"),
let event = Record { cols, vals }; };
let span = Span::test_data(); let span = Span::test_data();
let b = EventType::try_from_record(&event, span); let b = EventType::try_from_record(&event, span);

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
use crate::dataframe::values::NuExpression; use crate::dataframe::values::NuExpression;
@ -39,18 +39,20 @@ impl Command for ToNu {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let cols = vec!["index".into(), "a".into(), "b".into()]; let rec_1 = Value::test_record(record! {
let rec_1 = Value::test_record(Record { "index" => Value::test_int(0),
cols: cols.clone(), "a" => Value::test_int(1),
vals: vec![Value::test_int(0), Value::test_int(1), Value::test_int(2)], "b" => Value::test_int(2),
}); });
let rec_2 = Value::test_record(Record { let rec_2 = Value::test_record(record! {
cols: cols.clone(), "index" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(3), Value::test_int(4)], "a" => Value::test_int(3),
"b" => Value::test_int(4),
}); });
let rec_3 = Value::test_record(Record { let rec_3 = Value::test_record(record! {
cols, "index" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(4)], "a" => Value::test_int(3),
"b" => Value::test_int(4),
}); });
vec![ vec![
@ -67,9 +69,9 @@ impl Command for ToNu {
Example { Example {
description: "Convert a col expression into a nushell value", description: "Convert a col expression into a nushell value",
example: "dfr col a | dfr into-nu", example: "dfr col a | dfr into-nu",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["expr".into(), "value".into()], "expr" => Value::test_string("column"),
vals: vec![Value::test_string("column"), Value::test_string("a")], "value" => Value::test_string("a"),
})), })),
}, },
] ]

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -38,20 +38,12 @@ impl Command for ExprAlias {
description: "Creates and alias expression", description: "Creates and alias expression",
example: "dfr col a | dfr as new_a | dfr into-nu", example: "dfr col a | dfr as new_a | dfr into-nu",
result: { result: {
let cols = vec!["expr".into(), "value".into()]; let record = Value::test_record(record! {
let expr = Value::test_string("column"); "expr" => Value::test_record(record! {
let value = Value::test_string("a"); "expr" => Value::test_string("column"),
let expr = Value::test_record(Record { "value" => Value::test_string("a"),
cols, }),
vals: vec![expr, value], "alias" => Value::test_string("new_a"),
});
let cols = vec!["expr".into(), "alias".into()];
let value = Value::test_string("new_a");
let record = Value::test_record(Record {
cols,
vals: vec![expr, value],
}); });
Some(record) Some(record)

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
use polars::prelude::col; use polars::prelude::col;
@ -34,9 +34,9 @@ impl Command for ExprCol {
vec![Example { vec![Example {
description: "Creates a named column expression and converts it to a nu object", description: "Creates a named column expression and converts it to a nu object",
example: "dfr col a | dfr into-nu", example: "dfr col a | dfr into-nu",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["expr".into(), "value".into()], "expr" => Value::test_string("column"),
vals: vec![Value::test_string("column"), Value::test_string("a")], "value" => Value::test_string("a"),
})), })),
}] }]
} }

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -33,9 +33,9 @@ impl Command for ExprLit {
vec![Example { vec![Example {
description: "Created a literal expression and converts it to a nu object", description: "Created a literal expression and converts it to a nu object",
example: "dfr lit 2 | dfr into-nu", example: "dfr lit 2 | dfr into-nu",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["expr".into(), "value".into()], "expr" => Value::test_string("literal"),
vals: vec![Value::test_string("literal"), Value::test_string("2")], "value" => Value::test_string("2"),
})), })),
}] }]
} }

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
record, Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -32,27 +32,15 @@ impl Command for Fmt {
vec![Example { vec![Example {
description: "Get a record containing multiple formats for the number 42", description: "Get a record containing multiple formats for the number 42",
example: "42 | fmt", example: "42 | fmt",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "binary" => Value::test_string("0b101010"),
"binary".into(), "debug" => Value::test_string("42"),
"debug".into(), "display" => Value::test_string("42"),
"display".into(), "lowerexp" => Value::test_string("4.2e1"),
"lowerexp".into(), "lowerhex" => Value::test_string("0x2a"),
"lowerhex".into(), "octal" => Value::test_string("0o52"),
"octal".into(), "upperexp" => Value::test_string("4.2E1"),
"upperexp".into(), "upperhex" => Value::test_string("0x2A"),
"upperhex".into(),
],
vals: vec![
Value::test_string("0b101010"),
Value::test_string("42"),
Value::test_string("42"),
Value::test_string("4.2e1"),
Value::test_string("0x2a"),
Value::test_string("0o52"),
Value::test_string("4.2E1"),
Value::test_string("0x2A"),
],
})), })),
}] }]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape,
SyntaxShape, Type, Value, Type, Value,
}; };
use super::{vertical_rotate_value, VerticalDirection}; use super::{vertical_rotate_value, VerticalDirection};
@ -33,27 +33,23 @@ impl Command for RollDown {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let columns = vec!["a".to_string(), "b".to_string()];
vec![Example { vec![Example {
description: "Rolls rows down of a table", description: "Rolls rows down of a table",
example: "[[a b]; [1 2] [3 4] [5 6]] | roll down", example: "[[a b]; [1 2] [3 4] [5 6]] | roll down",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(5),
cols: columns.clone(), "b" => Value::test_int(6),
vals: vec![Value::test_int(5), Value::test_int(6)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: columns.clone(), "a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(2)], "b" => Value::test_int(2),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: columns, "a" => Value::test_int(3),
vals: vec![Value::test_int(3), Value::test_int(4)], "b" => Value::test_int(4),
}), }),
], ])),
Span::test_data(),
)),
}] }]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape,
SyntaxShape, Type, Value, Type, Value,
}; };
use super::{horizontal_rotate_value, HorizontalDirection}; use super::{horizontal_rotate_value, HorizontalDirection};
@ -45,50 +45,47 @@ impl Command for RollLeft {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let columns = vec!["a".to_string(), "b".to_string(), "c".to_string()];
let rotated_columns = vec!["b".to_string(), "c".to_string(), "a".to_string()];
vec![ vec![
Example { Example {
description: "Rolls columns of a record to the left", description: "Rolls columns of a record to the left",
example: "{a:1 b:2 c:3} | roll left", example: "{a:1 b:2 c:3} | roll left",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: rotated_columns.clone(), "b" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(1)], "c" => Value::test_int(3),
"a" => Value::test_int(1),
})), })),
}, },
Example { Example {
description: "Rolls columns of a table to the left", description: "Rolls columns of a table to the left",
example: "[[a b c]; [1 2 3] [4 5 6]] | roll left", example: "[[a b c]; [1 2 3] [4 5 6]] | roll left",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "b" => Value::test_int(2),
cols: rotated_columns.clone(), "c" => Value::test_int(3),
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(1)], "a" => Value::test_int(1),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: rotated_columns, "b" => Value::test_int(5),
vals: vec![Value::test_int(5), Value::test_int(6), Value::test_int(4)], "c" => Value::test_int(6),
"a" => Value::test_int(4),
}), }),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Rolls columns to the left without changing column names", description: "Rolls columns to the left without changing column names",
example: "[[a b c]; [1 2 3] [4 5 6]] | roll left --cells-only", example: "[[a b c]; [1 2 3] [4 5 6]] | roll left --cells-only",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(2),
cols: columns.clone(), "b" => Value::test_int(3),
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(1)], "c" => Value::test_int(1),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: columns, "a" => Value::test_int(5),
vals: vec![Value::test_int(5), Value::test_int(6), Value::test_int(4)], "b" => Value::test_int(6),
"c" => Value::test_int(4),
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape,
SyntaxShape, Type, Value, Type, Value,
}; };
use super::{horizontal_rotate_value, HorizontalDirection}; use super::{horizontal_rotate_value, HorizontalDirection};
@ -45,50 +45,47 @@ impl Command for RollRight {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let columns = vec!["a".to_string(), "b".to_string(), "c".to_string()];
let rotated_columns = vec!["c".to_string(), "a".to_string(), "b".to_string()];
vec![ vec![
Example { Example {
description: "Rolls columns of a record to the right", description: "Rolls columns of a record to the right",
example: "{a:1 b:2 c:3} | roll right", example: "{a:1 b:2 c:3} | roll right",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: rotated_columns.clone(), "c" => Value::test_int(3),
vals: vec![Value::test_int(3), Value::test_int(1), Value::test_int(2)], "a" => Value::test_int(1),
"b" => Value::test_int(2),
})), })),
}, },
Example { Example {
description: "Rolls columns to the right", description: "Rolls columns to the right",
example: "[[a b c]; [1 2 3] [4 5 6]] | roll right", example: "[[a b c]; [1 2 3] [4 5 6]] | roll right",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "c" => Value::test_int(3),
cols: rotated_columns.clone(), "a" => Value::test_int(1),
vals: vec![Value::test_int(3), Value::test_int(1), Value::test_int(2)], "b" => Value::test_int(2),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: rotated_columns, "c" => Value::test_int(6),
vals: vec![Value::test_int(6), Value::test_int(4), Value::test_int(5)], "a" => Value::test_int(4),
"b" => Value::test_int(5),
}), }),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Rolls columns to the right with fixed headers", description: "Rolls columns to the right with fixed headers",
example: "[[a b c]; [1 2 3] [4 5 6]] | roll right --cells-only", example: "[[a b c]; [1 2 3] [4 5 6]] | roll right --cells-only",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(3),
cols: columns.clone(), "b" => Value::test_int(1),
vals: vec![Value::test_int(3), Value::test_int(1), Value::test_int(2)], "c" => Value::test_int(2),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: columns, "a" => Value::test_int(6),
vals: vec![Value::test_int(6), Value::test_int(4), Value::test_int(5)], "b" => Value::test_int(4),
"c" => Value::test_int(5),
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape,
SyntaxShape, Type, Value, Type, Value,
}; };
use super::{vertical_rotate_value, VerticalDirection}; use super::{vertical_rotate_value, VerticalDirection};
@ -33,27 +33,23 @@ impl Command for RollUp {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let columns = vec!["a".to_string(), "b".to_string()];
vec![Example { vec![Example {
description: "Rolls rows up", description: "Rolls rows up",
example: "[[a b]; [1 2] [3 4] [5 6]] | roll up", example: "[[a b]; [1 2] [3 4] [5 6]] | roll up",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(3),
cols: columns.clone(), "b" => Value::test_int(4),
vals: vec![Value::test_int(3), Value::test_int(4)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: columns.clone(), "a" => Value::test_int(5),
vals: vec![Value::test_int(5), Value::test_int(6)], "b" => Value::test_int(6),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: columns, "a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(2)], "b" => Value::test_int(2),
}), }),
], ])),
Span::test_data(),
)),
}] }]
} }

View File

@ -1,9 +1,10 @@
use nu_engine::CallExt; use nu_engine::CallExt;
use nu_protocol::IntoPipelineData;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, PipelineData, Record, ShellError, Signature, SyntaxShape, Type,
SyntaxShape, Type, Value, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -38,142 +39,104 @@ impl Command for Rotate {
Example { Example {
description: "Rotate a record clockwise, producing a table (like `transpose` but with column order reversed)", description: "Rotate a record clockwise, producing a table (like `transpose` but with column order reversed)",
example: "{a:1, b:2} | rotate", example: "{a:1, b:2} | rotate",
result: Some(Value::list(vec![ result: Some(Value::test_list(vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column0".to_string(), "column1".to_string()], "column0" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_string("a")], "column1" => Value::test_string("a"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column0".to_string(), "column1".to_string()], "column0" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_string("b")], "column1" => Value::test_string("b"),
}), }),
], ],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Rotate 2x3 table clockwise", description: "Rotate 2x3 table clockwise",
example: "[[a b]; [1 2] [3 4] [5 6]] | rotate", example: "[[a b]; [1 2] [3 4] [5 6]] | rotate",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "column0" => Value::test_int(5),
"column0".to_string(), "column1" => Value::test_int(3),
"column1".to_string(), "column2" => Value::test_int(1),
"column2".to_string(), "column3" => Value::test_string("a"),
"column3".to_string(),
],
vals: vec![
Value::test_int(5),
Value::test_int(3),
Value::test_int(1),
Value::test_string("a"),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "column0" => Value::test_int(6),
"column0".to_string(), "column1" => Value::test_int(4),
"column1".to_string(), "column2" => Value::test_int(2),
"column2".to_string(), "column3" => Value::test_string("b"),
"column3".to_string(),
],
vals: vec![
Value::test_int(6),
Value::test_int(4),
Value::test_int(2),
Value::test_string("b"),
],
}), }),
], ],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Rotate table clockwise and change columns names", description: "Rotate table clockwise and change columns names",
example: "[[a b]; [1 2]] | rotate col_a col_b", example: "[[a b]; [1 2]] | rotate col_a col_b",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["col_a".to_string(), "col_b".to_string()], "col_a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_string("a")], "col_b" => Value::test_string("a"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["col_a".to_string(), "col_b".to_string()], "col_a" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_string("b")], "col_b" => Value::test_string("b"),
}), }),
], ],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Rotate table counter clockwise", description: "Rotate table counter clockwise",
example: "[[a b]; [1 2]] | rotate --ccw", example: "[[a b]; [1 2]] | rotate --ccw",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column0".to_string(), "column1".to_string()], "column0" => Value::test_string("b"),
vals: vec![Value::test_string("b"), Value::test_int(2)], "column1" => Value::test_int(2),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column0".to_string(), "column1".to_string()], "column0" => Value::test_string("a"),
vals: vec![Value::test_string("a"), Value::test_int(1)], "column1" => Value::test_int(1),
}), }),
], ],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Rotate table counter-clockwise", description: "Rotate table counter-clockwise",
example: "[[a b]; [1 2] [3 4] [5 6]] | rotate --ccw", example: "[[a b]; [1 2] [3 4] [5 6]] | rotate --ccw",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "column0" => Value::test_string("b"),
"column0".to_string(), "column1" => Value::test_int(2),
"column1".to_string(), "column2" => Value::test_int(4),
"column2".to_string(), "column3" => Value::test_int(6),
"column3".to_string(),
],
vals: vec![
Value::test_string("b"),
Value::test_int(2),
Value::test_int(4),
Value::test_int(6),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "column0" => Value::test_string("a"),
"column0".to_string(), "column1" => Value::test_int(1),
"column1".to_string(), "column2" => Value::test_int(3),
"column2".to_string(), "column3" => Value::test_int(5),
"column3".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_int(1),
Value::test_int(3),
Value::test_int(5),
],
}), }),
], ],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Rotate table counter-clockwise and change columns names", description: "Rotate table counter-clockwise and change columns names",
example: "[[a b]; [1 2]] | rotate --ccw col_a col_b", example: "[[a b]; [1 2]] | rotate --ccw col_a col_b",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["col_a".to_string(), "col_b".to_string()], "col_a" => Value::test_string("b"),
vals: vec![Value::test_string("b"), Value::test_int(2)], "col_b" => Value::test_int(2),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["col_a".to_string(), "col_b".to_string()], "col_a" => Value::test_string("a"),
vals: vec![Value::test_string("a"), Value::test_int(1)], "col_b" => Value::test_int(1),
}), }),
], ],
Span::test_data(),
)), )),
}, },
] ]

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Block, Call}; use nu_protocol::ast::{Block, Call};
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
PipelineIterator, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineIterator, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
use std::collections::HashSet; use std::collections::HashSet;
use std::iter::FromIterator; use std::iter::FromIterator;
@ -51,29 +51,15 @@ impl Command for UpdateCells {
$value $value
} }
}"#, }"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "2021-04-16" => Value::test_int(37),
cols: vec![ "2021-06-10" => Value::test_string(""),
"2021-04-16".into(), "2021-09-18" => Value::test_string(""),
"2021-06-10".into(), "2021-10-15" => Value::test_string(""),
"2021-09-18".into(), "2021-11-16" => Value::test_int(37),
"2021-10-15".into(), "2021-11-17" => Value::test_string(""),
"2021-11-16".into(), "2021-11-18" => Value::test_string(""),
"2021-11-17".into(), })])),
"2021-11-18".into(),
],
vals: vec![
Value::test_int(37),
Value::test_string(""),
Value::test_string(""),
Value::test_string(""),
Value::test_int(37),
Value::test_string(""),
Value::test_string(""),
],
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Update the zero value cells to empty strings in 2 last columns.", description: "Update the zero value cells to empty strings in 2 last columns.",
@ -87,29 +73,15 @@ impl Command for UpdateCells {
$value $value
} }
}"#, }"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "2021-04-16" => Value::test_int(37),
cols: vec![ "2021-06-10" => Value::test_int(0),
"2021-04-16".into(), "2021-09-18" => Value::test_int(0),
"2021-06-10".into(), "2021-10-15" => Value::test_int(0),
"2021-09-18".into(), "2021-11-16" => Value::test_int(37),
"2021-10-15".into(), "2021-11-17" => Value::test_string(""),
"2021-11-16".into(), "2021-11-18" => Value::test_string(""),
"2021-11-17".into(), })])),
"2021-11-18".into(),
],
vals: vec![
Value::test_int(37),
Value::test_int(0),
Value::test_int(0),
Value::test_int(0),
Value::test_int(37),
Value::test_string(""),
Value::test_string(""),
],
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -1,7 +1,7 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -37,19 +37,11 @@ impl Command for FromUrl {
vec![Example { vec![Example {
example: "'bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter' | from url", example: "'bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter' | from url",
description: "Convert url encoded string into a record", description: "Convert url encoded string into a record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "bread" => Value::test_string("baguette"),
"bread".to_string(), "cheese" => Value::test_string("comté"),
"cheese".to_string(), "meat" => Value::test_string("ham"),
"meat".to_string(), "fat" => Value::test_string("butter"),
"fat".to_string(),
],
vals: vec![
Value::test_string("baguette"),
Value::test_string("comté"),
Value::test_string("ham"),
Value::test_string("butter"),
],
})), })),
}] }]
} }

View File

@ -2,7 +2,7 @@ use heck::ToLowerCamelCase;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
use super::operate; use super::operate;
@ -79,13 +79,10 @@ impl Command for SubCommand {
Example { Example {
description: "convert a column from a table to camelCase", description: "convert a column from a table to camelCase",
example: r#"[[lang, gems]; [nu_test, 100]] | str camel-case lang"#, example: r#"[[lang, gems]; [nu_test, 100]] | str camel-case lang"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "lang" => Value::test_string("nuTest"),
cols: vec!["lang".to_string(), "gems".to_string()], "gems" => Value::test_int(100),
vals: vec![Value::test_string("nuTest"), Value::test_int(100)], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,7 +2,7 @@ use heck::ToKebabCase;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
use super::operate; use super::operate;
@ -79,13 +79,10 @@ impl Command for SubCommand {
Example { Example {
description: "convert a column from a table to kebab-case", description: "convert a column from a table to kebab-case",
example: r#"[[lang, gems]; [nuTest, 100]] | str kebab-case lang"#, example: r#"[[lang, gems]; [nuTest, 100]] | str kebab-case lang"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "lang" => Value::test_string("nu-test"),
cols: vec!["lang".to_string(), "gems".to_string()], "gems" => Value::test_int(100),
vals: vec![Value::test_string("nu-test"), Value::test_int(100)], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,7 +2,7 @@ use heck::ToUpperCamelCase;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
use super::operate; use super::operate;
@ -79,13 +79,10 @@ impl Command for SubCommand {
Example { Example {
description: "convert a column from a table to PascalCase", description: "convert a column from a table to PascalCase",
example: r#"[[lang, gems]; [nu_test, 100]] | str pascal-case lang"#, example: r#"[[lang, gems]; [nu_test, 100]] | str pascal-case lang"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "lang" => Value::test_string("NuTest"),
cols: vec!["lang".to_string(), "gems".to_string()], "gems" => Value::test_int(100),
vals: vec![Value::test_string("NuTest"), Value::test_int(100)], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,7 +2,7 @@ use heck::ToShoutySnakeCase;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
use super::operate; use super::operate;
@ -79,13 +79,10 @@ impl Command for SubCommand {
Example { Example {
description: "convert a column from a table to SCREAMING_SNAKE_CASE", description: "convert a column from a table to SCREAMING_SNAKE_CASE",
example: r#"[[lang, gems]; [nu_test, 100]] | str screaming-snake-case lang"#, example: r#"[[lang, gems]; [nu_test, 100]] | str screaming-snake-case lang"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "lang" => Value::test_string("NU_TEST"),
cols: vec!["lang".to_string(), "gems".to_string()], "gems" => Value::test_int(100),
vals: vec![Value::test_string("NU_TEST"), Value::test_int(100)], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,7 +2,7 @@ use heck::ToSnakeCase;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
use super::operate; use super::operate;
@ -78,13 +78,10 @@ impl Command for SubCommand {
Example { Example {
description: "convert a column from a table to snake_case", description: "convert a column from a table to snake_case",
example: r#"[[lang, gems]; [nuTest, 100]] | str snake-case lang"#, example: r#"[[lang, gems]; [nuTest, 100]] | str snake-case lang"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "lang" => Value::test_string("nu_test"),
cols: vec!["lang".to_string(), "gems".to_string()], "gems" => Value::test_int(100),
vals: vec![Value::test_string("nu_test"), Value::test_int(100)], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,7 +2,7 @@ use heck::ToTitleCase;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
use super::operate; use super::operate;
@ -74,13 +74,10 @@ impl Command for SubCommand {
Example { Example {
description: "convert a column from a table to Title Case", description: "convert a column from a table to Title Case",
example: r#"[[title, count]; ['nu test', 100]] | str title-case title"#, example: r#"[[title, count]; ['nu test', 100]] | str title-case title"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "title" => Value::test_string("Nu Test"),
cols: vec!["title".to_string(), "count".to_string()], "count" => Value::test_int(100),
vals: vec![Value::test_string("Nu Test"), Value::test_int(100)], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -1,11 +1,11 @@
use nu_cmd_base::input_handler::{operate, CmdArgument}; use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_cmd_base::util; use nu_cmd_base::util;
use nu_engine::CallExt; use nu_engine::CallExt;
use nu_protocol::record;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Range, Record, ShellError, Signature, Span, SyntaxShape, Type, Category, Example, PipelineData, Range, ShellError, Signature, Span, SyntaxShape, Type, Value,
Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -94,44 +94,34 @@ impl Command for BytesAt {
Example { Example {
description: "Get a subbytes `0x[10 01]` from the bytes `0x[33 44 55 10 01 13]`", description: "Get a subbytes `0x[10 01]` from the bytes `0x[33 44 55 10 01 13]`",
example: " 0x[33 44 55 10 01 13] | bytes at 3..<4", example: " 0x[33 44 55 10 01 13] | bytes at 3..<4",
result: Some(Value::binary(vec![0x10], Span::test_data())), result: Some(Value::test_binary(vec![0x10])),
}, },
Example { Example {
description: "Get a subbytes `0x[10 01 13]` from the bytes `0x[33 44 55 10 01 13]`", description: "Get a subbytes `0x[10 01 13]` from the bytes `0x[33 44 55 10 01 13]`",
example: " 0x[33 44 55 10 01 13] | bytes at 3..6", example: " 0x[33 44 55 10 01 13] | bytes at 3..6",
result: Some(Value::binary(vec![0x10, 0x01, 0x13], Span::test_data())), result: Some(Value::test_binary(vec![0x10, 0x01, 0x13])),
}, },
Example { Example {
description: "Get the remaining characters from a starting index", description: "Get the remaining characters from a starting index",
example: " { data: 0x[33 44 55 10 01 13] } | bytes at 3.. data", example: " { data: 0x[33 44 55 10 01 13] } | bytes at 3.. data",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["data".to_string()], "data" => Value::test_binary(vec![0x10, 0x01, 0x13]),
vals: vec![Value::test_binary(vec![0x10, 0x01, 0x13])],
})), })),
}, },
Example { Example {
description: "Get the characters from the beginning until ending index", description: "Get the characters from the beginning until ending index",
example: " 0x[33 44 55 10 01 13] | bytes at ..<4", example: " 0x[33 44 55 10 01 13] | bytes at ..<4",
result: Some(Value::binary( result: Some(Value::test_binary(vec![0x33, 0x44, 0x55, 0x10])),
vec![0x33, 0x44, 0x55, 0x10],
Span::test_data(),
)),
}, },
Example { Example {
description: description:
"Or the characters from the beginning until ending index inside a table", "Or the characters from the beginning until ending index inside a table",
example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes at 1.. ColB ColC"#, example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes at 1.. ColB ColC"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "ColA" => Value::test_binary(vec![0x11, 0x12, 0x13]),
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()], "ColB" => Value::test_binary(vec![0x15, 0x16]),
vals: vec![ "ColC" => Value::test_binary(vec![0x18, 0x19]),
Value::binary(vec![0x11, 0x12, 0x13], Span::test_data()), })])),
Value::binary(vec![0x15, 0x16], Span::test_data()),
Value::binary(vec![0x18, 0x19], Span::test_data()),
],
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath}; use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
struct Arguments { struct Arguments {
@ -95,33 +95,29 @@ impl Command for BytesIndexOf {
Example { Example {
description: "Returns all matched index", description: "Returns all matched index",
example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of --all 0x[33 44]", example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of --all 0x[33 44]",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(0), Value::test_int(5), Value::test_int(7)], Value::test_int(0),
Span::test_data(), Value::test_int(5),
)), Value::test_int(7),
])),
}, },
Example { Example {
description: "Returns all matched index, searching from end", description: "Returns all matched index, searching from end",
example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of --all --end 0x[33 44]", example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of --all --end 0x[33 44]",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(7), Value::test_int(5), Value::test_int(0)], Value::test_int(7),
Span::test_data(), Value::test_int(5),
)), Value::test_int(0),
])),
}, },
Example { Example {
description: "Returns index of pattern for specific column", description: "Returns index of pattern for specific column",
example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes index-of 0x[11] ColA ColC"#, example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes index-of 0x[11] ColA ColC"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "ColA" => Value::test_int(0),
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()], "ColB" => Value::binary(vec![0x14, 0x15, 0x16], Span::test_data()),
vals: vec![ "ColC" => Value::test_int(-1),
Value::test_int(0), })])),
Value::binary(vec![0x14, 0x15, 0x16], Span::test_data()),
Value::test_int(-1),
],
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, record, Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value, Type, Value,
}; };
@ -87,49 +87,33 @@ impl Command for BytesRemove {
Example { Example {
description: "Remove contents", description: "Remove contents",
example: "0x[10 AA FF AA FF] | bytes remove 0x[10 AA]", example: "0x[10 AA FF AA FF] | bytes remove 0x[10 AA]",
result: Some(Value::binary ( result: Some(Value::test_binary (
vec![0xFF, 0xAA, 0xFF], vec![0xFF, 0xAA, 0xFF],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Remove all occurrences of find binary in record field", description: "Remove all occurrences of find binary in record field",
example: "{ data: 0x[10 AA 10 BB 10] } | bytes remove --all 0x[10] data", example: "{ data: 0x[10 AA 10 BB 10] } | bytes remove --all 0x[10] data",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["data".to_string()], "data" => Value::test_binary(vec![0xAA, 0xBB])
vals: vec![Value::test_binary(vec![0xAA, 0xBB])]
})), })),
}, },
Example { Example {
description: "Remove occurrences of find binary from end", description: "Remove occurrences of find binary from end",
example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[10]", example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[10]",
result: Some(Value::binary ( result: Some(Value::test_binary (
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA], vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Remove all occurrences of find binary in table", description: "Remove all occurrences of find binary in table",
example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC", example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC",
result: Some(Value::list ( result: Some(Value::test_list (
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()], "ColA" => Value::test_binary ( vec![0x12, 0x13],),
vals: vec![ "ColB" => Value::test_binary ( vec![0x14, 0x15, 0x16],),
Value::binary ( "ColC" => Value::test_binary ( vec![0x17, 0x18, 0x19],),
vec![0x12, 0x13],
Span::test_data(),
),
Value::binary (
vec![0x14, 0x15, 0x16],
Span::test_data(),
),
Value::binary (
vec![0x17, 0x18, 0x19],
Span::test_data(),
),
]
})], })],
Span::test_data(),
)), )),
}, },
] ]

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, record, Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value, Type, Value,
}; };
@ -87,41 +87,26 @@ impl Command for BytesReplace {
Example { Example {
description: "Find and replace contents", description: "Find and replace contents",
example: "0x[10 AA FF AA FF] | bytes replace 0x[10 AA] 0x[FF]", example: "0x[10 AA FF AA FF] | bytes replace 0x[10 AA] 0x[FF]",
result: Some(Value::binary ( result: Some(Value::test_binary (
vec![0xFF, 0xFF, 0xAA, 0xFF], vec![0xFF, 0xFF, 0xAA, 0xFF],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Find and replace all occurrences of find binary", description: "Find and replace all occurrences of find binary",
example: "0x[10 AA 10 BB 10] | bytes replace --all 0x[10] 0x[A0]", example: "0x[10 AA 10 BB 10] | bytes replace --all 0x[10] 0x[A0]",
result: Some(Value::binary ( result: Some(Value::test_binary (
vec![0xA0, 0xAA, 0xA0, 0xBB, 0xA0], vec![0xA0, 0xAA, 0xA0, 0xBB, 0xA0],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Find and replace all occurrences of find binary in table", description: "Find and replace all occurrences of find binary in table",
example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes replace --all 0x[11] 0x[13] ColA ColC", example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes replace --all 0x[11] 0x[13] ColA ColC",
result: Some(Value::list ( result: Some(Value::test_list (
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()], "ColA" => Value::test_binary(vec![0x13, 0x12, 0x13]),
vals: vec![ "ColB" => Value::test_binary(vec![0x14, 0x15, 0x16]),
Value::binary ( "ColC" => Value::test_binary(vec![0x17, 0x18, 0x19]),
vec![0x13, 0x12, 0x13],
Span::test_data(),
),
Value::binary (
vec![0x14, 0x15, 0x16],
Span::test_data(),
),
Value::binary (
vec![0x17, 0x18, 0x19],
Span::test_data(),
),
],
})], })],
Span::test_data(),
)), )),
}, },
] ]

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
Spanned, SyntaxShape, Type, Value, Spanned, SyntaxShape, Type, Value,
}; };
use std::collections::HashMap; use std::collections::HashMap;
@ -51,28 +51,21 @@ impl Command for Histogram {
Example { Example {
description: "Compute a histogram for a list of numbers", description: "Compute a histogram for a list of numbers",
example: "[1 2 1] | histogram", example: "[1 2 1] | histogram",
result: Some(Value::list ( result: Some(Value::test_list (
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["value".to_string(), "count".to_string(), "quantile".to_string(), "percentage".to_string(), "frequency".to_string()], "value" => Value::test_int(1),
vals: vec![ "count" => Value::test_int(2),
Value::test_int(1), "quantile" => Value::test_float(0.6666666666666666),
Value::test_int(2), "percentage" => Value::test_string("66.67%"),
Value::test_float(0.6666666666666666), "frequency" => Value::test_string("******************************************************************"),
Value::test_string("66.67%"),
Value::test_string("******************************************************************"),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string(), "count".to_string(), "quantile".to_string(), "percentage".to_string(), "frequency".to_string()], "value" => Value::test_int(2),
vals: vec![ "count" => Value::test_int(1),
Value::test_int(2), "quantile" => Value::test_float(0.3333333333333333),
Value::test_int(1), "percentage" => Value::test_string("33.33%"),
Value::test_float(0.3333333333333333), "frequency" => Value::test_string("*********************************"),
Value::test_string("33.33%"),
Value::test_string("*********************************"),
],
})], })],
Span::test_data(),
) )
), ),
}, },

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -53,61 +53,52 @@ impl Command for SubCommand {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![ vec![
Example { Example {
description: "Convert value to boolean in table", description: "Convert value to boolean in table",
example: "[[value]; ['false'] ['1'] [0] [1.0] [true]] | into bool value", example: "[[value]; ['false'] ['1'] [0] [1.0] [true]] | into bool value",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "value" => Value::test_bool(false),
cols: vec!["value".to_string()],
vals: vec![Value::bool(false, span)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_bool(true),
vals: vec![Value::bool(true, span)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_bool(false),
vals: vec![Value::bool(false, span)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_bool(true),
vals: vec![Value::bool(true, span)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_bool(true),
vals: vec![Value::bool(true, span)],
}), }),
], ])),
span,
)),
}, },
Example { Example {
description: "Convert bool to boolean", description: "Convert bool to boolean",
example: "true | into bool", example: "true | into bool",
result: Some(Value::bool(true, span)), result: Some(Value::test_bool(true)),
}, },
Example { Example {
description: "convert int to boolean", description: "convert int to boolean",
example: "1 | into bool", example: "1 | into bool",
result: Some(Value::bool(true, span)), result: Some(Value::test_bool(true)),
}, },
Example { Example {
description: "convert float to boolean", description: "convert float to boolean",
example: "0.3 | into bool", example: "0.3 | into bool",
result: Some(Value::bool(true, span)), result: Some(Value::test_bool(true)),
}, },
Example { Example {
description: "convert float string to boolean", description: "convert float string to boolean",
example: "'0.0' | into bool", example: "'0.0' | into bool",
result: Some(Value::bool(false, span)), result: Some(Value::test_bool(false)),
}, },
Example { Example {
description: "convert string to boolean", description: "convert string to boolean",
example: "'true' | into bool", example: "'true' | into bool",
result: Some(Value::bool(true, span)), result: Some(Value::test_bool(true)),
}, },
] ]
} }

View File

@ -3,7 +3,7 @@ use nu_parser::{parse_unit_value, DURATION_UNIT_GROUPS};
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath, Expr}, ast::{Call, CellPath, Expr},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Unit, record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Unit,
Value, Value,
}; };
@ -64,65 +64,55 @@ impl Command for SubCommand {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![ vec![
Example { Example {
description: "Convert duration string to duration value", description: "Convert duration string to duration value",
example: "'7min' | into duration", example: "'7min' | into duration",
result: Some(Value::duration(7 * 60 * NS_PER_SEC, span)), result: Some(Value::test_duration(7 * 60 * NS_PER_SEC)),
}, },
Example { Example {
description: "Convert compound duration string to duration value", description: "Convert compound duration string to duration value",
example: "'1day 2hr 3min 4sec' | into duration", example: "'1day 2hr 3min 4sec' | into duration",
result: Some(Value::duration( result: Some(Value::test_duration(
(((((/* 1 * */24) + 2) * 60) + 3) * 60 + 4) * NS_PER_SEC, (((((/* 1 * */24) + 2) * 60) + 3) * 60 + 4) * NS_PER_SEC,
span,
)), )),
}, },
Example { Example {
description: "Convert table of duration strings to table of duration values", description: "Convert table of duration strings to table of duration values",
example: example:
"[[value]; ['1sec'] ['2min'] ['3hr'] ['4day'] ['5wk']] | into duration value", "[[value]; ['1sec'] ['2min'] ['3hr'] ['4day'] ['5wk']] | into duration value",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "value" => Value::test_duration(NS_PER_SEC),
cols: vec!["value".to_string()],
vals: vec![Value::duration(NS_PER_SEC, span)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_duration(2 * 60 * NS_PER_SEC),
vals: vec![Value::duration(2 * 60 * NS_PER_SEC, span)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_duration(3 * 60 * 60 * NS_PER_SEC),
vals: vec![Value::duration(3 * 60 * 60 * NS_PER_SEC, span)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_duration(4 * 24 * 60 * 60 * NS_PER_SEC),
vals: vec![Value::duration(4 * 24 * 60 * 60 * NS_PER_SEC, span)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_duration(5 * 7 * 24 * 60 * 60 * NS_PER_SEC),
vals: vec![Value::duration(5 * 7 * 24 * 60 * 60 * NS_PER_SEC, span)],
}), }),
], ])),
span,
)),
}, },
Example { Example {
description: "Convert duration to duration", description: "Convert duration to duration",
example: "420sec | into duration", example: "420sec | into duration",
result: Some(Value::duration(7 * 60 * NS_PER_SEC, span)), result: Some(Value::test_duration(7 * 60 * NS_PER_SEC)),
}, },
Example { Example {
description: "Convert a number of ns to duration", description: "Convert a number of ns to duration",
example: "1_234_567 | into duration", example: "1_234_567 | into duration",
result: Some(Value::duration(1_234_567, span)), result: Some(Value::test_duration(1_234_567)),
}, },
Example { Example {
description: "Convert a number of an arbitrary unit to duration", description: "Convert a number of an arbitrary unit to duration",
example: "1_234 | into duration --unit ms", example: "1_234 | into duration --unit ms",
result: Some(Value::duration(1_234 * 1_000_000, span)), result: Some(Value::test_duration(1_234 * 1_000_000)),
}, },
] ]
} }

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -79,45 +79,36 @@ impl Command for SubCommand {
Example { Example {
description: "Convert string to filesize in table", description: "Convert string to filesize in table",
example: r#"[[device size]; ["/dev/sda1" "200"] ["/dev/loop0" "50"]] | into filesize size"#, example: r#"[[device size]; ["/dev/sda1" "200"] ["/dev/loop0" "50"]] | into filesize size"#,
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "device" => Value::test_string("/dev/sda1"),
cols: vec!["device".to_string(), "size".to_string()], "size" => Value::test_filesize(200),
vals: vec![
Value::string("/dev/sda1".to_string(), Span::test_data()),
Value::filesize(200, Span::test_data()),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["device".to_string(), "size".to_string()], "device" => Value::test_string("/dev/loop0"),
vals: vec![ "size" => Value::test_filesize(50),
Value::string("/dev/loop0".to_string(), Span::test_data()),
Value::filesize(50, Span::test_data()),
],
}), }),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Convert string to filesize", description: "Convert string to filesize",
example: "'2' | into filesize", example: "'2' | into filesize",
result: Some(Value::filesize(2, Span::test_data())), result: Some(Value::test_filesize(2)),
}, },
Example { Example {
description: "Convert float to filesize", description: "Convert float to filesize",
example: "8.3 | into filesize", example: "8.3 | into filesize",
result: Some(Value::filesize(8, Span::test_data())), result: Some(Value::test_filesize(8)),
}, },
Example { Example {
description: "Convert int to filesize", description: "Convert int to filesize",
example: "5 | into filesize", example: "5 | into filesize",
result: Some(Value::filesize(5, Span::test_data())), result: Some(Value::test_filesize(5)),
}, },
Example { Example {
description: "Convert file size to filesize", description: "Convert file size to filesize",
example: "4KB | into filesize", example: "4KB | into filesize",
result: Some(Value::filesize(4000, Span::test_data())), result: Some(Value::test_filesize(4000)),
}, },
] ]
} }

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -62,9 +62,8 @@ impl Command for SubCommand {
Example { Example {
description: "Convert string to float in table", description: "Convert string to float in table",
example: "[[num]; ['5.01']] | into float num", example: "[[num]; ['5.01']] | into float num",
result: Some(Value::test_list(vec![Value::test_record(Record { result: Some(Value::test_list(vec![Value::test_record(record! {
cols: vec!["num".to_string()], "num" => Value::test_float(5.01),
vals: vec![Value::test_float(5.01)],
})])), })])),
}, },
Example { Example {

View File

@ -46,90 +46,62 @@ impl Command for SubCommand {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![ vec![
Example { Example {
description: "Convert from one row table to record", description: "Convert from one row table to record",
example: "[[value]; [false]] | into record", example: "[[value]; [false]] | into record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_bool(false),
vals: vec![Value::bool(false, span)],
})), })),
}, },
Example { Example {
description: "Convert from list to record", description: "Convert from list to record",
example: "[1 2 3] | into record", example: "[1 2 3] | into record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["0".to_string(), "1".to_string(), "2".to_string()], "0" => Value::test_int(1),
vals: vec![ "1" => Value::test_int(2),
Value::int(1, span), "2" => Value::test_int(3),
Value::int(2, span),
Value::int(3, span),
],
})), })),
}, },
Example { Example {
description: "Convert from range to record", description: "Convert from range to record",
example: "0..2 | into record", example: "0..2 | into record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["0".to_string(), "1".to_string(), "2".to_string()], "0" => Value::test_int(0),
vals: vec![ "1" => Value::test_int(1),
Value::int(0, span), "2" => Value::test_int(2),
Value::int(1, span),
Value::int(2, span),
],
})), })),
}, },
Example { Example {
description: "convert duration to record (weeks max)", description: "convert duration to record (weeks max)",
example: "(-500day - 4hr - 5sec) | into record", example: "(-500day - 4hr - 5sec) | into record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "week" => Value::test_int(71),
"week".into(), "day" => Value::test_int(3),
"day".into(), "hour" => Value::test_int(4),
"hour".into(), "second" => Value::test_int(5),
"second".into(), "sign" => Value::test_string("-"),
"sign".into(),
],
vals: vec![
Value::int(71, span),
Value::int(3, span),
Value::int(4, span),
Value::int(5, span),
Value::string("-", span),
],
})), })),
}, },
Example { Example {
description: "convert record to record", description: "convert record to record",
example: "{a: 1, b: 2} | into record", example: "{a: 1, b: 2} | into record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![Value::int(1, span), Value::int(2, span)], "b" => Value::test_int(2),
})), })),
}, },
Example { Example {
description: "convert date to record", description: "convert date to record",
example: "2020-04-12T22:10:57+02:00 | into record", example: "2020-04-12T22:10:57+02:00 | into record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "year" => Value::test_int(2020),
"year".into(), "month" => Value::test_int(4),
"month".into(), "day" => Value::test_int(12),
"day".into(), "hour" => Value::test_int(22),
"hour".into(), "minute" => Value::test_int(10),
"minute".into(), "second" => Value::test_int(57),
"second".into(), "timezone" => Value::test_string("+02:00"),
"timezone".into(),
],
vals: vec![
Value::int(2020, span),
Value::int(4, span),
Value::int(12, span),
Value::int(22, span),
Value::int(10, span),
Value::int(57, span),
Value::string("+02:00".to_string(), span),
],
})), })),
}, },
] ]

View File

@ -455,6 +455,8 @@ pub fn convert_sqlite_value_to_nu_value(value: ValueRef, span: Span) -> Value {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use nu_protocol::record;
use super::*; use super::*;
#[test] #[test]
@ -482,9 +484,8 @@ mod test {
.unwrap(); .unwrap();
let converted_db = read_entire_sqlite_db(db, Span::test_data(), None).unwrap(); let converted_db = read_entire_sqlite_db(db, Span::test_data(), None).unwrap();
let expected = Value::test_record(Record { let expected = Value::test_record(record! {
cols: vec!["person".to_string()], "person" => Value::test_list(vec![]),
vals: vec![Value::list(vec![], Span::test_data())],
}); });
assert_eq!(converted_db, expected); assert_eq!(converted_db, expected);
@ -512,24 +513,19 @@ mod test {
let converted_db = read_entire_sqlite_db(db, span, None).unwrap(); let converted_db = read_entire_sqlite_db(db, span, None).unwrap();
let expected = Value::test_record(Record { let expected = Value::test_record(record! {
cols: vec!["item".to_string()], "item" => Value::test_list(
vals: vec![Value::list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["id".to_string(), "name".to_string()], "id" => Value::test_int(123),
vals: vec![Value::int(123, span), Value::nothing(span)], "name" => Value::nothing(span),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["id".to_string(), "name".to_string()], "id" => Value::test_int(456),
vals: vec![ "name" => Value::test_string("foo bar"),
Value::int(456, span),
Value::string("foo bar".to_string(), span),
],
}), }),
], ]
span, ),
)],
}); });
assert_eq!(converted_db, expected); assert_eq!(converted_db, expected);

View File

@ -2,8 +2,8 @@ use chrono_tz::TZ_VARIANTS;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Signature, Span, Type, Value, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -52,13 +52,9 @@ impl Command for SubCommand {
vec![Example { vec![Example {
example: "date list-timezone | where timezone =~ Shanghai", example: "date list-timezone | where timezone =~ Shanghai",
description: "Show timezone(s) that contains 'Shanghai'", description: "Show timezone(s) that contains 'Shanghai'",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "timezone" => Value::test_string("Asia/Shanghai"),
cols: vec!["timezone".into()], })])),
vals: vec![Value::test_string("Asia/Shanghai")],
})],
Span::test_data(),
)),
}] }]
} }
} }

View File

@ -3,7 +3,7 @@ use chrono::{DateTime, Datelike, FixedOffset, Local, Timelike};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, PipelineData, Record, ShellError, ShellError::DatetimeParseError, record, Category, Example, PipelineData, ShellError, ShellError::DatetimeParseError,
ShellError::PipelineEmpty, Signature, Span, Type, Value, ShellError::PipelineEmpty, Signature, Span, Type, Value,
}; };
@ -49,56 +49,6 @@ impl Command for SubCommand {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let example_result_1 = || {
let span = Span::test_data();
let cols = vec![
"year".into(),
"month".into(),
"day".into(),
"hour".into(),
"minute".into(),
"second".into(),
"nanosecond".into(),
"timezone".into(),
];
let vals = vec![
Value::int(2020, span),
Value::int(4, span),
Value::int(12, span),
Value::int(22, span),
Value::int(10, span),
Value::int(57, span),
Value::int(123_000_000, span),
Value::string("+02:00", span),
];
Some(Value::test_record(Record { cols, vals }))
};
let example_result_2 = || {
let span = Span::test_data();
let cols = vec![
"year".into(),
"month".into(),
"day".into(),
"hour".into(),
"minute".into(),
"second".into(),
"nanosecond".into(),
"timezone".into(),
];
let vals = vec![
Value::int(2020, span),
Value::int(4, span),
Value::int(12, span),
Value::int(22, span),
Value::int(10, span),
Value::int(57, span),
Value::int(0, span),
Value::string("+02:00", span),
];
Some(Value::test_record(Record { cols, vals }))
};
vec![ vec![
Example { Example {
description: "Convert the current date into a record.", description: "Convert the current date into a record.",
@ -113,12 +63,30 @@ impl Command for SubCommand {
Example { Example {
description: "Convert a date string into a record.", description: "Convert a date string into a record.",
example: "'2020-04-12T22:10:57.123+02:00' | date to-record", example: "'2020-04-12T22:10:57.123+02:00' | date to-record",
result: example_result_1(), result: Some(Value::test_record(record!(
"year" => Value::test_int(2020),
"month" => Value::test_int(4),
"day" => Value::test_int(12),
"hour" => Value::test_int(22),
"minute" => Value::test_int(10),
"second" => Value::test_int(57),
"nanosecond" => Value::test_int(123_000_000),
"timezone" => Value::test_string("+02:00"),
))),
}, },
Example { Example {
description: "Convert a date into a record.", description: "Convert a date into a record.",
example: "'2020-04-12 22:10:57 +0200' | into datetime | date to-record", example: "'2020-04-12 22:10:57 +0200' | into datetime | date to-record",
result: example_result_2(), result: Some(Value::test_record(record!(
"year" => Value::test_int(2020),
"month" => Value::test_int(4),
"day" => Value::test_int(12),
"hour" => Value::test_int(22),
"minute" => Value::test_int(10),
"second" => Value::test_int(57),
"nanosecond" => Value::test_int(0),
"timezone" => Value::test_string("+02:00"),
))),
}, },
] ]
} }

View File

@ -3,7 +3,7 @@ use chrono::{DateTime, Datelike, FixedOffset, Local, Timelike};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, PipelineData, Record, ShellError, ShellError::DatetimeParseError, record, Category, Example, PipelineData, ShellError, ShellError::DatetimeParseError,
ShellError::PipelineEmpty, Signature, Span, Type, Value, ShellError::PipelineEmpty, Signature, Span, Type, Value,
}; };
@ -49,62 +49,6 @@ impl Command for SubCommand {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let example_result_1 = || {
let span = Span::test_data();
let cols = vec![
"year".into(),
"month".into(),
"day".into(),
"hour".into(),
"minute".into(),
"second".into(),
"nanosecond".into(),
"timezone".into(),
];
let vals = vec![
Value::int(2020, span),
Value::int(4, span),
Value::int(12, span),
Value::int(22, span),
Value::int(10, span),
Value::int(57, span),
Value::int(789, span),
Value::string("+02:00".to_string(), span),
];
Some(Value::list(
vec![Value::test_record(Record { cols, vals })],
span,
))
};
let example_result_2 = || {
let span = Span::test_data();
let cols = vec![
"year".into(),
"month".into(),
"day".into(),
"hour".into(),
"minute".into(),
"second".into(),
"nanosecond".into(),
"timezone".into(),
];
let vals = vec![
Value::int(2020, span),
Value::int(4, span),
Value::int(12, span),
Value::int(22, span),
Value::int(10, span),
Value::int(57, span),
Value::int(0, span),
Value::string("+02:00".to_string(), span),
];
Some(Value::list(
vec![Value::test_record(Record { cols, vals })],
span,
))
};
vec![ vec![
Example { Example {
description: "Convert the current date into a table.", description: "Convert the current date into a table.",
@ -119,12 +63,30 @@ impl Command for SubCommand {
Example { Example {
description: "Convert a given date into a table.", description: "Convert a given date into a table.",
example: "2020-04-12T22:10:57.000000789+02:00 | date to-table", example: "2020-04-12T22:10:57.000000789+02:00 | date to-table",
result: example_result_1(), result: Some(Value::test_list(vec![Value::test_record(record!(
"year" => Value::test_int(2020),
"month" => Value::test_int(4),
"day" => Value::test_int(12),
"hour" => Value::test_int(22),
"minute" => Value::test_int(10),
"second" => Value::test_int(57),
"nanosecond" => Value::test_int(789),
"timezone" => Value::test_string("+02:00".to_string()),
))])),
}, },
Example { Example {
description: "Convert a given date into a table.", description: "Convert a given date into a table.",
example: "'2020-04-12 22:10:57 +0200' | into datetime | date to-table", example: "'2020-04-12 22:10:57 +0200' | into datetime | date to-table",
result: example_result_2(), result: Some(Value::test_list(vec![Value::test_record(record!(
"year" => Value::test_int(2020),
"month" => Value::test_int(4),
"day" => Value::test_int(12),
"hour" => Value::test_int(22),
"minute" => Value::test_int(10),
"second" => Value::test_int(57),
"nanosecond" => Value::test_int(0),
"timezone" => Value::test_string("+02:00".to_string()),
))])),
}, },
] ]
} }

View File

@ -1,7 +1,7 @@
use nu_engine::CallExt; use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, engine::Command, engine::EngineState, engine::Stack, Category, Example, ast::Call, engine::Command, engine::EngineState, engine::Stack, record, Category, Example,
PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -54,26 +54,23 @@ impl Command for Compact {
Example { Example {
description: "Filter out all records where 'Hello' is null (returns nothing)", description: "Filter out all records where 'Hello' is null (returns nothing)",
example: r#"[["Hello" "World"]; [null 3]] | compact Hello"#, example: r#"[["Hello" "World"]; [null 3]] | compact Hello"#,
result: Some(Value::list(vec![], Span::test_data())), result: Some(Value::test_list(vec![])),
}, },
Example { Example {
description: "Filter out all records where 'World' is null (Returns the table)", description: "Filter out all records where 'World' is null (Returns the table)",
example: r#"[["Hello" "World"]; [null 3]] | compact World"#, example: r#"[["Hello" "World"]; [null 3]] | compact World"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "Hello" => Value::nothing(Span::test_data()),
cols: vec!["Hello".into(), "World".into()], "World" => Value::test_int(3),
vals: vec![Value::nothing(Span::test_data()), Value::test_int(3)], })])),
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Filter out all instances of nothing from a list (Returns [1,2])", description: "Filter out all instances of nothing from a list (Returns [1,2])",
example: r#"[1, null, 2] | compact"#, example: r#"[1, null, 2] | compact"#,
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(1), Value::test_int(2)], Value::test_int(1),
Span::test_data(), Value::test_int(2),
)), ])),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath}; use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
Record, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -66,14 +66,8 @@ impl Command for DropColumn {
example: "[[lib, extension]; [nu-lib, rs] [nu-core, rb]] | drop column", example: "[[lib, extension]; [nu-lib, rs] [nu-core, rb]] | drop column",
result: Some(Value::list( result: Some(Value::list(
vec![ vec![
Value::test_record(Record { Value::test_record(record!("lib" =>Value::test_string("nu-lib"))),
cols: vec!["lib".into()], Value::test_record(record!("lib" =>Value::test_string("nu-core"))),
vals: vec![Value::test_string("nu-lib")],
}),
Value::test_record(Record {
cols: vec!["lib".into()],
vals: vec![Value::test_string("nu-core")],
}),
], ],
Span::test_data(), Span::test_data(),
)), )),

View File

@ -3,8 +3,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Span, SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -41,42 +41,37 @@ impl Command for Drop {
Example { Example {
example: "[0,1,2,3] | drop", example: "[0,1,2,3] | drop",
description: "Remove the last item of a list", description: "Remove the last item of a list",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(0), Value::test_int(1), Value::test_int(2)], Value::test_int(0),
Span::test_data(), Value::test_int(1),
)), Value::test_int(2),
])),
}, },
Example { Example {
example: "[0,1,2,3] | drop 0", example: "[0,1,2,3] | drop 0",
description: "Remove zero item of a list", description: "Remove zero item of a list",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![
Value::test_int(0), Value::test_int(0),
Value::test_int(1), Value::test_int(1),
Value::test_int(2), Value::test_int(2),
Value::test_int(3), Value::test_int(3),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
example: "[0,1,2,3] | drop 2", example: "[0,1,2,3] | drop 2",
description: "Remove the last two items of a list", description: "Remove the last two items of a list",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(0), Value::test_int(1)], Value::test_int(0),
Span::test_data(), Value::test_int(1),
)), ])),
}, },
Example { Example {
description: "Remove the last row in a table", description: "Remove the last row in a table",
example: "[[a, b]; [1, 2] [3, 4]] | drop 1", example: "[[a, b]; [1, 2] [3, 4]] | drop 1",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "a" => Value::test_int(1),
cols: vec!["a".to_string(), "b".to_string()], "b" => Value::test_int(2),
vals: vec![Value::test_int(1), Value::test_int(2)], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -1,8 +1,8 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Signature, Span, Type, Value, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -31,23 +31,20 @@ impl Command for Enumerate {
vec![Example { vec![Example {
description: "Add an index to each element of a list", description: "Add an index to each element of a list",
example: r#"[a, b, c] | enumerate "#, example: r#"[a, b, c] | enumerate "#,
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "index" => Value::test_int(0),
cols: vec!["index".into(), "item".into()], "item" => Value::test_string("a"),
vals: vec![Value::test_int(0), Value::test_string("a")],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".into(), "item".into()], "index" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_string("b")], "item" => Value::test_string("b"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".into(), "item".into()], "index" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_string("c")], "item" => Value::test_string("c"),
}), }),
], ])),
Span::test_data(),
)),
}] }]
} }

View File

@ -3,8 +3,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, SyntaxShape, Type, Value, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -195,43 +195,36 @@ a variable. On the other hand, the "row condition" syntax is not supported."#
Example { Example {
description: "Filter items of a list according to a condition", description: "Filter items of a list according to a condition",
example: "[1 2] | filter {|x| $x > 1}", example: "[1 2] | filter {|x| $x > 1}",
result: Some(Value::list(vec![Value::test_int(2)], Span::test_data())), result: Some(Value::test_list(vec![Value::test_int(2)])),
}, },
Example { Example {
description: "Filter rows of a table according to a condition", description: "Filter rows of a table according to a condition",
example: "[{a: 1} {a: 2}] | filter {|x| $x.a > 1}", example: "[{a: 1} {a: 2}] | filter {|x| $x.a > 1}",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "a" => Value::test_int(2),
cols: vec!["a".to_string()], })])),
vals: vec![Value::test_int(2)],
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Filter rows of a table according to a stored condition", description: "Filter rows of a table according to a stored condition",
example: "let cond = {|x| $x.a > 1}; [{a: 1} {a: 2}] | filter $cond", example: "let cond = {|x| $x.a > 1}; [{a: 1} {a: 2}] | filter $cond",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "a" => Value::test_int(2),
cols: vec!["a".to_string()], })])),
vals: vec![Value::test_int(2)],
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Filter items of a range according to a condition", description: "Filter items of a range according to a condition",
example: "9..13 | filter {|el| $el mod 2 != 0}", example: "9..13 | filter {|el| $el mod 2 != 0}",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(9), Value::test_int(11), Value::test_int(13)], Value::test_int(9),
Span::test_data(), Value::test_int(11),
)), Value::test_int(13),
])),
}, },
Example { Example {
description: "List all numbers above 3, using an existing closure condition", description: "List all numbers above 3, using an existing closure condition",
example: "let a = {$in > 3}; [1, 2, 5, 6] | filter $a", example: "let a = {$in > 3}; [1, 2, 5, 6] | filter $a",
result: None, // TODO: This should work result: None, // TODO: This should work
// result: Some(Value::list( // result: Some(Value::test_list(
// vec![ // vec![
// Value::Int { // Value::Int {
// val: 5, // val: 5,
@ -242,7 +235,6 @@ a variable. On the other hand, the "row condition" syntax is not supported."#
// span: Span::test_data(), // span: Span::test_data(),
// }, // },
// ], // ],
// Span::test_data(),
// }), // }),
}, },
] ]

View File

@ -8,7 +8,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Config, Example, IntoInterruptiblePipelineData, IntoPipelineData, ListStream, record, Category, Config, Example, IntoInterruptiblePipelineData, IntoPipelineData, ListStream,
PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
@ -125,38 +125,27 @@ impl Command for Find {
Example { Example {
description: "Find value in records using regex", description: "Find value in records using regex",
example: r#"[[version name]; ['0.1.0' nushell] ['0.1.1' fish] ['0.2.0' zsh]] | find --regex "nu""#, example: r#"[[version name]; ['0.1.0' nushell] ['0.1.1' fish] ['0.2.0' zsh]] | find --regex "nu""#,
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["version".to_string(), "name".to_string()], "version" => Value::test_string("0.1.0"),
vals: vec![ "name" => Value::test_string("nushell".to_string()),
Value::test_string("0.1.0"),
Value::test_string("nushell".to_string()),
],
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Find inverted values in records using regex", description: "Find inverted values in records using regex",
example: r#"[[version name]; ['0.1.0' nushell] ['0.1.1' fish] ['0.2.0' zsh]] | find --regex "nu" --invert"#, example: r#"[[version name]; ['0.1.0' nushell] ['0.1.1' fish] ['0.2.0' zsh]] | find --regex "nu" --invert"#,
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record!{
cols: vec!["version".to_string(), "name".to_string()], "version" => Value::test_string("0.1.1"),
vals: vec![ "name" => Value::test_string("fish".to_string()),
Value::test_string("0.1.1"),
Value::test_string("fish".to_string()),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["version".to_string(), "name".to_string()], "version" => Value::test_string("0.2.0"),
vals: vec![ "name" =>Value::test_string("zsh".to_string()),
Value::test_string("0.2.0"),
Value::test_string("zsh".to_string()),
],
}), }),
], ],
Span::test_data(),
)), )),
}, },
Example { Example {
@ -191,16 +180,13 @@ impl Command for Find {
example: example:
"[[col1 col2 col3]; [moe larry curly] [larry curly moe]] | find moe --columns [col1]", "[[col1 col2 col3]; [moe larry curly] [larry curly moe]] | find moe --columns [col1]",
result: Some(Value::list( result: Some(Value::list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["col1".to_string(), "col2".to_string(), "col3".to_string()], "col1" => Value::test_string(
vals: vec![
Value::test_string(
"\u{1b}[37m\u{1b}[0m\u{1b}[41;37mmoe\u{1b}[0m\u{1b}[37m\u{1b}[0m" "\u{1b}[37m\u{1b}[0m\u{1b}[41;37mmoe\u{1b}[0m\u{1b}[37m\u{1b}[0m"
.to_string(), .to_string(),
), ),
Value::test_string("larry".to_string()), "col2" => Value::test_string("larry".to_string()),
Value::test_string("curly".to_string()), "col3" => Value::test_string("curly".to_string()),
],
})], })],
Span::test_data(), Span::test_data(),
)), )),

View File

@ -4,7 +4,8 @@ use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape,
Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -52,7 +53,7 @@ impl Command for Flatten {
Example { Example {
description: "flatten a table", description: "flatten a table",
example: "[[N, u, s, h, e, l, l]] | flatten ", example: "[[N, u, s, h, e, l, l]] | flatten ",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_string("N"), Value::test_string("N"),
Value::test_string("u"), Value::test_string("u"),
@ -61,7 +62,6 @@ impl Command for Flatten {
Value::test_string("e"), Value::test_string("e"),
Value::test_string("l"), Value::test_string("l"),
Value::test_string("l")], Value::test_string("l")],
Span::test_data()
)) ))
}, },
Example { Example {
@ -84,49 +84,33 @@ impl Command for Flatten {
example: "{ a: b, d: [ 1 2 3 4 ], e: [ 4 3 ] } | flatten d --all", example: "{ a: b, d: [ 1 2 3 4 ], e: [ 4 3 ] } | flatten d --all",
result: Some(Value::list( result: Some(Value::list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "d".to_string(), "e".to_string()], "a" => Value::test_string("b"),
vals: vec![ "d" => Value::test_int(1),
Value::test_string("b"), "e" => Value::test_list(
Value::test_int(1),
Value::list(
vec![Value::test_int(4), Value::test_int(3)], vec![Value::test_int(4), Value::test_int(3)],
Span::test_data(),
), ),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "d".to_string(), "e".to_string()], "a" => Value::test_string("b"),
vals: vec![ "d" => Value::test_int(2),
Value::test_string("b"), "e" => Value::test_list(
Value::test_int(2),
Value::list(
vec![Value::test_int(4), Value::test_int(3)], vec![Value::test_int(4), Value::test_int(3)],
Span::test_data(),
), ),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "d".to_string(), "e".to_string()], "a" => Value::test_string("b"),
vals: vec![ "d" => Value::test_int(3),
Value::test_string("b"), "e" => Value::test_list(
Value::test_int(3),
Value::list(
vec![Value::test_int(4), Value::test_int(3)], vec![Value::test_int(4), Value::test_int(3)],
Span::test_data(),
), ),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "d".to_string(), "e".to_string()], "a" => Value::test_string("b"),
vals: vec![ "d" => Value::test_int(4),
Value::test_string("b"), "e" => Value::test_list(
Value::test_int(4),
Value::list(
vec![Value::test_int(4), Value::test_int(3)], vec![Value::test_int(4), Value::test_int(3)],
Span::test_data()
) )
],
}), }),
], ],
Span::test_data(), Span::test_data(),

View File

@ -2,7 +2,7 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath}; use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
@ -68,48 +68,37 @@ impl Command for GroupBy {
Example { Example {
description: "Group using a block which is evaluated against each input value", description: "Group using a block which is evaluated against each input value",
example: "[foo.txt bar.csv baz.txt] | group-by { path parse | get extension }", example: "[foo.txt bar.csv baz.txt] | group-by { path parse | get extension }",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["txt".to_string(), "csv".to_string()], "txt" => Value::test_list(
vals: vec![
Value::list(
vec![ vec![
Value::test_string("foo.txt"), Value::test_string("foo.txt"),
Value::test_string("baz.txt"), Value::test_string("baz.txt"),
], ],
Span::test_data(),
), ),
Value::list( "csv" => Value::test_list(
vec![Value::test_string("bar.csv")], vec![Value::test_string("bar.csv")],
Span::test_data(),
), ),
],
})), })),
}, },
Example { Example {
description: "You can also group by raw values by leaving out the argument", description: "You can also group by raw values by leaving out the argument",
example: "['1' '3' '1' '3' '2' '1' '1'] | group-by", example: "['1' '3' '1' '3' '2' '1' '1'] | group-by",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["1".to_string(), "3".to_string(), "2".to_string()], "1" => Value::test_list(
vals: vec![
Value::list(
vec![ vec![
Value::test_string("1"), Value::test_string("1"),
Value::test_string("1"), Value::test_string("1"),
Value::test_string("1"), Value::test_string("1"),
Value::test_string("1"), Value::test_string("1"),
], ],
Span::test_data(),
), ),
Value::list( "3" => Value::test_list(
vec![Value::test_string("3"), Value::test_string("3")], vec![Value::test_string("3"), Value::test_string("3")],
Span::test_data(),
), ),
Value::list( "2" => Value::test_list(
vec![Value::test_string("2")], vec![Value::test_string("2")],
Span::test_data(),
), ),
],
})), })),
}, },
] ]

View File

@ -1,8 +1,8 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type,
Type, Value, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -31,47 +31,31 @@ impl Command for Headers {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let columns = vec!["a".to_string(), "b".to_string(), "c".to_string()];
vec![ vec![
Example { Example {
description: "Sets the column names for a table created by `split column`", description: "Sets the column names for a table created by `split column`",
example: r#""a b c|1 2 3" | split row "|" | split column " " | headers"#, example: r#""a b c|1 2 3" | split row "|" | split column " " | headers"#,
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "a" => Value::test_string("1"),
cols: columns.clone(), "b" => Value::test_string("2"),
vals: vec![ "c" => Value::test_string("3"),
Value::test_string("1"), })])),
Value::test_string("2"),
Value::test_string("3"),
],
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Columns which don't have data in their first row are removed", description: "Columns which don't have data in their first row are removed",
example: r#""a b c|1 2 3|1 2 3 4" | split row "|" | split column " " | headers"#, example: r#""a b c|1 2 3|1 2 3 4" | split row "|" | split column " " | headers"#,
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_string("1"),
cols: columns.clone(), "b" => Value::test_string("2"),
vals: vec![ "c" => Value::test_string("3"),
Value::test_string("1"),
Value::test_string("2"),
Value::test_string("3"),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: columns, "a" => Value::test_string("1"),
vals: vec![ "b" => Value::test_string("2"),
Value::test_string("1"), "c" => Value::test_string("3"),
Value::test_string("2"),
Value::test_string("3"),
],
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath, PathMember}; use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
Record, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -60,57 +60,44 @@ impl Command for Insert {
vec![Example { vec![Example {
description: "Insert a new entry into a single record", description: "Insert a new entry into a single record",
example: "{'name': 'nu', 'stars': 5} | insert alias 'Nushell'", example: "{'name': 'nu', 'stars': 5} | insert alias 'Nushell'",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["name".into(), "stars".into(), "alias".into()], "name" => Value::test_string("nu"),
vals: vec![ "stars" => Value::test_int(5),
Value::test_string("nu"), "alias" => Value::test_string("Nushell"),
Value::test_int(5),
Value::test_string("Nushell"),
],
})), })),
}, },
Example { Example {
description: "Insert a new column into a table, populating all rows", description: "Insert a new column into a table, populating all rows",
example: "[[project, lang]; ['Nushell', 'Rust']] | insert type 'shell'", example: "[[project, lang]; ['Nushell', 'Rust']] | insert type 'shell'",
result: Some(Value::list ( result: Some(Value::test_list (
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["project".into(), "lang".into(), "type".into()], "project" => Value::test_string("Nushell"),
vals: vec![Value::test_string("Nushell"), Value::test_string("Rust"), Value::test_string("shell")], "lang" => Value::test_string("Rust"),
"type" => Value::test_string("shell"),
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Insert a column with values equal to their row index, plus the value of 'foo' in each row", description: "Insert a column with values equal to their row index, plus the value of 'foo' in each row",
example: "[[foo]; [7] [8] [9]] | enumerate | insert bar {|e| $e.item.foo + $e.index } | flatten", example: "[[foo]; [7] [8] [9]] | enumerate | insert bar {|e| $e.item.foo + $e.index } | flatten",
result: Some(Value::list ( result: Some(Value::test_list (
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".into(), "foo".into(), "bar".into()], "index" => Value::test_int(0),
vals: vec![ "foo" => Value::test_int(7),
Value::test_int(0), "bar" => Value::test_int(7),
Value::test_int(7),
Value::test_int(7),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".into(),"foo".into(), "bar".into()], "index" => Value::test_int(1),
vals: vec![ "foo" => Value::test_int(8),
Value::test_int(1), "bar" => Value::test_int(9),
Value::test_int(8),
Value::test_int(9),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".into(), "foo".into(), "bar".into()], "index" => Value::test_int(2),
vals: vec![ "foo" => Value::test_int(9),
Value::test_int(2), "bar" => Value::test_int(11),
Value::test_int(9),
Value::test_int(11),
],
}), }),
], ],
Span::test_data(),
)), )),
}] }]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Config, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, record, Category, Config, Example, PipelineData, Record, ShellError, Signature, Span,
Type, Value, SyntaxShape, Type, Value,
}; };
use std::cmp::max; use std::cmp::max;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -108,13 +108,9 @@ impl Command for Join {
vec![Example { vec![Example {
description: "Join two tables", description: "Join two tables",
example: "[{a: 1 b: 2}] | join [{a: 1 c: 3}] a", example: "[{a: 1 b: 2}] | join [{a: 1 c: 3}] a",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "a" => Value::test_int(1), "b" => Value::test_int(2), "c" => Value::test_int(3),
cols: vec!["a".into(), "b".into(), "c".into()], })])),
vals: vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
})],
Span::test_data(),
)),
}] }]
} }
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, SyntaxShape, Type, Value, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -48,17 +48,17 @@ repeating this process with row 1, and so on."#
description: "Add an 'index' column to the input table", description: "Add an 'index' column to the input table",
result: Some(Value::list( result: Some(Value::list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["name".to_string(), "index".to_string()], "name" => Value::test_string("a"),
vals: vec![Value::test_string("a"), Value::test_int(1)], "index" => Value::test_int(1),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["name".to_string(), "index".to_string()], "name" => Value::test_string("b"),
vals: vec![Value::test_string("b"), Value::test_int(2)], "index" => Value::test_int(2),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["name".to_string(), "index".to_string()], "name" => Value::test_string("c"),
vals: vec![Value::test_string("c"), Value::test_int(3)], "index" => Value::test_int(3),
}), }),
], ],
Span::test_data(), Span::test_data(),
@ -67,21 +67,19 @@ repeating this process with row 1, and so on."#
Example { Example {
example: "{a: 1, b: 2} | merge {c: 3}", example: "{a: 1, b: 2} | merge {c: 3}",
description: "Merge two records", description: "Merge two records",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string(), "c".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)], "b" => Value::test_int(2),
"c" => Value::test_int(3),
})), })),
}, },
Example { Example {
example: "[{columnA: A0 columnB: B0}] | merge [{columnA: 'A0*'}]", example: "[{columnA: A0 columnB: B0}] | merge [{columnA: 'A0*'}]",
description: "Merge two tables, overwriting overlapping columns", description: "Merge two tables, overwriting overlapping columns",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "columnA" => Value::test_string("A0*"),
cols: vec!["columnA".to_string(), "columnB".to_string()], "columnB" => Value::test_string("B0"),
vals: vec![Value::test_string("A0*"), Value::test_string("B0")], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -52,52 +52,57 @@ impl Command for Move {
example: "[[name value index]; [foo a 1] [bar b 2] [baz c 3]] | move index --before name", example: "[[name value index]; [foo a 1] [bar b 2] [baz c 3]] | move index --before name",
description: "Move a column before the first column", description: "Move a column before the first column",
result: result:
Some(Value::list ( Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".to_string(), "name".to_string(), "value".to_string()], "index" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_string("foo"), Value::test_string("a")], "name" => Value::test_string("foo"),
"value" => Value::test_string("a"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".to_string(), "name".to_string(), "value".to_string()], "index" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_string("bar"), Value::test_string("b")], "name" => Value::test_string("bar"),
"value" => Value::test_string("b"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".to_string(), "name".to_string(), "value".to_string()], "index" => Value::test_int(3),
vals: vec![Value::test_int(3), Value::test_string("baz"), Value::test_string("c")], "name" => Value::test_string("baz"),
"value" => Value::test_string("c"),
}), }),
], ],
Span::test_data(),
)) ))
}, },
Example { Example {
example: "[[name value index]; [foo a 1] [bar b 2] [baz c 3]] | move value name --after index", example: "[[name value index]; [foo a 1] [bar b 2] [baz c 3]] | move value name --after index",
description: "Move multiple columns after the last column and reorder them", description: "Move multiple columns after the last column and reorder them",
result: result:
Some(Value::list ( Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".to_string(), "value".to_string(), "name".to_string()], "index" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_string("a"), Value::test_string("foo")], "value" => Value::test_string("a"),
"name" => Value::test_string("foo"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".to_string(), "value".to_string(), "name".to_string()], "index" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_string("b"), Value::test_string("bar")], "value" => Value::test_string("b"),
"name" => Value::test_string("bar"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["index".to_string(), "value".to_string(), "name".to_string()], "index" => Value::test_int(3),
vals: vec![Value::test_int(3), Value::test_string("c"), Value::test_string("baz")], "value" => Value::test_string("c"),
"name" => Value::test_string("baz"),
}), }),
], ],
Span::test_data(),
)) ))
}, },
Example { Example {
example: "{ name: foo, value: a, index: 1 } | move name --before index", example: "{ name: foo, value: a, index: 1 } | move name --before index",
description: "Move columns of a record", description: "Move columns of a record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["value".to_string(), "name".to_string(), "index".to_string()], "value" => Value::test_string("a"),
vals: vec![Value::test_string("a"), Value::test_string("foo"), Value::test_int(1)], "name" => Value::test_string("foo"),
"index" => Value::test_int(1),
})) }))
}, },
] ]

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath, PathMember}; use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
use std::cmp::Reverse; use std::cmp::Reverse;
@ -152,42 +152,36 @@ impl Command for Reject {
Example { Example {
description: "Reject a column in a table", description: "Reject a column in a table",
example: "[[a, b]; [1, 2]] | reject a", example: "[[a, b]; [1, 2]] | reject a",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["b".to_string()], "b" => Value::test_int(2),
vals: vec![Value::test_int(2)],
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Reject a row in a table", description: "Reject a row in a table",
example: "[[a, b]; [1, 2] [3, 4]] | reject 1", example: "[[a, b]; [1, 2] [3, 4]] | reject 1",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(2)], "b" => Value::test_int(2),
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Reject the specified field in a record", description: "Reject the specified field in a record",
example: "{a: 1, b: 2} | reject a", example: "{a: 1, b: 2} | reject a",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["b".into()], "b" => Value::test_int(2),
vals: vec![Value::test_int(2)],
})), })),
}, },
Example { Example {
description: "Reject a nested field in a record", description: "Reject a nested field in a record",
example: "{a: {b: 3, c: 5}} | reject a.b", example: "{a: {b: 3, c: 5}} | reject a.b",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".into()], "a" => Value::test_record(record! {
vals: vec![Value::test_record(Record { "c" => Value::test_int(5),
cols: vec!["c".into()], }),
vals: vec![Value::test_int(5)],
})],
})), })),
}, },
Example { Example {

View File

@ -3,7 +3,7 @@ use nu_engine::{eval_block_with_early_return, CallExt};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature,
SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
use std::collections::HashSet; use std::collections::HashSet;
@ -57,50 +57,43 @@ impl Command for Rename {
Example { Example {
description: "Rename a column", description: "Rename a column",
example: "[[a, b]; [1, 2]] | rename my_column", example: "[[a, b]; [1, 2]] | rename my_column",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "my_column" => Value::test_int(1),
cols: vec!["my_column".to_string(), "b".to_string()], "b" => Value::test_int(2),
vals: vec![Value::test_int(1), Value::test_int(2)], })])),
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Rename many columns", description: "Rename many columns",
example: "[[a, b, c]; [1, 2, 3]] | rename eggs ham bacon", example: "[[a, b, c]; [1, 2, 3]] | rename eggs ham bacon",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "eggs" => Value::test_int(1),
cols: vec!["eggs".to_string(), "ham".to_string(), "bacon".to_string()], "ham" => Value::test_int(2),
vals: vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)], "bacon" => Value::test_int(3),
})], })])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Rename a specific column", description: "Rename a specific column",
example: "[[a, b, c]; [1, 2, 3]] | rename --column { a: ham }", example: "[[a, b, c]; [1, 2, 3]] | rename --column { a: ham }",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "ham" => Value::test_int(1),
cols: vec!["ham".to_string(), "b".to_string(), "c".to_string()], "b" => Value::test_int(2),
vals: vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)], "c" => Value::test_int(3),
})], })])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Rename the fields of a record", description: "Rename the fields of a record",
example: "{a: 1 b: 2} | rename x y", example: "{a: 1 b: 2} | rename x y",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["x".to_string(), "y".to_string()], "x" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(2)], "y" => Value::test_int(2),
})), })),
}, },
Example { Example {
description: "Rename fields based on a given closure", description: "Rename fields based on a given closure",
example: "{abc: 1, bbc: 2} | rename --block {str replace --all 'b' 'z'}", example: "{abc: 1, bbc: 2} | rename --block {str replace --all 'b' 'z'}",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["azc".to_string(), "zzc".to_string()], "azc" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(2)], "zzc" => Value::test_int(2),
})), })),
}, },
] ]

View File

@ -1,8 +1,8 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Span, Type, Value, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -38,32 +38,24 @@ impl Command for Reverse {
Example { Example {
example: "[0,1,2,3] | reverse", example: "[0,1,2,3] | reverse",
description: "Reverse a list", description: "Reverse a list",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![
Value::test_int(3), Value::test_int(3),
Value::test_int(2), Value::test_int(2),
Value::test_int(1), Value::test_int(1),
Value::test_int(0), Value::test_int(0),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
example: "[{a: 1} {a: 2}] | reverse", example: "[{a: 1} {a: 2}] | reverse",
description: "Reverse a table", description: "Reverse a table",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(2),
cols: vec!["a".to_string()],
vals: vec![Value::test_int(2)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1)],
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath, PathMember}; use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
PipelineIterator, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineIterator, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
use std::collections::BTreeSet; use std::collections::BTreeSet;
@ -151,20 +151,17 @@ produce a table, a list will produce a list, and a record will produce a record.
Example { Example {
description: "Select a column in a table", description: "Select a column in a table",
example: "[{a: a b: b}] | select a", example: "[{a: a b: b}] | select a",
result: Some(Value::list ( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_string("a")
vals: vec![Value::test_string("a")]
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Select a field in a record", description: "Select a field in a record",
example: "{a: a b: b} | select a", example: "{a: a b: b} | select a",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_string("a")
vals: vec![Value::test_string("a")]
})), })),
}, },
Example { Example {

View File

@ -4,8 +4,8 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, SyntaxShape, Type, Value, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -46,21 +46,18 @@ impl Command for Skip {
Example { Example {
description: "Skip the first value of a list", description: "Skip the first value of a list",
example: "[2 4 6 8] | skip 1", example: "[2 4 6 8] | skip 1",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(4), Value::test_int(6), Value::test_int(8)], Value::test_int(4),
Span::test_data(), Value::test_int(6),
)), Value::test_int(8),
])),
}, },
Example { Example {
description: "Skip two rows of a table", description: "Skip two rows of a table",
example: "[[editions]; [2015] [2018] [2021]] | skip 2", example: "[[editions]; [2015] [2018] [2021]] | skip 2",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "editions" => Value::test_int(2021),
cols: vec!["editions".to_owned()], })])),
vals: vec![Value::test_int(2021)],
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Closure, Command, EngineState, Stack}, engine::{Closure, Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Span, SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -44,35 +44,30 @@ impl Command for SkipUntil {
Example { Example {
description: "Skip until the element is positive", description: "Skip until the element is positive",
example: "[-2 0 2 -1] | skip until {|x| $x > 0 }", example: "[-2 0 2 -1] | skip until {|x| $x > 0 }",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(2), Value::test_int(-1)], Value::test_int(2),
Span::test_data(), Value::test_int(-1),
)), ])),
}, },
Example { Example {
description: "Skip until the element is positive using stored condition", description: "Skip until the element is positive using stored condition",
example: "let cond = {|x| $x > 0 }; [-2 0 2 -1] | skip until $cond", example: "let cond = {|x| $x > 0 }; [-2 0 2 -1] | skip until $cond",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(2), Value::test_int(-1)], Value::test_int(2),
Span::test_data(), Value::test_int(-1),
)), ])),
}, },
Example { Example {
description: "Skip until the field value is positive", description: "Skip until the field value is positive",
example: "[{a: -2} {a: 0} {a: 2} {a: -1}] | skip until {|x| $x.a > 0 }", example: "[{a: -2} {a: 0} {a: 2} {a: -1}] | skip until {|x| $x.a > 0 }",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(2),
cols: vec!["a".to_string()],
vals: vec![Value::test_int(2)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(-1),
vals: vec![Value::test_int(-1)],
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Closure, Command, EngineState, Stack}, engine::{Closure, Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Span, SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -44,39 +44,35 @@ impl Command for SkipWhile {
Example { Example {
description: "Skip while the element is negative", description: "Skip while the element is negative",
example: "[-2 0 2 -1] | skip while {|x| $x < 0 }", example: "[-2 0 2 -1] | skip while {|x| $x < 0 }",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(0), Value::test_int(2), Value::test_int(-1)], Value::test_int(0),
Span::test_data(), Value::test_int(2),
)), Value::test_int(-1),
])),
}, },
Example { Example {
description: "Skip while the element is negative using stored condition", description: "Skip while the element is negative using stored condition",
example: "let cond = {|x| $x < 0 }; [-2 0 2 -1] | skip while $cond", example: "let cond = {|x| $x < 0 }; [-2 0 2 -1] | skip while $cond",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(0), Value::test_int(2), Value::test_int(-1)], Value::test_int(0),
Span::test_data(), Value::test_int(2),
)), Value::test_int(-1),
])),
}, },
Example { Example {
description: "Skip while the field value is negative", description: "Skip while the field value is negative",
example: "[{a: -2} {a: 0} {a: 2} {a: -1}] | skip while {|x| $x.a < 0 }", example: "[{a: -2} {a: 0} {a: 2} {a: -1}] | skip while {|x| $x.a < 0 }",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(0),
cols: vec!["a".to_string()],
vals: vec![Value::test_int(0)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(2),
vals: vec![Value::test_int(2)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(-1),
vals: vec![Value::test_int(-1)],
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use alphanumeric_sort::compare_str;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, Type, Value, Record, ShellError, Signature, Span, Type, Value,
}; };
use std::cmp::Ordering; use std::cmp::Ordering;
@ -113,17 +113,18 @@ impl Command for Sort {
Example { Example {
description: "Sort record by key (case-insensitive)", description: "Sort record by key (case-insensitive)",
example: "{b: 3, a: 4} | sort", example: "{b: 3, a: 4} | sort",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(4),
vals: vec![Value::test_int(4), Value::test_int(3)], "b" => Value::test_int(3),
})), })),
}, },
Example { Example {
description: "Sort record by value", description: "Sort record by value",
example: "{b: 4, a: 3, c:1} | sort -v", example: "{b: 4, a: 3, c:1} | sort -v",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["c".to_string(), "a".to_string(), "b".to_string()], "c" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(3), Value::test_int(4)], "a" => Value::test_int(3),
"b" => Value::test_int(4),
})), })),
}, },
] ]

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Span, SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -58,23 +58,20 @@ impl Command for SortBy {
Example { Example {
description: "Sort a table by a column (reversed order)", description: "Sort a table by a column (reversed order)",
example: "[[fruit count]; [apple 9] [pear 3] [orange 7]] | sort-by fruit --reverse", example: "[[fruit count]; [apple 9] [pear 3] [orange 7]] | sort-by fruit --reverse",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "fruit" => Value::test_string("pear"),
cols: vec!["fruit".to_string(), "count".to_string()], "count" => Value::test_int(3),
vals: vec![Value::test_string("pear"), Value::test_int(3)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("orange"),
vals: vec![Value::test_string("orange"), Value::test_int(7)], "count" => Value::test_int(7),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("apple"),
vals: vec![Value::test_string("apple"), Value::test_int(9)], "count" => Value::test_int(9),
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, SyntaxShape, Type, Value, Spanned, SyntaxShape, Type, Value,
}; };
@ -48,63 +48,32 @@ impl Command for SplitBy {
{ name: 'storm', lang: 'rs', 'year': '2021' } { name: 'storm', lang: 'rs', 'year': '2021' }
] ]
} | split-by lang"#, } | split-by lang"#,
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["rb".to_string(), "rs".to_string()], "rb" => Value::test_record(record! {
vals: vec![ "2019" => Value::test_list(
Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["2019".to_string()], "name" => Value::test_string("andres"),
vals: vec![Value::list( "lang" => Value::test_string("rb"),
vec![Value::test_record(Record { "year" => Value::test_string("2019"),
cols: vec![ })],
"name".to_string(), ),
"lang".to_string(), }),
"year".to_string(), "rs" => Value::test_record(record! {
], "2019" => Value::test_list(
vals: vec![ vec![Value::test_record(record! {
Value::test_string("andres"), "name" => Value::test_string("jt"),
Value::test_string("rb"), "lang" => Value::test_string("rs"),
Value::test_string("2019"), "year" => Value::test_string("2019"),
], })],
),
"2021" => Value::test_list(
vec![Value::test_record(record! {
"name" => Value::test_string("storm"),
"lang" => Value::test_string("rs"),
"year" => Value::test_string("2021"),
})], })],
Span::test_data(),
)],
}),
Value::test_record(Record {
cols: vec!["2019".to_string(), "2021".to_string()],
vals: vec![
Value::list(
vec![Value::test_record(Record {
cols: vec![
"name".to_string(),
"lang".to_string(),
"year".to_string(),
],
vals: vec![
Value::test_string("jt"),
Value::test_string("rs"),
Value::test_string("2019"),
],
})],
Span::test_data(),
), ),
Value::list(
vec![Value::test_record(Record {
cols: vec![
"name".to_string(),
"lang".to_string(),
"year".to_string(),
],
vals: vec![
Value::test_string("storm"),
Value::test_string("rs"),
Value::test_string("2021"),
],
})],
Span::test_data(),
),
],
}), }),
],
})), })),
}] }]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Span, SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -107,45 +107,41 @@ impl Command for Take {
Example { Example {
description: "Return the first item of a list/table", description: "Return the first item of a list/table",
example: "[1 2 3] | take 1", example: "[1 2 3] | take 1",
result: Some(Value::list(vec![Value::test_int(1)], Span::test_data())), result: Some(Value::test_list(vec![Value::test_int(1)])),
}, },
Example { Example {
description: "Return the first 2 items of a list/table", description: "Return the first 2 items of a list/table",
example: "[1 2 3] | take 2", example: "[1 2 3] | take 2",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(1), Value::test_int(2)], Value::test_int(1),
Span::test_data(), Value::test_int(2),
)), ])),
}, },
Example { Example {
description: "Return the first two rows of a table", description: "Return the first two rows of a table",
example: "[[editions]; [2015] [2018] [2021]] | take 2", example: "[[editions]; [2015] [2018] [2021]] | take 2",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "editions" => Value::test_int(2015),
cols: vec!["editions".to_string()],
vals: vec![Value::test_int(2015)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["editions".to_string()], "editions" => Value::test_int(2018),
vals: vec![Value::test_int(2018)],
}), }),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Return the first 2 bytes of a binary value", description: "Return the first 2 bytes of a binary value",
example: "0x[01 23 45] | take 2", example: "0x[01 23 45] | take 2",
result: Some(Value::binary(vec![0x01, 0x23], Span::test_data())), result: Some(Value::test_binary(vec![0x01, 0x23])),
}, },
Example { Example {
description: "Return the first 3 elements of a range", description: "Return the first 3 elements of a range",
example: "1..10 | take 3", example: "1..10 | take 3",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)], Value::test_int(1),
Span::test_data(), Value::test_int(2),
)), Value::test_int(3),
])),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Closure, Command, EngineState, Stack}, engine::{Closure, Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Span, SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -40,35 +40,30 @@ impl Command for TakeUntil {
Example { Example {
description: "Take until the element is positive", description: "Take until the element is positive",
example: "[-1 -2 9 1] | take until {|x| $x > 0 }", example: "[-1 -2 9 1] | take until {|x| $x > 0 }",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(-1), Value::test_int(-2)], Value::test_int(-1),
Span::test_data(), Value::test_int(-2),
)), ])),
}, },
Example { Example {
description: "Take until the element is positive using stored condition", description: "Take until the element is positive using stored condition",
example: "let cond = {|x| $x > 0 }; [-1 -2 9 1] | take until $cond", example: "let cond = {|x| $x > 0 }; [-1 -2 9 1] | take until $cond",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(-1), Value::test_int(-2)], Value::test_int(-1),
Span::test_data(), Value::test_int(-2),
)), ])),
}, },
Example { Example {
description: "Take until the field value is positive", description: "Take until the field value is positive",
example: "[{a: -1} {a: -2} {a: 9} {a: 1}] | take until {|x| $x.a > 0 }", example: "[{a: -1} {a: -2} {a: 9} {a: 1}] | take until {|x| $x.a > 0 }",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(-1),
cols: vec!["a".to_string()],
vals: vec![Value::test_int(-1)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(-2),
vals: vec![Value::test_int(-2)],
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Closure, Command, EngineState, Stack}, engine::{Closure, Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Span, SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -40,35 +40,30 @@ impl Command for TakeWhile {
Example { Example {
description: "Take while the element is negative", description: "Take while the element is negative",
example: "[-1 -2 9 1] | take while {|x| $x < 0 }", example: "[-1 -2 9 1] | take while {|x| $x < 0 }",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(-1), Value::test_int(-2)], Value::test_int(-1),
Span::test_data(), Value::test_int(-2),
)), ])),
}, },
Example { Example {
description: "Take while the element is negative using stored condition", description: "Take while the element is negative using stored condition",
example: "let cond = {|x| $x < 0 }; [-1 -2 9 1] | take while $cond", example: "let cond = {|x| $x < 0 }; [-1 -2 9 1] | take while $cond",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(-1), Value::test_int(-2)], Value::test_int(-1),
Span::test_data(), Value::test_int(-2),
)), ])),
}, },
Example { Example {
description: "Take while the field value is negative", description: "Take while the field value is negative",
example: "[{a: -1} {a: -2} {a: 9} {a: 1}] | take while {|x| $x.a < 0 }", example: "[{a: -1} {a: -2} {a: 9} {a: 1}] | take while {|x| $x.a < 0 }",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(-1),
cols: vec!["a".to_string()],
vals: vec![Value::test_int(-1)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(-2),
vals: vec![Value::test_int(-2)],
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -3,8 +3,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError,
Span, Spanned, SyntaxShape, Type, Value, Signature, Spanned, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -83,66 +83,54 @@ impl Command for Transpose {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![ vec![
Example { Example {
description: "Transposes the table contents with default column names", description: "Transposes the table contents with default column names",
example: "[[c1 c2]; [1 2]] | transpose", example: "[[c1 c2]; [1 2]] | transpose",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "column0" => Value::test_string("c1"),
cols: vec!["column0".to_string(), "column1".to_string()], "column1" => Value::test_int(1),
vals: vec![Value::test_string("c1"), Value::test_int(1)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column0".to_string(), "column1".to_string()], "column0" => Value::test_string("c2"),
vals: vec![Value::test_string("c2"), Value::test_int(2)], "column1" => Value::test_int(2),
}), }),
], ])),
span,
)),
}, },
Example { Example {
description: "Transposes the table contents with specified column names", description: "Transposes the table contents with specified column names",
example: "[[c1 c2]; [1 2]] | transpose key val", example: "[[c1 c2]; [1 2]] | transpose key val",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "key" => Value::test_string("c1"),
cols: vec!["key".to_string(), "val".to_string()], "val" => Value::test_int(1),
vals: vec![Value::test_string("c1"), Value::test_int(1)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["key".to_string(), "val".to_string()], "key" => Value::test_string("c2"),
vals: vec![Value::test_string("c2"), Value::test_int(2)], "val" => Value::test_int(2),
}), }),
], ])),
span,
)),
}, },
Example { Example {
description: description:
"Transposes the table without column names and specify a new column name", "Transposes the table without column names and specify a new column name",
example: "[[c1 c2]; [1 2]] | transpose --ignore-titles val", example: "[[c1 c2]; [1 2]] | transpose --ignore-titles val",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "val" => Value::test_int(1),
cols: vec!["val".to_string()],
vals: vec![Value::test_int(1)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["val".to_string()], "val" => Value::test_int(2),
vals: vec![Value::test_int(2)],
}), }),
], ])),
span,
)),
}, },
Example { Example {
description: "Transfer back to record with -d flag", description: "Transfer back to record with -d flag",
example: "{c1: 1, c2: 2} | transpose | transpose --ignore-titles -r -d", example: "{c1: 1, c2: 2} | transpose | transpose --ignore-titles -r -d",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["c1".to_string(), "c2".to_string()], "c1" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(2)], "c2" => Value::test_int(2),
})), })),
}, },
] ]

View File

@ -112,26 +112,24 @@ impl Command for Uniq {
Example { Example {
description: "Ignore differences in case when comparing input values", description: "Ignore differences in case when comparing input values",
example: "['hello' 'goodbye' 'Hello'] | uniq --ignore-case", example: "['hello' 'goodbye' 'Hello'] | uniq --ignore-case",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_string("hello"), Value::test_string("goodbye")], vec![Value::test_string("hello"), Value::test_string("goodbye")],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Return a table containing the distinct input values together with their counts", description: "Return a table containing the distinct input values together with their counts",
example: "[1 2 2] | uniq --count", example: "[1 2 2] | uniq --count",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string(), "count".to_string()], "value" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(1)], "count" => Value::test_int(1),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["value".to_string(), "count".to_string()], "value" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_int(2)], "count" => Value::test_int(2),
}), }),
], ],
Span::test_data(),
)), )),
}, },
] ]

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -92,23 +92,20 @@ impl Command for UniqBy {
vec![Example { vec![Example {
description: "Get rows from table filtered by column uniqueness ", description: "Get rows from table filtered by column uniqueness ",
example: "[[fruit count]; [apple 9] [apple 2] [pear 3] [orange 7]] | uniq-by fruit", example: "[[fruit count]; [apple 9] [apple 2] [pear 3] [orange 7]] | uniq-by fruit",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "fruit" => Value::test_string("apple"),
cols: vec!["fruit".to_string(), "count".to_string()], "count" => Value::test_int(9),
vals: vec![Value::test_string("apple"), Value::test_int(9)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("pear"),
vals: vec![Value::test_string("pear"), Value::test_int(3)], "count" => Value::test_int(3),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("orange"),
vals: vec![Value::test_string("orange"), Value::test_int(7)], "count" => Value::test_int(7),
}), }),
], ])),
Span::test_data(),
)),
}] }]
} }
} }

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath, PathMember}; use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
Record, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -57,42 +57,39 @@ impl Command for Update {
Example { Example {
description: "Update a column value", description: "Update a column value",
example: "{'name': 'nu', 'stars': 5} | update name 'Nushell'", example: "{'name': 'nu', 'stars': 5} | update name 'Nushell'",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["name".into(), "stars".into()], "name" => Value::test_string("Nushell"),
vals: vec![Value::test_string("Nushell"), Value::test_int(5)], "stars" => Value::test_int(5),
})), })),
}, },
Example { Example {
description: "Use in closure form for more involved updating logic", description: "Use in closure form for more involved updating logic",
example: "[[count fruit]; [1 'apple']] | enumerate | update item.count {|e| ($e.item.fruit | str length) + $e.index } | get item", example: "[[count fruit]; [1 'apple']] | enumerate | update item.count {|e| ($e.item.fruit | str length) + $e.index } | get item",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["count".into(), "fruit".into()], "count" => Value::test_int(5),
vals: vec![Value::test_int(5), Value::test_string("apple")], "fruit" => Value::test_string("apple"),
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Alter each value in the 'authors' column to use a single string instead of a list", description: "Alter each value in the 'authors' column to use a single string instead of a list",
example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|row| $row.authors | str join ','}", example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|row| $row.authors | str join ','}",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["project".into(), "authors".into()], "project" => Value::test_string("nu"),
vals: vec![Value::test_string("nu"), Value::test_string("Andrés,JT,Yehuda")], "authors" => Value::test_string("Andrés,JT,Yehuda"),
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "You can also use a simple command to update 'authors' to a single string", description: "You can also use a simple command to update 'authors' to a single string",
example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|| str join ','}", example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|| str join ','}",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["project".into(), "authors".into()], "project" => Value::test_string("nu"),
vals: vec![Value::test_string("nu"), Value::test_string("Andrés,JT,Yehuda")], "authors" => Value::test_string("Andrés,JT,Yehuda"),
})], })],
Span::test_data(),
)), )),
} }
] ]

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath, PathMember}; use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
Record, ShellError, Signature, Span, SyntaxShape, Type, Value, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -60,65 +60,62 @@ impl Command for Upsert {
vec![Example { vec![Example {
description: "Update a record's value", description: "Update a record's value",
example: "{'name': 'nu', 'stars': 5} | upsert name 'Nushell'", example: "{'name': 'nu', 'stars': 5} | upsert name 'Nushell'",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["name".into(), "stars".into()], "name" => Value::test_string("Nushell"),
vals: vec![Value::test_string("Nushell"), Value::test_int(5)], "stars" => Value::test_int(5),
})), })),
}, },
Example { Example {
description: "Update each row of a table", description: "Update each row of a table",
example: "[[name lang]; [Nushell ''] [Reedline '']] | upsert lang 'Rust'", example: "[[name lang]; [Nushell ''] [Reedline '']] | upsert lang 'Rust'",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["name".into(), "lang".into()], "name" => Value::test_string("Nushell"),
vals: vec![Value::test_string("Nushell"), Value::test_string("Rust")], "lang" => Value::test_string("Rust"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["name".into(), "lang".into()], "name" => Value::test_string("Reedline"),
vals: vec![Value::test_string("Reedline"), Value::test_string("Rust")], "lang" => Value::test_string("Rust"),
}), }),
], ],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Insert a new entry into a single record", description: "Insert a new entry into a single record",
example: "{'name': 'nu', 'stars': 5} | upsert language 'Rust'", example: "{'name': 'nu', 'stars': 5} | upsert language 'Rust'",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["name".into(), "stars".into(), "language".into()], "name" => Value::test_string("nu"),
vals: vec![Value::test_string("nu"), Value::test_int(5), Value::test_string("Rust")], "stars" => Value::test_int(5),
"language" => Value::test_string("Rust"),
})), })),
}, Example { }, Example {
description: "Use in closure form for more involved updating logic", description: "Use in closure form for more involved updating logic",
example: "[[count fruit]; [1 'apple']] | enumerate | upsert item.count {|e| ($e.item.fruit | str length) + $e.index } | get item", example: "[[count fruit]; [1 'apple']] | enumerate | upsert item.count {|e| ($e.item.fruit | str length) + $e.index } | get item",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["count".into(), "fruit".into()], "count" => Value::test_int(5),
vals: vec![Value::test_int(5), Value::test_string("apple")], "fruit" => Value::test_string("apple"),
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Upsert an int into a list, updating an existing value based on the index", description: "Upsert an int into a list, updating an existing value based on the index",
example: "[1 2 3] | upsert 0 2", example: "[1 2 3] | upsert 0 2",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_int(2), Value::test_int(2), Value::test_int(3)], vec![Value::test_int(2), Value::test_int(2), Value::test_int(3)],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Upsert an int into a list, inserting a new value based on the index", description: "Upsert an int into a list, inserting a new value based on the index",
example: "[1 2 3] | upsert 3 4", example: "[1 2 3] | upsert 3 4",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_int(1), Value::test_int(1),
Value::test_int(2), Value::test_int(2),
Value::test_int(3), Value::test_int(3),
Value::test_int(4), Value::test_int(4),
], ],
Span::test_data(),
)), )),
}, },
] ]

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, SyntaxShape, Type, Value, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -111,20 +111,17 @@ not supported."#
Example { Example {
description: "Filter rows of a table according to a condition", description: "Filter rows of a table according to a condition",
example: "[{a: 1} {a: 2}] | where a > 1", example: "[{a: 1} {a: 2}] | where a > 1",
result: Some(Value::list ( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(2),
vals: vec![Value::test_int(2)],
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Filter items of a list according to a condition", description: "Filter items of a list according to a condition",
example: "[1 2] | where {|x| $x > 1}", example: "[1 2] | where {|x| $x > 1}",
result: Some(Value::list ( result: Some(Value::test_list(
vec![Value::test_int(2)], vec![Value::test_int(2)],
Span::test_data(),
)), )),
}, },
Example { Example {

View File

@ -3,7 +3,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
Record, ShellError, Signature, Span, SyntaxShape, Type, Value, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -67,44 +67,32 @@ impl Command for Wrap {
Example { Example {
description: "Wrap a list into a table with a given column name", description: "Wrap a list into a table with a given column name",
example: "[1 2 3] | wrap num", example: "[1 2 3] | wrap num",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "num" => Value::test_int(1),
cols: vec!["num".into()],
vals: vec![Value::test_int(1)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["num".into()], "num" => Value::test_int(2),
vals: vec![Value::test_int(2)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["num".into()], "num" => Value::test_int(3),
vals: vec![Value::test_int(3)],
}), }),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Wrap a range into a table with a given column name", description: "Wrap a range into a table with a given column name",
example: "1..3 | wrap num", example: "1..3 | wrap num",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "num" => Value::test_int(1),
cols: vec!["num".into()],
vals: vec![Value::test_int(1)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["num".into()], "num" => Value::test_int(2),
vals: vec![Value::test_int(2)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["num".into()], "num" => Value::test_int(3),
vals: vec![Value::test_int(3)],
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -81,15 +81,11 @@ impl Command for FromCsv {
Example { Example {
description: "Convert comma-separated data to a table", description: "Convert comma-separated data to a table",
example: "\"ColA,ColB\n1,2\" | from csv", example: "\"ColA,ColB\n1,2\" | from csv",
result: Some(Value::list ( result: Some(Value::test_list (
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["ColA".to_string(), "ColB".to_string()], "ColA" => Value::test_int(1),
vals: vec![ "ColB" => Value::test_int(2),
Value::test_int(1),
Value::test_int(2),
],
})], })],
Span::test_data(),
)) ))
}, },
Example { Example {

View File

@ -1,7 +1,7 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, Type, Value, ShellError, Signature, Span, Type, Value,
}; };
@ -29,23 +29,16 @@ impl Command for FromJson {
Example { Example {
example: r#"'{ "a": 1 }' | from json"#, example: r#"'{ "a": 1 }' | from json"#,
description: "Converts json formatted string to table", description: "Converts json formatted string to table",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1)],
})), })),
}, },
Example { Example {
example: r#"'{ "a": 1, "b": [1, 2] }' | from json"#, example: r#"'{ "a": 1, "b": [1, 2] }' | from json"#,
description: "Converts json formatted string to table", description: "Converts json formatted string to table",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![ "b" => Value::test_list(vec![Value::test_int(1), Value::test_int(2)]),
Value::test_int(1),
Value::list(
vec![Value::test_int(1), Value::test_int(2)],
Span::test_data(),
),
],
})), })),
}, },
] ]

View File

@ -1,8 +1,8 @@
use nu_protocol::ast::{Call, Expr, Expression, PipelineElement}; use nu_protocol::ast::{Call, Expr, Expression, PipelineElement};
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet}; use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Range, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, Range, Record, ShellError,
Type, Unit, Value, Signature, Span, Type, Unit, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
pub struct FromNuon; pub struct FromNuon;
@ -27,23 +27,16 @@ impl Command for FromNuon {
Example { Example {
example: "'{ a:1 }' | from nuon", example: "'{ a:1 }' | from nuon",
description: "Converts nuon formatted string to table", description: "Converts nuon formatted string to table",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1)],
})), })),
}, },
Example { Example {
example: "'{ a:1, b: [1, 2] }' | from nuon", example: "'{ a:1, b: [1, 2] }' | from nuon",
description: "Converts nuon formatted string to table", description: "Converts nuon formatted string to table",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![ "b" => Value::test_list(vec![Value::test_int(1), Value::test_int(2)]),
Value::test_int(1),
Value::list(
vec![Value::test_int(1), Value::test_int(2)],
Span::test_data(),
),
],
})), })),
}, },
] ]

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, SyntaxShape, Type, Value, Spanned, SyntaxShape, Type, Value,
}; };
@ -44,30 +44,28 @@ impl Command for FromSsv {
example: r#"'FOO BAR example: r#"'FOO BAR
1 2' | from ssv"#, 1 2' | from ssv"#,
description: "Converts ssv formatted string to table", description: "Converts ssv formatted string to table",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["FOO".to_string(), "BAR".to_string()], "FOO" => Value::test_string("1"),
vals: vec![Value::test_string("1"), Value::test_string("2")], "BAR" => Value::test_string("2"),
})], })],
Span::test_data(),
)), )),
}, Example { }, Example {
example: r#"'FOO BAR example: r#"'FOO BAR
1 2' | from ssv --noheaders"#, 1 2' | from ssv --noheaders"#,
description: "Converts ssv formatted string to table but not treating the first row as column names", description: "Converts ssv formatted string to table but not treating the first row as column names",
result: Some( result: Some(
Value::list( Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column1".to_string(), "column2".to_string()], "column1" => Value::test_string("FOO"),
vals: vec![Value::test_string("FOO"), Value::test_string("BAR")], "column2" => Value::test_string("BAR"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column1".to_string(), "column2".to_string()], "column1" => Value::test_string("1"),
vals: vec![Value::test_string("1"), Value::test_string("2")], "column2" => Value::test_string("2"),
}), }),
], ],
Span::test_data(),
) )
), ),
}] }]

View File

@ -1,7 +1,7 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, Type, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value, Value,
}; };
@ -28,24 +28,19 @@ impl Command for FromToml {
Example { Example {
example: "'a = 1' | from toml", example: "'a = 1' | from toml",
description: "Converts toml formatted string to record", description: "Converts toml formatted string to record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1)],
})), })),
}, },
Example { Example {
example: "'a = 1 example: "'a = 1
b = [1, 2]' | from toml", b = [1, 2]' | from toml",
description: "Converts toml formatted string to record", description: "Converts toml formatted string to record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![ "b" => Value::test_list(vec![
Value::test_int(1), Value::test_int(1),
Value::list( Value::test_int(2)],),
vec![Value::test_int(1), Value::test_int(2)],
Span::test_data(),
),
],
})), })),
}, },
] ]

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -75,15 +75,11 @@ impl Command for FromTsv {
Example { Example {
description: "Convert tab-separated data to a table", description: "Convert tab-separated data to a table",
example: "\"ColA\tColB\n1\t2\" | from tsv", example: "\"ColA\tColB\n1\t2\" | from tsv",
result: Some(Value::list ( result: Some(Value::test_list (
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["ColA".to_string(), "ColB".to_string()], "ColA" => Value::test_int(1),
vals: vec![ "ColB" => Value::test_int(2),
Value::test_int(1),
Value::test_int(2),
],
})], })],
Span::test_data(),
)) ))
}, },
Example { Example {

View File

@ -3,8 +3,8 @@ use indexmap::map::IndexMap;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, Type, record, Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
Value, Type, Value,
}; };
use roxmltree::NodeType; use roxmltree::NodeType;
@ -69,45 +69,22 @@ string. This way content of every tag is always a table and is easier to parse"#
<remember>Event</remember> <remember>Event</remember>
</note>' | from xml"#, </note>' | from xml"#,
description: "Converts xml formatted string to record", description: "Converts xml formatted string to record",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ COLUMN_TAG_NAME => Value::test_string("note"),
COLUMN_TAG_NAME.to_string(), COLUMN_ATTRS_NAME => Value::test_record(Record::new()),
COLUMN_ATTRS_NAME.to_string(), COLUMN_CONTENT_NAME => Value::test_list(vec![
COLUMN_CONTENT_NAME.to_string(), Value::test_record(record! {
], COLUMN_TAG_NAME => Value::test_string("remember"),
vals: vec![ COLUMN_ATTRS_NAME => Value::test_record(Record::new()),
Value::test_string("note"), COLUMN_CONTENT_NAME => Value::test_list(vec![
Value::test_record(Record::new()), Value::test_record(record! {
Value::list( COLUMN_TAG_NAME => Value::test_nothing(),
vec![Value::test_record(Record { COLUMN_ATTRS_NAME => Value::test_nothing(),
cols: vec![ COLUMN_CONTENT_NAME => Value::test_string("Event"),
COLUMN_TAG_NAME.to_string(),
COLUMN_ATTRS_NAME.to_string(),
COLUMN_CONTENT_NAME.to_string(),
],
vals: vec![
Value::test_string("remember"),
Value::test_record(Record::new()),
Value::list(
vec![Value::test_record(Record {
cols: vec![
COLUMN_TAG_NAME.to_string(),
COLUMN_ATTRS_NAME.to_string(),
COLUMN_CONTENT_NAME.to_string(),
],
vals: vec![
Value::test_nothing(),
Value::test_nothing(),
Value::test_string("Event"),
],
})], })],
Span::test_data(),
), ),
],
})], })],
Span::test_data(),
), ),
],
})), })),
}] }]
} }
@ -344,28 +321,18 @@ mod tests {
attrs: IndexMap<&str, &str>, attrs: IndexMap<&str, &str>,
content: &[Value], content: &[Value],
) -> Value { ) -> Value {
Value::test_record(Record { Value::test_record(record! {
cols: vec![ COLUMN_TAG_NAME => string(tag),
COLUMN_TAG_NAME.into(), COLUMN_ATTRS_NAME => attributes(attrs),
COLUMN_ATTRS_NAME.into(), COLUMN_CONTENT_NAME => table(content),
COLUMN_CONTENT_NAME.into(),
],
vals: vec![string(tag), attributes(attrs), table(content)],
}) })
} }
fn content_string(value: impl Into<String>) -> Value { fn content_string(value: impl Into<String>) -> Value {
Value::test_record(Record { Value::test_record(record! {
cols: vec![ COLUMN_TAG_NAME => Value::nothing(Span::test_data()),
COLUMN_TAG_NAME.into(), COLUMN_ATTRS_NAME => Value::nothing(Span::test_data()),
COLUMN_ATTRS_NAME.into(), COLUMN_CONTENT_NAME => string(value),
COLUMN_CONTENT_NAME.into(),
],
vals: vec![
Value::nothing(Span::test_data()),
Value::nothing(Span::test_data()),
string(value),
],
}) })
} }

View File

@ -3,7 +3,7 @@ use itertools::Itertools;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, Type, record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value, Value,
}; };
use serde::de::Deserialize; use serde::de::Deserialize;
@ -221,30 +221,22 @@ pub fn get_examples() -> Vec<Example<'static>> {
Example { Example {
example: "'a: 1' | from yaml", example: "'a: 1' | from yaml",
description: "Converts yaml formatted string to table", description: "Converts yaml formatted string to table",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1)],
})), })),
}, },
Example { Example {
example: "'[ a: 1, b: [1, 2] ]' | from yaml", example: "'[ a: 1, b: [1, 2] ]' | from yaml",
description: "Converts yaml formatted string to table", description: "Converts yaml formatted string to table",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(1),
cols: vec!["a".to_string()],
vals: vec![Value::test_int(1)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["b".to_string()], "b" => Value::test_list(
vals: vec![Value::list( vec![Value::test_int(1), Value::test_int(2)],),
vec![Value::test_int(1), Value::test_int(2)],
Span::test_data(),
)],
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }
@ -274,17 +266,15 @@ mod test {
TestCase { TestCase {
description: "Double Curly Braces With Quotes", description: "Double Curly Braces With Quotes",
input: r#"value: "{{ something }}""#, input: r#"value: "{{ something }}""#,
expected: Ok(Value::test_record(Record { expected: Ok(Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_string("{{ something }}"),
vals: vec![Value::test_string("{{ something }}")],
})), })),
}, },
TestCase { TestCase {
description: "Double Curly Braces Without Quotes", description: "Double Curly Braces Without Quotes",
input: r#"value: {{ something }}"#, input: r#"value: {{ something }}"#,
expected: Ok(Value::test_record(Record { expected: Ok(Value::test_record(record! {
cols: vec!["value".to_string()], "value" => Value::test_string("{{ something }}"),
vals: vec![Value::test_string("{{ something }}")],
})), })),
}, },
]; ];
@ -335,19 +325,16 @@ mod test {
Span::test_data(), Span::test_data(),
); );
let expected: Result<Value, ShellError> = Ok(Value::list( let expected: Result<Value, ShellError> = Ok(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_string("b"),
cols: vec!["a".to_string(), "b".to_string()], "b" => Value::test_string("c"),
vals: vec![Value::test_string("b"), Value::test_string("c")],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_string("g"),
vals: vec![Value::test_string("g"), Value::test_string("h")], "b" => Value::test_string("h"),
}), }),
], ]));
Span::test_data(),
));
// Unfortunately the eq function for Value doesn't compare well enough to detect // Unfortunately the eq function for Value doesn't compare well enough to detect
// ordering errors in List columns or values. // ordering errors in List columns or values.
@ -388,37 +375,32 @@ mod test {
let test_cases: Vec<TestCase> = vec![ let test_cases: Vec<TestCase> = vec![
TestCase { TestCase {
input: "Key: !Value ${TEST}-Test-role", input: "Key: !Value ${TEST}-Test-role",
expected: Ok(Value::test_record(Record { expected: Ok(Value::test_record(record! {
cols: vec!["Key".to_string()], "Key" => Value::test_string("!Value ${TEST}-Test-role"),
vals: vec![Value::test_string("!Value ${TEST}-Test-role")],
})), })),
}, },
TestCase { TestCase {
input: "Key: !Value test-${TEST}", input: "Key: !Value test-${TEST}",
expected: Ok(Value::test_record(Record { expected: Ok(Value::test_record(record! {
cols: vec!["Key".to_string()], "Key" => Value::test_string("!Value test-${TEST}"),
vals: vec![Value::test_string("!Value test-${TEST}")],
})), })),
}, },
TestCase { TestCase {
input: "Key: !Value", input: "Key: !Value",
expected: Ok(Value::test_record(Record { expected: Ok(Value::test_record(record! {
cols: vec!["Key".to_string()], "Key" => Value::test_string("!Value"),
vals: vec![Value::test_string("!Value")],
})), })),
}, },
TestCase { TestCase {
input: "Key: !True", input: "Key: !True",
expected: Ok(Value::test_record(Record { expected: Ok(Value::test_record(record! {
cols: vec!["Key".to_string()], "Key" => Value::test_string("!True"),
vals: vec![Value::test_string("!True")],
})), })),
}, },
TestCase { TestCase {
input: "Key: !123", input: "Key: !123",
expected: Ok(Value::test_record(Record { expected: Ok(Value::test_record(record! {
cols: vec!["Key".to_string()], "Key" => Value::test_string("!123"),
vals: vec![Value::test_string("!123")],
})), })),
}, },
]; ];

View File

@ -320,7 +320,7 @@ fn get_padded_string(text: String, desired_length: usize, padding_character: cha
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use nu_protocol::{Config, IntoPipelineData, Record, Span, Value}; use nu_protocol::{record, Config, IntoPipelineData, Value};
fn one(string: &str) -> String { fn one(string: &str) -> String {
string string
@ -342,9 +342,8 @@ mod tests {
#[test] #[test]
fn render_h1() { fn render_h1() {
let value = Value::test_record(Record { let value = Value::test_record(record! {
cols: vec!["H1".to_string()], "H1" => Value::test_string("Ecuador"),
vals: vec![Value::test_string("Ecuador")],
}); });
assert_eq!(fragment(value, false, &Config::default()), "# Ecuador\n"); assert_eq!(fragment(value, false, &Config::default()), "# Ecuador\n");
@ -352,9 +351,8 @@ mod tests {
#[test] #[test]
fn render_h2() { fn render_h2() {
let value = Value::test_record(Record { let value = Value::test_record(record! {
cols: vec!["H2".to_string()], "H2" => Value::test_string("Ecuador"),
vals: vec![Value::test_string("Ecuador")],
}); });
assert_eq!(fragment(value, false, &Config::default()), "## Ecuador\n"); assert_eq!(fragment(value, false, &Config::default()), "## Ecuador\n");
@ -362,9 +360,8 @@ mod tests {
#[test] #[test]
fn render_h3() { fn render_h3() {
let value = Value::test_record(Record { let value = Value::test_record(record! {
cols: vec!["H3".to_string()], "H3" => Value::test_string("Ecuador"),
vals: vec![Value::test_string("Ecuador")],
}); });
assert_eq!(fragment(value, false, &Config::default()), "### Ecuador\n"); assert_eq!(fragment(value, false, &Config::default()), "### Ecuador\n");
@ -372,9 +369,8 @@ mod tests {
#[test] #[test]
fn render_blockquote() { fn render_blockquote() {
let value = Value::test_record(Record { let value = Value::test_record(record! {
cols: vec!["BLOCKQUOTE".to_string()], "BLOCKQUOTE" => Value::test_string("Ecuador"),
vals: vec![Value::test_string("Ecuador")],
}); });
assert_eq!(fragment(value, false, &Config::default()), "> Ecuador\n"); assert_eq!(fragment(value, false, &Config::default()), "> Ecuador\n");
@ -382,23 +378,17 @@ mod tests {
#[test] #[test]
fn render_table() { fn render_table() {
let value = Value::list( let value = Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "country" => Value::test_string("Ecuador"),
cols: vec!["country".to_string()],
vals: vec![Value::test_string("Ecuador")],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["country".to_string()], "country" => Value::test_string("New Zealand"),
vals: vec![Value::test_string("New Zealand")],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["country".to_string()], "country" => Value::test_string("USA"),
vals: vec![Value::test_string("USA")],
}), }),
], ]);
Span::test_data(),
);
assert_eq!( assert_eq!(
table( table(

View File

@ -3,7 +3,7 @@ use crate::math::utils::run_with_function;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -52,9 +52,9 @@ impl Command for SubCommand {
Example { Example {
description: "Find the maxima of the columns of a table", description: "Find the maxima of the columns of a table",
example: "[{a: 1 b: 3} {a: 2 b: -1}] | math max", example: "[{a: 1 b: 3} {a: 2 b: -1}] | math max",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(2),
vals: vec![Value::test_int(2), Value::test_int(3)], "b" => Value::test_int(3),
})), })),
}, },
] ]

View File

@ -5,7 +5,7 @@ use crate::math::utils::run_with_function;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -56,9 +56,9 @@ impl Command for SubCommand {
Example { Example {
description: "Compute the medians of the columns of a table", description: "Compute the medians of the columns of a table",
example: "[{a: 1 b: 3} {a: 2 b: -1} {a: -3 b: 5}] | math median", example: "[{a: 1 b: 3} {a: 2 b: -1} {a: -3 b: 5}] | math median",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(3)], "b" => Value::test_int(3),
})), })),
}, },
] ]

View File

@ -3,7 +3,7 @@ use crate::math::utils::run_with_function;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -52,9 +52,9 @@ impl Command for SubCommand {
Example { Example {
description: "Compute the minima of the columns of a table", description: "Compute the minima of the columns of a table",
example: "[{a: 1 b: 3} {a: 2 b: -1}] | math min", example: "[{a: 1 b: 3} {a: 2 b: -1}] | math min",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(-1)], "b" => Value::test_int(-1),
})), })),
}, },
Example { Example {

View File

@ -2,7 +2,7 @@ use crate::math::utils::run_with_function;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
@ -82,23 +82,20 @@ impl Command for SubCommand {
Example { Example {
description: "Compute the mode(s) of a list of numbers", description: "Compute the mode(s) of a list of numbers",
example: "[3 3 9 12 12 15] | math mode", example: "[3 3 9 12 12 15] | math mode",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![Value::test_int(3), Value::test_int(12)], Value::test_int(3),
Span::test_data(), Value::test_int(12),
)), ])),
}, },
Example { Example {
description: "Compute the mode(s) of the columns of a table", description: "Compute the mode(s) of the columns of a table",
example: "[{a: 1 b: 3} {a: 2 b: -1} {a: 1 b: 5}] | math mode", example: "[{a: 1 b: 3} {a: 2 b: -1} {a: 1 b: 5}] | math mode",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::list(vec![Value::test_int(1)], Span::test_data()),
vals: vec![ "b" => Value::list(
Value::list(vec![Value::test_int(1)], Span::test_data()),
Value::list(
vec![Value::test_int(-1), Value::test_int(3), Value::test_int(5)], vec![Value::test_int(-1), Value::test_int(3), Value::test_int(5)],
Span::test_data(), Span::test_data(),
), ),
],
})), })),
}, },
] ]

View File

@ -2,8 +2,7 @@ use super::url;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
Type, Value,
}; };
use url::Url; use url::Url;
@ -56,36 +55,20 @@ impl Command for SubCommand {
vec![Example { vec![Example {
description: "Parses a url", description: "Parses a url",
example: "'http://user123:pass567@www.example.com:8081/foo/bar?param1=section&p2=&f[name]=vldc#hello' | url parse", example: "'http://user123:pass567@www.example.com:8081/foo/bar?param1=section&p2=&f[name]=vldc#hello' | url parse",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "scheme" => Value::test_string("http"),
"scheme".to_string(), "username" => Value::test_string("user123"),
"username".to_string(), "password" => Value::test_string("pass567"),
"password".to_string(), "host" => Value::test_string("www.example.com"),
"host".to_string(), "port" => Value::test_string("8081"),
"port".to_string(), "path" => Value::test_string("/foo/bar"),
"path".to_string(), "query" => Value::test_string("param1=section&p2=&f[name]=vldc"),
"query".to_string(), "fragment" => Value::test_string("hello"),
"fragment".to_string(), "params" => Value::test_record(record! {
"params".to_string(), "param1" => Value::test_string("section"),
], "p2" => Value::test_string(""),
vals: vec![ "f[name]" => Value::test_string("vldc"),
Value::test_string("http"),
Value::test_string("user123"),
Value::test_string("pass567"),
Value::test_string("www.example.com"),
Value::test_string("8081"),
Value::test_string("/foo/bar"),
Value::test_string("param1=section&p2=&f[name]=vldc"),
Value::test_string("hello"),
Value::test_record(Record {
cols: vec!["param1".to_string(), "p2".to_string(), "f[name]".to_string()],
vals: vec![
Value::test_string("section"),
Value::test_string(""),
Value::test_string("vldc"),
],
}), }),
],
})), })),
}] }]
} }

View File

@ -97,23 +97,17 @@ On Windows, an extra 'prefix' column is added."#
#[cfg(windows)] #[cfg(windows)]
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
use nu_protocol::record;
vec![ vec![
Example { Example {
description: "Parse a single path", description: "Parse a single path",
example: r"'C:\Users\viking\spam.txt' | path parse", example: r"'C:\Users\viking\spam.txt' | path parse",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "prefix" => Value::test_string("C:"),
"prefix".into(), "parent" => Value::test_string(r"C:\Users\viking"),
"parent".into(), "stem" => Value::test_string("spam"),
"stem".into(), "extension" => Value::test_string("txt"),
"extension".into(),
],
vals: vec![
Value::test_string("C:"),
Value::test_string(r"C:\Users\viking"),
Value::test_string("spam"),
Value::test_string("txt"),
],
})), })),
}, },
Example { Example {
@ -124,52 +118,28 @@ On Windows, an extra 'prefix' column is added."#
Example { Example {
description: "Ignore the extension", description: "Ignore the extension",
example: r"'C:\Users\viking.d' | path parse --extension ''", example: r"'C:\Users\viking.d' | path parse --extension ''",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "prefix" => Value::test_string("C:"),
"prefix".into(), "parent" => Value::test_string(r"C:\Users"),
"parent".into(), "stem" => Value::test_string("viking.d"),
"stem".into(), "extension" => Value::test_string(""),
"extension".into(),
],
vals: vec![
Value::test_string("C:"),
Value::test_string(r"C:\Users"),
Value::test_string("viking.d"),
Value::test_string(""),
],
})), })),
}, },
Example { Example {
description: "Parse all paths in a list", description: "Parse all paths in a list",
example: r"[ C:\Users\viking.d C:\Users\spam.txt ] | path parse", example: r"[ C:\Users\viking.d C:\Users\spam.txt ] | path parse",
result: Some(Value::test_list(vec![ result: Some(Value::test_list(vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "prefix" => Value::test_string("C:"),
"prefix".into(), "parent" => Value::test_string(r"C:\Users"),
"parent".into(), "stem" => Value::test_string("viking"),
"stem".into(), "extension" => Value::test_string("d"),
"extension".into(),
],
vals: vec![
Value::test_string("C:"),
Value::test_string(r"C:\Users"),
Value::test_string("viking"),
Value::test_string("d"),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "prefix" => Value::test_string("C:"),
"prefix".into(), "parent" => Value::test_string(r"C:\Users"),
"parent".into(), "stem" => Value::test_string("spam"),
"stem".into(), "extension" => Value::test_string("txt"),
"extension".into(),
],
vals: vec![
Value::test_string("C:"),
Value::test_string(r"C:\Users"),
Value::test_string("spam"),
Value::test_string("txt"),
],
}), }),
])), ])),
}, },
@ -178,17 +148,16 @@ On Windows, an extra 'prefix' column is added."#
#[cfg(not(windows))] #[cfg(not(windows))]
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
use nu_protocol::record;
vec![ vec![
Example { Example {
description: "Parse a path", description: "Parse a path",
example: r"'/home/viking/spam.txt' | path parse", example: r"'/home/viking/spam.txt' | path parse",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["parent".into(), "stem".into(), "extension".into()], "parent" => Value::test_string("/home/viking"),
vals: vec![ "stem" => Value::test_string("spam"),
Value::test_string("/home/viking"), "extension" => Value::test_string("txt"),
Value::test_string("spam"),
Value::test_string("txt"),
],
})), })),
}, },
Example { Example {
@ -199,34 +168,25 @@ On Windows, an extra 'prefix' column is added."#
Example { Example {
description: "Ignore the extension", description: "Ignore the extension",
example: r"'/etc/conf.d' | path parse --extension ''", example: r"'/etc/conf.d' | path parse --extension ''",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["parent".into(), "stem".into(), "extension".into()], "parent" => Value::test_string("/etc"),
vals: vec![ "stem" => Value::test_string("conf.d"),
Value::test_string("/etc"), "extension" => Value::test_string(""),
Value::test_string("conf.d"),
Value::test_string(""),
],
})), })),
}, },
Example { Example {
description: "Parse all paths in a list", description: "Parse all paths in a list",
example: r"[ /home/viking.d /home/spam.txt ] | path parse", example: r"[ /home/viking.d /home/spam.txt ] | path parse",
result: Some(Value::test_list(vec![ result: Some(Value::test_list(vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["parent".into(), "stem".into(), "extension".into()], "parent" => Value::test_string("/home"),
vals: vec![ "stem" => Value::test_string("viking"),
Value::test_string("/home"), "extension" => Value::test_string("d"),
Value::test_string("viking"),
Value::test_string("d"),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["parent".into(), "stem".into(), "extension".into()], "parent" => Value::test_string("/home"),
vals: vec![ "stem" => Value::test_string("spam"),
Value::test_string("/home"), "extension" => Value::test_string("txt"),
Value::test_string("spam"),
Value::test_string("txt"),
],
}), }),
])), ])),
}, },

View File

@ -227,137 +227,119 @@ pub fn compare(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use nu_protocol::{Record, Span, Value}; use nu_protocol::{record, Value};
#[test] #[test]
fn test_sort_value() { fn test_sort_value() {
let val = Value::list( let val = Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "fruit" => Value::test_string("pear"),
cols: vec!["fruit".to_string(), "count".to_string()], "count" => Value::test_int(3),
vals: vec![Value::test_string("pear"), Value::test_int(3)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("orange"),
vals: vec![Value::test_string("orange"), Value::test_int(7)], "count" => Value::test_int(7),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("apple"),
vals: vec![Value::test_string("apple"), Value::test_int(9)], "count" => Value::test_int(9),
}), }),
], ]);
Span::test_data(),
);
let sorted_alphabetically = let sorted_alphabetically =
sort_value(&val, vec!["fruit".to_string()], true, false, false).unwrap(); sort_value(&val, vec!["fruit".to_string()], true, false, false).unwrap();
assert_eq!( assert_eq!(
sorted_alphabetically, sorted_alphabetically,
Value::list( Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "fruit" => Value::test_string("apple"),
cols: vec!["fruit".to_string(), "count".to_string()], "count" => Value::test_int(9),
vals: vec![Value::test_string("apple"), Value::test_int(9)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("orange"),
vals: vec![Value::test_string("orange"), Value::test_int(7)], "count" => Value::test_int(7),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("pear"),
vals: vec![Value::test_string("pear"), Value::test_int(3)], "count" => Value::test_int(3),
}), }),
], ],)
Span::test_data(),
)
); );
let sorted_by_count_desc = let sorted_by_count_desc =
sort_value(&val, vec!["count".to_string()], false, false, false).unwrap(); sort_value(&val, vec!["count".to_string()], false, false, false).unwrap();
assert_eq!( assert_eq!(
sorted_by_count_desc, sorted_by_count_desc,
Value::list( Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "fruit" => Value::test_string("apple"),
cols: vec!["fruit".to_string(), "count".to_string()], "count" => Value::test_int(9),
vals: vec![Value::test_string("apple"), Value::test_int(9)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("orange"),
vals: vec![Value::test_string("orange"), Value::test_int(7)], "count" => Value::test_int(7),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("pear"),
vals: vec![Value::test_string("pear"), Value::test_int(3)], "count" => Value::test_int(3),
}), }),
], ],)
Span::test_data(),
)
); );
} }
#[test] #[test]
fn test_sort_value_in_place() { fn test_sort_value_in_place() {
let mut val = Value::list( let mut val = Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "fruit" => Value::test_string("pear"),
cols: vec!["fruit".to_string(), "count".to_string()], "count" => Value::test_int(3),
vals: vec![Value::test_string("pear"), Value::test_int(3)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("orange"),
vals: vec![Value::test_string("orange"), Value::test_int(7)], "count" => Value::test_int(7),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("apple"),
vals: vec![Value::test_string("apple"), Value::test_int(9)], "count" => Value::test_int(9),
}), }),
], ]);
Span::test_data(),
);
sort_value_in_place(&mut val, vec!["fruit".to_string()], true, false, false).unwrap(); sort_value_in_place(&mut val, vec!["fruit".to_string()], true, false, false).unwrap();
assert_eq!( assert_eq!(
val, val,
Value::list( Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "fruit" => Value::test_string("apple"),
cols: vec!["fruit".to_string(), "count".to_string()], "count" => Value::test_int(9),
vals: vec![Value::test_string("apple"), Value::test_int(9)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("orange"),
vals: vec![Value::test_string("orange"), Value::test_int(7)], "count" => Value::test_int(7),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("pear"),
vals: vec![Value::test_string("pear"), Value::test_int(3)], "count" => Value::test_int(3),
}), }),
], ],)
Span::test_data(),
)
); );
sort_value_in_place(&mut val, vec!["count".to_string()], false, false, false).unwrap(); sort_value_in_place(&mut val, vec!["count".to_string()], false, false, false).unwrap();
assert_eq!( assert_eq!(
val, val,
Value::list( Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "fruit" => Value::test_string("apple"),
cols: vec!["fruit".to_string(), "count".to_string()], "count" => Value::test_int(9),
vals: vec![Value::test_string("apple"), Value::test_int(9)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("orange"),
vals: vec![Value::test_string("orange"), Value::test_int(7)], "count" => Value::test_int(7),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["fruit".to_string(), "count".to_string()], "fruit" => Value::test_string("pear"),
vals: vec![Value::test_string("pear"), Value::test_int(3)], "count" => Value::test_int(3),
}), }),
], ],)
Span::test_data(),
)
); );
} }
} }

View File

@ -6,8 +6,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, Range, Record, ShellError, record, Category, Example, IntoInterruptiblePipelineData, PipelineData, Range, Record,
Signature, Span, Spanned, SyntaxShape, Type, Value, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
}; };
type Input<'t> = Peekable<CharIndices<'t>>; type Input<'t> = Peekable<CharIndices<'t>>;
@ -58,26 +58,15 @@ impl Command for DetectColumns {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![ vec![
Example { Example {
description: "Splits string across multiple columns", description: "Splits string across multiple columns",
example: "'a b c' | detect columns --no-headers", example: "'a b c' | detect columns --no-headers",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "column0" => Value::test_string("a"),
cols: vec![ "column1" => Value::test_string("b"),
"column0".to_string(), "column2" => Value::test_string("c"),
"column1".to_string(), })])),
"column2".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
})],
span,
)),
}, },
Example { Example {
description: "", description: "",

View File

@ -6,7 +6,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, ListStream, PipelineData, Record, ShellError, Signature, Span, Spanned, record, Category, Example, ListStream, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
@ -43,13 +43,10 @@ impl Command for Parse {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let result = Value::list( let result = Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "foo" => Value::test_string("hi"),
cols: vec!["foo".to_string(), "bar".to_string()], "bar" => Value::test_string("there"),
vals: vec![Value::test_string("hi"), Value::test_string("there")], })]);
})],
Span::test_data(),
);
vec![ vec![
Example { Example {
@ -65,55 +62,46 @@ impl Command for Parse {
Example { Example {
description: "Parse a string using fancy-regex named capture group pattern", description: "Parse a string using fancy-regex named capture group pattern",
example: "\"foo bar.\" | parse --regex '\\s*(?<name>\\w+)(?=\\.)'", example: "\"foo bar.\" | parse --regex '\\s*(?<name>\\w+)(?=\\.)'",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["name".to_string()], "name" => Value::test_string("bar"),
vals: vec![Value::test_string("bar")],
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Parse a string using fancy-regex capture group pattern", description: "Parse a string using fancy-regex capture group pattern",
example: "\"foo! bar.\" | parse --regex '(\\w+)(?=\\.)|(\\w+)(?=!)'", example: "\"foo! bar.\" | parse --regex '(\\w+)(?=\\.)|(\\w+)(?=!)'",
result: Some(Value::list( result: Some(Value::test_list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["capture0".to_string(), "capture1".to_string()], "capture0" => Value::test_string(""),
vals: vec![Value::test_string(""), Value::test_string("foo")], "capture1" => Value::test_string("foo"),
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["capture0".to_string(), "capture1".to_string()], "capture0" => Value::test_string("bar"),
vals: vec![Value::test_string("bar"), Value::test_string("")], "capture1" => Value::test_string(""),
}), }),
], ],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Parse a string using fancy-regex look behind pattern", description: "Parse a string using fancy-regex look behind pattern",
example: example:
"\" @another(foo bar) \" | parse --regex '\\s*(?<=[() ])(@\\w+)(\\([^)]*\\))?\\s*'", "\" @another(foo bar) \" | parse --regex '\\s*(?<=[() ])(@\\w+)(\\([^)]*\\))?\\s*'",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["capture0".to_string(), "capture1".to_string()], "capture0" => Value::test_string("@another"),
vals: vec![ "capture1" => Value::test_string("(foo bar)"),
Value::test_string("@another"),
Value::test_string("(foo bar)"),
],
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Parse a string using fancy-regex look ahead atomic group pattern", description: "Parse a string using fancy-regex look ahead atomic group pattern",
example: "\"abcd\" | parse --regex '^a(bc(?=d)|b)cd$'", example: "\"abcd\" | parse --regex '^a(bc(?=d)|b)cd$'",
result: Some(Value::list( result: Some(Value::test_list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["capture0".to_string()], "capture0" => Value::test_string("b"),
vals: vec![Value::test_string("b")],
})], })],
Span::test_data(),
)), )),
}, },
] ]

View File

@ -2,7 +2,7 @@ use fancy_regex::Regex;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::{fmt, str}; use std::{fmt, str};
@ -58,61 +58,34 @@ impl Command for Size {
Example { Example {
description: "Count the number of words in a string", description: "Count the number of words in a string",
example: r#""There are seven words in this sentence" | size"#, example: r#""There are seven words in this sentence" | size"#,
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "lines" => Value::test_int(1),
"lines".into(), "words" => Value::test_int(7),
"words".into(), "bytes" => Value::test_int(38),
"bytes".into(), "chars" => Value::test_int(38),
"chars".into(), "graphemes" => Value::test_int(38),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(7),
Value::test_int(38),
Value::test_int(38),
Value::test_int(38),
],
})), })),
}, },
Example { Example {
description: "Counts unicode characters", description: "Counts unicode characters",
example: r#"'今天天气真好' | size "#, example: r#"'今天天气真好' | size "#,
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "lines" => Value::test_int(1),
"lines".into(), "words" => Value::test_int(6),
"words".into(), "bytes" => Value::test_int(18),
"bytes".into(), "chars" => Value::test_int(6),
"chars".into(), "graphemes" => Value::test_int(6),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(6),
Value::test_int(18),
Value::test_int(6),
Value::test_int(6),
],
})), })),
}, },
Example { Example {
description: "Counts Unicode characters correctly in a string", description: "Counts Unicode characters correctly in a string",
example: r#""Amélie Amelie" | size"#, example: r#""Amélie Amelie" | size"#,
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "lines" => Value::test_int(1),
"lines".into(), "words" => Value::test_int(2),
"words".into(), "bytes" => Value::test_int(15),
"bytes".into(), "chars" => Value::test_int(14),
"chars".into(), "graphemes" => Value::test_int(13),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(15),
Value::test_int(14),
Value::test_int(13),
],
})), })),
}, },
] ]

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, record, Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned,
Type, Value, SyntaxShape, Type, Value,
}; };
use regex::Regex; use regex::Regex;
@ -63,74 +63,48 @@ impl Command for SubCommand {
Example { Example {
description: "Split a string into columns by the specified separator", description: "Split a string into columns by the specified separator",
example: "'a--b--c' | split column '--'", example: "'a--b--c' | split column '--'",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "column1" => Value::test_string("a"),
cols: vec![ "column2" => Value::test_string("b"),
"column1".to_string(), "column3" => Value::test_string("c"),
"column2".to_string(), })])),
"column3".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Split a string into columns of char and remove the empty columns", description: "Split a string into columns of char and remove the empty columns",
example: "'abc' | split column --collapse-empty ''", example: "'abc' | split column --collapse-empty ''",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "column1" => Value::test_string("a"),
cols: vec![ "column2" => Value::test_string("b"),
"column1".to_string(), "column3" => Value::test_string("c"),
"column2".to_string(), })])),
"column3".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Split a list of strings into a table", description: "Split a list of strings into a table",
example: "['a-b' 'c-d'] | split column -", example: "['a-b' 'c-d'] | split column -",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "column1" => Value::test_string("a"),
cols: vec!["column1".to_string(), "column2".to_string()], "column2" => Value::test_string("b"),
vals: vec![Value::test_string("a"), Value::test_string("b")],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column1".to_string(), "column2".to_string()], "column1" => Value::test_string("c"),
vals: vec![Value::test_string("c"), Value::test_string("d")], "column2" => Value::test_string("d"),
}), }),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Split a list of strings into a table, ignoring padding", description: "Split a list of strings into a table, ignoring padding",
example: r"['a - b' 'c - d'] | split column --regex '\s*-\s*'", example: r"['a - b' 'c - d'] | split column --regex '\s*-\s*'",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "column1" => Value::test_string("a"),
cols: vec!["column1".to_string(), "column2".to_string()], "column2" => Value::test_string("b"),
vals: vec![Value::test_string("a"), Value::test_string("b")],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["column1".to_string(), "column2".to_string()], "column1" => Value::test_string("c"),
vals: vec![Value::test_string("c"), Value::test_string("d")], "column2" => Value::test_string("d"),
}), }),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,10 +2,9 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath; use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::record;
use nu_protocol::Category; use nu_protocol::Category;
use nu_protocol::{ use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct SubCommand; pub struct SubCommand;
@ -68,13 +67,10 @@ impl Command for SubCommand {
Example { Example {
description: "Capitalize a column in a table", description: "Capitalize a column in a table",
example: "[[lang, gems]; [nu_test, 100]] | str capitalize lang", example: "[[lang, gems]; [nu_test, 100]] | str capitalize lang",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "lang" => Value::test_string("Nu_test"),
cols: vec!["lang".to_string(), "gems".to_string()], "gems" => Value::test_int(100),
vals: vec![Value::test_string("Nu_test"), Value::test_int(100)], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -2,10 +2,9 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath; use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::record;
use nu_protocol::Category; use nu_protocol::Category;
use nu_protocol::{ use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct SubCommand; pub struct SubCommand;
@ -68,24 +67,18 @@ impl Command for SubCommand {
Example { Example {
description: "Downcase contents", description: "Downcase contents",
example: "[[ColA ColB]; [Test ABC]] | str downcase ColA", example: "[[ColA ColB]; [Test ABC]] | str downcase ColA",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "ColA" => Value::test_string("test"),
cols: vec!["ColA".to_string(), "ColB".to_string()], "ColB" => Value::test_string("ABC"),
vals: vec![Value::test_string("test"), Value::test_string("ABC")], })])),
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Downcase contents", description: "Downcase contents",
example: "[[ColA ColB]; [Test ABC]] | str downcase ColA ColB", example: "[[ColA ColB]; [Test ABC]] | str downcase ColA ColB",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "ColA" => Value::test_string("test"),
cols: vec!["ColA".to_string(), "ColB".to_string()], "ColB" => Value::test_string("abc"),
vals: vec![Value::test_string("test"), Value::test_string("abc")], })])),
})],
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -3,8 +3,9 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath; use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::record;
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -89,32 +90,26 @@ impl Command for SubCommand {
Example { Example {
description: "Check if input contains string in a record", description: "Check if input contains string in a record",
example: "{ ColA: test, ColB: 100 } | str contains 'e' ColA", example: "{ ColA: test, ColB: 100 } | str contains 'e' ColA",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["ColA".to_string(), "ColB".to_string()], "ColA" => Value::test_bool(true),
vals: vec![Value::test_bool(true), Value::test_int(100)], "ColB" => Value::test_int(100),
})), })),
}, },
Example { Example {
description: "Check if input contains string in a table", description: "Check if input contains string in a table",
example: " [[ColA ColB]; [test 100]] | str contains --ignore-case 'E' ColA", example: " [[ColA ColB]; [test 100]] | str contains --ignore-case 'E' ColA",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "ColA" => Value::test_bool(true),
cols: vec!["ColA".to_string(), "ColB".to_string()], "ColB" => Value::test_int(100),
vals: vec![Value::test_bool(true), Value::test_int(100)], })])),
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Check if input contains string in a table", description: "Check if input contains string in a table",
example: " [[ColA ColB]; [test hello]] | str contains 'e' ColA ColB", example: " [[ColA ColB]; [test hello]] | str contains 'e' ColA ColB",
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "ColA" => Value::test_bool(true),
cols: vec!["ColA".to_string(), "ColB".to_string()], "ColB" => Value::test_bool(true),
vals: vec![Value::test_bool(true), Value::test_bool(true)], })])),
})],
Span::test_data(),
)),
}, },
Example { Example {
description: "Check if input string contains 'banana'", description: "Check if input string contains 'banana'",
@ -124,26 +119,20 @@ impl Command for SubCommand {
Example { Example {
description: "Check if list contains string", description: "Check if list contains string",
example: "[one two three] | str contains o", example: "[one two three] | str contains o",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![
Value::test_bool(true), Value::test_bool(true),
Value::test_bool(true), Value::test_bool(true),
Value::test_bool(false), Value::test_bool(false),
], ])),
Span::test_data(),
)),
}, },
Example { Example {
description: "Check if list does not contain string", description: "Check if list does not contain string",
example: "[one two three] | str contains --not o", example: "[one two three] | str contains --not o",
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![
Value::test_bool(false), Value::test_bool(false),
Value::test_bool(false), Value::test_bool(false),
Value::test_bool(true), Value::test_bool(true),
], ])),
Span::test_data(),
)),
}, },
] ]
} }

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
levenshtein_distance, Category, Example, PipelineData, Record, ShellError, Signature, Span, levenshtein_distance, record, Category, Example, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value, SyntaxShape, Type, Value,
}; };
@ -80,25 +80,21 @@ impl Command for SubCommand {
Example { Example {
description: "Compute edit distance between strings in table and another string, using cell paths", description: "Compute edit distance between strings in table and another string, using cell paths",
example: "[{a: 'nutshell' b: 'numetal'}] | str distance 'nushell' 'a' 'b'", example: "[{a: 'nutshell' b: 'numetal'}] | str distance 'nushell' 'a' 'b'",
result: Some(Value::list ( result: Some(Value::test_list (
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(4)], "b" => Value::test_int(4),
}) })])),
],
Span::test_data(),
)),
}, },
Example { Example {
description: "Compute edit distance between strings in record and another string, using cell paths", description: "Compute edit distance between strings in record and another string, using cell paths",
example: "{a: 'nutshell' b: 'numetal'} | str distance 'nushell' a b", example: "{a: 'nutshell' b: 'numetal'} | str distance 'nushell' a b",
result: Some( result: Some(
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(1),
vals: vec![Value::test_int(1), Value::test_int(4)], "b" => Value::test_int(4),
}) })),
),
}] }]
} }
} }

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, record, Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value, Type, Value,
}; };
@ -132,29 +132,22 @@ impl Command for SubCommand {
description: "Find and replace all occurrences of find string in table using regular expression", description: "Find and replace all occurrences of find string in table using regular expression",
example: example:
"[[ColA ColB ColC]; [abc abc ads]] | str replace --all --regex 'b' 'z' ColA ColC", "[[ColA ColB ColC]; [abc abc ads]] | str replace --all --regex 'b' 'z' ColA ColC",
result: Some(Value::list ( result: Some(Value::test_list (
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()], "ColA" => Value::test_string("azc"),
vals: vec![ "ColB" => Value::test_string("abc"),
Value::test_string("azc"), "ColC" => Value::test_string("ads"),
Value::test_string("abc"),
Value::test_string("ads"),
],
})], })],
Span::test_data(),
)), )),
}, },
Example { Example {
description: "Find and replace all occurrences of find string in record using regular expression", description: "Find and replace all occurrences of find string in record using regular expression",
example: example:
"{ KeyA: abc, KeyB: abc, KeyC: ads } | str replace --all --regex 'b' 'z' KeyA KeyC", "{ KeyA: abc, KeyB: abc, KeyC: ads } | str replace --all --regex 'b' 'z' KeyA KeyC",
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["KeyA".to_string(), "KeyB".to_string(), "KeyC".to_string()], "KeyA" => Value::test_string("azc"),
vals: vec![ "KeyB" => Value::test_string("abc"),
Value::test_string("azc"), "KeyC" => Value::test_string("ads"),
Value::test_string("abc"),
Value::test_string("ads"),
],
})), })),
}, },
Example { Example {

View File

@ -2,7 +2,7 @@ use fancy_regex::Regex;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value, record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::{fmt, str}; use std::{fmt, str};
@ -48,61 +48,34 @@ impl Command for SubCommand {
Example { Example {
description: "Count the number of words in a string", description: "Count the number of words in a string",
example: r#""There are seven words in this sentence" | str stats"#, example: r#""There are seven words in this sentence" | str stats"#,
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "lines" => Value::test_int(1),
"lines".into(), "words" => Value::test_int(7),
"words".into(), "bytes" => Value::test_int(38),
"bytes".into(), "chars" => Value::test_int(38),
"chars".into(), "graphemes" => Value::test_int(38),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(7),
Value::test_int(38),
Value::test_int(38),
Value::test_int(38),
],
})), })),
}, },
Example { Example {
description: "Counts unicode characters", description: "Counts unicode characters",
example: r#"'今天天气真好' | str stats "#, example: r#"'今天天气真好' | str stats "#,
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "lines" => Value::test_int(1),
"lines".into(), "words" => Value::test_int(6),
"words".into(), "bytes" => Value::test_int(18),
"bytes".into(), "chars" => Value::test_int(6),
"chars".into(), "graphemes" => Value::test_int(6),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(6),
Value::test_int(18),
Value::test_int(6),
Value::test_int(6),
],
})), })),
}, },
Example { Example {
description: "Counts Unicode characters correctly in a string", description: "Counts Unicode characters correctly in a string",
example: r#""Amélie Amelie" | str stats"#, example: r#""Amélie Amelie" | str stats"#,
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "lines" => Value::test_int(1),
"lines".into(), "words" => Value::test_int(2),
"words".into(), "bytes" => Value::test_int(15),
"bytes".into(), "chars" => Value::test_int(14),
"chars".into(), "graphemes" => Value::test_int(13),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(15),
Value::test_int(14),
Value::test_int(13),
],
})), })),
}, },
] ]

View File

@ -2,6 +2,7 @@ use lscolors::{LsColors, Style};
use nu_color_config::color_from_hex; use nu_color_config::color_from_hex;
use nu_color_config::{StyleComputer, TextStyle}; use nu_color_config::{StyleComputer, TextStyle};
use nu_engine::{env::get_config, env_to_string, CallExt}; use nu_engine::{env::get_config, env_to_string, CallExt};
use nu_protocol::record;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
@ -130,7 +131,6 @@ impl Command for Table {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![ vec![
Example { Example {
description: "List the files in current directory, with indexes starting from 1.", description: "List the files in current directory, with indexes starting from 1.",
@ -140,53 +140,44 @@ impl Command for Table {
Example { Example {
description: "Render data in table view", description: "Render data in table view",
example: r#"[[a b]; [1 2] [3 4]] | table"#, example: r#"[[a b]; [1 2] [3 4]] | table"#,
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(1),
cols: vec!["a".to_string(), "b".to_string()], "b" => Value::test_int(2),
vals: vec![Value::test_int(1), Value::test_int(2)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(3),
vals: vec![Value::test_int(3), Value::test_int(4)], "b" => Value::test_int(4),
}), }),
], ])),
span,
)),
}, },
Example { Example {
description: "Render data in table view (expanded)", description: "Render data in table view (expanded)",
example: r#"[[a b]; [1 2] [2 [4 4]]] | table --expand"#, example: r#"[[a b]; [1 2] [2 [4 4]]] | table --expand"#,
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(1),
cols: vec!["a".to_string(), "b".to_string()], "b" => Value::test_int(2),
vals: vec![Value::test_int(1), Value::test_int(2)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(3),
vals: vec![Value::test_int(3), Value::test_int(4)], "b" => Value::test_int(4),
}), }),
], ])),
span,
)),
}, },
Example { Example {
description: "Render data in table view (collapsed)", description: "Render data in table view (collapsed)",
example: r#"[[a b]; [1 2] [2 [4 4]]] | table --collapse"#, example: r#"[[a b]; [1 2] [2 [4 4]]] | table --collapse"#,
result: Some(Value::list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(Record { "a" => Value::test_int(1),
cols: vec!["a".to_string(), "b".to_string()], "b" => Value::test_int(2),
vals: vec![Value::test_int(1), Value::test_int(2)],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec!["a".to_string(), "b".to_string()], "a" => Value::test_int(3),
vals: vec![Value::test_int(3), Value::test_int(4)], "b" => Value::test_int(4),
}), }),
], ])),
span,
)),
}, },
] ]
} }

View File

@ -3846,23 +3846,22 @@ fn get_filesize_format(format_value: &str, filesize_metric: Option<bool>) -> (By
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Record, Value};
use super::{Record, Span, Value}; use crate::record;
mod is_empty { mod is_empty {
use super::*; use super::*;
#[test] #[test]
fn test_string() { fn test_string() {
let value = Value::string("", Span::unknown()); let value = Value::test_string("");
assert!(value.is_empty()); assert!(value.is_empty());
} }
#[test] #[test]
fn test_list() { fn test_list() {
let list_with_no_values = Value::list(vec![], Span::unknown()); let list_with_no_values = Value::test_list(vec![]);
let list_with_one_empty_string = let list_with_one_empty_string = Value::test_list(vec![Value::test_string("")]);
Value::list(vec![Value::string("", Span::unknown())], Span::unknown());
assert!(list_with_no_values.is_empty()); assert!(list_with_no_values.is_empty());
assert!(!list_with_one_empty_string.is_empty()); assert!(!list_with_one_empty_string.is_empty());
@ -3872,21 +3871,18 @@ mod tests {
fn test_record() { fn test_record() {
let no_columns_nor_cell_values = Value::test_record(Record::new()); let no_columns_nor_cell_values = Value::test_record(Record::new());
let one_column_and_one_cell_value_with_empty_strings = Value::test_record(Record { let one_column_and_one_cell_value_with_empty_strings = Value::test_record(record! {
cols: vec![String::from("")], "" => Value::test_string(""),
vals: vec![Value::string("", Span::unknown())],
}); });
let one_column_with_a_string_and_one_cell_value_with_empty_string = let one_column_with_a_string_and_one_cell_value_with_empty_string =
Value::test_record(Record { Value::test_record(record! {
cols: vec![String::from("column")], "column" => Value::test_string(""),
vals: vec![Value::string("", Span::unknown())],
}); });
let one_column_with_empty_string_and_one_value_with_a_string = let one_column_with_empty_string_and_one_value_with_a_string =
Value::test_record(Record { Value::test_record(record! {
cols: vec![String::from("")], "" => Value::test_string("text"),
vals: vec![Value::string("text", Span::unknown())],
}); });
assert!(no_columns_nor_cell_values.is_empty()); assert!(no_columns_nor_cell_values.is_empty());
@ -3903,24 +3899,15 @@ mod tests {
#[test] #[test]
fn test_list() { fn test_list() {
let list_of_ints = Value::list(vec![Value::int(0, Span::unknown())], Span::unknown()); let list_of_ints = Value::test_list(vec![Value::test_int(0)]);
let list_of_floats = let list_of_floats = Value::test_list(vec![Value::test_float(0.0)]);
Value::list(vec![Value::float(0.0, Span::unknown())], Span::unknown()); let list_of_ints_and_floats =
let list_of_ints_and_floats = Value::list( Value::test_list(vec![Value::test_int(0), Value::test_float(0.0)]);
vec![ let list_of_ints_and_floats_and_bools = Value::test_list(vec![
Value::int(0, Span::unknown()), Value::test_int(0),
Value::float(0.0, Span::unknown()), Value::test_float(0.0),
], Value::test_bool(false),
Span::unknown(), ]);
);
let list_of_ints_and_floats_and_bools = Value::list(
vec![
Value::int(0, Span::unknown()),
Value::float(0.0, Span::unknown()),
Value::bool(false, Span::unknown()),
],
Span::unknown(),
);
assert_eq!(list_of_ints.get_type(), Type::List(Box::new(Type::Int))); assert_eq!(list_of_ints.get_type(), Type::List(Box::new(Type::Int)));
assert_eq!(list_of_floats.get_type(), Type::List(Box::new(Type::Float))); assert_eq!(list_of_floats.get_type(), Type::List(Box::new(Type::Float)));
assert_eq!( assert_eq!(
@ -3941,13 +3928,10 @@ mod tests {
#[test] #[test]
fn test_datetime() { fn test_datetime() {
let string = Value::date( let string = Value::test_date(DateTime::from_naive_utc_and_offset(
DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_millis(-123456789).unwrap(), NaiveDateTime::from_timestamp_millis(-123456789).unwrap(),
FixedOffset::east_opt(0).unwrap(), FixedOffset::east_opt(0).unwrap(),
), ))
Span::unknown(),
)
.into_string("", &Default::default()); .into_string("", &Default::default());
// We need to cut the humanized part off for tests to work, because // We need to cut the humanized part off for tests to work, because
@ -3958,13 +3942,10 @@ mod tests {
#[test] #[test]
fn test_negative_year_datetime() { fn test_negative_year_datetime() {
let string = Value::date( let string = Value::test_date(DateTime::from_naive_utc_and_offset(
DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_millis(-72135596800000).unwrap(), NaiveDateTime::from_timestamp_millis(-72135596800000).unwrap(),
FixedOffset::east_opt(0).unwrap(), FixedOffset::east_opt(0).unwrap(),
), ))
Span::unknown(),
)
.into_string("", &Default::default()); .into_string("", &Default::default());
// We need to cut the humanized part off for tests to work, because // We need to cut the humanized part off for tests to work, because

View File

@ -2,7 +2,7 @@ use eml_parser::eml::*;
use eml_parser::EmlParser; use eml_parser::EmlParser;
use indexmap::map::IndexMap; use indexmap::map::IndexMap;
use nu_plugin::{EvaluatedCall, LabeledError}; use nu_plugin::{EvaluatedCall, LabeledError};
use nu_protocol::{record, PluginExample, Record, ShellError, Span, Value}; use nu_protocol::{record, PluginExample, ShellError, Span, Value};
const DEFAULT_BODY_PREVIEW: usize = 50; const DEFAULT_BODY_PREVIEW: usize = 50;
pub const CMD_NAME: &str = "from eml"; pub const CMD_NAME: &str = "from eml";
@ -24,31 +24,17 @@ Subject: Welcome
To: someone@somewhere.com To: someone@somewhere.com
Test' | from eml" Test' | from eml"
.into(), .into(),
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "Subject" => Value::test_string("Welcome"),
"Subject".to_string(), "From" => Value::test_record(record! {
"From".to_string(), "Name" => Value::nothing(Span::test_data()),
"To".to_string(), "Address" => Value::test_string("test@email.com"),
"Body".to_string(),
],
vals: vec![
Value::test_string("Welcome"),
Value::test_record(Record {
cols: vec!["Name".to_string(), "Address".to_string()],
vals: vec![
Value::nothing(Span::test_data()),
Value::test_string("test@email.com"),
],
}), }),
Value::test_record(Record { "To" => Value::test_record(record! {
cols: vec!["Name".to_string(), "Address".to_string()], "Name" => Value::nothing(Span::test_data()),
vals: vec![ "Address" => Value::test_string("someone@somewhere.com"),
Value::nothing(Span::test_data()),
Value::test_string("someone@somewhere.com"),
],
}), }),
Value::test_string("Test"), "Body" => Value::test_string("Test"),
],
})), })),
}, },
PluginExample { PluginExample {
@ -58,31 +44,17 @@ Subject: Welcome
To: someone@somewhere.com To: someone@somewhere.com
Test' | from eml -b 1" Test' | from eml -b 1"
.into(), .into(),
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec![ "Subject" => Value::test_string("Welcome"),
"Subject".to_string(), "From" => Value::test_record(record! {
"From".to_string(), "Name" => Value::nothing(Span::test_data()),
"To".to_string(), "Address" => Value::test_string("test@email.com"),
"Body".to_string(),
],
vals: vec![
Value::test_string("Welcome"),
Value::test_record(Record {
cols: vec!["Name".to_string(), "Address".to_string()],
vals: vec![
Value::nothing(Span::test_data()),
Value::test_string("test@email.com"),
],
}), }),
Value::test_record(Record { "To" => Value::test_record(record! {
cols: vec!["Name".to_string(), "Address".to_string()], "Name" => Value::nothing(Span::test_data()),
vals: vec![ "Address" => Value::test_string("someone@somewhere.com"),
Value::nothing(Span::test_data()),
Value::test_string("someone@somewhere.com"),
],
}), }),
Value::test_string("T"), "Body" => Value::test_string("T"),
],
})), })),
}, },
] ]

View File

@ -2,7 +2,7 @@ use ical::parser::ical::component::*;
use ical::property::Property; use ical::property::Property;
use indexmap::map::IndexMap; use indexmap::map::IndexMap;
use nu_plugin::{EvaluatedCall, LabeledError}; use nu_plugin::{EvaluatedCall, LabeledError};
use nu_protocol::{record, PluginExample, Record, ShellError, Span, Value}; use nu_protocol::{record, PluginExample, ShellError, Span, Value};
use std::io::BufReader; use std::io::BufReader;
pub const CMD_NAME: &str = "from ics"; pub const CMD_NAME: &str = "from ics";
@ -55,29 +55,15 @@ pub fn examples() -> Vec<PluginExample> {
END:VCALENDAR' | from ics" END:VCALENDAR' | from ics"
.into(), .into(),
description: "Converts ics formatted string to table".into(), description: "Converts ics formatted string to table".into(),
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "properties" => Value::test_list(vec![]),
cols: vec![ "events" => Value::test_list(vec![]),
"properties".to_string(), "alarms" => Value::test_list(vec![]),
"events".to_string(), "to-Dos" => Value::test_list(vec![]),
"alarms".to_string(), "journals" => Value::test_list(vec![]),
"to-Dos".to_string(), "free-busys" => Value::test_list(vec![]),
"journals".to_string(), "timezones" => Value::test_list(vec![]),
"free-busys".to_string(), })])),
"timezones".to_string(),
],
vals: vec![
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
],
})],
Span::test_data(),
)),
}] }]
} }

View File

@ -1,5 +1,5 @@
use nu_plugin::{EvaluatedCall, LabeledError}; use nu_plugin::{EvaluatedCall, LabeledError};
use nu_protocol::{PluginExample, Record, ShellError, Value}; use nu_protocol::{record, PluginExample, Record, ShellError, Value};
pub const CMD_NAME: &str = "from ini"; pub const CMD_NAME: &str = "from ini";
@ -57,12 +57,11 @@ a=1
b=2' | from ini" b=2' | from ini"
.into(), .into(),
description: "Converts ini formatted string to record".into(), description: "Converts ini formatted string to record".into(),
result: Some(Value::test_record(Record { result: Some(Value::test_record(record! {
cols: vec!["foo".to_string()], "foo" => Value::test_record(record! {
vals: vec![Value::test_record(Record { "a" => Value::test_string("1"),
cols: vec!["a".to_string(), "b".to_string()], "b" => Value::test_string("2"),
vals: vec![Value::test_string("1"), Value::test_string("2")], }),
})],
})), })),
}] }]
} }

View File

@ -2,7 +2,7 @@ use ical::parser::vcard::component::*;
use ical::property::Property; use ical::property::Property;
use indexmap::map::IndexMap; use indexmap::map::IndexMap;
use nu_plugin::{EvaluatedCall, LabeledError}; use nu_plugin::{EvaluatedCall, LabeledError};
use nu_protocol::{record, PluginExample, Record, ShellError, Span, Value}; use nu_protocol::{record, PluginExample, ShellError, Span, Value};
pub const CMD_NAME: &str = "from vcf"; pub const CMD_NAME: &str = "from vcf";
@ -55,53 +55,27 @@ EMAIL:foo@bar.com
END:VCARD' | from vcf" END:VCARD' | from vcf"
.into(), .into(),
description: "Converts ics formatted string to table".into(), description: "Converts ics formatted string to table".into(),
result: Some(Value::list( result: Some(Value::test_list(vec![Value::test_record(record! {
vec![Value::test_record(Record { "properties" => Value::test_list(
cols: vec!["properties".to_string()],
vals: vec![Value::list(
vec![ vec![
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "name" => Value::test_string("N"),
"name".to_string(), "value" => Value::test_string("Foo"),
"value".to_string(), "params" => Value::nothing(Span::test_data()),
"params".to_string(),
],
vals: vec![
Value::test_string("N"),
Value::test_string("Foo"),
Value::nothing(Span::test_data()),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "name" => Value::test_string("FN"),
"name".to_string(), "value" => Value::test_string("Bar"),
"value".to_string(), "params" => Value::nothing(Span::test_data()),
"params".to_string(),
],
vals: vec![
Value::test_string("FN"),
Value::test_string("Bar"),
Value::nothing(Span::test_data()),
],
}), }),
Value::test_record(Record { Value::test_record(record! {
cols: vec![ "name" => Value::test_string("EMAIL"),
"name".to_string(), "value" => Value::test_string("foo@bar.com"),
"value".to_string(), "params" => Value::nothing(Span::test_data()),
"params".to_string(),
],
vals: vec![
Value::test_string("EMAIL"),
Value::test_string("foo@bar.com"),
Value::nothing(Span::test_data()),
],
}), }),
], ],
Span::test_data(), ),
)], })])),
})],
Span::test_data(),
)),
}] }]
} }

View File

@ -111,7 +111,7 @@ fn build_xpath(xpath_str: &str, span: Span) -> Result<sxd_xpath::XPath, LabeledE
mod tests { mod tests {
use super::execute_xpath_query as query; use super::execute_xpath_query as query;
use nu_plugin::EvaluatedCall; use nu_plugin::EvaluatedCall;
use nu_protocol::{Record, Span, Spanned, Value}; use nu_protocol::{record, Span, Spanned, Value};
#[test] #[test]
fn position_function_in_predicate() { fn position_function_in_predicate() {
@ -133,9 +133,8 @@ mod tests {
let actual = query("", &call, &text, Some(spanned_str)).expect("test should not fail"); let actual = query("", &call, &text, Some(spanned_str)).expect("test should not fail");
let expected = Value::list( let expected = Value::list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["count(//a/*[posit...".to_string()], "count(//a/*[posit..." => Value::test_float(1.0),
vals: vec![Value::test_float(1.0)],
})], })],
Span::test_data(), Span::test_data(),
); );
@ -163,9 +162,8 @@ mod tests {
let actual = query("", &call, &text, Some(spanned_str)).expect("test should not fail"); let actual = query("", &call, &text, Some(spanned_str)).expect("test should not fail");
let expected = Value::list( let expected = Value::list(
vec![Value::test_record(Record { vec![Value::test_record(record! {
cols: vec!["count(//*[contain...".to_string()], "count(//*[contain..." => Value::test_float(1.0),
vals: vec![Value::test_float(1.0)],
})], })],
Span::test_data(), Span::test_data(),
); );