aboutsummaryrefslogtreecommitdiff
path: root/tests/ranking.sh
blob: 7b691067ce47fc9eb6b6247282dffd214ef0d3f8 (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
#!/bin/sh
set -eu

. tests/lib.sh

test_picking_first_makes_it_be_always_first() {
	OUT="$(mktemp)"
	ERR="$(mktemp)"
	PROFILE="always-picks-first-$(uuidgen)"
	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
}

INPUT='a'
for letter in {b..z}; do
	INPUT="$INPUT
$letter"
done

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

	echo "$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
		echo 'Previous choice did not appear at the beginning of the list'
		printf '\nexpected: %s\ngot: %s\n' "$1" "$FIRST"
		exit 1
	fi
}

test_promoting_values() {
	PROFILE="promoting-$(uuidgen)"

	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_higher_values_loose_tie() {
	PROFILE="higher-loose-tie-$(uuidgen)"

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

test_smaller_values_win_tie() {
	PROFILE="smaller-win-tie-$(uuidgen)"

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

test_many_sequential_picks() {
	PROFILE="many-sequential-picks-$(uuidgen)"

	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 'Bad order!\n\nexpected: %s\ngot:      %s\n' \
			"$(echo "$EXPECTED" | tr '\n' ' ')" \
			"$(echo "$ACTUAL"   | tr '\n' ' ')"
		exit 1
	fi
}

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