-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuff.test.sh
executable file
·112 lines (99 loc) · 2.82 KB
/
shuff.test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
. $(dirname $0)/../test.sh
. $(dirname $0)/../../src/lib/shuff.sh
set -e
function test_map {
local input="foo
bar"
local result=$(echo "$input" | map echo mapped)
assert_equal "$result" "mapped foo
mapped bar"
}
run_test test_map
function test_map_parallel {
local result=$(printf "foo\nbar\n" | map_parallel echo mapped)
local sorted_result=$(echo "$result" | sort)
assert_equal "$sorted_result" "mapped bar
mapped foo"
}
run_test test_map_parallel
function not_foo {
if [[ $1 != 'foo' ]]; then
echo $1
fi
}
function test_filter {
local result=$(echo "foo
bar" | filter not_foo)
assert_equal "$result" "bar"
}
run_test test_filter
# starting point function for functional morphing
# this is used by other functions
function greet {
local salutation=$1
local title=$2
local firstName=$3
local lastName=$4
echo "$salutation, $title $firstName $lastName!"
}
function test_partial {
partial greet say_hello Hello
partial say_hello say_hello_to_ms "Ms."
partial say_hello_to_ms say_hello_to_ms_jane_jones Jane Jones
local greeting=$(say_hello_to_ms_jane_jones)
assert_equal "$greeting" "Hello, Ms. Jane Jones!"
unset say_hello_to_ms_jane_jones
unset say_hello_to_ms
unset say_hello
}
run_test test_partial
function test_partial_right {
partial_right greet greet_ms_jane_jones "Ms." Jane Jones
local greeting=$(greet_ms_jane_jones Hello)
assert_equal "$greeting" "Hello, Ms. Jane Jones!"
unset greet_ms_jane_jones
}
run_test test_partial_right
function test_thunk {
thunk greet greet_ms_jane_jones "Ms." Jane Jones
local greeting=$(greet_ms_jane_jones Hello)
greeting=$(greet )
assert_equal "$greeting" "Hello, Ms. Jane Jones!"
unset greet_ms_jane_jones
}
run_test test_partial_right
function append {
local line=$1
local accumulator=$2
if [[ -z $accumulator ]]; then
local comma=""
else
local comma=","
fi
echo "${accumulator}${comma}${line}"
}
function append_sum {
local line=$1
local accumulator=
if [ -z $2 ]; then
accumulator=0
else
accumulator=$2
fi
echo `expr $line + $accumulator`
# echo "$(( $line + $accumulator ))"
}
function test_reduce {
local result=$(printf "foo\nbar" | reduce "oof" append)
assert_equal "$result" "oof,foo,bar"
local result_sum=$(printf "1\n2\n3\n4\n5" | reduce 5 append_sum)
assert_equal "$result_sum" "20"
}
run_test test_reduce
function test_concat_arrays {
local result=$(concat_arrays '["one","two"]' '["a","b"]')
assert_equal "$result" '["one","two","a","b"]'
local pojo_result=$(concat_arrays '[{"foo":"bar1"},{"foo":"bar2"}]' '[{"foo":"bar3"},{"foo":"bar4"}]')
assert_equal "$pojo_result" '[{"foo":"bar1"},{"foo":"bar2"},{"foo":"bar3"},{"foo":"bar4"}]'
}
run_test test_concat_arrays