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.
120 lines
3.4 KiB
Rust
120 lines
3.4 KiB
Rust
use nu_test_support::nu;
|
|
|
|
// Tests happy paths
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_cell_path() {
|
|
let actual = nu!(r#"{d: '2021'} | into datetime d"#);
|
|
|
|
assert!(actual.out.contains("years ago"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record() {
|
|
let actual = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5, millisecond: 6, microsecond: 7, nanosecond: 8, timezone: '+01:00'} | into datetime | into record"#
|
|
);
|
|
let expected = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5, millisecond: 6, microsecond: 7, nanosecond: 8, timezone: '+01:00'}"#
|
|
);
|
|
|
|
assert_eq!(expected.out, actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_very_old() {
|
|
let actual = nu!(r#"{year: -100, timezone: '+02:00'} | into datetime | into record"#);
|
|
let expected = nu!(
|
|
r#"{year: -100, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0, timezone: '+02:00'}"#
|
|
);
|
|
|
|
assert_eq!(expected.out, actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_defaults() {
|
|
let actual = nu!(r#"{year: 2025, timezone: '+02:00'} | into datetime | into record"#);
|
|
let expected = nu!(
|
|
r#"{year: 2025, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0, timezone: '+02:00'}"#
|
|
);
|
|
|
|
assert_eq!(expected.out, actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_round_trip() {
|
|
let actual = nu!(
|
|
r#"(1743348798 | into datetime | into record | into datetime | into int) == 1743348798"#
|
|
);
|
|
|
|
assert!(actual.out.contains("true"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_table_column() {
|
|
let actual = nu!(r#"[[date]; ["2022-01-01"] ["2023-01-01"]] | into datetime date"#);
|
|
|
|
assert!(actual.out.contains(" ago"));
|
|
}
|
|
|
|
// Tests error paths
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_fails_with_wrong_type() {
|
|
let actual = nu!(r#"{year: '2023'} | into datetime"#);
|
|
|
|
assert!(actual
|
|
.err
|
|
.contains("nu::shell::only_supports_this_input_type"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_fails_with_invalid_date_time_values() {
|
|
let actual = nu!(r#"{year: 2023, month: 13} | into datetime"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_fails_with_invalid_timezone() {
|
|
let actual = nu!(r#"{year: 2023, timezone: '+100:00'} | into datetime"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
|
}
|
|
|
|
// Tests invalid usage
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_fails_with_unknown_key() {
|
|
let actual = nu!(r#"{year: 2023, unknown: 1} | into datetime"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::unsupported_input"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_incompatible_with_format_flag() {
|
|
let actual = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --format ''"#
|
|
);
|
|
|
|
assert!(actual.err.contains("nu::shell::incompatible_parameters"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_incompatible_with_timezone_flag() {
|
|
let actual = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --timezone UTC"#
|
|
);
|
|
|
|
assert!(actual.err.contains("nu::shell::incompatible_parameters"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_incompatible_with_offset_flag() {
|
|
let actual = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --offset 1"#
|
|
);
|
|
|
|
assert!(actual.err.contains("nu::shell::incompatible_parameters"));
|
|
}
|