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
99 changed files with 1592 additions and 2540 deletions

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"),
})),
},
]