blob: 93aa5fc57191b01459e57348510c04706ff97d25 [file] [log] [blame]
Sean Dague53753292014-12-04 19:38:15 -05001#!/usr/bin/env bash
2
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15# we always start with no errors
16ERROR=0
Ian Wienand1cb809d2015-04-17 12:55:38 +100017PASS=0
Sean Dague53753292014-12-04 19:38:15 -050018FAILED_FUNCS=""
19
Ian Wienand165afa22015-05-25 11:29:48 +100020# pass a test, printing out MSG
21# usage: passed message
Ian Wienand1cb809d2015-04-17 12:55:38 +100022function passed {
23 local lineno=$(caller 0 | awk '{print $1}')
24 local function=$(caller 0 | awk '{print $2}')
25 local msg="$1"
26 if [ -z "$msg" ]; then
27 msg="OK"
28 fi
29 PASS=$((PASS+1))
Ian Wienand165afa22015-05-25 11:29:48 +100030 echo "PASS: $function:L$lineno $msg"
Ian Wienand1cb809d2015-04-17 12:55:38 +100031}
32
Ian Wienand165afa22015-05-25 11:29:48 +100033# fail a test, printing out MSG
34# usage: failed message
Ian Wienand1cb809d2015-04-17 12:55:38 +100035function failed {
36 local lineno=$(caller 0 | awk '{print $1}')
37 local function=$(caller 0 | awk '{print $2}')
38 local msg="$1"
39 FAILED_FUNCS+="$function:L$lineno\n"
40 echo "ERROR: $function:L$lineno!"
41 echo " $msg"
42 ERROR=$((ERROR+1))
43}
44
Ian Wienand165afa22015-05-25 11:29:48 +100045# assert string comparision of val1 equal val2, printing out msg
46# usage: assert_equal val1 val2 msg
Sean Dague53753292014-12-04 19:38:15 -050047function assert_equal {
48 local lineno=`caller 0 | awk '{print $1}'`
49 local function=`caller 0 | awk '{print $2}'`
50 local msg=$3
Ian Wienand165afa22015-05-25 11:29:48 +100051
52 if [ -z "$msg" ]; then
53 msg="OK"
54 fi
Sean Dague53753292014-12-04 19:38:15 -050055 if [[ "$1" != "$2" ]]; then
56 FAILED_FUNCS+="$function:L$lineno\n"
57 echo "ERROR: $1 != $2 in $function:L$lineno!"
58 echo " $msg"
Ian Wienand1cb809d2015-04-17 12:55:38 +100059 ERROR=$((ERROR+1))
Sean Dague53753292014-12-04 19:38:15 -050060 else
Ian Wienand1cb809d2015-04-17 12:55:38 +100061 PASS=$((PASS+1))
Ian Wienand165afa22015-05-25 11:29:48 +100062 echo "PASS: $function:L$lineno - $msg"
Sean Dague53753292014-12-04 19:38:15 -050063 fi
64}
65
Ian Wienand165afa22015-05-25 11:29:48 +100066# print a summary of passing and failing tests, exiting
67# with an error if we have failed tests
68# usage: report_results
Sean Dague53753292014-12-04 19:38:15 -050069function report_results {
Ian Wienand1cb809d2015-04-17 12:55:38 +100070 echo "$PASS Tests PASSED"
71 if [[ $ERROR -gt 1 ]]; then
72 echo
73 echo "The following $ERROR tests FAILED"
74 echo -e "$FAILED_FUNCS"
75 echo "---"
Sean Dague53753292014-12-04 19:38:15 -050076 exit 1
77 fi
78}