From d97562f6e844254610349dfa2daf3cf18470b083 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Thu, 5 Dec 2024 14:53:33 +0100 Subject: [PATCH] fix multiline strings in NDNUON (#14519) - should close https://github.com/nushell/nushell/issues/14517 # Description this will change `to ndnuon` so that newlines are encoded as a literal `\n` which `from ndnuon` is already able to handle # User-Facing Changes users should be able to encode multiline strings in NDNUON # Tests + Formatting new tests have been added: - they don't pass on the first commit - they do pass with the fix # After Submitting --- crates/nu-std/std/formats/mod.nu | 2 +- crates/nu-std/tests/test_formats.nu | 14 ++++++++++++++ crates/nu-std/tests/test_std_formats.nu | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/nu-std/std/formats/mod.nu b/crates/nu-std/std/formats/mod.nu index addfa4c1c9..a0484e42f4 100644 --- a/crates/nu-std/std/formats/mod.nu +++ b/crates/nu-std/std/formats/mod.nu @@ -36,5 +36,5 @@ export def "from ndnuon" []: [string -> any] { # Convert structured data to NDNUON, i.e. newline-delimited NUON export def "to ndnuon" []: [any -> string] { - each { to nuon --raw } | to text + each { to nuon --raw | str replace --all "\n" '\n' } | to text } diff --git a/crates/nu-std/tests/test_formats.nu b/crates/nu-std/tests/test_formats.nu index ec1f6adb47..44a89eeaad 100644 --- a/crates/nu-std/tests/test_formats.nu +++ b/crates/nu-std/tests/test_formats.nu @@ -128,3 +128,17 @@ def to_ndnuon_single_object [] { let expect = "{a: 1}" assert equal $result $expect "could not convert to NDNUON" } + +#[test] +def to_ndnuon_multiline_strings [] { + let result = "foo\n\\n\nbar" | to ndnuon + let expect = '"foo\n\\n\nbar"' + assert equal $result $expect "could not convert multiline string to NDNUON" +} + +#[test] +def from_ndnuon_multiline_strings [] { + let result = '"foo\n\\n\nbar"' | from ndnuon + let expect = ["foo\n\\n\nbar"] + assert equal $result $expect "could not convert multiline string from NDNUON" +} diff --git a/crates/nu-std/tests/test_std_formats.nu b/crates/nu-std/tests/test_std_formats.nu index ed6b079d7e..1ca7516c20 100644 --- a/crates/nu-std/tests/test_std_formats.nu +++ b/crates/nu-std/tests/test_std_formats.nu @@ -128,3 +128,17 @@ def to_ndnuon_single_object [] { let expect = "{a: 1}" assert equal $result $expect "could not convert to NDNUON" } + +#[test] +def to_ndnuon_multiline_strings [] { + let result = "foo\n\\n\nbar" | formats to ndnuon + let expect = '"foo\n\\n\nbar"' + assert equal $result $expect "could not convert multiline string to NDNUON" +} + +#[test] +def from_ndnuon_multiline_strings [] { + let result = '"foo\n\\n\nbar"' | formats from ndnuon + let expect = ["foo\n\\n\nbar"] + assert equal $result $expect "could not convert multiline string from NDNUON" +}