|
8 | 8 | # Success
|
9 | 9 | # ```
|
10 | 10 | # ```shell-session
|
11 |
| -# $ x=42 |
12 |
| -# $ expects "$x" to_be 10 |
13 |
| -# FAIL: expected {actual} to_be 10, but {actual} is: 42 |
| 11 | +# $ x=10 |
| 12 | +# $ expects "$x" not to_be 42 && echo Success |
| 13 | +# Success |
14 | 14 | # ```
|
15 | 15 |
|
16 | 16 | actual_value=$1
|
17 |
| -compare_method=$2 |
18 |
| -expected_value=$3 |
| 17 | +shift |
| 18 | + |
| 19 | +# Parse "not to_*" or just "to_*" |
| 20 | +is_negated=false |
| 21 | +if [[ $1 == not ]] ; then |
| 22 | + is_negated=true |
| 23 | + shift |
| 24 | +fi |
19 | 25 |
|
20 |
| -function get_compare_method () { |
21 |
| - local compare_method=$1 |
22 |
| - case $compare_method in |
23 |
| - to_equal) |
24 |
| - printf '%s\n' '==' |
| 26 | +matcher=$1 |
| 27 | +expected_value=$2 |
| 28 | + |
| 29 | +function test_value () { |
| 30 | + local actual=$1 matcher=$2 expected=$3 |
| 31 | + case $matcher in |
| 32 | + to_be|to_equal) |
| 33 | + [[ $actual -eq $expected ]] |
| 34 | + return $? |
25 | 35 | ;;
|
26 |
| - not_to_equal) |
27 |
| - printf '%s\n' '!=' |
| 36 | + to_be_less_than) |
| 37 | + [[ $actual -lt $expected ]] |
| 38 | + return $? |
28 | 39 | ;;
|
29 |
| - to_be) |
30 |
| - printf '%s\n' '-eq' |
| 40 | + to_be_greater_than) |
| 41 | + [[ $actual -gt $expected ]] |
| 42 | + return $? |
31 | 43 | ;;
|
32 |
| - not_to_be) |
33 |
| - printf '%s\n' '-ne' |
| 44 | + to_contain) |
| 45 | + [[ $actual == *"$expected"* ]] |
| 46 | + return $? |
34 | 47 | ;;
|
35 |
| - to_be_less_than) |
36 |
| - printf '%s\n' '-lt' |
| 48 | + to_match) |
| 49 | + [[ $actual =~ $expected ]] |
| 50 | + return $? |
37 | 51 | ;;
|
38 |
| - to_be_less_than_or_equal) |
39 |
| - printf '%s\n' '-le' |
| 52 | + to_be_true) |
| 53 | + [[ $actual == true ]] |
| 54 | + return $? |
40 | 55 | ;;
|
41 |
| - to_be_greater_than) |
42 |
| - printf '%s\n' '-gt' |
| 56 | + to_be_false) |
| 57 | + [[ $actual == false ]] |
| 58 | + return $? |
| 59 | + ;; |
| 60 | + to_be_defined) |
| 61 | + [[ -n $actual ]] |
| 62 | + return $? |
43 | 63 | ;;
|
44 |
| - to_be_greater_than_or_equal) |
45 |
| - printf '%s\n' '-ge' |
| 64 | + *) |
| 65 | + echo "Unknown matcher: $matcher" >&2 |
| 66 | + exit 1 |
46 | 67 | ;;
|
47 | 68 | esac
|
48 | 69 | }
|
49 | 70 |
|
50 |
| -compare_operator=$(get_compare_method "$compare_method") |
51 |
| -if [[ $compare_operator == '' ]] ; then |
52 |
| - echo "Unknown compare method: $compare_method" >&2 |
53 |
| - exit 1 |
54 |
| -fi |
| 71 | +function format_failure_message () { |
| 72 | + local actual=$1 not=$2 matcher=$3 expected=$4 |
| 73 | + local visible_actual=$actual |
| 74 | + |
| 75 | + if [[ -z $actual ]] ; then |
| 76 | + visible_actual=empty |
| 77 | + fi |
55 | 78 |
|
56 |
| -if eval "[[ \"$actual_value\" $compare_operator \"$expected_value\" ]]" ; then |
| 79 | + # Special case for to_be_defined matcher |
| 80 | + if [[ $matcher == to_be_defined && -z $not ]] ; then |
| 81 | + printf "FAIL: expected {actual} %s%s, but {actual} is %s" \ |
| 82 | + "$not" "$matcher" "$visible_actual" |
| 83 | + else |
| 84 | + if [[ -n $expected ]] ; then |
| 85 | + printf "FAIL: expected {actual} %s%s %s, but {actual} is: %s" \ |
| 86 | + "$not" "$matcher" "$expected" "$visible_actual" |
| 87 | + else |
| 88 | + printf "FAIL: expected {actual} %s%s, but {actual} is: %s" \ |
| 89 | + "$not" "$matcher" "$visible_actual" |
| 90 | + fi |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +# Execute test and handle result |
| 95 | +if test_value "$actual_value" "$matcher" "$expected_value" ; then |
| 96 | + if $is_negated ; then |
| 97 | + format_failure_message "$actual_value" "not " "$matcher" "$expected_value" >&2 |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + exit 0 |
| 101 | +else |
| 102 | + if ! $is_negated ; then |
| 103 | + format_failure_message "$actual_value" "" "$matcher" "$expected_value" >&2 |
| 104 | + exit 1 |
| 105 | + fi |
57 | 106 | exit 0
|
58 | 107 | fi
|
59 | 108 |
|
60 |
| -[[ $actual_value == '' ]] \ |
61 |
| - && visible_actual_value='(empty or not defined)' \ |
62 |
| - || visible_actual_value=$actual_value |
63 |
| -echo "FAIL: expected {actual} $compare_method $expected_value, but {actual} is: $visible_actual_value" >&2 |
64 |
| -exit 1 |
65 |
| - |
66 | 109 | # The MIT License (MIT)
|
67 | 110 | #
|
68 | 111 | # Copyright (c) 2025- aiya000
|
|
0 commit comments