Fix quoting of empty string in to nuon (#7632)

Closes https://github.com/nushell/nushell/issues/7631
This commit is contained in:
Stefan Holderbach 2022-12-30 09:49:35 +01:00 committed by GitHub
parent b543063749
commit be31182969
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -221,6 +221,9 @@ static NEEDS_QUOTES_REGEX: Lazy<Regex> = Lazy::new(|| {
});
fn needs_quotes(string: &str) -> bool {
if string.is_empty() {
return true;
}
// These are case-sensitive keywords
match string {
// `true`/`false`/`null` are active keywords in JSON and NUON

View File

@ -296,6 +296,41 @@ fn to_nuon_converts_columns_with_spaces() {
assert!(actual.err.is_empty());
}
#[test]
fn to_nuon_quotes_empty_string() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
let test = ""; $test | to nuon
"#
));
assert!(actual.err.is_empty());
assert_eq!(actual.out, r#""""#)
}
#[test]
fn to_nuon_quotes_empty_string_in_list() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
let test = [""]; $test | to nuon | from nuon | $in == [""]
"#
));
assert!(actual.err.is_empty());
assert_eq!(actual.out, "true")
}
#[test]
fn to_nuon_quotes_empty_string_in_table() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
let test = [[a, b]; ['', la] [le lu]]; $test | to nuon | from nuon
"#
));
assert!(actual.err.is_empty());
}
#[test]
fn does_not_quote_strings_unnecessarily() {
let actual = nu!(
@ -331,7 +366,7 @@ fn quotes_some_strings_necessarily() {
'-11.0..<-15.0', '11.0..<-15.0', '-11.0..<15.0',
'-11.0..', '11.0..', '..15.0', '..-15.0', '..<15.0', '..<-15.0',
'2000-01-01', '2022-02-02T14:30:00', '2022-02-02T14:30:00+05:00',
',',
',',''
'&&'
] | to nuon | from nuon | describe
"#