xref: /openbmc/google-misc/nemora-postd/subprojects/ncsid/test/test_lib.sh (revision dab96f131fb3a46d93f1093feccc9095d8589ece)
1*# Copyright 2021 Google LLC
2*#
3*# Licensed under the Apache License, Version 2.0 (the "License");
4*# you may not use this file except in compliance with the License.
5*# You may obtain 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,
11*# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*# See the License for the specific language governing permissions and
13*# limitations under the License.
14*
15*# Compares two strings and prints out an error message if they are not equal
16*StrEq() {
17*  if [ "$1" != "$2" ]; then
18*    echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]} Mismatched strings" >&2
19*    echo "  Expected: $2" >&2
20*    echo "  Got:      $1" >&2
21*    exit 1
22*  fi
23*}
24*
25*TESTS=()
26*
27*# Runs tests and emits output specified by the Test Anything Protocol
28*# https://testanything.org/
29*TestAnythingMain() {
30*  set -o nounset
31*  set -o errexit
32*  set -o pipefail
33*
34*  echo "TAP version 13"
35*  echo "1..${#TESTS[@]}"
36*
37*  local i
38*  for ((i=0; i <${#TESTS[@]}; ++i)); do
39*    local t="${TESTS[i]}"
40*    local tap_i=$((i + 1))
41*    if ! "$t"; then
42*      printf "not "
43*    fi
44*    printf "ok %d - %s\n" "$tap_i" "$t"
45*  done
46*}
47*