Skip to content

Commit d76eba5

Browse files
testing Added global_assert_errors count.
1 parent e080a37 commit d76eba5

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

testing/Readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Testing
2+
Returns non-zero exit code when assertion fails.
3+
Total count of assertion errors stored in `global_assert_errors`.
24

35
## test_assertions
46
Unit test functions

testing/test_assertions

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
global_assert_errors=0
4+
35
function assert_exited_with_success() {
46
local description
57
local exit_code
@@ -28,6 +30,7 @@ function assert_exited_with_success() {
2830
fi
2931
fi
3032

33+
(( global_assert_errors += ${exit_code} ))
3134
return ${exit_code}
3235
}
3336

@@ -61,7 +64,7 @@ function assert_contains_text() {
6164
if [[ ! "${text}" = *"${check}"* ]]
6265
then
6366
(( exit_code += 1 ))
64-
echo "Failure: ${check} not found" >&2
67+
echo "Failure: '${check}' not found" >&2
6568
fi
6669
done
6770

@@ -71,6 +74,8 @@ function assert_contains_text() {
7174
else
7275
echo "SUCCESS ${description}"
7376
fi
77+
78+
(( global_assert_errors += ${exit_code} ))
7479
fi
7580
fi
7681

testing/test_assertions_work

+5
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,27 @@ assert_result_fails_with_error_text "$(assert_contains_text description 'input'
2626

2727
if [ ${run_tests} -eq 1 ]
2828
then
29+
global_assert_errors=0
2930
echo
3031
echo '---Example tests---'
3132
echo 'This should succeed'
3233
laugh_maniacally_and_exit 0 >/dev/null
3334
assert_exited_with_success 'Happy laughter' ${?}
35+
echo "Global assert errors: ${global_assert_errors}"
3436
echo
3537
echo 'This should fail'
3638
laugh_maniacally_and_exit 1 >/dev/null
3739
assert_exited_with_success 'Sad laughter' ${?}
40+
echo "Global assert errors: ${global_assert_errors}"
3841
echo
3942
echo 'This should find some text'
4043
laugh_result=$(laugh_maniacally_and_exit 0)
4144
assert_exited_with_success 'Textual laughter' ${?}
4245
assert_contains_text 'laughed a lot' "${laugh_result}" 'muah' 'ha' 'phew'
46+
echo "Global assert errors: ${global_assert_errors}"
4347
echo
4448
echo 'This should fail to find yuck and muck'
4549
assert_contains_text 'laughed a lot text search' "${laugh_result}" 'muah' 'ha' 'phew' 'yuck' 'muck'
50+
echo "Global assert errors: ${global_assert_errors}"
4651
echo "And now the exit code is ${?}"
4752
fi

0 commit comments

Comments
 (0)