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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,10 @@
use nu_engine::CallExt;
use nu_protocol::IntoPipelineData;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
SyntaxShape, Type, Value,
record, Category, Example, PipelineData, Record, ShellError, Signature, SyntaxShape, Type,
Value,
};
#[derive(Clone)]
@ -38,142 +39,104 @@ impl Command for Rotate {
Example {
description: "Rotate a record clockwise, producing a table (like `transpose` but with column order reversed)",
example: "{a:1, b:2} | rotate",
result: Some(Value::list(vec![
Value::test_record(Record {
cols: vec!["column0".to_string(), "column1".to_string()],
vals: vec![Value::test_int(1), Value::test_string("a")],
result: Some(Value::test_list(vec![
Value::test_record(record! {
"column0" => Value::test_int(1),
"column1" => Value::test_string("a"),
}),
Value::test_record(Record {
cols: vec!["column0".to_string(), "column1".to_string()],
vals: vec![Value::test_int(2), Value::test_string("b")],
Value::test_record(record! {
"column0" => Value::test_int(2),
"column1" => Value::test_string("b"),
}),
],
Span::test_data(),
)),
},
Example {
description: "Rotate 2x3 table clockwise",
example: "[[a b]; [1 2] [3 4] [5 6]] | rotate",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec![
"column0".to_string(),
"column1".to_string(),
"column2".to_string(),
"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! {
"column0" => Value::test_int(5),
"column1" => Value::test_int(3),
"column2" => Value::test_int(1),
"column3" => Value::test_string("a"),
}),
Value::test_record(Record {
cols: vec![
"column0".to_string(),
"column1".to_string(),
"column2".to_string(),
"column3".to_string(),
],
vals: vec![
Value::test_int(6),
Value::test_int(4),
Value::test_int(2),
Value::test_string("b"),
],
Value::test_record(record! {
"column0" => Value::test_int(6),
"column1" => Value::test_int(4),
"column2" => Value::test_int(2),
"column3" => Value::test_string("b"),
}),
],
Span::test_data(),
)),
},
Example {
description: "Rotate table clockwise and change columns names",
example: "[[a b]; [1 2]] | rotate col_a col_b",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["col_a".to_string(), "col_b".to_string()],
vals: vec![Value::test_int(1), Value::test_string("a")],
Value::test_record(record! {
"col_a" => Value::test_int(1),
"col_b" => Value::test_string("a"),
}),
Value::test_record(Record {
cols: vec!["col_a".to_string(), "col_b".to_string()],
vals: vec![Value::test_int(2), Value::test_string("b")],
Value::test_record(record! {
"col_a" => Value::test_int(2),
"col_b" => Value::test_string("b"),
}),
],
Span::test_data(),
)),
},
Example {
description: "Rotate table counter clockwise",
example: "[[a b]; [1 2]] | rotate --ccw",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["column0".to_string(), "column1".to_string()],
vals: vec![Value::test_string("b"), Value::test_int(2)],
Value::test_record(record! {
"column0" => Value::test_string("b"),
"column1" => Value::test_int(2),
}),
Value::test_record(Record {
cols: vec!["column0".to_string(), "column1".to_string()],
vals: vec![Value::test_string("a"), Value::test_int(1)],
Value::test_record(record! {
"column0" => Value::test_string("a"),
"column1" => Value::test_int(1),
}),
],
Span::test_data(),
)),
},
Example {
description: "Rotate table counter-clockwise",
example: "[[a b]; [1 2] [3 4] [5 6]] | rotate --ccw",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec![
"column0".to_string(),
"column1".to_string(),
"column2".to_string(),
"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! {
"column0" => Value::test_string("b"),
"column1" => Value::test_int(2),
"column2" => Value::test_int(4),
"column3" => Value::test_int(6),
}),
Value::test_record(Record {
cols: vec![
"column0".to_string(),
"column1".to_string(),
"column2".to_string(),
"column3".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_int(1),
Value::test_int(3),
Value::test_int(5),
],
Value::test_record(record! {
"column0" => Value::test_string("a"),
"column1" => Value::test_int(1),
"column2" => Value::test_int(3),
"column3" => Value::test_int(5),
}),
],
Span::test_data(),
)),
},
Example {
description: "Rotate table counter-clockwise and change columns names",
example: "[[a b]; [1 2]] | rotate --ccw col_a col_b",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["col_a".to_string(), "col_b".to_string()],
vals: vec![Value::test_string("b"), Value::test_int(2)],
Value::test_record(record! {
"col_a" => Value::test_string("b"),
"col_b" => Value::test_int(2),
}),
Value::test_record(Record {
cols: vec!["col_a".to_string(), "col_b".to_string()],
vals: vec![Value::test_string("a"), Value::test_int(1)],
Value::test_record(record! {
"col_a" => Value::test_string("a"),
"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::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
PipelineIterator, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
PipelineIterator, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use std::collections::HashSet;
use std::iter::FromIterator;
@ -51,29 +51,15 @@ impl Command for UpdateCells {
$value
}
}"#,
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec![
"2021-04-16".into(),
"2021-06-10".into(),
"2021-09-18".into(),
"2021-10-15".into(),
"2021-11-16".into(),
"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(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"2021-04-16" => Value::test_int(37),
"2021-06-10" => Value::test_string(""),
"2021-09-18" => Value::test_string(""),
"2021-10-15" => Value::test_string(""),
"2021-11-16" => Value::test_int(37),
"2021-11-17" => Value::test_string(""),
"2021-11-18" => Value::test_string(""),
})])),
},
Example {
description: "Update the zero value cells to empty strings in 2 last columns.",
@ -87,29 +73,15 @@ impl Command for UpdateCells {
$value
}
}"#,
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec![
"2021-04-16".into(),
"2021-06-10".into(),
"2021-09-18".into(),
"2021-10-15".into(),
"2021-11-16".into(),
"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(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"2021-04-16" => Value::test_int(37),
"2021-06-10" => Value::test_int(0),
"2021-09-18" => Value::test_int(0),
"2021-10-15" => Value::test_int(0),
"2021-11-16" => Value::test_int(37),
"2021-11-17" => Value::test_string(""),
"2021-11-18" => Value::test_string(""),
})])),
},
]
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack};
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 {
@ -95,33 +95,29 @@ impl Command for BytesIndexOf {
Example {
description: "Returns all matched index",
example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of --all 0x[33 44]",
result: Some(Value::list(
vec![Value::test_int(0), Value::test_int(5), Value::test_int(7)],
Span::test_data(),
)),
result: Some(Value::test_list(vec![
Value::test_int(0),
Value::test_int(5),
Value::test_int(7),
])),
},
Example {
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]",
result: Some(Value::list(
vec![Value::test_int(7), Value::test_int(5), Value::test_int(0)],
Span::test_data(),
)),
result: Some(Value::test_list(vec![
Value::test_int(7),
Value::test_int(5),
Value::test_int(0),
])),
},
Example {
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"#,
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::test_int(0),
Value::binary(vec![0x14, 0x15, 0x16], Span::test_data()),
Value::test_int(-1),
],
})],
Span::test_data(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"ColA" => Value::test_int(0),
"ColB" => Value::binary(vec![0x14, 0x15, 0x16], Span::test_data()),
"ColC" => Value::test_int(-1),
})])),
},
]
}

