-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmp.subr
86 lines (77 loc) · 2.08 KB
/
cmp.subr
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# cmp.subr
# Inexact comparisons
# By J. Stuart McMurray
# Created 20241109
# Last Modified 20241126
# tap_is notes whether something we got was expected. These will be compared
# with [ ... = ... ].
#
# Arguments:
# $1 - The got
# $2 - The expected
# $3 - Test name (optional)
# $4 - Test file (optional)
# $5 - Test line (optional)
tap_is() { tap_cmp_ok "$1" "=" "$2" "$3" "$4" "$5"; }
# tap_is notes whether something we got was anything but somthing. These will
# be compared with [ ... != ... ].
#
# Arguments:
# $1 - The got
# $2 - The not expected
# $3 - Test name (optional)
# $4 - Test file (optional)
# $5 - Test line (optional)
tap_isnt() { tap_cmp_ok "$1" "!=" "$2" "$3" "$4" "$5"; }
# tap_cmp_ok compares two arguments with any of test(1)'s binary operators.
# It is similar to test $1 $2 $3; tap_ok $? but with better logging and less
# typing. It's also handy for comparing numbers with -eq and -ne.
#
# Arguments:
# $1 - The got
# $2 - test(1) operator
# $3 - The expected
# $4 - Test name (optional)
# $5 - Test file (optional)
# $6 - Test line (optional)
tap_cmp_ok() {
# Don't do anything if we've bailed.
if [ -n "${_tap_bailo}" ]; then
return
fi
# Make sure we have enough arguments
if [ 3 -gt $# ]; then
echo "Not enough arguments" >&2
return $TAP_RETURN_NOT_ENOUGH_ARGUMENTS
fi
# Do the comparing itself.
_tap_ok=0
test "$1" "$2" "$3" || _tap_ok=$?
tap_ok "${_tap_ok}" "$4" "$5" "$6"
# If the test passed, no need to print anything else.
if [ 0 -eq ${_tap_ok} ]; then
return
fi
# If we're in a TODO state, write to stdout.
_tap_pf=tap_diag
if [ -n "$TAP_TODO" ]; then
_tap_pf=tap_note
fi
# Report the fail nicely.
case $2 in
"="|"=="|"-eq")
${_tap_pf} " got: '$1'"
${_tap_pf} " expected: '$3'"
;;
"!="|"-ne")
${_tap_pf} " got: '$1'"
${_tap_pf} " expected: anything else"
;;
*)
${_tap_pf} " '$1'"
${_tap_pf} " $2"
${_tap_pf} " '$3'"
;;
esac
}
# vim: ft=sh