nushell/crates/nu-command/tests/commands/detect_columns.rs
Wind a15462fd00
Change default algorithm in detect columns (#12277)
# Description
@fdncred found another histogram based algorithm to detect columns, and
rewrite it in rust: https://github.com/fdncred/guess-width

I have tested it manually, and it works good with `df`, `docker ps`,
`^ps`. This pr is going to use the algorithm in `detect columns`

Fix: #4183

The pitfall of new algorithm:
1. it may not works well if there isn't too much rows of input
2. it may not works well if the length of value is less than the header
to value, e.g:
```
c1 c2 c3 c4 c5
a b c d e
g h i j k
g a a q d
a v c q q | detect columns
```
In this case, users might need to use ~~`--old`~~ `--legacy` to make it
works well.

# User-Facing Changes
User might need to add ~~`--old`~~ `--legacy` to scripts if they find
`detect columns` in their scripts broken.

# Tests + Formatting
Done

# After Submitting
NaN
2024-03-26 13:57:55 +08:00

97 lines
2.6 KiB
Rust

use nu_test_support::{nu, pipeline, playground::Playground};
#[test]
fn detect_columns_with_legacy() {
let cases = [(
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
"[[c1,c2,c3,c4,c5]; [a,b,c,d,e]]",
)];
Playground::setup("detect_columns_test_1", |dirs, _| {
for case in cases.into_iter() {
let out = nu!(
cwd: dirs.test(),
"({} | detect columns --legacy) == {}",
case.0,
case.1
);
assert_eq!(
out.out, "true",
"({} | detect columns --legacy) == {}",
case.0, case.1
);
}
});
}
#[test]
fn detect_columns_with_legacy_and_flag_c() {
let cases = [
(
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
"[[c1,c3,c4,c5]; ['a b',c,d,e]]",
"0..1",
),
(
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
"[[c1,c2,c3,c4]; [a,b,c,'d e']]",
"(-2)..(-1)",
),
(
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
"[[c1,c2,c3]; [a,b,'c d e']]",
"2..",
),
];
Playground::setup("detect_columns_test_1", |dirs, _| {
for case in cases.into_iter() {
let out = nu!(
cwd: dirs.test(),
"({} | detect columns --legacy --combine-columns {}) == {}",
case.0,
case.2,
case.1,
);
assert_eq!(
out.out, "true",
"({} | detect columns --legacy --combine-columns {}) == {}",
case.0, case.2, case.1
);
}
});
}
#[test]
fn detect_columns_with_flag_c() {
let body = "$\"
total 284K(char nl)
drwxr-xr-x 2 root root 4.0K Mar 20 08:28 =(char nl)
drwxr-xr-x 4 root root 4.0K Mar 20 08:18 ~(char nl)
-rw-r--r-- 1 root root 3.0K Mar 20 07:23 ~asdf(char nl)\"";
let expected = "[
['column0', 'column1', 'column2', 'column3', 'column4', 'column5', 'column8'];
['drwxr-xr-x', '2', 'root', 'root', '4.0K', 'Mar 20 08:28', '='],
['drwxr-xr-x', '4', 'root', 'root', '4.0K', 'Mar 20 08:18', '~'],
['-rw-r--r--', '1', 'root', 'root', '3.0K', 'Mar 20 07:23', '~asdf']
]";
let range = "5..7";
let cmd = format!(
"({} | detect columns -c {} -s 1 --no-headers) == {}",
pipeline(body),
range,
pipeline(expected),
);
println!("debug cmd: {cmd}");
Playground::setup("detect_columns_test_1", |dirs, _| {
let out = nu!(
cwd: dirs.test(),
cmd,
);
println!("{}", out.out);
assert_eq!(out.out, "true");
})
}