Skip to content

Instantly share code, notes, and snippets.

@shakna-israel
Created July 1, 2016 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakna-israel/475577e62eb568ceb15c8902d3ac27c1 to your computer and use it in GitHub Desktop.
Save shakna-israel/475577e62eb568ceb15c8902d3ac27c1 to your computer and use it in GitHub Desktop.
Unit Testing Framework for Scheme
;
; Unit test framework for scheme, originally by http://c2.com/cgi/wiki?JohnClonts
; Heavily modified by me, https://github.com/shakna-israel
;
#|
This is a brain-dead simple unit-testing framework.
It differs wildly from John Clonts' which is where it began life (http://c2.com/cgi/wiki?SchemeUnit), but that was mainly aimed at number equivalency testing.
Basic Usage:
(check-equal? tested-expr expected-result testing-message)
(test-results)
You use multiple check-equal? expressions, and then finally call test-results at the end.
test-results gives you a simple breakdown, and exits the testing program, with either 0 if no failures, or 1 for the exit code if there are failures.
For example:
(check-equal? (+ 1 1) 2 "1 + 1 is 2.")
(check-equal? (+ 1 1) 1 "1 + 1 is 1.")
(test-results)
Which, when run, outputs (but coloured with ANSI codes):
PASS:
1 + 1 is 2.
FAILURE:
1 + 1 is 1.
Expected:
1
Received:
2
Results:
Pass: 1 out of 2
Fail: 1 out of 2
|#
(define test-passes 0)
(define test-fails 0)
(define test-totals 0)
(define test-results
(lambda ()
(display "\033[1mResults:\033[0m\n")
(display (string-append "\033[32mPass: \033[0m" (number->string test-passes) " out of " (number->string test-totals) "\n"))
(display (string-append "\033[31mFail: \033[0m" (number->string test-fails) " out of " (number->string test-totals)))
(display "\n")
(if (= test-fails 0)
(exit 0)
(exit 1)
)
)
)
(define reportmsg
(lambda (msg)
(set! test-totals (+ test-totals 1))
(set! test-passes (+ test-passes 1))
(print "\033[32mPASS: \033[0m")
(print msg)
(newline)
)
)
(define reporterr
(lambda (msg a b)
(set! test-totals (+ test-totals 1))
(set! test-fails (+ test-fails 1))
(print "\033[31mFAILURE: \033[0m")
(print msg)
(print "Expected:")
(print b)
(print "Received:")
(print a)
(newline)
)
)
(define check-equal-number?
(lambda (a b msg)
(if (= a b)
(reportmsg msg)
(reporterr msg a b)
)
)
)
(define check-equal-other?
(lambda (a b msg)
(if (equal? a b)
(reportmsg msg)
(reporterr msg a b)
)
)
)
(define check-equal?
(lambda (a b msg)
(cond
[(number? b) (check-equal-number? a b msg)]
[else (check-equal-other? a b msg)]
)
)
)
;
; End testing framework definition
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment