To nuon escapes (#6660)

* Add tests for "to nuon" escaping handling

* Fix "to nuon" not escaping double quotations

* Fix "to nuon" double backslash

Fix value_to_string_without_quotes leaving escaped backslash in
non-quoted strings
This commit is contained in:
Artemiy 2022-10-04 14:25:21 +03:00 committed by GitHub
parent 5921c19bc0
commit 79ce13abef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 5 deletions

View File

@ -173,12 +173,11 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
fn value_to_string_without_quotes(v: &Value, span: Span) -> Result<String, ShellError> { fn value_to_string_without_quotes(v: &Value, span: Span) -> Result<String, ShellError> {
match v { match v {
Value::String { val, .. } => Ok({ Value::String { val, .. } => Ok({
let mut quoted = escape_quote_string(val); if needs_quotes(val) {
if !needs_quotes(val) { escape_quote_string(val)
quoted.remove(0); } else {
quoted.pop(); val.clone()
} }
quoted
}), }),
_ => value_to_string(v, span), _ => value_to_string(v, span),
} }
@ -209,6 +208,7 @@ fn needs_quotes(string: &str) -> bool {
|| string.contains('\t') || string.contains('\t')
|| string.contains('\n') || string.contains('\n')
|| string.contains('\r') || string.contains('\r')
|| string.contains('\"')
} }
#[cfg(test)] #[cfg(test)]

View File

@ -103,6 +103,51 @@ fn to_nuon_escaping2() {
assert_eq!(actual.out, "hello\\world"); assert_eq!(actual.out, "hello\\world");
} }
#[test]
fn to_nuon_escaping3() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
["hello\\world"]
| to nuon
| from nuon
| $in == [hello\world]
"#
));
assert_eq!(actual.out, "true");
}
#[test]
fn to_nuon_escaping4() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
["hello\"world"]
| to nuon
| from nuon
| $in == ["hello\"world"]
"#
));
assert_eq!(actual.out, "true");
}
#[test]
fn to_nuon_escaping5() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
{s: "hello\"world"}
| to nuon
| from nuon
| $in == {s: "hello\"world"}
"#
));
assert_eq!(actual.out, "true");
}
#[test] #[test]
fn to_nuon_negative_int() { fn to_nuon_negative_int() {
let actual = nu!( let actual = nu!(