diff --git a/tests/t1000-addlist.sh b/tests/t1000-addlist.sh new file mode 100755 index 0000000..123a1f8 --- /dev/null +++ b/tests/t1000-addlist.sh @@ -0,0 +1,58 @@ +#!/bin/sh + +test_description='basic add and list functionality + +This test just makes sure the basic add and list +command work, including support for filtering. +' +. ./test-lib.sh + +# +# Add and list +# +test_todo_session 'basic add/list' <>> todo.sh add notice the daisies +TODO: 'notice the daisies' added on line 1. + +>>> todo.sh list +1 notice the daisies +-- +TODO: 1 of 1 tasks shown from $HOME/todo.txt + +>>> todo.sh add smell the roses +TODO: 'smell the roses' added on line 2. + +>>> todo.sh list +1 notice the daisies +2 smell the roses +-- +TODO: 2 of 2 tasks shown from $HOME/todo.txt +EOF + +# +# Filter +# +test_todo_session 'basic list filtering' <>> todo.sh list daisies +1 notice the daisies +-- +TODO: 1 of 2 tasks shown from $HOME/todo.txt + +>>> todo.sh list smell +2 smell the roses +-- +TODO: 1 of 2 tasks shown from $HOME/todo.txt +EOF + +test_todo_session 'case-insensitive filtering' <>> todo.sh add smell the uppercase Roses +TODO: 'smell the uppercase Roses' added on line 3. + +>>> todo.sh list roses +2 smell the roses +3 smell the uppercase Roses +-- +TODO: 2 of 3 tasks shown from $HOME/todo.txt +EOF + +test_done diff --git a/tests/t1100-replace.sh b/tests/t1100-replace.sh new file mode 100755 index 0000000..d198bd7 --- /dev/null +++ b/tests/t1100-replace.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +test_description='basic replace functionality + +Ensure we can replace items successfully. +' +. ./test-lib.sh + +# +# Set up the basic todo.txt +# +todo.sh add notice the daisies > /dev/null + +test_todo_session 'replace usage' <>> todo.sh replace adf asdfa +=== 1 +usage: $(which todo.sh) replace ITEM# "UPDATED ITEM" +EOF + +test_todo_session 'basic replace' <>> todo.sh replace 1 "smell the cows" +1: notice the daisies +replaced with +1: smell the cows + +>>> todo.sh list +1 smell the cows +-- +TODO: 1 of 1 tasks shown from $HOME/todo.txt + +>>> todo.sh replace 1 smell the roses +1: smell the cows +replaced with +1: smell the roses + +>>> todo.sh list +1 smell the roses +-- +TODO: 1 of 1 tasks shown from $HOME/todo.txt +EOF + +cat > todo.txt <>> todo.sh replace 1 smell the cheese +1: smell the cows +replaced with +1: smell the cheese + +>>> todo.sh replace 3 jump on hay +3: thrash some hay +replaced with +3: jump on hay + +>>> todo.sh replace 4 collect the eggs +4: chase the chickens +replaced with +4: collect the eggs +EOF + +test_todo_session 'replace error' << EOF +>>> todo.sh replace 10 "hej!" +=== 1 +10: No such todo. +EOF + +test_done diff --git a/tests/test-lib.sh b/tests/test-lib.sh index 086d12c..300f1e5 100644 --- a/tests/test-lib.sh +++ b/tests/test-lib.sh @@ -434,6 +434,60 @@ test_init_todo () { cd "$owd" } +# Generate and run a series of tests based on a transcript. +# Usage: test_todo_session "description" <>> command +# output1 +# output2 +# >>> command +# === exit status +# output3 +# output4 +# EOF +test_todo_session () { + test "$#" = 1 || + error "bug in the test script: extra args to test_todo_session" + subnum=0 + cmd="" + status=0 + > expect + while read line + do + case $line in + ">>> "*) + test -z "$cmd" || error "bug in the test script: missing blank line separator in test_todo_session" + cmd=${line#>>> } + ;; + "=== "*) + status=${line#=== } + ;; + "") + if [ ! -z "$cmd" ]; then + if [ $status = 0 ]; then + test_expect_success "$1 $subnum" "$cmd > output && test_cmp expect output" + else + test_expect_success "$1 $subnum" "$cmd > output || test $? = $status && test_cmp expect output" + fi + + subnum=$(($subnum + 1)) + cmd="" + status=0 + > expect + fi + ;; + *) + echo $line >> expect + ;; + esac + done + if [ ! -z "$cmd" ]; then + if [ $status = 0 ]; then + test_expect_success "$1 $subnum" "$cmd > output && test_cmp expect output" + else + test_expect_success "$1 $subnum" "$cmd > output || test $? = $status && test_cmp expect output" + fi + fi +} test_init_todo "$test" # Use -P to resolve symlinks in our working directory so that the cwd