very simple generic unit test support

Any other programming language that is not covered in the Web Scripting category.

very simple generic unit test support

Postby hafner on Thu Apr 13, 2006 11:02 pm

Hi,

I'd like to show, how easy it is to do unit testing in the Scheme
programming language without additional tools or libraries. Here I've
written a few lines which are sufficient to be appropriate generic
test code:
Code: Select all
(define-macro (break-on-error?) #t)

(define-macro (deftest name testproc)
  `(define (,name)
     (mk-err-report-cond ,name (call/cc ,testproc))))

(define (runtests maintestproc)
  (call/cc
   (lambda (break)
     (define (check testproc)
       (define err-report (testproc))
       (when err-report
             (write err-report)
             (newline)
             (if (break-on-error?)
                 (break))))
     (maintestproc check))))

(define (mk-err-report-cond location details)
  (and location details
       `((tag . err-report)
         (location . ,location)
         (details . ,details))))
Here's a small example for (errornous) code to be tested:
Code: Select all
(define (my-double x)
  "not implemented")
(define (my-square x)
  (+ x x)) ;;wrong
The test may look somehow like that:
Code: Select all
(deftest test-my-double
  (lambda (return)
    (let ((res (my-double 3)))
      (if (not (number? res))
          (return 'not-a-number))
      (if (not (= res 6))
          (return 'oops!)))
    #f))

(deftest test-my-square
  (lambda (return)
    (let ((res (my-square 5)) (w 25))
      (if (not (number? res))
          (return 'not-a-number))
      (if (not (= res w))
          (return `(wrong: ,res should-be: ,w))))
    #f))

(runtests
(lambda (check)
   (check test-my-double)
   (check test-my-square)
   ))
The test report record format can be easily adapted by modifying
mk-err-report-cond; here it's just an easy association list
(convenient for further processing by Scheme procedures).

Depending on whether the first failing test shall break further test
execution or not, the definition of why break-on-error? may be
modified.

The unhygienic macro system (define-macro) is used rather than the
hygienic ones because of simplicity.

I'm used to have fun when reading Scheme source code, so I hope you
enjoy it, too!

Regards
Thomas
hafner
Contributer
Contributer
 
Posts: 73
Joined: Tue Mar 07, 2006 2:18 pm
Location: München (Germany)

Postby kotuk on Sun Apr 16, 2006 10:32 am

Can you sujest any unit-testing tool for C++?
kotuk
Contributer
Contributer
 
Posts: 24
Joined: Sat Apr 15, 2006 7:42 pm

huh?

Postby selket on Sun Apr 16, 2006 2:10 pm

Sorry, please forgive my ignorance, but what is unit testing?
selket
Contributer
Contributer
 
Posts: 44
Joined: Sat Apr 15, 2006 11:59 pm
Location: Chicago, IL

Re: huh?

Postby hafner on Mon Apr 17, 2006 9:37 pm

selket wrote:Sorry, please forgive my ignorance, but what is unit testing?
No problem, please see here: http://c2.com/cgi/wiki?UnitTest.
In fact I would like to practice these http://c2.com/cgi/wiki?ProgrammerTest, but unit test is more popular. Only the purpose makes the difference, so I wanted to avoid confusing people by using the expression programmer test.
Thomas
hafner
Contributer
Contributer
 
Posts: 73
Joined: Tue Mar 07, 2006 2:18 pm
Location: München (Germany)


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron