Skip to content

Commit 9417d49

Browse files
Dan RivettDan Rivett
authored andcommitted
Adding in assert-extras.sh which contains convenience assertions such as: assert_contains, assert_matches, assert_success, assert_failure.
1 parent fd13e9a commit 9417d49

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

assert-extras.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
3+
# assert-extras.sh 1.1 - supplementary bash unit testing functions
4+
# Note: This script should be sourced together with assert.sh,
5+
# it is dependent on the functionality provided by that script.
6+
7+
assert_success() {
8+
assert_raises "$@" 0
9+
}
10+
11+
# assert_failure <command> [stdin]
12+
assert_failure() {
13+
(( tests_ran++ )) || :
14+
[[ -z "$DISCOVERONLY" ]] || return
15+
status=0
16+
(eval "$1" <<< ${2:-}) > /dev/null 2>&1 || status=$?
17+
if [[ "$status" != "0" ]]; then
18+
[[ -z "$DEBUG" ]] || echo -n .
19+
return
20+
fi
21+
_assert_fail "program terminated with a zero return code;" \
22+
"expecting non-zero return code" "$1" "$2"
23+
}
24+
25+
# assert_contains <command> <expected output...>
26+
assert_contains() {
27+
_assert_with_grep '-F' "$@"
28+
}
29+
30+
# assert_matches <command> <expected output...>
31+
assert_matches() {
32+
_assert_with_grep '-E' "$@"
33+
}
34+
35+
# assert_startswith <command> <expected start to stdout>
36+
assert_startswith() {
37+
assert_success "[[ '$($1)' == '$2'* ]]"
38+
}
39+
40+
# assert_endswith <command> <expected start to stdout>
41+
assert_endswith() {
42+
assert_success "[[ '$($1)' == *'$2' ]]"
43+
}
44+
45+
# _assert_with_grep <grep modifiers> <command> <expected output...>
46+
_assert_with_grep() {
47+
local grep_modifier="$1"
48+
local output="$($2)"
49+
shift 2
50+
51+
while [ $# != 0 ]; do
52+
assert_raises "echo '$output' | $GREP $grep_modifier '$1'" 0 || return 1
53+
shift
54+
done
55+
}
56+
57+
# Returns the resolved command, preferring any gnu-versions of the cmd (prefixed with 'g') on
58+
# non-Linux systems such as Mac OS, and falling back to the standard version if not.
59+
_cmd() {
60+
local cmd="$1"
61+
62+
local gnu_cmd="g$cmd"
63+
local gnu_cmd_found=$(which "$gnu_cmd" 2> /dev/null)
64+
if [ "$gnu_cmd_found" ]; then
65+
echo "$gnu_cmd_found"
66+
else
67+
if [ "$(uname)" == 'Darwin' ]; then
68+
echo "Warning: Cannot find gnu version of command '$cmd' ($gnu_cmd) on path." \
69+
"Falling back to standard command" >&2
70+
fi
71+
echo "cmd"
72+
fi
73+
}
74+
75+
GREP=$(_cmd grep)
76+
SED=$(_cmd sed)

test-extras.sh

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env bash
2+
3+
source assert.sh
4+
source assert-extras.sh
5+
6+
assert "echo foo" "foo"
7+
assert_raises "true" 0
8+
assert_raises "exit 127" 127
9+
10+
assert_end sanity
11+
12+
###
13+
### assert_success tests
14+
###
15+
16+
# Tests expecting success
17+
assert_success "true"
18+
assert_success "echo foo"
19+
20+
# Tests expecting failure
21+
assert_raises 'assert_success "false"' 1
22+
assert_raises 'assert_success "exit 1"' 1
23+
24+
assert_end assert_success
25+
26+
###
27+
### assert_failure tests
28+
###
29+
30+
# Tests expecting success
31+
assert_failure "false"
32+
assert_failure "exit 1"
33+
assert_failure "exit -1"
34+
assert_failure "exit 42"
35+
assert_failure "exit -42"
36+
37+
# Tests expecting failure
38+
assert_raises 'assert_failure "true"' 1
39+
assert_raises 'assert_failure "echo foo"' 1
40+
41+
assert_end assert_failure
42+
43+
###
44+
### assert_contains tests
45+
###
46+
47+
# Tests expecting success
48+
assert_contains "echo foo" "foo"
49+
assert_contains "echo foobar" "foo"
50+
assert_contains "echo foo bar" "foo"
51+
assert_contains "echo foo bar" "bar"
52+
assert_contains "echo foo bar" "foo bar"
53+
54+
# Tests expecting failure
55+
assert_failure 'assert_contains "echo foo" "foot"'
56+
assert_failure 'assert_contains "echo foo" "f.."'
57+
58+
# Multi-word argument tests
59+
assert_contains "echo foo bar" "foo bar"
60+
assert_failure 'assert_contains "echo foo; echo bar" "foo bar"'
61+
62+
# Multi-argument tests
63+
assert_contains "echo foo bar baz" "foo" "baz"
64+
assert_failure 'assert_contains "echo foo bar baz" "bar" "foo baz"'
65+
66+
assert_end assert_contains
67+
68+
###
69+
### assert_matches tests
70+
###
71+
72+
# Tests expecting success
73+
assert_matches "echo foo" "f.."
74+
assert_matches "echo foobar" "f."
75+
assert_matches "echo foo bar" "^foo bar$"
76+
assert_matches "echo foo bar" "[az ]+"
77+
78+
# Tests expecting failure
79+
assert_failure 'assert_matches "echo foot" "foo$"'
80+
81+
# Multi-word argument tests
82+
assert_matches "echo foo bar" "foo .*"
83+
assert_failure 'assert_matches "echo foo; echo bar" "foo .*"'
84+
85+
# Multi-argument tests
86+
assert_matches "echo foo bar baz" "^f.." "baz$"
87+
assert_failure 'assert_matches "echo foo bar baz" "bar" "foo baz"'
88+
89+
assert_end assert_matches
90+
91+
###
92+
### assert_startswith tests
93+
###
94+
95+
# Tests expecting success
96+
assert_startswith "echo foo" "f"
97+
assert_startswith "echo foo" "foo"
98+
assert_startswith "echo foo; echo bar" "foo"
99+
100+
# Tests expecting failure
101+
assert_failure 'assert_startswith "echo foo" "oo"'
102+
assert_failure 'assert_startswith "echo foo; echo bar" "foo bar"'
103+
assert_failure 'assert_startswith "echo foo" "."'
104+
105+
assert_end assert_startswith
106+
107+
###
108+
### assert_endswith tests
109+
###
110+
111+
# Tests expecting success
112+
assert_endswith "echo foo" "oo"
113+
assert_endswith "echo foo" "foo"
114+
assert_endswith "echo foo; echo bar" "bar"
115+
116+
# Tests expecting failure
117+
assert_failure 'assert_endswith "echo foo" "f"'
118+
assert_failure 'assert_endswith "echo foo; echo bar" "foo bar"'
119+
assert_failure 'assert_endswith "echo foo" "."'
120+
121+
assert_end assert_endswith

0 commit comments

Comments
 (0)