aboutsummaryrefslogtreecommitdiff
path: root/tests/ranking.sh
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2022-11-18 00:43:10 -0300
committerEuAndreh <eu@euandre.org>2022-11-25 23:14:05 -0300
commit694c2dbc6fa71ee6582bade3ecc82e7b6ace83bf (patch)
treec946b6a8b60a1a150b01b19ef699e38e4e7e96b4 /tests/ranking.sh
parentsrc/remembering.in: Use indented heredoc for usage() and help() (diff)
downloadremembering-694c2dbc6fa71ee6582bade3ecc82e7b6ace83bf.tar.gz
remembering-694c2dbc6fa71ee6582bade3ecc82e7b6ace83bf.tar.xz
Refactor: rewrite logic to make it simpler and faster
Diffstat (limited to 'tests/ranking.sh')
-rwxr-xr-xtests/ranking.sh109
1 files changed, 63 insertions, 46 deletions
diff --git a/tests/ranking.sh b/tests/ranking.sh
index 6649156..ea4365d 100755
--- a/tests/ranking.sh
+++ b/tests/ranking.sh
@@ -40,7 +40,7 @@ pick_x() {
echo "${2:-$INPUT}" | \
./src/remembering \
-p "$PROFILE" \
- -c "tee -a /dev/stderr | grep \"$PICK\"" \
+ -- sh -c "tee -a /dev/stderr | grep -F \"$PICK\"" \
1>"$OUT" 2>"$ERR"
STATUS=$?
assert_status 0
@@ -56,26 +56,28 @@ assert_first() {
fi
}
-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'
+BASE_PROFILE='0 profile a
+0 profile b
+0 profile c
+0 profile d
+0 profile e'
+BASE_PROFILE_A_PICKED='1 profile a
+0 profile b
+0 profile c
+0 profile d
+0 profile e'
assert_profile() {
if [ "$(cat "$XDG_DATA_HOME/remembering/$1")" != "$2" ]; then
printf '\n%s: Bad profile merge (%s).\n\nExpected:\n%s\nGot\n%s\n' \
"$(ERROR)" "$PROFILE" "$2" "$(cat "$XDG_DATA_HOME/remembering/$1")" >&2
+ print_debug_info
exit 1
fi
}
test_picking_first_makes_it_be_always_first() {
testing 'picking first makes it be always first'
+ N="$LINENO"
OUT="$(mktemp)"
ERR="$(mktemp)"
PROFILE="always-picks-first-$(uuid)"
@@ -83,7 +85,7 @@ test_picking_first_makes_it_be_always_first() {
printf 'always-picked\nnever-picked\n' | \
./src/remembering \
-p "$PROFILE" \
- -c 'head -n1' \
+ -- head -n1 \
1>"$OUT" 2>"$ERR"
STATUS=$?
assert_status 0
@@ -95,6 +97,7 @@ test_picking_first_makes_it_be_always_first() {
test_promoting_values() {
testing 'promoting values'
+ N="$LINENO"
PROFILE="promoting-$(uuid)"
pick_x h
@@ -109,6 +112,7 @@ test_promoting_values() {
test_higher_values_loose_tie() {
testing 'higher values loose tie'
+ N="$LINENO"
PROFILE="higher-loose-tie-$(uuid)"
pick_x f
@@ -122,6 +126,7 @@ test_higher_values_loose_tie() {
test_smaller_values_win_tie() {
testing 'smaller values win tie'
+ N="$LINENO"
PROFILE="smaller-win-tie-$(uuid)"
pick_x d
@@ -135,6 +140,7 @@ test_smaller_values_win_tie() {
test_many_sequential_picks() {
testing 'many sequential pick'
+ N="$LINENO"
PROFILE="many-sequential-picks-$(uuid)"
pick_x b
@@ -181,14 +187,15 @@ s'
test_pick_inexisting_value() {
testing 'pick inexisting value'
+ N="$LINENO"
OUT="$(mktemp)"
ERR="$(mktemp)"
PROFILE="pick-inexisting-value-$(uuid)"
- echo '0:a
-0:b
-0:c
-0:d
-0:e' > "$XDG_DATA_HOME/remembering/$PROFILE"
+ echo '0 profile a
+0 profile b
+0 profile c
+0 profile d
+0 profile e' > "$XDG_DATA_HOME/remembering/$PROFILE"
INPUT='a
b
c
@@ -198,46 +205,48 @@ e'
echo "$INPUT" | \
./src/remembering \
-p "$PROFILE" \
- -c 'echo f' \
- 1>"$OUT" 2>"$ERR"
+ -- echo f \
+ 1>"$OUT" # 2>"$ERR"
STATUS=$?
assert_status 0
assert_stdout f
- assert_profile "$PROFILE" '1:f
-0:a
-0:b
-0:c
-0:d
-0:e'
-
+ assert_profile "$PROFILE" '0 profile a
+0 profile b
+0 profile c
+0 profile d
+0 profile e
+1 profile f'
test_ok
}
test_stdin_profile_merging() {
testing 'STDIN/profile merging'
+ N="$LINENO"
PROFILE="stdin-profile-merging-$(uuid)"
- echo '0:a
-0:b
-0:c
-0:z' > "$XDG_DATA_HOME/remembering/$PROFILE"
+ echo '0 profile a
+0 profile b
+0 profile c
+0 profile z' > "$XDG_DATA_HOME/remembering/$PROFILE"
INPUT='a
b
c
d
e'
+ EXPECTED='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
+ assert_stderr "$EXPECTED"
test_ok
}
test_stdin_is_larger_than_profile() {
testing 'STDIN is larger than profile'
+ N="$LINENO"
PROFILE="stdin-is-larger-than-profile-$(uuid)"
- echo '0:a' > "$XDG_DATA_HOME/remembering/$PROFILE"
+ echo '0 profile a' > "$XDG_DATA_HOME/remembering/$PROFILE"
INPUT='a
b
c
@@ -250,6 +259,7 @@ e'
test_stdin_is_smaller_than_profile() {
testing 'STDIN is smaller than profile'
+ N="$LINENO"
PROFILE="stdin-is-smaller-than-profile-$(uuid)"
echo "$BASE_PROFILE" > "$XDG_DATA_HOME/remembering/$PROFILE"
INPUT='a'
@@ -260,6 +270,7 @@ test_stdin_is_smaller_than_profile() {
test_stdin_is_empty() {
testing 'STDIN is empty'
+ N="$LINENO"
PROFILE="stdin-is-empty-$(uuid)"
echo "$BASE_PROFILE" > "$XDG_DATA_HOME/remembering/$PROFILE"
OUT="$(mktemp)"
@@ -268,11 +279,12 @@ test_stdin_is_empty() {
printf '' | \
./src/remembering \
-p "$PROFILE" \
- -c 'tee -a /dev/stderr | head -n1' \
+ -- sh -c 'tee -a /dev/stderr | head -n1' \
1>"$OUT" 2>"$ERR"
STATUS=$?
assert_status 0
+ assert_stderr ''
assert_stdout ''
assert_profile "$PROFILE" "$BASE_PROFILE"
test_ok
@@ -280,6 +292,7 @@ test_stdin_is_empty() {
test_profile_does_not_exist() {
testing 'profile does not exist'
+ N="$LINENO"
PROFILE="profile-does-not-exist-$(uuid)"
INPUT='a
b
@@ -293,6 +306,7 @@ e'
test_profile_is_empty() {
testing 'profile is empty'
+ N="$LINENO"
PROFILE="profile-is-empty-$(uuid)"
printf '' > "$XDG_DATA_HOME/remembering/$PROFILE"
INPUT='a
@@ -307,11 +321,12 @@ e'
test_names_with_spaces() {
testing 'names with spaces'
+ N="$LINENO"
PROFILE="names-with-spaces-$(uuid)"
INPUT='a b c
d e f'
- EXPECTED='1:a b c
-0:d e f'
+ EXPECTED='1 profile a b c
+0 profile d e f'
pick_x 'a b c' "$INPUT"
assert_profile "$PROFILE" "$EXPECTED"
test_ok
@@ -319,13 +334,14 @@ d e f'
test_really_long_list() {
testing 'really long list'
+ N="$LINENO"
OUT="$(mktemp)"
ERR="$(mktemp)"
PROFILE="really-long-list-$(uuid)"
seq 999999 | \
./src/remembering \
-p "$PROFILE" \
- -c 'head -n1' \
+ -- head -n1 \
1>"$OUT" 2>"$ERR"
STATUS=$?
assert_status 0
@@ -336,15 +352,16 @@ test_really_long_list() {
test_utf8_commands() {
testing 'UTF-8 commands'
+ N="$LINENO"
PROFILE="utf8-commands-$(uuid)"
INPUT='❤️
á
è
ŭ 😀'
- EXPECTED='0:á
-0:è
-1:ŭ 😀
-0:❤️'
+ EXPECTED='0 profile á
+0 profile è
+1 profile ŭ 😀
+0 profile ❤️'
pick_x 'ŭ 😀' "$INPUT"
assert_profile "$PROFILE" "$EXPECTED"
test_ok
@@ -355,7 +372,7 @@ test_promoting_values
test_higher_values_loose_tie
test_smaller_values_win_tie
test_many_sequential_picks
-# test_pick_inexisting_value
+test_pick_inexisting_value
test_stdin_profile_merging
test_stdin_is_larger_than_profile
test_stdin_is_smaller_than_profile
@@ -363,5 +380,5 @@ test_stdin_is_empty
test_profile_does_not_exist
test_profile_is_empty
test_names_with_spaces
-# test_really_long_list
+test_really_long_list
test_utf8_commands