Skip to content

Commit

Permalink
Add testing of todo_completion.
Browse files Browse the repository at this point in the history
Before adding any more features to todo_completion, I feel like I need test coverage, so this is a first stab at testing the completion results, via a new test function test_todo_completion.
Some basic tests showcase the capabilities.

Note: test-lib.sh now uses arrays, therefore all tests must use /bin/bash, not /bin/sh to avoid errors when sourcing test-lib. For consistency with todo.sh, we should have used Bash everywhere, anyway. Also note that t2000-multiline.sh needs some more quoting to avoid "Bash: ambiguous redirect" errors.
  • Loading branch information
inkarkat committed Feb 23, 2012
1 parent 46afb7f commit 3b90d09
Show file tree
Hide file tree
Showing 32 changed files with 126 additions and 48 deletions.
15 changes: 13 additions & 2 deletions tests/README
Expand Up @@ -125,10 +125,10 @@ Writing Tests
-------------

The test script is written as a shell script. It should start
with the standard "#!/bin/sh" with copyright notices, and an
with the standard "#!/bin/bash" with copyright notices, and an
assignment to variable 'test_description', like this:

#!/bin/sh
#!/bin/bash
#
# Copyright (c) 2005 Junio C Hamano
#
Expand Down Expand Up @@ -186,6 +186,17 @@ library for your script to use.
expected output. (See below for how to generate transcripts
easily.)

- test_todo_completion <message> <cmdline> <completions>

This takes three strings as parameter. Based on <cmdline>,
the todo_completion script is triggered in the current test
environment and completions are compared with <completions>,
which should be a space-separated list. Include a trailing
space in <cmdline> when you want to check new argument
completion; otherwise, completion is triggered with the
context of the last argument. <message> should state what it
is testing.

- test_tick [interval]

The test harness has an internal view of time which is
Expand Down
2 changes: 1 addition & 1 deletion tests/t0000-config.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='todo.sh configuration file location
Expand Down
2 changes: 1 addition & 1 deletion tests/t0001-null.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='todo.sh basic null functionality test.
Expand Down
2 changes: 1 addition & 1 deletion tests/t0002-actions.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='todo.sh actions.d
Expand Down
2 changes: 1 addition & 1 deletion tests/t0100-code-nobacktick.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='no old-style backtick command substitution
Expand Down
2 changes: 1 addition & 1 deletion tests/t1000-addlist.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic add and list functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1010-add-date.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='test the date on add feature
Expand Down
2 changes: 1 addition & 1 deletion tests/t1020-addtolistfile.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic addto and list functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1030-addto-date.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='test the date on addto feature
Expand Down
2 changes: 1 addition & 1 deletion tests/t1100-replace.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic replace functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1200-pri.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic priority functionality
'
Expand Down
2 changes: 1 addition & 1 deletion tests/t1250-listpri.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='list priority functionality
'
Expand Down
2 changes: 1 addition & 1 deletion tests/t1260-listprj.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='list project functionality
'
Expand Down
2 changes: 1 addition & 1 deletion tests/t1300-ls.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
#

test_description='list functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1310-listcon.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
#

test_description='listcon functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1320-listproj.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
#

test_description='listproj functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1330-ls-highlighting.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
#

test_description='list highlighting
Expand Down
2 changes: 1 addition & 1 deletion tests/t1350-listall.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='listall functionality
'
Expand Down
2 changes: 1 addition & 1 deletion tests/t1400-prepend.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic prepend functionality
'
Expand Down
2 changes: 1 addition & 1 deletion tests/t1500-do.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='do functionality
'
Expand Down
2 changes: 1 addition & 1 deletion tests/t1600-append.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic append functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1700-depri.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic depriority functionality
'
Expand Down
2 changes: 1 addition & 1 deletion tests/t1800-del.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic del functionality
'
Expand Down
2 changes: 1 addition & 1 deletion tests/t1900-archive.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='archive functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1910-deduplicate.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='deduplicate functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t1950-report.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='report functionality
Expand Down
34 changes: 17 additions & 17 deletions tests/t2000-multiline.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='Multi-line functionality'

Expand All @@ -8,18 +8,18 @@ test_description='Multi-line functionality'
# Create the expected file
echo "1 smell the cheese
TODO: Replaced task with:
1 eat apples eat oranges drink milk">$HOME/expect.multi
1 eat apples eat oranges drink milk">"$HOME/expect.multi"

