mirror of
https://github.com/nushell/nushell.git
synced 2025-04-29 23:54:26 +02:00
Closes #15543 # Description 1. Simplify code in ``datetime.rs`` based on a suggestion in my last PR on "datetime from record" 1. Make ``into duration`` work with durations inside a record, provided as a cell path 1. Make ``into duration`` work with durations as record # User-Facing Changes ```nushell # Happy paths ~> {d: '1hr'} | into duration d ╭───┬─────╮ │ d │ 1hr │ ╰───┴─────╯ ~> {week: 10, day: 2, sign: '+'} | into duration 10wk 2day # Error paths and invalid usage ~> {week: 10, day: 2, sign: 'x'} | into duration Error: nu:🐚:incorrect_value × Incorrect value. ╭─[entry #4:1:26] 1 │ {week: 10, day: 2, sign: 'x'} | into duration · ─┬─ ──────┬────── · │ ╰── encountered here · ╰── Invalid sign. Allowed signs are +, - ╰──── ~> {week: 10, day: -2, sign: '+'} | into duration Error: nu:🐚:incorrect_value × Incorrect value. ╭─[entry #5:1:17] 1 │ {week: 10, day: -2, sign: '+'} | into duration · ─┬ ──────┬────── · │ ╰── encountered here · ╰── number should be positive ╰──── ~> {week: 10, day: '2', sign: '+'} | into duration Error: nu:🐚:only_supports_this_input_type × Input type not supported. ╭─[entry #6:1:17] 1 │ {week: 10, day: '2', sign: '+'} | into duration · ─┬─ ──────┬────── · │ ╰── only int input data is supported · ╰── input type: string ╰──── ~> {week: 10, unknown: 1} | into duration Error: nu:🐚:unsupported_input × Unsupported input ╭─[entry #7:1:1] 1 │ {week: 10, unknown: 1} | into duration · ───────────┬────────── ──────┬────── · │ ╰── Column 'unknown' is not valid for a structured duration. Allowed columns are: week, day, hour, minute, second, millisecond, microsecond, nanosecond, sign · ╰── value originates from here ╰──── ~> {week: 10, day: 2, sign: '+'} | into duration --unit sec Error: nu:🐚:incompatible_parameters × Incompatible parameters. ╭─[entry #2:1:33] 1 │ {week: 10, day: 2, sign: '+'} | into duration --unit sec · ──────┬────── ─────┬──── · │ ╰── the units should be included in the record · ╰── got a record as input ╰──── ``` # Tests + Formatting - Add examples and integration tests for ``into duration`` - Add one test for ``into duration`` # After Submitting If this is merged in time, I'll update my PR on the "datetime handling highlights" for the release notes.
111 lines
3.0 KiB
Rust
111 lines
3.0 KiB
Rust
use nu_test_support::nu;
|
|
|
|
// Tests happy paths
|
|
|
|
#[test]
|
|
fn into_duration_float() {
|
|
let actual = nu!(r#"1.07min | into duration"#);
|
|
|
|
assert_eq!("1min 4sec 200ms", actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_from_record_cell_path() {
|
|
let actual = nu!(r#"{d: '1hr'} | into duration d"#);
|
|
let expected = nu!(r#"{d: 1hr}"#);
|
|
|
|
assert_eq!(expected.out, actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_from_record() {
|
|
let actual = nu!(
|
|
r#"{week: 10, day: 1, hour: 2, minute: 3, second: 4, millisecond: 5, microsecond: 6, nanosecond: 7, sign: '+'} | into duration | into record"#
|
|
);
|
|
let expected = nu!(
|
|
r#"{week: 10, day: 1, hour: 2, minute: 3, second: 4, millisecond: 5, microsecond: 6, nanosecond: 7, sign: '+'}"#
|
|
);
|
|
|
|
assert_eq!(expected.out, actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_from_record_negative() {
|
|
let actual = nu!(
|
|
r#"{week: 10, day: 1, hour: 2, minute: 3, second: 4, millisecond: 5, microsecond: 6, nanosecond: 7, sign: '-'} | into duration | into record"#
|
|
);
|
|
let expected = nu!(
|
|
r#"{week: 10, day: 1, hour: 2, minute: 3, second: 4, millisecond: 5, microsecond: 6, nanosecond: 7, sign: '-'}"#
|
|
);
|
|
|
|
assert_eq!(expected.out, actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_from_record_defaults() {
|
|
let actual = nu!(r#"{} | into duration | into int"#);
|
|
|
|
assert_eq!("0".to_string(), actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_from_record_round_trip() {
|
|
let actual = nu!(
|
|
r#"('10wk 1day 2hr 3min 4sec 5ms 6µs 7ns' | into duration | into record | into duration | into string) == '10wk 1day 2hr 3min 4sec 5ms 6µs 7ns'"#
|
|
);
|
|
|
|
assert!(actual.out.contains("true"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_table_column() {
|
|
let actual =
|
|
nu!(r#"[[value]; ['1sec'] ['2min'] ['3hr'] ['4day'] ['5wk']] | into duration value"#);
|
|
let expected = nu!(r#"[[value]; [1sec] [2min] [3hr] [4day] [5wk]]"#);
|
|
|
|
assert_eq!(actual.out, expected.out);
|
|
}
|
|
|
|
// Tests error paths
|
|
|
|
#[test]
|
|
fn into_duration_from_record_fails_with_wrong_type() {
|
|
let actual = nu!(r#"{week: '10'} | into duration"#);
|
|
|
|
assert!(actual
|
|
.err
|
|
.contains("nu::shell::only_supports_this_input_type"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_from_record_fails_with_invalid_date_time_values() {
|
|
let actual = nu!(r#"{week: -10} | into duration"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_from_record_fails_with_invalid_sign() {
|
|
let actual = nu!(r#"{week: 10, sign: 'x'} | into duration"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
|
}
|
|
|
|
// Tests invalid usage
|
|
|
|
#[test]
|
|
fn into_duration_from_record_fails_with_unknown_key() {
|
|
let actual = nu!(r#"{week: 10, unknown: 1} | into duration"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::unsupported_input"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_duration_from_record_incompatible_with_unit_flag() {
|
|
let actual = nu!(
|
|
r#"{week: 10, day: 1, hour: 2, minute: 3, second: 4, sign: '-'} | into duration --unit sec"#
|
|
);
|
|
|
|
assert!(actual.err.contains("nu::shell::incompatible_parameters"));
|
|
}
|