nushell/crates/nu-command/tests/commands/split_column.rs
nome 5101b5e306
Add --number flag to split column (#13831)
This allows parsing of data (e.g. key-value pairs) where the last column
may contain the delimiter.

- this PR should close #13742

# Description

Adds a `--number (-n)` flag to `split column`, analogous to `split row
--number`.

```
~> ['author: Salina Yoon' r#'title: Where's Ellie?: A Hide-and-Seek Book'#] | split column --number 2 ': ' key value
╭───┬────────┬──────────────────────────────────────╮
│ # │  key   │                value                 │
├───┼────────┼──────────────────────────────────────┤
│ 0 │ author │ Salina Yoon                          │
│ 1 │ title  │ Where's Ellie?: A Hide-and-Seek Book │
╰───┴────────┴──────────────────────────────────────╯
```

# User-Facing Changes
* `split column` gains a `--number` option

# Tests + Formatting
Tests included in strings::split::column::test::test_examples and
commands::split_column::to_column.

# After Submitting
Reference documentation is auto-generated from code. No other
documentation updates necessary.
2024-09-12 07:16:33 -05:00

63 lines
1.6 KiB
Rust

use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn to_column() {
Playground::setup("split_column_test_1", |dirs, sandbox| {
sandbox.with_files(&[
FileWithContentToBeTrimmed(
"sample.txt",
r#"
importer,shipper,tariff_item,name,origin
"#,
),
FileWithContentToBeTrimmed(
"sample2.txt",
r#"
importer , shipper , tariff_item , name , origin
"#,
),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.txt
| lines
| str trim
| split column ","
| get column2
"#
));
assert!(actual.out.contains("shipper"));
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open sample.txt
| lines
| str trim
| split column -n 3 ","
| get column3
"#
));
assert!(actual.out.contains("tariff_item,name,origin"));
let actual = nu!(
cwd: dirs.test(), pipeline(
r"
open sample2.txt
| lines
| str trim
| split column --regex '\s*,\s*'
| get column2
"
));
assert!(actual.out.contains("shipper"));
})
}