View File

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

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape,
record, Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value,
};
@ -87,41 +87,26 @@ impl Command for BytesReplace {
Example {
description: "Find and replace contents",
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],
Span::test_data(),
)),
},
Example {
description: "Find and replace all occurrences of find binary",
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],
Span::test_data(),
)),
},
Example {
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",
result: Some(Value::list (
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::binary (
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(),
),
],
result: Some(Value::test_list (
vec![Value::test_record(record! {
"ColA" => Value::test_binary(vec![0x13, 0x12, 0x13]),
"ColB" => Value::test_binary(vec![0x14, 0x15, 0x16]),
"ColC" => Value::test_binary(vec![0x17, 0x18, 0x19]),
})],
Span::test_data(),
)),
},
]

View File

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

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
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)]
@ -53,61 +53,52 @@ impl Command for SubCommand {
}
fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![
Example {
description: "Convert value to boolean in table",
example: "[[value]; ['false'] ['1'] [0] [1.0] [true]] | into bool value",
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::bool(false, span)],
}),
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::bool(true, span)],
}),
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::bool(false, span)],
}),
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::bool(true, span)],
}),
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::bool(true, span)],
}),
],
span,
)),
result: Some(Value::test_list(vec![
Value::test_record(record! {
"value" => Value::test_bool(false),
}),
Value::test_record(record! {
"value" => Value::test_bool(true),
}),
Value::test_record(record! {
"value" => Value::test_bool(false),
}),
Value::test_record(record! {
"value" => Value::test_bool(true),
}),
Value::test_record(record! {
"value" => Value::test_bool(true),
}),
])),
},
Example {
description: "Convert bool to boolean",
example: "true | into bool",
result: Some(Value::bool(true, span)),
result: Some(Value::test_bool(true)),
},
Example {
description: "convert int to boolean",
example: "1 | into bool",
result: Some(Value::bool(true, span)),
result: Some(Value::test_bool(true)),
},
Example {
description: "convert float to boolean",
example: "0.3 | into bool",
result: Some(Value::bool(true, span)),
result: Some(Value::test_bool(true)),
},
Example {
description: "convert float string to boolean",
example: "'0.0' | into bool",
result: Some(Value::bool(false, span)),
result: Some(Value::test_bool(false)),
},
Example {
description: "convert string to boolean",
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::{
ast::{Call, CellPath, Expr},
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,
};
@ -64,65 +64,55 @@ impl Command for SubCommand {
}
fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![
Example {
description: "Convert duration string to duration value",
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 {
description: "Convert compound duration string to duration value",
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,
span,
)),
},
Example {
description: "Convert table of duration strings to table of duration values",
example:
"[[value]; ['1sec'] ['2min'] ['3hr'] ['4day'] ['5wk']] | into duration value",
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::duration(NS_PER_SEC, span)],
}),
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::duration(2 * 60 * NS_PER_SEC, span)],
}),
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::duration(3 * 60 * 60 * NS_PER_SEC, span)],
}),
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::duration(4 * 24 * 60 * 60 * NS_PER_SEC, span)],
}),
Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::duration(5 * 7 * 24 * 60 * 60 * NS_PER_SEC, span)],
}),
],
span,
)),
result: Some(Value::test_list(vec![
Value::test_record(record! {
"value" => Value::test_duration(NS_PER_SEC),
}),
Value::test_record(record! {
"value" => Value::test_duration(2 * 60 * NS_PER_SEC),
}),
Value::test_record(record! {
"value" => Value::test_duration(3 * 60 * 60 * NS_PER_SEC),
}),
Value::test_record(record! {
"value" => Value::test_duration(4 * 24 * 60 * 60 * NS_PER_SEC),
}),
Value::test_record(record! {
"value" => Value::test_duration(5 * 7 * 24 * 60 * 60 * NS_PER_SEC),
}),
])),
},
Example {
description: "Convert duration to 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 {
description: "Convert a number of ns to duration",
example: "1_234_567 | into duration",
result: Some(Value::duration(1_234_567, span)),
result: Some(Value::test_duration(1_234_567)),
},
Example {
description: "Convert a number of an arbitrary unit to duration",
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::{
ast::{Call, CellPath},
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)]
@ -79,45 +79,36 @@ impl Command for SubCommand {
Example {
description: "Convert string to filesize in table",
example: r#"[[device size]; ["/dev/sda1" "200"] ["/dev/loop0" "50"]] | into filesize size"#,
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["device".to_string(), "size".to_string()],
vals: vec![
Value::string("/dev/sda1".to_string(), Span::test_data()),
Value::filesize(200, Span::test_data()),
],
}),
Value::test_record(Record {
cols: vec!["device".to_string(), "size".to_string()],
vals: vec![
Value::string("/dev/loop0".to_string(), Span::test_data()),
Value::filesize(50, Span::test_data()),
],
}),
],
Span::test_data(),
)),
result: Some(Value::test_list(vec![
Value::test_record(record! {
"device" => Value::test_string("/dev/sda1"),
"size" => Value::test_filesize(200),
}),
Value::test_record(record! {
"device" => Value::test_string("/dev/loop0"),
"size" => Value::test_filesize(50),
}),
])),
},
Example {
description: "Convert string to filesize",
example: "'2' | into filesize",
result: Some(Value::filesize(2, Span::test_data())),
result: Some(Value::test_filesize(2)),
},
Example {
description: "Convert float to filesize",
example: "8.3 | into filesize",
result: Some(Value::filesize(8, Span::test_data())),
result: Some(Value::test_filesize(8)),
},
Example {
description: "Convert int to filesize",
example: "5 | into filesize",
result: Some(Value::filesize(5, Span::test_data())),
result: Some(Value::test_filesize(5)),
},
Example {
description: "Convert file size to 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::{
ast::{Call, CellPath},
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)]
@ -62,9 +62,8 @@ impl Command for SubCommand {
Example {
description: "Convert string to float in table",
example: "[[num]; ['5.01']] | into float num",
result: Some(Value::test_list(vec![Value::test_record(Record {
cols: vec!["num".to_string()],
vals: vec![Value::test_float(5.01)],
result: Some(Value::test_list(vec![Value::test_record(record! {
"num" => Value::test_float(5.01),
})])),
},
Example {

View File

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

View File

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

View File

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

View File

@ -3,7 +3,7 @@ use chrono::{DateTime, Datelike, FixedOffset, Local, Timelike};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, PipelineData, Record, ShellError, ShellError::DatetimeParseError,
record, Category, Example, PipelineData, ShellError, ShellError::DatetimeParseError,
ShellError::PipelineEmpty, Signature, Span, Type, Value,
};
@ -49,56 +49,6 @@ impl Command for SubCommand {
}
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![
Example {
description: "Convert the current date into a record.",
@ -113,12 +63,30 @@ impl Command for SubCommand {
Example {
description: "Convert a date string into a 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 {
description: "Convert a date into a 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::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, PipelineData, Record, ShellError, ShellError::DatetimeParseError,
record, Category, Example, PipelineData, ShellError, ShellError::DatetimeParseError,
ShellError::PipelineEmpty, Signature, Span, Type, Value,
};
@ -49,62 +49,6 @@ impl Command for SubCommand {
}
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![
Example {
description: "Convert the current date into a table.",
@ -119,12 +63,30 @@ impl Command for SubCommand {
Example {
description: "Convert a given date into a 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 {
description: "Convert a given date into a 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_protocol::{
ast::Call, engine::Command, engine::EngineState, engine::Stack, Category, Example,
PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
ast::Call, engine::Command, engine::EngineState, engine::Stack, record, Category, Example,
PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -54,26 +54,23 @@ impl Command for Compact {
Example {
description: "Filter out all records where 'Hello' is null (returns nothing)",
example: r#"[["Hello" "World"]; [null 3]] | compact Hello"#,
result: Some(Value::list(vec![], Span::test_data())),
result: Some(Value::test_list(vec![])),
},
Example {
description: "Filter out all records where 'World' is null (Returns the table)",
example: r#"[["Hello" "World"]; [null 3]] | compact World"#,
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["Hello".into(), "World".into()],
vals: vec![Value::nothing(Span::test_data()), Value::test_int(3)],
})],
Span::test_data(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"Hello" => Value::nothing(Span::test_data()),
"World" => Value::test_int(3),
})])),
},
Example {
description: "Filter out all instances of nothing from a list (Returns [1,2])",
example: r#"[1, null, 2] | compact"#,
result: Some(Value::list(
vec![Value::test_int(1), Value::test_int(2)],
Span::test_data(),
)),
result: Some(Value::test_list(vec![
Value::test_int(1),
Value::test_int(2),
])),
},
]
}

View File

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

View File

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

View File

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

View File

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

View File

@ -8,7 +8,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
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,
};
@ -125,38 +125,27 @@ impl Command for Find {
Example {
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""#,
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["version".to_string(), "name".to_string()],
vals: vec![
Value::test_string("0.1.0"),
Value::test_string("nushell".to_string()),
],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"version" => Value::test_string("0.1.0"),
"name" => Value::test_string("nushell".to_string()),
})],
Span::test_data(),
)),
},
Example {
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"#,
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["version".to_string(), "name".to_string()],
vals: vec![
Value::test_string("0.1.1"),
Value::test_string("fish".to_string()),
],
Value::test_record(record!{
"version" => Value::test_string("0.1.1"),
"name" => Value::test_string("fish".to_string()),
}),
Value::test_record(Record {
cols: vec!["version".to_string(), "name".to_string()],
vals: vec![
Value::test_string("0.2.0"),
Value::test_string("zsh".to_string()),
],
Value::test_record(record! {
"version" => Value::test_string("0.2.0"),
"name" =>Value::test_string("zsh".to_string()),
}),
],
Span::test_data(),
)),
},
Example {
@ -191,16 +180,13 @@ impl Command for Find {
example:
"[[col1 col2 col3]; [moe larry curly] [larry curly moe]] | find moe --columns [col1]",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["col1".to_string(), "col2".to_string(), "col3".to_string()],
vals: vec![
Value::test_string(
vec![Value::test_record(record! {
"col1" => Value::test_string(
"\u{1b}[37m\u{1b}[0m\u{1b}[41;37mmoe\u{1b}[0m\u{1b}[37m\u{1b}[0m"
.to_string(),
),
Value::test_string("larry".to_string()),
Value::test_string("curly".to_string()),
],
"col2" => Value::test_string("larry".to_string()),
"col3" => Value::test_string("curly".to_string()),
})],
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::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
record, Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape,
Type, Value,
};
#[derive(Clone)]
@ -52,7 +53,7 @@ impl Command for Flatten {
Example {
description: "flatten a table",
example: "[[N, u, s, h, e, l, l]] | flatten ",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_string("N"),
Value::test_string("u"),
@ -61,7 +62,6 @@ impl Command for Flatten {
Value::test_string("e"),
Value::test_string("l"),
Value::test_string("l")],
Span::test_data()
))
},
Example {
@ -84,49 +84,33 @@ impl Command for Flatten {
example: "{ a: b, d: [ 1 2 3 4 ], e: [ 4 3 ] } | flatten d --all",
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["a".to_string(), "d".to_string(), "e".to_string()],
vals: vec![
Value::test_string("b"),
Value::test_int(1),
Value::list(
Value::test_record(record! {
"a" => Value::test_string("b"),
"d" => Value::test_int(1),
"e" => Value::test_list(
vec![Value::test_int(4), Value::test_int(3)],
Span::test_data(),
),
],
}),
Value::test_record(Record {
cols: vec!["a".to_string(), "d".to_string(), "e".to_string()],
vals: vec![
Value::test_string("b"),
Value::test_int(2),
Value::list(
Value::test_record(record! {
"a" => Value::test_string("b"),
"d" => Value::test_int(2),
"e" => Value::test_list(
vec![Value::test_int(4), Value::test_int(3)],
Span::test_data(),
),
],
}),
Value::test_record(Record {
cols: vec!["a".to_string(), "d".to_string(), "e".to_string()],
vals: vec![
Value::test_string("b"),
Value::test_int(3),
Value::list(
Value::test_record(record! {
"a" => Value::test_string("b"),
"d" => Value::test_int(3),
"e" => Value::test_list(
vec![Value::test_int(4), Value::test_int(3)],
Span::test_data(),
),
],
}),
Value::test_record(Record {
cols: vec!["a".to_string(), "d".to_string(), "e".to_string()],
vals: vec![
Value::test_string("b"),
Value::test_int(4),
Value::list(
Value::test_record(record! {
"a" => Value::test_string("b"),
"d" => Value::test_int(4),
"e" => Value::test_list(
vec![Value::test_int(4), Value::test_int(3)],
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::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
record, Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
@ -68,48 +68,37 @@ impl Command for GroupBy {
Example {
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 }",
result: Some(Value::test_record(Record {
cols: vec!["txt".to_string(), "csv".to_string()],
vals: vec![
Value::list(
result: Some(Value::test_record(record! {
"txt" => Value::test_list(
vec![
Value::test_string("foo.txt"),
Value::test_string("baz.txt"),
],
Span::test_data(),
),
Value::list(
"csv" => Value::test_list(
vec![Value::test_string("bar.csv")],
Span::test_data(),
),
],
})),
},
Example {
description: "You can also group by raw values by leaving out the argument",
example: "['1' '3' '1' '3' '2' '1' '1'] | group-by",
result: Some(Value::test_record(Record {
cols: vec!["1".to_string(), "3".to_string(), "2".to_string()],
vals: vec![
Value::list(
result: Some(Value::test_record(record! {
"1" => Value::test_list(
vec![
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")],
Span::test_data(),
),
Value::list(
"2" => Value::test_list(
vec![Value::test_string("2")],
Span::test_data(),
),
],
})),
},
]

View File

@ -1,8 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
Type, Value,
record, Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type,
Value,
};
#[derive(Clone)]
@ -31,47 +31,31 @@ impl Command for Headers {
}
fn examples(&self) -> Vec<Example> {
let columns = vec!["a".to_string(), "b".to_string(), "c".to_string()];
vec![
Example {
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"#,
result: Some(Value::list(
vec![Value::test_record(Record {
cols: columns.clone(),
vals: vec![
Value::test_string("1"),
Value::test_string("2"),
Value::test_string("3"),
],
})],
Span::test_data(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"a" => Value::test_string("1"),
"b" => Value::test_string("2"),
"c" => Value::test_string("3"),
})])),
},
Example {
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"#,
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: columns.clone(),
vals: vec![
Value::test_string("1"),
Value::test_string("2"),
Value::test_string("3"),
],
}),
Value::test_record(Record {
cols: columns,
vals: vec![
Value::test_string("1"),
Value::test_string("2"),
Value::test_string("3"),
],
}),
],
Span::test_data(),
)),
result: Some(Value::test_list(vec![
Value::test_record(record! {
"a" => Value::test_string("1"),
"b" => Value::test_string("2"),
"c" => Value::test_string("3"),
}),
Value::test_record(record! {
"a" => Value::test_string("1"),
"b" => Value::test_string("2"),
"c" => Value::test_string("3"),
}),
])),
},
]
}

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -60,57 +60,44 @@ impl Command for Insert {
vec![Example {
description: "Insert a new entry into a single record",
example: "{'name': 'nu', 'stars': 5} | insert alias 'Nushell'",
result: Some(Value::test_record(Record {
cols: vec!["name".into(), "stars".into(), "alias".into()],
vals: vec![
Value::test_string("nu"),
Value::test_int(5),
Value::test_string("Nushell"),
],
result: Some(Value::test_record(record! {
"name" => Value::test_string("nu"),
"stars" => Value::test_int(5),
"alias" => Value::test_string("Nushell"),
})),
},
Example {
description: "Insert a new column into a table, populating all rows",
example: "[[project, lang]; ['Nushell', 'Rust']] | insert type 'shell'",
result: Some(Value::list (
vec![Value::test_record(Record {
cols: vec!["project".into(), "lang".into(), "type".into()],
vals: vec![Value::test_string("Nushell"), Value::test_string("Rust"), Value::test_string("shell")],
result: Some(Value::test_list (
vec![Value::test_record(record! {
"project" => Value::test_string("Nushell"),
"lang" => Value::test_string("Rust"),
"type" => Value::test_string("shell"),
})],
Span::test_data(),
)),
},
Example {
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",
result: Some(Value::list (
result: Some(Value::test_list (
vec![
Value::test_record(Record {
cols: vec!["index".into(), "foo".into(), "bar".into()],
vals: vec![
Value::test_int(0),
Value::test_int(7),
Value::test_int(7),
],
Value::test_record(record! {
"index" => Value::test_int(0),
"foo" => Value::test_int(7),
"bar" => Value::test_int(7),
}),
Value::test_record(Record {
cols: vec!["index".into(),"foo".into(), "bar".into()],
vals: vec![
Value::test_int(1),
Value::test_int(8),
Value::test_int(9),
],
Value::test_record(record! {
"index" => Value::test_int(1),
"foo" => Value::test_int(8),
"bar" => Value::test_int(9),
}),
Value::test_record(Record {
cols: vec!["index".into(), "foo".into(), "bar".into()],
vals: vec![
Value::test_int(2),
Value::test_int(9),
Value::test_int(11),
],
Value::test_record(record! {
"index" => Value::test_int(2),
"foo" => Value::test_int(9),
"bar" => 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::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape,
Type, Value,
record, Category, Config, Example, PipelineData, Record, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
use std::cmp::max;
use std::collections::{HashMap, HashSet};
@ -108,13 +108,9 @@ impl Command for Join {
vec![Example {
description: "Join two tables",
example: "[{a: 1 b: 2}] | join [{a: 1 c: 3}] a",
result: Some(Value::list(
vec![Value::test_record(Record {
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(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"a" => Value::test_int(1), "b" => Value::test_int(2), "c" => Value::test_int(3),
})])),
}]
}
}

View File

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

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record,
ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
#[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",
description: "Move a column before the first column",
result:
Some(Value::list (
Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["index".to_string(), "name".to_string(), "value".to_string()],
vals: vec![Value::test_int(1), Value::test_string("foo"), Value::test_string("a")],
Value::test_record(record! {
"index" => Value::test_int(1),
"name" => Value::test_string("foo"),
"value" => Value::test_string("a"),
}),
Value::test_record(Record {
cols: vec!["index".to_string(), "name".to_string(), "value".to_string()],
vals: vec![Value::test_int(2), Value::test_string("bar"), Value::test_string("b")],
Value::test_record(record! {
"index" => Value::test_int(2),
"name" => Value::test_string("bar"),
"value" => Value::test_string("b"),
}),
Value::test_record(Record {
cols: vec!["index".to_string(), "name".to_string(), "value".to_string()],
vals: vec![Value::test_int(3), Value::test_string("baz"), Value::test_string("c")],
Value::test_record(record! {
"index" => Value::test_int(3),
"name" => Value::test_string("baz"),
"value" => Value::test_string("c"),
}),
],
Span::test_data(),
))
},
Example {
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",
result:
Some(Value::list (
Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["index".to_string(), "value".to_string(), "name".to_string()],
vals: vec![Value::test_int(1), Value::test_string("a"), Value::test_string("foo")],
Value::test_record(record! {
"index" => Value::test_int(1),
"value" => Value::test_string("a"),
"name" => Value::test_string("foo"),
}),
Value::test_record(Record {
cols: vec!["index".to_string(), "value".to_string(), "name".to_string()],
vals: vec![Value::test_int(2), Value::test_string("b"), Value::test_string("bar")],
Value::test_record(record! {
"index" => Value::test_int(2),
"value" => Value::test_string("b"),
"name" => Value::test_string("bar"),
}),
Value::test_record(Record {
cols: vec!["index".to_string(), "value".to_string(), "name".to_string()],
vals: vec![Value::test_int(3), Value::test_string("c"), Value::test_string("baz")],
Value::test_record(record! {
"index" => Value::test_int(3),
"value" => Value::test_string("c"),
"name" => Value::test_string("baz"),
}),
],
Span::test_data(),
))
},
Example {
example: "{ name: foo, value: a, index: 1 } | move name --before index",
description: "Move columns of a record",
result: Some(Value::test_record(Record {
cols: vec!["value".to_string(), "name".to_string(), "index".to_string()],
vals: vec![Value::test_string("a"), Value::test_string("foo"), Value::test_int(1)],
result: Some(Value::test_record(record! {
"value" => Value::test_string("a"),
"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::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
use std::cmp::Reverse;
@ -152,42 +152,36 @@ impl Command for Reject {
Example {
description: "Reject a column in a table",
example: "[[a, b]; [1, 2]] | reject a",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["b".to_string()],
vals: vec![Value::test_int(2)],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"b" => Value::test_int(2),
})],
Span::test_data(),
)),
},
Example {
description: "Reject a row in a table",
example: "[[a, b]; [1, 2] [3, 4]] | reject 1",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(1), Value::test_int(2)],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"a" => Value::test_int(1),
"b" => Value::test_int(2),
})],
Span::test_data(),
)),
},
Example {
description: "Reject the specified field in a record",
example: "{a: 1, b: 2} | reject a",
result: Some(Value::test_record(Record {
cols: vec!["b".into()],
vals: vec![Value::test_int(2)],
result: Some(Value::test_record(record! {
"b" => Value::test_int(2),
})),
},
Example {
description: "Reject a nested field in a record",
example: "{a: {b: 3, c: 5}} | reject a.b",
result: Some(Value::test_record(Record {
cols: vec!["a".into()],
vals: vec![Value::test_record(Record {
cols: vec!["c".into()],
vals: vec![Value::test_int(5)],
})],
result: Some(Value::test_record(record! {
"a" => Value::test_record(record! {
"c" => Value::test_int(5),
}),
})),
},
Example {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -112,26 +112,24 @@ impl Command for Uniq {
Example {
description: "Ignore differences in case when comparing input values",
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")],
Span::test_data(),
)),
},
Example {
description: "Return a table containing the distinct input values together with their counts",
example: "[1 2 2] | uniq --count",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["value".to_string(), "count".to_string()],
vals: vec![Value::test_int(1), Value::test_int(1)],
Value::test_record(record! {
"value" => Value::test_int(1),
"count" => Value::test_int(1),
}),
Value::test_record(Record {
cols: vec!["value".to_string(), "count".to_string()],
vals: vec![Value::test_int(2), Value::test_int(2)],
Value::test_record(record! {
"value" => 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::engine::{Command, EngineState, Stack};
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)]
@ -92,23 +92,20 @@ impl Command for UniqBy {
vec![Example {
description: "Get rows from table filtered by column uniqueness ",
example: "[[fruit count]; [apple 9] [apple 2] [pear 3] [orange 7]] | uniq-by fruit",
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["fruit".to_string(), "count".to_string()],
vals: vec![Value::test_string("apple"), Value::test_int(9)],
}),
Value::test_record(Record {
cols: vec!["fruit".to_string(), "count".to_string()],
vals: vec![Value::test_string("pear"), Value::test_int(3)],
}),
Value::test_record(Record {
cols: vec!["fruit".to_string(), "count".to_string()],
vals: vec![Value::test_string("orange"), Value::test_int(7)],
}),
],
Span::test_data(),
)),
result: Some(Value::test_list(vec![
Value::test_record(record! {
"fruit" => Value::test_string("apple"),
"count" => Value::test_int(9),
}),
Value::test_record(record! {
"fruit" => Value::test_string("pear"),
"count" => Value::test_int(3),
}),
Value::test_record(record! {
"fruit" => Value::test_string("orange"),
"count" => Value::test_int(7),
}),
])),
}]
}
}

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -57,42 +57,39 @@ impl Command for Update {
Example {
description: "Update a column value",
example: "{'name': 'nu', 'stars': 5} | update name 'Nushell'",
result: Some(Value::test_record(Record {
cols: vec!["name".into(), "stars".into()],
vals: vec![Value::test_string("Nushell"), Value::test_int(5)],
result: Some(Value::test_record(record! {
"name" => Value::test_string("Nushell"),
"stars" => Value::test_int(5),
})),
},
Example {
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",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["count".into(), "fruit".into()],
vals: vec![Value::test_int(5), Value::test_string("apple")],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"count" => Value::test_int(5),
"fruit" => Value::test_string("apple"),
})],
Span::test_data(),
)),
},
Example {
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 ','}",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["project".into(), "authors".into()],
vals: vec![Value::test_string("nu"), Value::test_string("Andrés,JT,Yehuda")],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"project" => Value::test_string("nu"),
"authors" => Value::test_string("Andrés,JT,Yehuda"),
})],
Span::test_data(),
)),
},
Example {
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 ','}",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["project".into(), "authors".into()],
vals: vec![Value::test_string("nu"), Value::test_string("Andrés,JT,Yehuda")],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"project" => Value::test_string("nu"),
"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::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -60,65 +60,62 @@ impl Command for Upsert {
vec![Example {
description: "Update a record's value",
example: "{'name': 'nu', 'stars': 5} | upsert name 'Nushell'",
result: Some(Value::test_record(Record {
cols: vec!["name".into(), "stars".into()],
vals: vec![Value::test_string("Nushell"), Value::test_int(5)],
result: Some(Value::test_record(record! {
"name" => Value::test_string("Nushell"),
"stars" => Value::test_int(5),
})),
},
Example {
description: "Update each row of a table",
example: "[[name lang]; [Nushell ''] [Reedline '']] | upsert lang 'Rust'",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["name".into(), "lang".into()],
vals: vec![Value::test_string("Nushell"), Value::test_string("Rust")],
Value::test_record(record! {
"name" => Value::test_string("Nushell"),
"lang" => Value::test_string("Rust"),
}),
Value::test_record(Record {
cols: vec!["name".into(), "lang".into()],
vals: vec![Value::test_string("Reedline"), Value::test_string("Rust")],
Value::test_record(record! {
"name" => Value::test_string("Reedline"),
"lang" => Value::test_string("Rust"),
}),
],
Span::test_data(),
)),
},
Example {
description: "Insert a new entry into a single record",
example: "{'name': 'nu', 'stars': 5} | upsert language 'Rust'",
result: Some(Value::test_record(Record {
cols: vec!["name".into(), "stars".into(), "language".into()],
vals: vec![Value::test_string("nu"), Value::test_int(5), Value::test_string("Rust")],
result: Some(Value::test_record(record! {
"name" => Value::test_string("nu"),
"stars" => Value::test_int(5),
"language" => Value::test_string("Rust"),
})),
}, Example {
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",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["count".into(), "fruit".into()],
vals: vec![Value::test_int(5), Value::test_string("apple")],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"count" => Value::test_int(5),
"fruit" => Value::test_string("apple"),
})],
Span::test_data(),
)),
},
Example {
description: "Upsert an int into a list, updating an existing value based on the index",
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)],
Span::test_data(),
)),
},
Example {
description: "Upsert an int into a list, inserting a new value based on the index",
example: "[1 2 3] | upsert 3 4",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(3),
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::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record,
ShellError, Signature, Span, SyntaxShape, Type, Value,
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -111,20 +111,17 @@ not supported."#
Example {
description: "Filter rows of a table according to a condition",
example: "[{a: 1} {a: 2}] | where a > 1",
result: Some(Value::list (
vec![Value::test_record(Record {
cols: vec!["a".to_string()],
vals: vec![Value::test_int(2)],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"a" => Value::test_int(2),
})],
Span::test_data(),
)),
},
Example {
description: "Filter items of a list according to a condition",
example: "[1 2] | where {|x| $x > 1}",
result: Some(Value::list (
result: Some(Value::test_list(
vec![Value::test_int(2)],
Span::test_data(),
)),
},
Example {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,8 +3,8 @@ use indexmap::map::IndexMap;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, Type,
Value,
record, Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
Type, Value,
};
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>
</note>' | from xml"#,
description: "Converts xml formatted string to record",
result: Some(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_string("note"),
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_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(),
),
],
result: Some(Value::test_record(record! {
COLUMN_TAG_NAME => Value::test_string("note"),
COLUMN_ATTRS_NAME => Value::test_record(Record::new()),
COLUMN_CONTENT_NAME => Value::test_list(vec![
Value::test_record(record! {
COLUMN_TAG_NAME => Value::test_string("remember"),
COLUMN_ATTRS_NAME => Value::test_record(Record::new()),
COLUMN_CONTENT_NAME => Value::test_list(vec![
Value::test_record(record! {
COLUMN_TAG_NAME => Value::test_nothing(),
COLUMN_ATTRS_NAME => Value::test_nothing(),
COLUMN_CONTENT_NAME => Value::test_string("Event"),
})],
Span::test_data(),
),
],
})],
),
})),
}]
}
@ -344,28 +321,18 @@ mod tests {
attrs: IndexMap<&str, &str>,
content: &[Value],
) -> Value {
Value::test_record(Record {
cols: vec![
COLUMN_TAG_NAME.into(),
COLUMN_ATTRS_NAME.into(),
COLUMN_CONTENT_NAME.into(),
],
vals: vec![string(tag), attributes(attrs), table(content)],
Value::test_record(record! {
COLUMN_TAG_NAME => string(tag),
COLUMN_ATTRS_NAME => attributes(attrs),
COLUMN_CONTENT_NAME => table(content),
})
}
fn content_string(value: impl Into<String>) -> Value {
Value::test_record(Record {
cols: vec![
COLUMN_TAG_NAME.into(),
COLUMN_ATTRS_NAME.into(),
COLUMN_CONTENT_NAME.into(),
],
vals: vec![
Value::nothing(Span::test_data()),
Value::nothing(Span::test_data()),
string(value),
],
Value::test_record(record! {
COLUMN_TAG_NAME => Value::nothing(Span::test_data()),
COLUMN_ATTRS_NAME => Value::nothing(Span::test_data()),
COLUMN_CONTENT_NAME => string(value),
})
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, ListStream, PipelineData, Record, ShellError, Signature, Span, Spanned,
record, Category, Example, ListStream, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
};
@ -43,13 +43,10 @@ impl Command for Parse {
}
fn examples(&self) -> Vec<Example> {
let result = Value::list(
vec![Value::test_record(Record {
cols: vec!["foo".to_string(), "bar".to_string()],
vals: vec![Value::test_string("hi"), Value::test_string("there")],
})],
Span::test_data(),
);
let result = Value::test_list(vec![Value::test_record(record! {
"foo" => Value::test_string("hi"),
"bar" => Value::test_string("there"),
})]);
vec![
Example {
@ -65,55 +62,46 @@ impl Command for Parse {
Example {
description: "Parse a string using fancy-regex named capture group pattern",
example: "\"foo bar.\" | parse --regex '\\s*(?<name>\\w+)(?=\\.)'",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["name".to_string()],
vals: vec![Value::test_string("bar")],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"name" => Value::test_string("bar"),
})],
Span::test_data(),
)),
},
Example {
description: "Parse a string using fancy-regex capture group pattern",
example: "\"foo! bar.\" | parse --regex '(\\w+)(?=\\.)|(\\w+)(?=!)'",
result: Some(Value::list(
result: Some(Value::test_list(
vec![
Value::test_record(Record {
cols: vec!["capture0".to_string(), "capture1".to_string()],
vals: vec![Value::test_string(""), Value::test_string("foo")],
Value::test_record(record! {
"capture0" => Value::test_string(""),
"capture1" => Value::test_string("foo"),
}),
Value::test_record(Record {
cols: vec!["capture0".to_string(), "capture1".to_string()],
vals: vec![Value::test_string("bar"), Value::test_string("")],
Value::test_record(record! {
"capture0" => Value::test_string("bar"),
"capture1" => Value::test_string(""),
}),
],
Span::test_data(),
)),
},
Example {
description: "Parse a string using fancy-regex look behind pattern",
example:
"\" @another(foo bar) \" | parse --regex '\\s*(?<=[() ])(@\\w+)(\\([^)]*\\))?\\s*'",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["capture0".to_string(), "capture1".to_string()],
vals: vec![
Value::test_string("@another"),
Value::test_string("(foo bar)"),
],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"capture0" => Value::test_string("@another"),
"capture1" => Value::test_string("(foo bar)"),
})],
Span::test_data(),
)),
},
Example {
description: "Parse a string using fancy-regex look ahead atomic group pattern",
example: "\"abcd\" | parse --regex '^a(bc(?=d)|b)cd$'",
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["capture0".to_string()],
vals: vec![Value::test_string("b")],
result: Some(Value::test_list(
vec![Value::test_record(record! {
"capture0" => 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::engine::{Command, EngineState, Stack};
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::{fmt, str};
@ -58,61 +58,34 @@ impl Command for Size {
Example {
description: "Count the number of words in a string",
example: r#""There are seven words in this sentence" | size"#,
result: Some(Value::test_record(Record {
cols: vec![
"lines".into(),
"words".into(),
"bytes".into(),
"chars".into(),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(7),
Value::test_int(38),
Value::test_int(38),
Value::test_int(38),
],
result: Some(Value::test_record(record! {
"lines" => Value::test_int(1),
"words" => Value::test_int(7),
"bytes" => Value::test_int(38),
"chars" => Value::test_int(38),
"graphemes" => Value::test_int(38),
})),
},
Example {
description: "Counts unicode characters",
example: r#"'今天天气真好' | size "#,
result: Some(Value::test_record(Record {
cols: vec![
"lines".into(),
"words".into(),
"bytes".into(),
"chars".into(),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(6),
Value::test_int(18),
Value::test_int(6),
Value::test_int(6),
],
result: Some(Value::test_record(record! {
"lines" => Value::test_int(1),
"words" => Value::test_int(6),
"bytes" => Value::test_int(18),
"chars" => Value::test_int(6),
"graphemes" => Value::test_int(6),
})),
},
Example {
description: "Counts Unicode characters correctly in a string",
example: r#""Amélie Amelie" | size"#,
result: Some(Value::test_record(Record {
cols: vec![
"lines".into(),
"words".into(),
"bytes".into(),
"chars".into(),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(15),
Value::test_int(14),
Value::test_int(13),
],
result: Some(Value::test_record(record! {
"lines" => Value::test_int(1),
"words" => Value::test_int(2),
"bytes" => Value::test_int(15),
"chars" => Value::test_int(14),
"graphemes" => Value::test_int(13),
})),
},
]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,7 @@ use fancy_regex::Regex;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
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::{fmt, str};
@ -48,61 +48,34 @@ impl Command for SubCommand {
Example {
description: "Count the number of words in a string",
example: r#""There are seven words in this sentence" | str stats"#,
result: Some(Value::test_record(Record {
cols: vec![
"lines".into(),
"words".into(),
"bytes".into(),
"chars".into(),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(7),
Value::test_int(38),
Value::test_int(38),
Value::test_int(38),
],
result: Some(Value::test_record(record! {
"lines" => Value::test_int(1),
"words" => Value::test_int(7),
"bytes" => Value::test_int(38),
"chars" => Value::test_int(38),
"graphemes" => Value::test_int(38),
})),
},
Example {
description: "Counts unicode characters",
example: r#"'今天天气真好' | str stats "#,
result: Some(Value::test_record(Record {
cols: vec![
"lines".into(),
"words".into(),
"bytes".into(),
"chars".into(),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(6),
Value::test_int(18),
Value::test_int(6),
Value::test_int(6),
],
result: Some(Value::test_record(record! {
"lines" => Value::test_int(1),
"words" => Value::test_int(6),
"bytes" => Value::test_int(18),
"chars" => Value::test_int(6),
"graphemes" => Value::test_int(6),
})),
},
Example {
description: "Counts Unicode characters correctly in a string",
example: r#""Amélie Amelie" | str stats"#,
result: Some(Value::test_record(Record {
cols: vec![
"lines".into(),
"words".into(),
"bytes".into(),
"chars".into(),
"graphemes".into(),
],
vals: vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(15),
Value::test_int(14),
Value::test_int(13),
],
result: Some(Value::test_record(record! {
"lines" => Value::test_int(1),
"words" => Value::test_int(2),
"bytes" => Value::test_int(15),
"chars" => Value::test_int(14),
"graphemes" => 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::{StyleComputer, TextStyle};
use nu_engine::{env::get_config, env_to_string, CallExt};
use nu_protocol::record;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
@ -130,7 +131,6 @@ impl Command for Table {
}
fn examples(&self) -> Vec<Example> {
let span = Span::test_data();
vec![
Example {
description: "List the files in current directory, with indexes starting from 1.",
@ -140,53 +140,44 @@ impl Command for Table {
Example {
description: "Render data in table view",
example: r#"[[a b]; [1 2] [3 4]] | table"#,
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(1), Value::test_int(2)],
}),
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(3), Value::test_int(4)],
}),
],
span,
)),
result: Some(Value::test_list(vec![
Value::test_record(record! {
"a" => Value::test_int(1),
"b" => Value::test_int(2),
}),
Value::test_record(record! {
"a" => Value::test_int(3),
"b" => Value::test_int(4),
}),
])),
},
Example {
description: "Render data in table view (expanded)",
example: r#"[[a b]; [1 2] [2 [4 4]]] | table --expand"#,
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(1), Value::test_int(2)],
}),
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(3), Value::test_int(4)],
}),
],
span,
)),
result: Some(Value::test_list(vec![
Value::test_record(record! {
"a" => Value::test_int(1),
"b" => Value::test_int(2),
}),
Value::test_record(record! {
"a" => Value::test_int(3),
"b" => Value::test_int(4),
}),
])),
},
Example {
description: "Render data in table view (collapsed)",
example: r#"[[a b]; [1 2] [2 [4 4]]] | table --collapse"#,
result: Some(Value::list(
vec![
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(1), Value::test_int(2)],
}),
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_int(3), Value::test_int(4)],
}),
],
span,
)),
result: Some(Value::test_list(vec![
Value::test_record(record! {
"a" => Value::test_int(1),
"b" => Value::test_int(2),
}),
Value::test_record(record! {
"a" => Value::test_int(3),
"b" => Value::test_int(4),
}),
])),
},
]
}

View File

@ -3846,23 +3846,22 @@ fn get_filesize_format(format_value: &str, filesize_metric: Option<bool>) -> (By
#[cfg(test)]
mod tests {
use super::{Record, Span, Value};
use super::{Record, Value};
use crate::record;
mod is_empty {
use super::*;
#[test]
fn test_string() {
let value = Value::string("", Span::unknown());
let value = Value::test_string("");
assert!(value.is_empty());
}
#[test]
fn test_list() {
let list_with_no_values = Value::list(vec![], Span::unknown());
let list_with_one_empty_string =
Value::list(vec![Value::string("", Span::unknown())], Span::unknown());
let list_with_no_values = Value::test_list(vec![]);
let list_with_one_empty_string = Value::test_list(vec![Value::test_string("")]);
assert!(list_with_no_values.is_empty());
assert!(!list_with_one_empty_string.is_empty());
@ -3872,21 +3871,18 @@ mod tests {
fn test_record() {
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 {
cols: vec![String::from("")],
vals: vec![Value::string("", Span::unknown())],
let one_column_and_one_cell_value_with_empty_strings = Value::test_record(record! {
"" => Value::test_string(""),
});
let one_column_with_a_string_and_one_cell_value_with_empty_string =
Value::test_record(Record {
cols: vec![String::from("column")],
vals: vec![Value::string("", Span::unknown())],
Value::test_record(record! {
"column" => Value::test_string(""),
});
let one_column_with_empty_string_and_one_value_with_a_string =
Value::test_record(Record {
cols: vec![String::from("")],
vals: vec![Value::string("text", Span::unknown())],
Value::test_record(record! {
"" => Value::test_string("text"),
});
assert!(no_columns_nor_cell_values.is_empty());
@ -3903,24 +3899,15 @@ mod tests {
#[test]
fn test_list() {
let list_of_ints = Value::list(vec![Value::int(0, Span::unknown())], Span::unknown());
let list_of_floats =
Value::list(vec![Value::float(0.0, Span::unknown())], Span::unknown());
let list_of_ints_and_floats = Value::list(
vec![
Value::int(0, Span::unknown()),
Value::float(0.0, Span::unknown()),
],
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(),
);
let list_of_ints = Value::test_list(vec![Value::test_int(0)]);
let list_of_floats = Value::test_list(vec![Value::test_float(0.0)]);
let list_of_ints_and_floats =
Value::test_list(vec![Value::test_int(0), Value::test_float(0.0)]);
let list_of_ints_and_floats_and_bools = Value::test_list(vec![
Value::test_int(0),
Value::test_float(0.0),
Value::test_bool(false),
]);
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!(
@ -3941,13 +3928,10 @@ mod tests {
#[test]
fn test_datetime() {
let string = Value::date(
DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_millis(-123456789).unwrap(),
FixedOffset::east_opt(0).unwrap(),
),
Span::unknown(),
)
let string = Value::test_date(DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_millis(-123456789).unwrap(),
FixedOffset::east_opt(0).unwrap(),
))
.into_string("", &Default::default());
// We need to cut the humanized part off for tests to work, because
@ -3958,13 +3942,10 @@ mod tests {
#[test]
fn test_negative_year_datetime() {
let string = Value::date(
DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_millis(-72135596800000).unwrap(),
FixedOffset::east_opt(0).unwrap(),
),
Span::unknown(),
)
let string = Value::test_date(DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_millis(-72135596800000).unwrap(),
FixedOffset::east_opt(0).unwrap(),
))
.into_string("", &Default::default());
// 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 indexmap::map::IndexMap;
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;
pub const CMD_NAME: &str = "from eml";
@ -24,31 +24,17 @@ Subject: Welcome
To: someone@somewhere.com
Test' | from eml"
.into(),
result: Some(Value::test_record(Record {
cols: vec![
"Subject".to_string(),
"From".to_string(),
"To".to_string(),
"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"),
],
result: Some(Value::test_record(record! {
"Subject" => Value::test_string("Welcome"),
"From" => Value::test_record(record! {
"Name" => Value::nothing(Span::test_data()),
"Address" => Value::test_string("test@email.com"),
}),
Value::test_record(Record {
cols: vec!["Name".to_string(), "Address".to_string()],
vals: vec![
Value::nothing(Span::test_data()),
Value::test_string("someone@somewhere.com"),
],
"To" => Value::test_record(record! {
"Name" => Value::nothing(Span::test_data()),
"Address" => Value::test_string("someone@somewhere.com"),
}),
Value::test_string("Test"),
],
"Body" => Value::test_string("Test"),
})),
},
PluginExample {
@ -58,31 +44,17 @@ Subject: Welcome
To: someone@somewhere.com
Test' | from eml -b 1"
.into(),
result: Some(Value::test_record(Record {
cols: vec![
"Subject".to_string(),
"From".to_string(),
"To".to_string(),
"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"),
],
result: Some(Value::test_record(record! {
"Subject" => Value::test_string("Welcome"),
"From" => Value::test_record(record! {
"Name" => Value::nothing(Span::test_data()),
"Address" => Value::test_string("test@email.com"),
}),
Value::test_record(Record {
cols: vec!["Name".to_string(), "Address".to_string()],
vals: vec![
Value::nothing(Span::test_data()),
Value::test_string("someone@somewhere.com"),
],
"To" => Value::test_record(record! {
"Name" => Value::nothing(Span::test_data()),
"Address" => 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 indexmap::map::IndexMap;
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;
pub const CMD_NAME: &str = "from ics";
@ -55,29 +55,15 @@ pub fn examples() -> Vec<PluginExample> {
END:VCALENDAR' | from ics"
.into(),
description: "Converts ics formatted string to table".into(),
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec![
"properties".to_string(),
"events".to_string(),
"alarms".to_string(),
"to-Dos".to_string(),
"journals".to_string(),
"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(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"properties" => Value::test_list(vec![]),
"events" => Value::test_list(vec![]),
"alarms" => Value::test_list(vec![]),
"to-Dos" => Value::test_list(vec![]),
"journals" => Value::test_list(vec![]),
"free-busys" => Value::test_list(vec![]),
"timezones" => Value::test_list(vec![]),
})])),
}]
}

View File

@ -1,5 +1,5 @@
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";
@ -57,12 +57,11 @@ a=1
b=2' | from ini"
.into(),
description: "Converts ini formatted string to record".into(),
result: Some(Value::test_record(Record {
cols: vec!["foo".to_string()],
vals: vec![Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_string("1"), Value::test_string("2")],
})],
result: Some(Value::test_record(record! {
"foo" => Value::test_record(record! {
"a" => Value::test_string("1"),
"b" => Value::test_string("2"),
}),
})),
}]
}

View File

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

View File

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