summaryrefslogtreecommitdiff
path: root/packages/assert.source.sh
blob: 0c7157f35f2641938187c4d51fe4aad28860bc69 (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
# Unit test for whether 2 given strings equal.
assert::equals () {
    local -r result="$1"; shift
    local -r expected="$1"; shift
    local -r callee=${FUNCNAME[1]}

    if [[ "$result" != "$expected" ]]; then
        cat <<ERROR | log::pipe ERROR
In $callee expected
    '$expected'
But got
    '$result'
ERROR
        exit 2
    fi

    log VERBOSE "Result in $callee as expected: '$expected'"
}

# Unit test for whether a given string is not empty.
assert::not_empty () {
    local -r name="$1"; shift
    local -r content="$1"; shift
    local -r callee=${FUNCNAME[1]}

    if [ -z "$content" ]; then
        log ERROR "In $callee expected '$name' not to be empty!"
        exit 2
    fi

    log VERBOSE "Result in $callee as expected not empty"
}

# Unit test for whether a given string matches a regex.
assert::matches () {
    local -r name="$1"; shift
    local -r content="$1"; shift
    local -r regex="$1"; shift
    local -r callee=${FUNCNAME[1]}

    if ! $GREP -q -E "$regex" <<< "$content"; then
        log ERROR "In $callee expected '$name' to match '$regex'"
        exit 2
    fi

    log VERBOSE "Matching in $callee as expected"
}

# Checks if all the Bash scripts here are good.
assert::shellcheck () {
    set -e
	shellcheck \
		--norc \
		--external-sources \
		--check-sourced \
		--exclude=SC2155,SC2010,SC2154,SC1090,SC2012 \
		./"$0"
	set +e
}