| 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 |  | 
| Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 49 | # assert string comparision of val1 equal val2, printing out msg | 
 | 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 | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 95 | # Print a summary of passing and failing tests and exit | 
 | 96 | # (with an error if we have failed tests) | 
| Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 97 | #  usage: report_results | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 98 | function report_results { | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 99 |     echo "$PASS Tests PASSED" | 
| Ian Wienand | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 100 |     if [[ $ERROR -gt 0 ]]; then | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 101 |         echo | 
 | 102 |         echo "The following $ERROR tests FAILED" | 
 | 103 |         echo -e "$FAILED_FUNCS" | 
 | 104 |         echo "---" | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 105 |         exit 1 | 
 | 106 |     fi | 
| Ian Wienand | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 107 |     exit 0 | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 108 | } |