From 5770b1527035d4b9f5540e60e249cbc5745dfd42 Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Sat, 10 Oct 2020 07:30:48 +0200 Subject: [PATCH] Use iterator chain instead of string concat. (#2655) * Use iterator chain instead of string concat * Add regression test for multi-value lines --- crates/nu-cli/src/commands/lines.rs | 6 +++--- crates/nu-cli/tests/commands/lines.rs | 15 +++++++++++++++ tests/fixtures/formats/sample-simple.json | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/formats/sample-simple.json diff --git a/crates/nu-cli/src/commands/lines.rs b/crates/nu-cli/src/commands/lines.rs index 5b6f163493..2b486a0cac 100644 --- a/crates/nu-cli/src/commands/lines.rs +++ b/crates/nu-cli/src/commands/lines.rs @@ -75,9 +75,9 @@ async fn lines(args: CommandArgs, registry: &CommandRegistry) -> Result { let mut leftover_string = leftover_string.lock(); - let st = (&*leftover_string).clone() + &st; - - let mut lines: Vec = st.lines().map(|x| x.to_string()).collect(); + let lo_lines = (&*leftover_string).lines().map(|x| x.to_string()); + let st_lines = st.lines().map(|x| x.to_string()); + let mut lines: Vec = lo_lines.chain(st_lines).collect(); leftover_string.clear(); diff --git a/crates/nu-cli/tests/commands/lines.rs b/crates/nu-cli/tests/commands/lines.rs index 6f394fad87..efb77ea77b 100644 --- a/crates/nu-cli/tests/commands/lines.rs +++ b/crates/nu-cli/tests/commands/lines.rs @@ -34,3 +34,18 @@ fn lines_proper_buffering() { assert_eq!(actual.out, "[8194,4]"); } + +#[test] +fn lines_multi_value_split() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + open sample-simple.json + | get first second + | lines + | count + "# + )); + + assert_eq!(actual.out, "6"); +} diff --git a/tests/fixtures/formats/sample-simple.json b/tests/fixtures/formats/sample-simple.json new file mode 100644 index 0000000000..7986b6e546 --- /dev/null +++ b/tests/fixtures/formats/sample-simple.json @@ -0,0 +1,4 @@ +{ + "first": "first", + "second": "this\nshould\nbe\nseparate\nlines" +} \ No newline at end of file