Lisp Me Unit

If you are going to use LispMe you will need a unit test framework. Here is a simple one I created. Put the following two blocks of code into two memos. The first is a set of tests I used to test LispMeUnit as I wrote it and are provided so that you can modify LispMeUnit yourself. --DonWells

updated August 27. Using lambda expressions as tests seems to work better. It was hard keeping the quotes in the right places.--DonWells

Does LispMe support DefMacro?? If so, there are lots of good CommonLisp testing idioms which would let you make this even prettier.

I added a macro to use instead of addTest.
Using LispMe Unit

It actually isn't that hard. Make sure you have the standard library loaded, because LispMeUnit needs it. (Generally you will be needing it anyway) To add a test it is as easy as:
 (defTest "Test Name"
   (assertFoo "Assertation 1 Name" args ... )
   ...
   (assertFoo "Assertation n Name" args ... ))

Then...
 (clearTests)
 (TestRunner)

It really isn't that hard.
 ; LispMeUnit unit tests
 ; evaluate (LispMeUnitTests?)
 ; no error messages should
 ; pop up, output should look
 ; reasonable.  First 4 tests
 ; run and all pass.  Then
 ; 14 tests run and 7 fail.

(define (LispMeUnitTests?) (allPassTests) (TestRunner) (if (not (= (length *tests*) 4)) (error "Should be 4 tests")) (if (not (null? *failures*)) (error "should not have any fails")) (newline) (allTests) (TestRunner) (if (not (= (length *tests*) 14)) (error "Should have 14 tests")) (if (not (= (length *failures*) 7)) (error "should have 7 fails")) (map (lambda (aFailure) (if (not (equal? "should fail" (substring (cdr aFailure) 0 11))) (error "Was not supposed to fail"))) *failures*) #n)

(define (allTests) (clearTests) (assertTrueTests) (call/ccTests) (functionTestTests) (assertFalseTests) (assertNullTests) (assertTests))

(define (assertTrueTests) (defTest "test assertTrue" (assertTrue "should be true" #t)) (defTest "Test assertTrue" (assertTrue "should fail" #f)))

(define (call/ccTests) (defTest "Test call/cc works" (assertTrue "should not be seen" #t) (assertTrue "should fail" #f) (assertTrue "should not be seen" #f)))

(define (functionTestTests) (addTest "Test adding a function" addingFunctionTest))

(define (addingFunctionTest) (assertTrue "shouldPass" #t))

(define (assertFalseTests) (defTest "test assertFalse" (assertFalse "should pass" #f)) (defTest "Test assertFalse" (assertTrue "should fail" #t)))

(define (assertNullTests) (defTest "test assertNull?" (assertNull? "should pass" '())) (defTest "test assertNull?" (assertNull? "should pass" '()) (assertNull? "should fail" #t)) (defTest "test assertNull?" (assertNull? "should fail" #f)) (defTest "test assertNull?" (assertNull? "should fail" '(a b c))))

(define (assertTests) (defTest "Test assert" (assert "eq passes" eq? #t #t)) (defTest "Test assert" (assert "should fail" eq? #t #f)) (defTest "Test assert" (assert "equal passes" equal? "abc" "abc")) (defTest "Test assert" (assert "should fail" eqv? "abc" "abc")))

(define (allPassTests) (clearTests) (defTest "test 100% works" (assertTrue "should be true" #t)) (defTest "test 100% works" (assertTrue "should pass" #t)) (defTest "test 100% works" (assertTrue "should pass" #t)) (addTest "test 100% works" addingFunctionTest))

 ; LispMeUnit
 ; written by Don Wells
 ; http://c2.com/cgi/wiki?LispMeUnit

;;; list of (testName . closure) (define *tests* '()) ;;; list of ((testName . closure) . failMessage) (define *failures* '()) ;;; continuation to exit a test due to failure (define *exitTest* '()) ;;; current (testName . closure) (define *test* '())

(define (TestRunner) (clearFailures) (for-each runTest *tests*) (testReport) #n)

(define (clearFailures) (set! *failures* '()))

(define (runTest aTest) (set! *test* aTest) (call/cc (lambda (exitTest) (set! *exitTest* exitTest) (display ".") ((cdr aTest)))))

(define (testReport) (newline) (if (null? *failures*) (displayPassReport) (displayFailReport)))

(define (displayPassReport) (display "OK (") (display (length *tests*)) (display " tests)"))

(define (displayFailReport) (display "Tests Run: ") (display (length *tests*)) (display " Failures: ") (display (length *failures*)) (for-each displayFailure *failures*))

(define (displayFailure aFailure) (newline) (display "-> ") (display (caar aFailure)) (display ": ") (display (cdr aFailure)))

(define (clearTests) (set! *tests* '()))

(define (addTest aName aTest) (set! *tests* (cons (cons aName aTest) *tests*)))

(macro (defTest args) (list 'addTest (cadr args) (cons 'lambda (cons '() (cddr args)))))

(define (assertTrue aMessage aBoolean) (if (not aBoolean) (fail aMessage)))

(define (assertFalse aMessage aBoolean) (if aBoolean (fail aMessage)))

(define (assertNull? aMessage anObject) (if (not (null? anObject)) (failedCompare aMessage '() anObject)))

(define (assert aMessage anOperation expectedObject anObject) (if (not (anOperation expectedObject anObject)) (failedCompare aMessage expectedObject anObject)))

(define (failedCompare aMessage expectedObject anObject) (fail (string-append aMessage ": Expected " (object->string expectedObject) " got " (object->string anObject))))

(define (fail aMessage) (set! *failures* (cons (cons *test* aMessage) *failures*)) (display "F") (*exitTest* #f))


See also: LispMe, LispMeObjects
CategoryScheme

EditText of this page (last edited January 22, 2006) or FindPage with title or text search