test_expect_success 'multiline squash item replace' '
(
# Prepare single line todo file
cat /dev/null > $HOME/todo.txt
cat /dev/null > "$HOME/todo.txt"
"$HOME/bin/todo.sh" add smell the cheese
# Run replace
"$HOME/bin/todo.sh" replace 1 "eat apples
eat oranges
drink milk" > $HOME/output.multi
drink milk" > "$HOME/output.multi"
# Test output against expected
diff "$HOME/output.multi" "$HOME/expect.multi"
Expand All @@ -34,18 +34,18 @@ fi
## Add test
# Create the expected file
echo "2 eat apples eat oranges drink milk
TODO: 2 added.">$HOME/expect.multi
TODO: 2 added.">"$HOME/expect.multi"

test_expect_success 'multiline squash item add' '
(
# Prepare single line todo file
cat /dev/null > $HOME/todo.txt
cat /dev/null > "$HOME/todo.txt"
"$HOME/bin/todo.sh" add smell the cheese
# Run add
"$HOME/bin/todo.sh" add "eat apples
eat oranges
drink milk" > $HOME/output.multi
drink milk" > "$HOME/output.multi"
# Test output against expected
diff "$HOME/output.multi" "$HOME/expect.multi"
Expand All @@ -59,18 +59,18 @@ fi

## Append test
# Create the expected file
echo "1 smell the cheese eat apples eat oranges drink milk">$HOME/expect.multi
echo "1 smell the cheese eat apples eat oranges drink milk">"$HOME/expect.multi"

test_expect_success 'multiline squash item append' '
(
# Prepare single line todo file
cat /dev/null > $HOME/todo.txt
cat /dev/null > "$HOME/todo.txt"
"$HOME/bin/todo.sh" add smell the cheese
# Run append
"$HOME/bin/todo.sh" append 1 "eat apples
eat oranges
drink milk" > $HOME/output.multi
drink milk" > "$HOME/output.multi"
# Test output against expected
diff "$HOME/output.multi" "$HOME/expect.multi"
Expand All @@ -84,18 +84,18 @@ fi

## Prepend test
# Create the expected file
echo "1 eat apples eat oranges drink milk smell the cheese">$HOME/expect.multi
echo "1 eat apples eat oranges drink milk smell the cheese">"$HOME/expect.multi"

test_expect_success 'multiline squash item prepend' '
(
# Prepare single line todo file
cat /dev/null > $HOME/todo.txt
cat /dev/null > "$HOME/todo.txt"
"$HOME/bin/todo.sh" add smell the cheese
# Run prepend
"$HOME/bin/todo.sh" prepend 1 "eat apples
eat oranges
drink milk" > $HOME/output.multi
drink milk" > "$HOME/output.multi"
# Test output against expected
diff "$HOME/output.multi" "$HOME/expect.multi"
Expand All @@ -110,18 +110,18 @@ fi
## Multiple line addition
# Create the expected file
echo "2 eat apples
TODO: 2 added." > $HOME/expect.multi
TODO: 2 added." > "$HOME/expect.multi"
echo "3 eat oranges
TODO: 3 added." >>$HOME/expect.multi
TODO: 3 added." >>"$HOME/expect.multi"
echo "4 drink milk
TODO: 4 added." >> $HOME/expect.multi
TODO: 4 added." >>"$HOME/expect.multi"

test_expect_success 'actual multiline add' '
(
# Run addm
"$HOME/bin/todo.sh" addm "eat apples
eat oranges
drink milk" > $HOME/output.multi
drink milk" > "$HOME/output.multi"
# Test output against expected
diff "$HOME/output.multi" "$HOME/expect.multi"
Expand Down
18 changes: 18 additions & 0 deletions tests/t6000-completion.sh
@@ -0,0 +1,18 @@
#!/bin/bash
#

test_description='Bash completion functionality
This test checks basic todo_completion of actions and options
'
. ./test-lib.sh

readonly ACTIONS='add a addto addm append app archive command del rm depri dp do help list ls listall lsa listcon lsc listfile lf listpri lsp listproj lsprj move mv prepend prep pri p replace report shorthelp'
readonly OPTIONS='-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x'

test_todo_completion 'all arguments' 'todo.sh ' "$ACTIONS $OPTIONS"
test_todo_completion 'arguments beginning with a' 'todo.sh a' 'add a addto addm append app archive'
test_todo_completion 'all options' 'todo.sh -' "$OPTIONS"
test_todo_completion 'all actions after command action' 'todo.sh command ' "$ACTIONS"

test_done
2 changes: 1 addition & 1 deletion tests/t8000-actions.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='custom actions functionality
Expand Down
2 changes: 1 addition & 1 deletion tests/t9999-testsuite_example.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='basic tests imported from previous framework
'
Expand Down
51 changes: 50 additions & 1 deletion tests/test-lib.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
#
# Copyright (c) 2005 Junio C Hamano
#
Expand Down Expand Up @@ -632,6 +632,55 @@ EOF
exit 0
}

test_todo_completion () {
test "$#" = 3 ||
error "bug in the test script: not 3 parameters to test_todo_completion"
if ! test_skip "$@"
then
description=$1
expected=$3

if [ "${2: -1}" = ' ' ]
then
offset=0
say >&3 "expecting completions after: '$2'"
else
offset=1
say >&3 "expecting context completions for: '$2'"
fi

SAVEIFS=$IFS
IFS=' ' set -- $2
COMP_WORDS=("$@")
COMP_CWORD=$(($# - $offset))
IFS=' ' set -- $expected
EXPECT=("$@")

source "$TEST_DIRECTORY/../todo_completion"
_todo

IFS=$'\n'
printf '%s\n' "${EXPECT[*]}" > expect
printf '%s\n' "${COMPREPLY[*]}" > output
IFS=$SAVEIFS

if [ ${#COMPREPLY[@]} -eq ${#EXPECT[@]} ]
then
if [ "${COMPREPLY[*]}" = "${EXPECT[*]}" ]
then
test_ok_ "$description"
else
test_failure_ "$description" "Differing completion(s):
$(test_cmp expect output)"
fi
else
test_failure_ "$description" "Expected ${#EXPECT[@]} completion(s), got ${#COMPREPLY[@]}:
$(test_cmp expect output)"
fi
fi
echo >&3 ""
}

test_init_todo "$test"
# Use -P to resolve symlinks in our working directory so that the pwd
# in subprocesses equals our $PWD (for pathname comparisons).
Expand Down
2 changes: 1 addition & 1 deletion tests/testshell.sh
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

test_description='Providing an interactive shell in the proper environment'
. ./test-lib.sh
Expand Down

0 comments on commit 3b90d09

Please sign in to comment.