summaryrefslogtreecommitdiff
path: root/tests/include/testassertions.sh
blob: 63c663cdad302aab8e6c1535d9684a26ba2dcd60 (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
164
165
166
167
168
# Adapted from 
# shfuncs : test suite common shell functions
# which was shipped with the TET example code.

. "$TEST_CODE_DIR/include/testfuncs.sh"

assert_exit() # execute command (saving output) and check exit code
{
    EXPECT="$1"
    shift 1

    # $1 is command, $2 is expected exit code (0 or "N" for non-zero)
    ( $@ > out.stdout 2> out.stderr ) 
    CODE="$?"

    if [ -z "$EXPECT" ]; then
	EXPECT=0;
    fi
    if [ "$EXPECT" = N -a "$CODE" -eq 0 ]; then
	test_fail "Command ($*) gave exit code $CODE, expected nonzero"
    elif [ "$EXPECT" != N ] && [ "$EXPECT" -ne "$CODE" ]; then
	test_fail "Command ($*) gave exit code $CODE, expected $EXPECT"
    fi
}

assert_file() {
	if [ ! -e "$1" ] ; then
		test_fail "'$1' does not exist"
	elif [ ! -f "$1" ] ; then
		test_fail "'$1' is not a regular file"
	elif [ ! -s "$1" ] ; then
		test_fail "$1 exists, but is empty"
	fi
}

assert_nofile() {
	if [ -e "$1" ] ; then
		test_fail "'$1' exists."
	fi
}


assert_nostdout() # check that nothing went to stdout
{
    if [ -s out.stdout ]
    then
	test_infoline "Unexpected output written to stdout, as shown below:"
	infofile out.stdout stdout:
	test_fail
    fi
}

assert_nostderr() # check that nothing went to stderr
{
    if [ -s out.stderr ]
    then
	test_infoline "Unexpected output written to stderr, as shown below:"
	infofile out.stderr stderr:
	test_fail
    fi
}

assert_stderr() # check that stderr matches expected error
{
    # $1 is file containing regexp for expected error
    # if no argument supplied, just check out.stderr is not empty

    case "$1" in
    "")
	if [ ! -s out.stderr ]
	then
	    test_infoline "Expected output to stderr, but none written"
	    test_fail
	fi
	;;
    *)
	expfile="$1"
	OK=Y
	exec 4<&0 0< "$expfile" 3< out.stderr
	while read expline
	do
	    if read line <&3
	    then
		if expr "$line" : "$expline" > /dev/null
		then
		    :
		else
		    OK=N
		    break
		fi
	    else
		OK=N
	    fi
	done
	exec 0<&4 3<&- 4<&-
	if [ "$OK" = N ]
	then
	    test_infoline "Incorrect output written to stderr, as shown below"
	    infofile "$expfile" "expected stderr:"
	    infofile out.stderr "received stderr:"
	    test_fail
	fi
	;;
    esac
}

assert_stdout() # check that stderr matches expected error
{
    # $1 is file containing regexp for expected error
    # if no argument supplied, just check out.stderr is not empty

    case "$1" in
    "")
	if [ ! -s out.stdout ]
	then
	    test_infoline "Expected output to stdout, but none written"
	    test_fail
	fi
	;;
    *)
	expfile="$1"

	if [ ! -e "$expfile" ] ; then
		test_status NORESULT "Could not find file '$expfile' to look up expected pattern!"
		return
	fi
	#expline="$*"
	#expfile="out.stdout"
	OK=Y
	exec 4<&0 0< "$expfile" 3< out.stdout
	while read expline
	do
	    if read line <&3
	    then
		if expr "$line" : "$expline" > /dev/null
		then
		    :
		else
		    OK=N
		    break
		fi
	    else
		OK=N
	    fi
	done
	exec 0<&4 3<&- 4<&-
	if [ "$OK" = N ]
	then
	    test_infoline "Incorrect output written to stdout, as shown below"
	    infofile "$expfile" "expected stdout:"
	    infofile out.stdout "received stdout:"
	    test_fail
	fi
	;;
    esac
}


infofile() # write file to journal using tet_infoline
{
    # $1 is file name, $2 is prefix for tet_infoline

    prefix="$2"
    while read line
    do
	test_infoline "$prefix$line"
    done < "$1"
}