| 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 | 165afa2 | 2015-05-25 11:29:48 +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 | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 66 | # print a summary of passing and failing tests, exiting | 
 | 67 | # with an error if we have failed tests | 
 | 68 | #  usage: report_results | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 69 | function report_results { | 
| Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 70 |     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 Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 76 |         exit 1 | 
 | 77 |     fi | 
 | 78 | } |