| 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 { | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 23 |     local lineno | 
 | 24 |     lineno=$(caller 0 | awk '{print $1}') | 
 | 25 |     local function | 
 | 26 |     function=$(caller 0 | awk '{print $2}') | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 27 |     local msg="$1" | 
 | 28 |     if [ -z "$msg" ]; then | 
 | 29 |         msg="OK" | 
 | 30 |     fi | 
 | 31 |     PASS=$((PASS+1)) | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 32 |     echo "PASS: $function:L$lineno - $msg" | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 33 | } | 
 | 34 |  | 
| Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 35 | # fail a test, printing out MSG | 
 | 36 | #  usage: failed message | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 37 | function failed { | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 38 |     local lineno | 
 | 39 |     lineno=$(caller 0 | awk '{print $1}') | 
 | 40 |     local function | 
 | 41 |     function=$(caller 0 | awk '{print $2}') | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 42 |     local msg="$1" | 
 | 43 |     FAILED_FUNCS+="$function:L$lineno\n" | 
 | 44 |     echo "ERROR: $function:L$lineno!" | 
 | 45 |     echo "   $msg" | 
 | 46 |     ERROR=$((ERROR+1)) | 
 | 47 | } | 
 | 48 |  | 
| Swapnil Kulkarni (coolsvap) | 7f0be4f | 2015-11-20 10:52:59 +0530 | [diff] [blame] | 49 | # assert string comparison of val1 equal val2, printing out msg | 
| Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 50 | #  usage: assert_equal val1 val2 msg | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 51 | function assert_equal { | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 52 |     local lineno | 
 | 53 |     lineno=`caller 0 | awk '{print $1}'` | 
 | 54 |     local function | 
 | 55 |     function=`caller 0 | awk '{print $2}'` | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 56 |     local msg=$3 | 
| Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 57 |  | 
 | 58 |     if [ -z "$msg" ]; then | 
 | 59 |         msg="OK" | 
 | 60 |     fi | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 61 |     if [[ "$1" != "$2" ]]; then | 
 | 62 |         FAILED_FUNCS+="$function:L$lineno\n" | 
 | 63 |         echo "ERROR: $1 != $2 in $function:L$lineno!" | 
 | 64 |         echo "  $msg" | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 65 |         ERROR=$((ERROR+1)) | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 66 |     else | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 67 |         PASS=$((PASS+1)) | 
| Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 68 |         echo "PASS: $function:L$lineno - $msg" | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 69 |     fi | 
 | 70 | } | 
 | 71 |  | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 72 | # assert variable is empty/blank, printing out msg | 
 | 73 | #  usage: assert_empty VAR msg | 
 | 74 | function assert_empty { | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 75 |     local lineno | 
 | 76 |     lineno=`caller 0 | awk '{print $1}'` | 
 | 77 |     local function | 
 | 78 |     function=`caller 0 | awk '{print $2}'` | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 79 |     local msg=$2 | 
 | 80 |  | 
 | 81 |     if [ -z "$msg" ]; then | 
 | 82 |         msg="OK" | 
 | 83 |     fi | 
 | 84 |     if [[ ! -z ${!1} ]]; then | 
 | 85 |         FAILED_FUNCS+="$function:L$lineno\n" | 
 | 86 |         echo "ERROR: $1 not empty in $function:L$lineno!" | 
 | 87 |         echo "  $msg" | 
 | 88 |         ERROR=$((ERROR+1)) | 
 | 89 |     else | 
 | 90 |         PASS=$((PASS+1)) | 
 | 91 |         echo "PASS: $function:L$lineno - $msg" | 
 | 92 |     fi | 
 | 93 | } | 
 | 94 |  | 
| Ian Wienand | 2ba36cd | 2015-11-12 13:52:36 +1100 | [diff] [blame] | 95 | # assert the arguments evaluate to true | 
 | 96 | #  assert_true "message" arg1 arg2 | 
 | 97 | function assert_true { | 
 | 98 |     local lineno | 
 | 99 |     lineno=`caller 0 | awk '{print $1}'` | 
 | 100 |     local function | 
 | 101 |     function=`caller 0 | awk '{print $2}'` | 
 | 102 |     local msg=$1 | 
 | 103 |     shift | 
 | 104 |  | 
 | 105 |     $@ | 
 | 106 |     if [ $? -eq 0 ]; then | 
 | 107 |         PASS=$((PASS+1)) | 
 | 108 |         echo "PASS: $function:L$lineno - $msg" | 
 | 109 |     else | 
 | 110 |         FAILED_FUNCS+="$function:L$lineno\n" | 
 | 111 |         echo "ERROR: test failed in $function:L$lineno!" | 
 | 112 |         echo "  $msg" | 
 | 113 |         ERROR=$((ERROR+1)) | 
 | 114 |     fi | 
 | 115 | } | 
 | 116 |  | 
 | 117 | # assert the arguments evaluate to false | 
 | 118 | #  assert_false "message" arg1 arg2 | 
 | 119 | function assert_false { | 
 | 120 |     local lineno | 
 | 121 |     lineno=`caller 0 | awk '{print $1}'` | 
 | 122 |     local function | 
 | 123 |     function=`caller 0 | awk '{print $2}'` | 
 | 124 |     local msg=$1 | 
 | 125 |     shift | 
 | 126 |  | 
 | 127 |     $@ | 
 | 128 |     if [ $? -eq 0 ]; then | 
 | 129 |         FAILED_FUNCS+="$function:L$lineno\n" | 
 | 130 |         echo "ERROR: test failed in $function:L$lineno!" | 
 | 131 |         echo "  $msg" | 
 | 132 |         ERROR=$((ERROR+1)) | 
 | 133 |     else | 
 | 134 |         PASS=$((PASS+1)) | 
 | 135 |         echo "PASS: $function:L$lineno - $msg" | 
 | 136 |     fi | 
 | 137 | } | 
 | 138 |  | 
 | 139 |  | 
| Ian Wienand | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 140 | # Print a summary of passing and failing tests and exit | 
 | 141 | # (with an error if we have failed tests) | 
| Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 142 | #  usage: report_results | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 143 | function report_results { | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 144 |     echo "$PASS Tests PASSED" | 
| Ian Wienand | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 145 |     if [[ $ERROR -gt 0 ]]; then | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 146 |         echo | 
 | 147 |         echo "The following $ERROR tests FAILED" | 
 | 148 |         echo -e "$FAILED_FUNCS" | 
 | 149 |         echo "---" | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 150 |         exit 1 | 
 | 151 |     fi | 
| Ian Wienand | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 152 |     exit 0 | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 153 | } |