forked from ztombol/bats-assert
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathassert_equal.bash
More file actions
60 lines (58 loc) · 1.27 KB
/
assert_equal.bash
File metadata and controls
60 lines (58 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# assert_equal
# ============
#
# Summary: Fail if the actual and expected values are not equal.
#
# Usage: assert_equal [-d] <actual> <expected>
#
# Options:
# <actual> The value being compared.
# <expected> The value to compare against.
# -d, --diff Show diff between `expected` and `$output`
#
# ```bash
# @test 'assert_equal()' {
# assert_equal 'have' 'want'
# }
# ```
#
# IO:
# STDERR - expected and actual values, on failure
# Globals:
# none
# Returns:
# 0 - if values equal
# 1 - otherwise
#
# On failure, the expected and actual values are displayed.
#
# ```
# -- values do not equal --
# expected : want
# actual : have
# --
# ```
#
# If the `--diff` option is set, a diff between the expected and actual output is shown.
assert_equal() {
local -i show_diff=0
while (( $# > 0 )); do
case "$1" in
-d|--diff) show_diff=$(( $# > 1 )); shift ;;
*) break ;;
esac
done
if [[ $1 != "$2" ]]; then
if (( show_diff )); then
diff -u <(echo "$1") <(echo "$2") | tail -n +3 \
| batslib_decorate 'values do not equal' \
| fail
else
batslib_print_kv_single_or_multi 8 \
'expected' "$2" \
'actual' "$1" \
| batslib_decorate 'values do not equal' \
| fail
fi
fi
}