summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authormo <mo.khan@gmail.com>2019-05-25 18:40:20 -0600
committermo <mo.khan@gmail.com>2019-05-25 18:40:20 -0600
commit859a8a20d006e82cc9d2fa7f2d6af36169f345c3 (patch)
tree1d19ad92fb3a50c101fe07ae638c5f5852593291 /test
parent1ce4711b658015a76f04418426abeefd5c9ad44c (diff)
test out problem 6
Diffstat (limited to 'test')
-rw-r--r--test/problem-6.bats16
-rw-r--r--test/test_helper.bash72
2 files changed, 88 insertions, 0 deletions
diff --git a/test/problem-6.bats b/test/problem-6.bats
index 77d64f5..6340545 100644
--- a/test/problem-6.bats
+++ b/test/problem-6.bats
@@ -1,6 +1,22 @@
#!/usr/bin/env bats
+load test_helper
+
@test "testing" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}
+
+@test "invoke with a single integer" {
+ run problem-6.sh 2
+
+ assert_success
+ #assert_equal "squares: 4\nsum: 2\n", $output
+}
+
+@test "invoke with multiple integers" {
+ run problem-6.sh 1 2 3 4 5 6 7 8 9 10
+
+ assert_success
+ #assert_equal "squares: 1 4 9 16 25 36 49 64 81 100\nsum: 55\n", $output
+}
diff --git a/test/test_helper.bash b/test/test_helper.bash
new file mode 100644
index 0000000..b4c49cf
--- /dev/null
+++ b/test/test_helper.bash
@@ -0,0 +1,72 @@
+export TMP="$BATS_TEST_DIRNAME/tmp"
+
+teardown() {
+ rm -fr "${TMP:?}"/*
+}
+
+assert() {
+ if ! "$@"; then
+ flunk "failed: $@"
+ fi
+}
+
+refute() {
+ if "$@"; then
+ flunk "expected to fail: $@"
+ fi
+}
+
+flunk() {
+ { if [ "$#" -eq 0 ]; then cat -
+ else echo "$@"
+ fi
+ } | sed "s:${TMP}:\${TMP}:g" >&2
+ return 1
+}
+
+assert_success() {
+ if [ "$status" -ne 0 ]; then
+ { echo "command failed with exit status $status"
+ echo "output: $output"
+ } | flunk
+ elif [ "$#" -gt 0 ]; then
+ assert_output "$1"
+ fi
+}
+
+assert_failure() {
+ if [ "$status" -eq 0 ]; then
+ flunk "expected failed exit status"
+ elif [ "$#" -gt 0 ]; then
+ assert_output "$1"
+ fi
+}
+
+assert_equal() {
+ if [ "$1" != "$2" ]; then
+ { echo "expected: $1"
+ echo "actual: $2"
+ } | flunk
+ fi
+}
+
+assert_output() {
+ local expected
+ if [ $# -eq 0 ]; then expected="$(cat -)"
+ else expected="$1"
+ fi
+ assert_equal "$expected" "$output"
+}
+
+assert_output_contains() {
+ local expected="$1"
+ if [ -z "$expected" ]; then
+ echo "assert_output_contains needs an argument" >&2
+ return 1
+ fi
+ echo "$output" | $(type -p ggrep grep | head -1) -F "$expected" >/dev/null || {
+ { echo "expected output to contain $expected"
+ echo "actual: $output"
+ } | flunk
+ }
+}