aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2021-01-24 03:11:52 -0300
committerEuAndreh <eu@euandre.org>2021-01-24 03:11:52 -0300
commitee6bc4f2ce6da48f25a29931a178159e5801b5d9 (patch)
treee5b13643b29bd51f06d382fde00ed3ce01199423 /tests
parentTODOs.md: Add task-b2c26218-5f21-4efd-afbd-ff6af57a358a (diff)
downloadremembering-ee6bc4f2ce6da48f25a29931a178159e5801b5d9.tar.gz
remembering-ee6bc4f2ce6da48f25a29931a178159e5801b5d9.tar.xz
Add tests/ranking.sh
Diffstat (limited to '')
-rwxr-xr-xtests/cli-opts.sh10
-rwxr-xr-xtests/lib.sh14
-rwxr-xr-xtests/ranking.sh133
3 files changed, 146 insertions, 11 deletions
diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh
index 0b6090a..cca2228 100755
--- a/tests/cli-opts.sh
+++ b/tests/cli-opts.sh
@@ -5,8 +5,8 @@ set -u
assert_usage() {
if ! grep -Fq 'Usage' "$1"; then
- echo 'Expected to find "Usage" text, it was missing:'
- cat "$ERR"
+ echo 'Expected to find "Usage" text, it was missing:' >&2
+ cat "$ERR" >&2
exit 1
fi
}
@@ -79,11 +79,7 @@ test_valid_options() {
STATUS=$?
assert_status 0
assert_empty_stderr
- if [ "$(cat "$OUT")" != 'a' ]; then
- echo "Expected STDOUT ($OUT) to be 'a', but was:"
- cat "$OUT"
- exit 1
- fi
+ assert_stdout 'a'
}
test_help_flags() {
diff --git a/tests/lib.sh b/tests/lib.sh
index f42db2d..efc9368 100755
--- a/tests/lib.sh
+++ b/tests/lib.sh
@@ -1,11 +1,9 @@
#!/bin/sh
-# shellcheck disable=2034
export XDG_DATA_HOME="$PWD/tests/test-profiles"
OUT=
ERR=
STATUS=
-PROFILE=
assert_status() {
if [ "$STATUS" != "$1" ]; then
@@ -16,8 +14,16 @@ assert_status() {
assert_empty_stderr() {
if [ "$(cat "$ERR")" != '' ]; then
- echo "Expected STDERR ($ERR) to be empty, but has content:"
- cat "$ERR"
+ echo "Expected STDERR ($ERR) to be empty, but has content:" >&2
+ cat "$ERR" >&2
+ exit 1
+ fi
+}
+
+assert_stdout() {
+ if [ "$(cat "$OUT")" != "$1" ]; then
+ echo "Bad STDOUT ($OUT), expected '$1', got:" >&2
+ cat "$OUT" >&2
exit 1
fi
}
diff --git a/tests/ranking.sh b/tests/ranking.sh
new file mode 100755
index 0000000..7b69106
--- /dev/null
+++ b/tests/ranking.sh
@@ -0,0 +1,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