aboutsummaryrefslogtreecommitdiff
path: root/tests/ranking.sh
blob: ea9c2c6bdc5daf9bcca61a06a024977d55d8d858 (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/sh
set -eu

. tests/lib.sh

test_picking_first_makes_it_be_always_first() {
	testing 'picking first makes it be always first'
	OUT="$(mktemp)"
	ERR="$(mktemp)"
	PROFILE="always-picks-first-$(uuid)"
	for _ in $(seq 10); do
		printf 'always-picked\nnever-picked\n' | \
			sh remembering \
				-p "$PROFILE" \
				-c 'head -n1' \
				1>"$OUT" 2>"$ERR"
		STATUS=$?
		assert_status 0
		assert_empty_stderr
		assert_stdout 'always-picked'
	done
	test_ok
}

INPUT='a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
w
x
y
z'

pick_x() {
	OUT="$(mktemp)"
	ERR="$(mktemp)"
	PICK="$1"

	echo "${2:-$INPUT}" | \
		sh remembering \
			-p "$PROFILE" \
			-c "tee -a /dev/stderr | grep \"$PICK\"" \
			1>"$OUT" 2>"$ERR"
	STATUS=$?
	assert_status 0
	assert_stdout "$PICK"
}

assert_first() {
	FIRST="$(head -n1 "$ERR")"
	if [ "$FIRST" != "$1" ]; then
		printf '\nERR: Previous choice did not appear at the beginning of the list:\n\nexpected: %s\ngot:      %s\n' \
			"$1" "$FIRST" >&2
		exit 1
	fi
}

test_promoting_values() {
	testing 'promoting values'
	PROFILE="promoting-$(uuid)"

	pick_x h
	pick_x z # just to get the new STDIN
	assert_first h

	pick_x h
	pick_x z
	assert_first h
	test_ok
}

test_higher_values_loose_tie() {
	testing 'higher values loose tie'
	PROFILE="higher-loose-tie-$(uuid)"

	pick_x f
	pick_x f
	pick_x g
	pick_x g
	pick_x z
	assert_first f
	test_ok
}

test_smaller_values_win_tie() {
	testing 'smaller values win tie'
	PROFILE="smaller-win-tie-$(uuid)"

	pick_x d
	pick_x d
	pick_x c
	pick_x c
	pick_x z
	assert_first c
	test_ok
}

test_many_sequential_picks() {
	testing 'many sequential pick'
	PROFILE="many-sequential-picks-$(uuid)"

	pick_x b
	pick_x r
	pick_x l
	pick_x r
	pick_x s
	pick_x a
	pick_x d
	pick_x m
	pick_x g
	pick_x g
	pick_x l
	pick_x l
	pick_x f
	pick_x f
	pick_x f
	pick_x l
	pick_x a

	pick_x z
	assert_first l

	EXPECTED='l
f
a
g
r
b
d
m
s'
	ACTUAL="$(head -n9 "$ERR")"
	if [ "$ACTUAL" != "$EXPECTED" ]; then
		printf '\nERR: Bad order!\n\nexpected: %s\ngot:      %s\n' \
			"$(echo "$EXPECTED" | tr '\n' ' ')" \
			"$(echo "$ACTUAL"   | tr '\n' ' ')" \
			>&2
		exit 1
	fi
	test_ok
}

test_stdin_profile_merging() {
	testing 'STDIN/profile merging'
	PROFILE="stdin-profile-merging-$(uuid)"
	echo '0:a
0:b
0:c
0:z' > "$XDG_DATA_HOME/$PROFILE"
	INPUT='a
b
c
d
e'
	pick_x a "$INPUT"
	if [ "$(cat "$ERR")" != "$INPUT" ]; then
		printf '\nERR: Bad profile merge.\n\nExpected:\n%s\nGot:\n%s\n' \
			"$INPUT" "$(cat "$ERR")" >&2
		exit 1
	fi
	test_ok
}

BASE_PROFILE='0:a
0:b
0:c
0:d
0:e'
BASE_PROFILE_A_PICKED='1:a
0:b
0:c
0:d
0:e'
assert_profile() {
	if [ "$(cat "$XDG_DATA_HOME/$1")" != "$2" ]; then
		printf '\nERR: Bad profile merge (%s).\n\nExpected:\n%s\nGot\n%s\n' \
			"$PROFILE" "$2" "$(cat "$XDG_DATA_HOME/$1")" >&2
		exit 1
	fi
}

test_stdin_is_larger_than_profile() {
	testing 'STDIN is larger than profile'
	PROFILE="stdin-is-larger-than-profile-$(uuid)"
	echo '0:a' > "$XDG_DATA_HOME/$PROFILE"
	INPUT='a
b
c
d
e'
	pick_x a "$INPUT"
	assert_profile "$PROFILE" "$BASE_PROFILE_A_PICKED"
	test_ok
}

test_stdin_is_smaller_than_profile() {
	testing 'STDIN is smaller than profile'
	PROFILE="stdin-is-smaller-than-profile-$(uuid)"
	echo "$BASE_PROFILE" > "$XDG_DATA_HOME/$PROFILE"
	INPUT='a'
	pick_x a "$INPUT"
	assert_profile "$PROFILE" "$BASE_PROFILE_A_PICKED"
	test_ok
}

test_stdin_is_empty() {
	testing 'STDIN is empty'
	PROFILE="stdin-is-empty-$(uuid)"
	echo "$BASE_PROFILE" > "$XDG_DATA_HOME/$PROFILE"
	INPUT=''
	pick_x '' "$INPUT"
	assert_profile "$PROFILE" "$BASE_PROFILE"
	test_ok
}


test_profile_does_not_exist() {
	testing 'profile does not exist'
	PROFILE="profile-does-not-exist-$(uuid)"
	INPUT='a
b
c
d
e'
	pick_x a "$INPUT"
	assert_profile "$PROFILE" "$BASE_PROFILE_A_PICKED"
	test_ok
}

test_profile_is_empty() {
	testing 'profile is empty'
	PROFILE="profile-is-empty-$(uuid)"
	printf '' > "$XDG_DATA_HOME/$PROFILE"
	INPUT='a
b
c
d
e'
	pick_x a "$INPUT"
	assert_profile "$PROFILE" "$BASE_PROFILE_A_PICKED"
	test_ok
}

test_picking_first_makes_it_be_always_first
test_promoting_values
test_higher_values_loose_tie
test_smaller_values_win_tie
test_many_sequential_picks
test_stdin_profile_merging
test_stdin_is_larger_than_profile
test_stdin_is_smaller_than_profile
test_stdin_is_empty
test_profile_does_not_exist
test_profile_is_empty