Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1 | #!/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 |
| 16 | ERROR=0 |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 17 | PASS=0 |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 18 | FAILED_FUNCS="" |
| 19 | |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 20 | # pass a test, printing out MSG |
| 21 | # usage: passed message |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 22 | function 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 Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 30 | echo "PASS: $function:L$lineno - $msg" |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 31 | } |
| 32 | |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 33 | # fail a test, printing out MSG |
| 34 | # usage: failed message |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 35 | function 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 Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 45 | # assert string comparision of val1 equal val2, printing out msg |
| 46 | # usage: assert_equal val1 val2 msg |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 47 | function assert_equal { |
| 48 | local lineno=`caller 0 | awk '{print $1}'` |
| 49 | local function=`caller 0 | awk '{print $2}'` |
| 50 | local msg=$3 |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 51 | |
| 52 | if [ -z "$msg" ]; then |
| 53 | msg="OK" |
| 54 | fi |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 55 | if [[ "$1" != "$2" ]]; then |
| 56 | FAILED_FUNCS+="$function:L$lineno\n" |
| 57 | echo "ERROR: $1 != $2 in $function:L$lineno!" |
| 58 | echo " $msg" |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 59 | ERROR=$((ERROR+1)) |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 60 | else |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 61 | PASS=$((PASS+1)) |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 62 | echo "PASS: $function:L$lineno - $msg" |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 63 | fi |
| 64 | } |
| 65 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 66 | # assert variable is empty/blank, printing out msg |
| 67 | # usage: assert_empty VAR msg |
| 68 | function assert_empty { |
| 69 | local lineno=`caller 0 | awk '{print $1}'` |
| 70 | local function=`caller 0 | awk '{print $2}'` |
| 71 | local msg=$2 |
| 72 | |
| 73 | if [ -z "$msg" ]; then |
| 74 | msg="OK" |
| 75 | fi |
| 76 | if [[ ! -z ${!1} ]]; then |
| 77 | FAILED_FUNCS+="$function:L$lineno\n" |
| 78 | echo "ERROR: $1 not empty in $function:L$lineno!" |
| 79 | echo " $msg" |
| 80 | ERROR=$((ERROR+1)) |
| 81 | else |
| 82 | PASS=$((PASS+1)) |
| 83 | echo "PASS: $function:L$lineno - $msg" |
| 84 | fi |
| 85 | } |
| 86 | |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 87 | # print a summary of passing and failing tests, exiting |
| 88 | # with an error if we have failed tests |
| 89 | # usage: report_results |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 90 | function report_results { |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 91 | echo "$PASS Tests PASSED" |
| 92 | if [[ $ERROR -gt 1 ]]; then |
| 93 | echo |
| 94 | echo "The following $ERROR tests FAILED" |
| 95 | echo -e "$FAILED_FUNCS" |
| 96 | echo "---" |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 97 | exit 1 |
| 98 | fi |
| 99 | } |