summaryrefslogtreecommitdiff
path: root/test/tap.sh
blob: 388526918e373de0c4a1dada45489e7add9772f3 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright 2014 Andrew Gregory <andrew.gregory.8@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# Project URL: http://github.com/andrewgregory/tap.sh

declare -i tap_planned=0 tap_run=0 tap_failed=0 tap_passed=0
declare tap_todo=''

tap_plan() {
    tap_planned=$1
    printf "1..%d\n" "$tap_planned"
}

tap_done_testing() {
    tap_plan $tap_run
}

tap_skip_all() {
    printf "1..0 # SKIP"
    _tap_print_reason " " "$@"
    printf "\n"
}

tap_diag() {
    if [[ -n $tap_todo ]]; then
        tap_note "$@"
    else
        tap_note "$@" 1>&2
    fi
}

tap_note() {
    printf "# "
    printf -- "$@"
    printf "\n"
}

tap_bail() {
    printf "Bail out!"
    _tap_print_reason " " "$@"
    printf "\n"
}

tap_finish() {
    local tap_todo=''
    if (( tap_planned != tap_run )); then
        tap_note "Looks like you planned %d tests but ran %d." "$tap_planned" "$tap_run"
    elif (( tap_planned == tap_run && tap_failed == 0 )); then
        tap_note "All %d tests successfully run." "$tap_planned"
    else
        tap_note "Failed %d of %d tests." "$tap_failed" "$tap_planned"
    fi
    (( tap_planned == tap_run && tap_failed == 0 ))
}

tap_skip() {
    local -i count="$1"; shift
    while (( count-- )); do
        (( tap_run++ ))
        printf "ok %d # SKIP" "$tap_run"
        _tap_print_reason " " "$@"
        printf "\n"
    done
}

_tap_print_reason() {
    local sep="$1"; shift
    if [[ $# -gt 0 ]]; then
        printf "%s" "$sep"
        printf -- "$@"
    fi
}

tap_ok() {
    local ok="$1"; shift
    (( tap_run++ ))
    if [[ $ok -eq 0 ]]; then
        (( tap_passed++ ))
        printf "ok %d" "$tap_run"
    else
        (( tap_failed++ ))
        printf "not ok %d" "$tap_run"
    fi
    _tap_print_reason " - " "$@"
    if [[ -n $tap_todo ]]; then
        printf " # TODO %s" "$tap_todo"
    fi
    printf "\n"
    if [[ $ok -ne 0 ]]; then
        local line func file
        local -i i=0

        read line func file < <(caller $i)
        while [[ -n $func && $func == tap_* ]]; do
            (( i++ ))
            read line func file < <(caller $i)
        done

        if [[ -n $file ]]; then
            file=${file##*/}
            if [[ -n $tap_todo ]]; then
                tap_diag "  Failed (TODO) test at %s line %d." "${file}" "$line"
            else
                tap_diag "  Failed test at %s line %d." "${file}" "$line"
            fi
        fi
    fi
    return $ok
}

tap_is_str() {
    local got="$1" expected="$2"; shift 2
    [[ $got == $expected ]]
    local ret=$?
    if ! tap_ok $ret "$@"; then
        tap_diag "         got: '%s'" "$got"
        tap_diag "    expected: '%s'" "$expected"
    fi
    return $ret
}

tap_is_int() {
    local got="$1" expected="$2"; shift 2
    [[ $got -eq $expected ]]
    local ret=$?
    if ! tap_ok $ret "$@"; then
        tap_diag "         got: '%s'" "$got"
        tap_diag "    expected: '%s'" "$expected"
    fi
    return $ret
}

tap_diff() {
    local got="$1" expected="$2"; shift 2
    local output ret
    output="$(diff -u --label got --label expected "$got" "$expected" 2>&1)"
    ret=$?
    if ! tap_ok $ret "$@"; then
        while IFS= read line; do
            tap_diag "$line"
        done <<<"$output"
    fi
    return $ret
}

# vim: ft=sh