From 9dac782c34da57b401a7513c01381c24cb8f0fef Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Sat, 8 Oct 2016 18:04:53 -0400 Subject: [PATCH] a couple of handy parts for unit testing. verify_correct_input reports if the input sent to it differs from an answer file. run_test_and_verify runs a test app (providing it with a known input file), gathers the test's output, then verifies that the output is what was expected in an answer file (leverages verify_correct_input). --- scripts/unit_test/run_test_and_verify.sh | 13 ++++++++ scripts/unit_test/verify_correct_input.sh | 37 +++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 scripts/unit_test/run_test_and_verify.sh create mode 100644 scripts/unit_test/verify_correct_input.sh diff --git a/scripts/unit_test/run_test_and_verify.sh b/scripts/unit_test/run_test_and_verify.sh new file mode 100644 index 00000000..cb037459 --- /dev/null +++ b/scripts/unit_test/run_test_and_verify.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# a simple unit test component that takes three parameters: the test to run, the +# input file to give that test, and the expected correct output file from the test. +# the script will complain if there is an error in the test output. otherwise it +# says nothing, but the script's return value can also be checked. + +to_run="$1"; shift +input_file="$1"; shift +answer_file="$1"; shift + +eval "$to_run" < "$input_file" | bash "$FEISTY_MEOW_SCRIPTS/unit_test/verify_correct_input.sh" "$answer_file" + diff --git a/scripts/unit_test/verify_correct_input.sh b/scripts/unit_test/verify_correct_input.sh new file mode 100644 index 00000000..28063eef --- /dev/null +++ b/scripts/unit_test/verify_correct_input.sh @@ -0,0 +1,37 @@ +#/bin/bash + +# a simple component of unit testing which verifies that the input matches the output. + +# the single parameter to the script is a file that contains the correct answer. + +source "$FEISTY_MEOW_SCRIPTS/core/functions.sh" + +answer_file="$1"; shift + +if [ -z "$answer_file" -o ! -f "$answer_file" ]; then + echo This script needs a parameter which is a valid file filled with the + echo correct version of the input. + exit 1 +fi + +input_save_file="$(mktemp "$TMP/zz_verify_input.XXXXXX")" + +while read line; do + echo $line >>"$input_save_file" +done + +diff -q "$input_save_file" "$answer_file" +if [ $? -ne 0 ]; then + sep 76 + echo "The provided input differs from the correct answer!" + echo -e "\nAnswer file has:\n==============" + cat "$answer_file" + echo -e "==============\nBut the input data has:\n==============" + cat "$input_save_file" + echo -e "==============" + sep 76 + false # set bad exit value. +fi + +exit $? + -- 2.34.1