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

@ -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);