Permalink
Browse files

i can't believe i just wrote a bash unit testing framework

  • Loading branch information...
ninjudd committed Aug 30, 2012
1 parent 2eabacc commit 1a97b8d48c5a788f2d9ded20bbc7aa8add370585
Showing with 103 additions and 1 deletion.
  1. +2 −0 Makefile
  2. +1 −1 bin/drip
  3. +11 −0 test/drip/commands
  4. +89 −0 test/run
View
@@ -25,5 +25,7 @@ install: jar
release: jar
scp ${JAR} pom.xml clojars@clojars.org:
+test: jar
+ ./test/test
.PHONY: all jar compile clean install release
View
@@ -215,7 +215,7 @@ function run_main {
function run_drip_command {
case $drip_command in
version)
- echo "drip version \"$DRIP_VERSION\""
+ echo "drip version \"$DRIP_VERSION\"" >&2
java -version
;;
kill)
View
@@ -0,0 +1,11 @@
+function test_version_command {
+ testing "version command"
+
+ assert_match "^drip version" "$(../bin/drip version 2>&1)"
+
+ assert true
+ assert false
+}
+
+
+
View
@@ -0,0 +1,89 @@
+#!/usr/bin/env bash
+
+tests=0
+passed=0
+failed=0
+
+function passed {
+ let passed=passed+1
+}
+
+function failed {
+ let failed=failed+1
+ echo
+ echo " $failed) FAILURE in $test"
+ stacktrace 1
+}
+
+function assert {
+ if eval "$@"; then
+ passed
+ else
+ failed
+ echo " assertion failed: $@"
+ fi
+}
+
+function assert_equal {
+ if [ "$1" == "$2" ]; then
+ passed
+ else
+ failed
+ echo " expected: $1"
+ echo " actual: $2"
+ fi
+}
+
+function assert_match {
+ if [[ "$2" =~ $1 ]]; then
+ passed
+ else
+ failed
+ echo " match failed"
+ echo " expected =~ $1"
+ echo " actual $2"
+ fi
+}
+
+function stacktrace() {
+ let frame=$1+1
+
+ while caller=$(caller $frame); do
+ local line=$(expr "$caller" : '\([0-9]*\)')
+ local func=$(expr "$caller" : '[0-9]* \([^ ]*\)')
+ local file=$(expr "$caller" : '[0-9]* [^ ]* \([^ ]*\)')
+ echo " $file:$line in $func"
+ ((frame++))
+ done
+}
+
+function test_results {
+ echo
+ echo "Ran $tests tests containing $((passed + failed)) assertions."
+ echo "$failed failures."
+
+ if [ $failed != 0 ]; then
+ echo "Tests failed."
+ fi
+}
+
+function run_all_tests {
+ pushd $(dirname $0) > /dev/null
+ for test_file in */**; do
+ [[ $test_file =~ ^[[:alnum:]_/]*$ ]] || continue
+ echo
+ echo $0 $test_file
+ source $test_file
+
+ while read line; do
+ if test=$(expr "$line" : 'function \(test_[^ ]*\) {'); then
+ let tests=tests+1
+ eval $test
+ fi
+ done < $test_file
+ done
+ popd > /dev/null
+}
+
+run_all_tests
+test_results

0 comments on commit 1a97b8d

Please sign in to comment.