1
0
mirror of https://github.com/nushell/nushell.git synced 2025-08-02 08:32:59 +02:00
Files
.cargo
.githooks
.github
assets
benches
clippy
crates
nu-cli
nu-cmd-base
nu-cmd-extra
nu-cmd-lang
nu-cmd-plugin
nu-color-config
nu-command
src
tests
commands
assignment
base
bytes
conversions
database
date
debug
hash_
math
move_
network
path
platform
query
random
skip
str_
take
url
alias.rs
all.rs
any.rs
append.rs
break_.rs
cal.rs
cd.rs
chunk_by.rs
chunks.rs
compact.rs
complete.rs
config_env_default.rs
config_nu_default.rs
continue_.rs
debug_info.rs
def.rs
default.rs
detect_columns.rs
do_.rs
drop.rs
du.rs
each.rs
echo.rs
empty.rs
error_make.rs
every.rs
exec.rs
export_def.rs
fill.rs
filter.rs
find.rs
first.rs
flatten.rs
for_.rs
format.rs
generate.rs
get.rs
glob.rs
griddle.rs
group_by.rs
headers.rs
help.rs
histogram.rs
ignore.rs
insert.rs
inspect.rs
interleave.rs
into_datetime.rs
into_duration.rs
into_filesize.rs
into_int.rs
job.rs
join.rs
last.rs
length.rs
let_.rs
lines.rs
loop_.rs
ls.rs
match_.rs
merge.rs
merge_deep.rs
mktemp.rs
mod.rs
mut_.rs
nu_check.rs
open.rs
par_each.rs
parse.rs
prepend.rs
print.rs
redirection.rs
reduce.rs
reject.rs
rename.rs
return_.rs
reverse.rs
rm.rs
roll.rs
rotate.rs
run_external.rs
save.rs
select.rs
semicolon.rs
seq.rs
seq_char.rs
seq_date.rs
slice.rs
sort.rs
sort_by.rs
source_env.rs
split_column.rs
split_row.rs
start.rs
stor.rs
table.rs
tee.rs
terminal.rs
to_text.rs
transpose.rs
try_.rs
ucp.rs
ulimit.rs
umkdir.rs
uname.rs
uniq.rs
uniq_by.rs
update.rs
upsert.rs
use_.rs
utouch.rs
where_.rs
which.rs
while_.rs
window.rs
with_env.rs
wrap.rs
zip.rs
format_conversions
string
main.rs
sort_utils.rs
Cargo.toml
LICENSE
README.md
nu-derive-value
nu-engine
nu-explore
nu-glob
nu-json
nu-lsp
nu-parser
nu-path
nu-plugin
nu-plugin-core
nu-plugin-engine
nu-plugin-protocol
nu-plugin-test-support
nu-pretty-hex
nu-protocol
nu-std
nu-system
nu-table
nu-term-grid
nu-test-support
nu-utils
nu_plugin_custom_values
nu_plugin_example
nu_plugin_formats
nu_plugin_gstat
nu_plugin_inc
nu_plugin_javascript
nu_plugin_nu_example
nu_plugin_polars
nu_plugin_python
nu_plugin_query
nu_plugin_stress_internals
nuon
README.md
devdocs
docker
scripts
src
tests
wix
.gitattributes
.gitignore
CITATION.cff
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Cargo.lock
Cargo.toml
Cross.toml
LICENSE
README.md
SECURITY.md
rust-toolchain.toml
toolkit.nu
typos.toml
nushell/crates/nu-command/tests/commands/reduce.rs
2025-05-13 16:49:30 +02:00

132 lines
2.9 KiB
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn reduce_table_column() {
let actual = nu!(pipeline(
r#"
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
| from json
| get total
| reduce --fold 20 { |it, acc| $it + $acc ** 1.05}
| into string -d 1
"#
));
assert_eq!(actual.out, "180.6");
}
#[test]
fn reduce_table_column_with_path() {
let actual = nu!(pipeline(
"
[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]
| reduce --fold 20 { |it, acc| $it.total + $acc ** 1.05}
| into string -d 1
"
));
assert_eq!(actual.out, "180.6");
}
#[test]
fn reduce_rows_example() {
let actual = nu!(pipeline(
"
[[a,b]; [1,2] [3,4]]
| reduce --fold 1.6 { |it, acc| $acc * ($it.a | into int) + ($it.b | into int) }
"
));
assert_eq!(actual.out, "14.8");
}
#[test]
fn reduce_with_return_in_closure() {
let actual = nu!(pipeline(
"
[1, 2] | reduce --fold null { |it, state|
if $it == 1 {
return 10
};
return ($it * $state)
}
"
));
assert_eq!(actual.out, "20");
assert!(actual.err.is_empty());
}
#[test]
fn reduce_enumerate_example() {
let actual = nu!(pipeline(
"
echo one longest three bar | enumerate
| reduce { |it, acc| if ($it.item | str length) > ($acc.item | str length) {echo $it} else {echo $acc}}
| get index
"
));
assert_eq!(actual.out, "1");
}
#[test]
fn reduce_enumerate_integer_addition_example() {
let actual = nu!(pipeline(
"
echo [1 2 3 4]
| enumerate
| reduce { |it, acc| { index: ($it.index) item: ($acc.item + $it.item)} }
| get item
"
));
assert_eq!(actual.out, "10");
}
#[test]
fn folding_with_tables() {
let actual = nu!(pipeline(
"
echo [10 20 30 40]
| reduce --fold [] { |it, acc|
with-env { value: $it } {
echo $acc | append (10 * ($env.value | into int))
}
}
| math sum
"
));
assert_eq!(actual.out, "1000");
}
#[test]
fn error_reduce_fold_type_mismatch() {
let actual = nu!(pipeline(
"echo a b c | reduce --fold 0 { |it, acc| $acc + $it }"
));
assert!(
actual
.err
.contains("nu::shell::operator_incompatible_types")
);
}
#[test]
fn error_reduce_empty() {
let actual = nu!(pipeline("reduce { |it, acc| $acc + $it }"));
assert!(actual.err.contains("no input value was piped in"));
}
#[test]
fn enumerate_reduce_example() {
let actual = nu!(pipeline(
"[one longest three bar] | enumerate | reduce {|it, acc| if ($it.item | str length) > ($acc.item | str length) { $it } else { $acc }} | get index"
));
assert_eq!(actual.out, "1");
}