bash-fun/test/map_test.sh

33 lines
1,001 B
Bash
Raw Normal View History

2018-02-13 18:11:27 -05:00
#!/bin/bash
testMapEmptyList() {
assertEquals "" "$(list | list_map lambda x . 'echo $(($x + 1))')"
2018-02-13 18:11:27 -05:00
}
testMapEmptyList_ifNoArgumentsInLambda() {
assertEquals "" "$(list | list_map lambda . 'echo 3')"
2018-02-13 18:11:27 -05:00
}
testMapOneElementList() {
assertEquals "3" "$(list 2 | list_map lambda x . 'echo $(($x + 1))')"
2018-02-13 18:11:27 -05:00
}
testMapList() {
assertEquals "2 3 4 5 6" "$(list {1..5} | list_map lambda x . 'echo $(($x + 1))' | unlist)"
2018-02-13 18:11:27 -05:00
}
testMapList_ifNoArgumentsInLambda() {
assertEquals "9 9 9 9 9" "$(list {1..5} | list_map lambda . 'echo 9' | unlist)"
2018-02-13 18:11:27 -05:00
}
testMapList_ifManyArgumentsInLambda() {
list {1..5} | list_map lambda x y . 'echo $(($x + $y))' 2> /dev/null \
2018-02-13 18:11:27 -05:00
&& fail "There should be syntax error, because map is an one argument operation"
}
testFlatMap() {
assertEquals "1 2 3 2 3 3" "$(list {1..3} | list_map lambda x . 'seq $x 3' | unlist)"
assertEquals "d e h l l l o o r w" "$(list hello world | list_map lambda x . 'command fold -w 1 <<< $x' | sort | unlist)"
2019-08-30 19:52:16 -04:00
}
2018-02-13 18:11:27 -05:00
. ./shunit2-init.sh