allow Range to expand to array-like when converting to json (#8047)

# Description
Fixes #8002, which expands ranges `1..3` to expand to array-like when
saving and converting to json. Now,

```
> 1..3 | save foo.json
# foo.json
[
    1,
    2,
    3
]


> 1..3 | to json
[
    1,
    2,
    3
]
```
# User-Facing Changes

_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- [X] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [X] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- [X] `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
David Matos
2023-02-24 22:31:33 +01:00
committed by GitHub
parent 253b223e65
commit 42f0b55de0
8 changed files with 94 additions and 0 deletions

View File

@ -283,3 +283,21 @@ fn save_list_stream() {
assert_eq!(actual, "a\nb\nc\nd\n")
})
}
#[test]
fn writes_out_range() {
Playground::setup("save_test_14", |dirs, sandbox| {
sandbox.with_files(vec![]);
let expected_file = dirs.test().join("list_sample.json");
nu!(
cwd: dirs.root(),
r#"1..3 | save save_test_14/list_sample.json"#,
);
let actual = file_contents(expected_file);
println!("{actual}");
assert_eq!(actual, "[\n 1,\n 2,\n 3\n]")
})
}

View File

@ -108,3 +108,26 @@ fn top_level_values_from_json() {
assert_eq!(actual.out, type_name);
}
}
#[test]
fn ranges_to_json_as_array() {
let value = r#"[ 1, 2, 3]"#;
let actual = nu!(r#"1..3 | to json"#);
assert_eq!(actual.out, value);
}
#[test]
fn unbounded_from_in_range_fails() {
let actual = nu!(r#"1.. | to json"#);
assert!(actual.err.contains("Cannot create range"));
}
#[test]
fn inf_in_range_fails() {
let actual = nu!(r#"inf..5 | to json"#);
assert!(actual.err.contains("Cannot create range"));
let actual = nu!(r#"5..inf | to json"#);
assert!(actual.err.contains("Cannot create range"));
let actual = nu!(r#"-inf..inf | to json"#);
assert!(actual.err.contains("Cannot create range"));
}