diff --git a/test/helper.dst b/test/helper.dst new file mode 100644 index 00000000..b1797a67 --- /dev/null +++ b/test/helper.dst @@ -0,0 +1,23 @@ +# Helper code for running tests + +(var num-tests-passed 0) +(var num-tests-run 0) +(var suite-num 0) + +(defn assert [x e] + (++ num-tests-run) + (when x (++ num-tests-passed)) + (print (if x + " \e[32m✔\e[0m " + " \e[31m✘\e[0m ") e) + x) + +(defn start-suite [x] + (:= suite-num x) + (print "\nRunning test suite " x " tests...\n")) + +(defn end-suite [] + (print "\nTest suite " suite-num " finished.") + (print num-tests-passed " of " num-tests-run " tests passed.\n") + (if (not= num-tests-passed num-tests-run) (exit 1))) + diff --git a/test/suite0.dst b/test/suite0.dst index 4d342cf0..4aa0d429 100644 --- a/test/suite0.dst +++ b/test/suite0.dst @@ -18,20 +18,8 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -(print "\nRunning Suite 0 tests...\n") - -(var num-tests-passed 0) -(var num-tests-run 0) -(def assert (fn [x e] - (:= num-tests-run (+ 1 num-tests-run)) - (if x - (do - (print " \e[32m✔\e[0m " e) - (:= num-tests-passed (+ 1 num-tests-passed)) - x) - (do - (print " \e[31m✘\e[0m " e) - x)))) +(import "test/helper.dst") +(start-suite 0) (assert (= 10 (+ 1 2 3 4)) "addition") (assert (= -8 (- 1 2 3 4)) "subtraction") @@ -283,8 +271,5 @@ (++ i)) (assert (= i 6) "when macro")) +(end-suite) -# report - -(print "\n" num-tests-passed " of " num-tests-run " tests passed\n") -(if (not= num-tests-passed num-tests-run) (exit 1)) diff --git a/test/suite1.dst b/test/suite1.dst index d6d40e1e..efb6a42b 100644 --- a/test/suite1.dst +++ b/test/suite1.dst @@ -18,20 +18,8 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -(print "\nRunning Suite 1 Tests...\n") - -(var num-tests-passed 0) -(var num-tests-run 0) -(def assert (fn [x e] - (:= num-tests-run (+ 1 num-tests-run)) - (if x - (do - (print " \e[32m✔\e[0m " e) - (:= num-tests-passed (+ 1 num-tests-passed)) - x) - (do - (print " \e[31m✘\e[0m " e) - x)))) +(import "test/helper.dst") +(start-suite 1) (assert (= 400.0 (sqrt 160000)) "sqrt(160000)=400") (assert (= (real 400) (sqrt 160000)) "sqrt(160000)=400") @@ -52,5 +40,4 @@ (assert (= (myfun true) 8) "check do form regression") (assert (= (myfun false) 9) "check do form regression") -(print "\n" num-tests-passed " of " num-tests-run " tests passed\n") -(if (not= num-tests-passed num-tests-run) (exit 1)) +(end-suite)