Changeset 39584 in project
- Timestamp:
- 02/08/21 16:45:11 (3 weeks ago)
- Location:
- release/5/simple-tests
- Files:
-
- 6 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
release/5/simple-tests/tags/3.1/simple-tests.egg
r39512 r39584 4 4 (category testing) 5 5 (license "BSD") 6 (version " 2.3.2")6 (version "3.1") 7 7 (author "Juergen Lorenz") 8 (components (extension simple-tests 9 (csc-options "-O3" "-d0")))) 8 (component-options (csc-options "-O3" "-d1")) 9 (components (extension simple-tests))) 10 -
release/5/simple-tests/tags/3.1/simple-tests.scm
r39512 r39584 1 2 ; Author: Juergen Lorenz 3 ; ju (at) jugilo (dot) de 4 ; 5 ; Copyright (c) 2011-2021, Juergen Lorenz 1 ; Copyright (c) 2013-2021 , Juergen Lorenz, ju (at) jugilo (dot) de 6 2 ; All rights reserved. 7 3 ; … … 16 12 ; notice, this list of conditions and the following disclaimer in the 17 13 ; documentation and/or other materials provided with the distribution. 18 ;19 14 ; Neither the name of the author nor the names of its contributors may be 20 15 ; used to endorse or promote products derived from this software without 21 16 ; specific prior written permission. 22 ; 17 ; 23 18 ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 24 19 ; IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED … … 32 27 ; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 28 ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 ; 29 30 35 31 36 32 #|[ 37 33 This is a simple Unit Test Framework inspired by Peter Seibel's 38 34 "Practical Common Lisp" together with some routines which might be 39 useful for debugging. 40 A second test interface is added with version 2.0 41 ]|# 42 35 useful for debugging. It underwent several changes in the maintenance 36 process, most of them are now marked deprecated but are still there in 37 favor of backwards compatibility. 38 39 For the future, it's sufficient to use only the following six routines, 40 the parameter verbose? and the macros pe, ppp, check, make-tester and 41 test-all. 42 43 pe, ppp and check are mostly used in the development phase, make-tester 44 and test-all are the actual test routines to go into tests/run.scm. 45 46 A tester is a nullary predicate which produces a lot of information as 47 side-effects provided the parameter verbose? is true. These testers are 48 invoked in test-all. 49 50 pe is a combination of pretty-print and expand enhanced with additional 51 text; ppp pretty-prints a list of expressions and its values, while 52 check does the same but accompanies these computed values with expected 53 ones, allowing for local variables in the checks. 54 ]|# 43 55 44 56 (module simple-tests ( 45 ; common46 simple-tests57 verbose? 58 writeln 47 59 and? 48 writeln49 60 pe 61 xpr:val 50 62 ppp 51 63 ppp* 64 xpr:val* 52 65 ppp** 53 xpr:val54 xpr:val*66 (define-test *failures* *locations*) 67 (compound-test *failures* group-on-cdrs) 55 68 == 56 ; old interface57 define-test58 (compound-test group-on-cdrs)59 *locations*60 *failures*61 ; new interface62 69 check 63 70 define-checks 64 71 do-checks 65 (check-all check-all-proc) 72 define-tester 73 (test-all test-all-proc) 74 (check-all test-all-proc) 75 simple-tests 66 76 ) 67 77 68 (import scheme (chicken base) (chicken syntax) (chicken pretty-print)) 78 (import scheme 79 (only (chicken base) 80 print case-lambda cut chop exit receive make-parameter) 81 (only (chicken syntax) expand) 82 (only (chicken module) import-for-syntax) 83 (only (chicken pretty-print) pp) 84 ) 69 85 70 86 (import-for-syntax (only (chicken base) chop)) … … 72 88 ;;;;;; Common interface ;;;;;; 73 89 74 ;;; (simple-tests [sym]) 75 ;;; --------------------- 76 ;;; documentation procedure 77 (define simple-tests 78 (let ( 79 (signatures '((simple-tests 80 procedure: 81 (simple-tests sym ..) 82 "documentation procedure") 83 (and? 84 procedure: 85 (and? xpr ...) 86 "Pascal like and procedure") 87 (writeln 88 procedure: 89 (writeln xpr ....) 90 "write analog of print") 91 (pe 92 procedure: 93 (pe macro-code) 94 " composes pretty-print and expand") 95 (ppp 96 macro: 97 (ppp xpr ...) 98 " print each xpr quoted in a headline" 99 "and pretty-print xpr's computed value") 100 (ppp* 101 macro: 102 (ppp* xpr ypr . xpr-yprs) 103 "print each xpr quoted in a headline" 104 "and pretty-print xpr's computed and" 105 "expected value, ypr") 106 (ppp** 107 macro: 108 (ppp** ((var val) ...) xpr ypr . xpr-yprs) 109 "wraps ppp* into a let") 110 (xpr:val 111 macro: 112 (xpr:val xpr ...) 113 "alias to ppp") 114 (xpr:val* 115 macro: 116 (xpr:val* xpr ypr . xpr-yprs) 117 "alias to ppp*") 118 (== 119 procedure: 120 (==) 121 (== x) 122 (== type? type-equal?) 123 "generic type equality as curried procedure:" 124 "the first resets the local database," 125 "the second is the curried equality check" 126 "and the third adds a new equality procedure" 127 "to the local database") 128 129 (define-test 130 macro: 131 (define-test (name . parameters) form . forms) 132 "creates a test function") 133 (compound-test 134 macro: 135 (compound-test (name) test . tests) 136 "checks all tests created with define-test" 137 "and reports a summary of results") 138 139 (check 140 macro: 141 (check ((var val) ...) xpr ypr . xpr-yprs) 142 "compares xpr and ypr .... with == in the" 143 "environment defined by (var val) ...") 144 (define-checks 145 macro: 146 (define-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 147 "returns a unary predicate, name?," 148 "comparing xpr with ypr ...." 149 "and using var val ... within this checks." 150 "verbose? controls the reported results") 151 (do-checks 152 macro: 153 (do-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 154 "alias to define-checks") 155 (check-all 156 macro: 157 (check-all name check-xpr ....) 158 "checks all check-expressions created by do-check" 159 "and reports the results"))) 160 ) 161 (case-lambda 162 (() (map car signatures)) 163 ((sym) 164 (let ((pair (assq sym signatures))) 165 (if pair 166 (for-each print (cdr pair)) 167 (print "Choose one of " (map car signatures)))))))) 168 90 #|[ 91 (verbose? ..) 92 --- parameter --- 93 gets or sets the value of the parameter verbose? 94 ]|# 95 (define verbose? 96 (make-parameter #t 97 (lambda (x) 98 (if (not x) 99 x 100 #t)))) 101 102 #|[ 103 (writeln xpr ...) 104 --- procedure --- 105 write analog of print, expressions separated by whitespace 106 ]|# 169 107 (define (writeln . args) 170 108 (for-each (lambda (a) … … 174 112 (newline)) 175 113 176 ;;; (and? . xprs) 177 ;;; ------------- 178 ;;; non-short-circuited and which executes all side-effects 114 #|[ 115 (and? . xprs) 116 --- procedure --- 117 non-short-circuited and which executes all side-effects 118 ]|# 179 119 (define (and? . xprs) 180 120 (let ((result #t)) … … 183 123 result)) 184 124 185 ;;; (pe macro-code) 186 ;;; --------------- 187 ;;; composes pretty-print and expand 125 #|[ 126 (pe macro-code) 127 --- macro --- 128 composes pretty-print and expand with additional information 129 ]|# 188 130 (define (pe macro-code) 189 (pp (expand macro-code))) 190 191 #|[ 192 The following macro, xpr:val, pretty-prints the literal representation 193 of each of its arguments as well as their respective values. The call 194 to eval-when guarantees, that the whole expression does nothing in 195 compiled code. 196 ]|# 197 198 ;;; (xpr:val xpr ...) 199 ;;; ----------------- 200 ;;; print each xpr quoted in a headline and pretty-print xpr's computed 201 ;;; value. 131 (newline) 132 (print "Macro expansion:") 133 (print "----------------") 134 (pp macro-code) 135 (print "->") 136 (pp (expand macro-code)) 137 (print "----------------") 138 (newline)) 139 140 ;;; The following macro, xpr:val, pretty-prints the literal representation 141 ;;; of each of its arguments as well as their respective values. The call 142 ;;; to eval-when guarantees, that the whole expression does nothing in 143 ;;; compiled code. 144 145 #|[ 146 (xpr:val xpr ...) 147 --- macro --- 148 Deprecated! 149 Print each xpr quoted in a headline and pretty-print xpr's computed 150 value. 151 ]|# 202 152 (define-syntax xpr:val 203 153 (syntax-rules () … … 212 162 (else))))) 213 163 214 ;;; (ppp xpr ...) 215 ;;; ------------- 216 ;;; print each xpr quoted in a headline and pretty-print xpr's computed 217 ;;; value. Alias to xpr:val. 164 #|[ 165 (ppp xpr ...) 166 --- macro --- 167 print each xpr quoted in a headline and pretty-print xpr's computed 168 value. Alias to xpr:val. 169 ]|# 218 170 (define-syntax ppp 219 171 (syntax-rules () 220 172 ((_ xpr ...) 221 (xpr:val xpr ...)))) 173 (cond-expand 174 ((not compiling) 175 (begin (print "Computing " 'xpr " ...") 176 (pp xpr) 177 ) 178 ... 179 ) 180 (else))))) 222 181 223 182 (define-syntax help-ppp* ; internal … … 235 194 )) 236 195 ; 237 ;;;; (ppp* {xpr ypr} ...) 238 ;;; -------------------- 239 ;;; print each xpr quoted in a headline and pretty-print xpr's computed 240 ;;; and expected value, ypr. 196 #|[ 197 (ppp* {xpr ypr} ...) 198 --- macro --- 199 Deprecated! 200 Print each xpr quoted in a headline and pretty-print xpr's computed 201 and expected value, ypr. 202 ]|# 241 203 (define-syntax ppp* 242 204 (syntax-rules () … … 247 209 (else))))) 248 210 249 ;;; (xpr:val* {xpr ypr} ...) 250 ;;; ------------------------ 251 ;;; print each xpr quoted in a headline and pretty-print xpr's computed 252 ;;; and expected value, ypr. 253 ;;; Alias to ppp* 254 (define-syntax xpr:val* 211 #|[ 212 (xpr:val* {xpr ypr} ...) 213 --- macro --- 214 Deprecated! 215 Print each xpr quoted in a headline and pretty-print xpr's computed 216 and expected value, ypr. 217 Alias to ppp* 218 ]|# 219 (define-syntax xpr:val* ; deprecated 255 220 (syntax-rules () 256 221 ((_ . pairs) 257 222 (ppp* . pairs)))) 258 223 259 ;;; (ppp** ((var val) ...) xpr ypr . other xpr-ypr-pairs) 260 ;;; ----------------------------------------------------- 261 ;;; ppp* wrapped into a let 224 #|[ 225 (ppp** ((var val) ...) xpr ypr . other-xpr-ypr-pairs) 226 --- macro --- 227 Deprecated! 228 ppp* wrapped into a let 229 ]|# 262 230 (define-syntax ppp** 263 231 (syntax-rules () … … 268 236 ;;;;;;;; old interface ;;;;;;;;; 269 237 270 ;; helper macro because I don't want to export it 271 (define-syntax disp 238 (define-syntax disp ; internal 272 239 (syntax-rules () 273 240 ((_) … … 277 244 ;;; ------------------------ 278 245 ;;; reports succuss or failure of form and updates failures if necessary 279 (define-syntax report-result 246 (define-syntax report-result ; internal 280 247 (syntax-rules () 281 248 ((_ loc form) … … 299 266 #f))))) 300 267 301 ;;; (check-em . forms) ;; internal268 ;;; (check-em . forms) 302 269 ;;; ------------------ 303 270 ;;; report result of all forms 304 (define-syntax check-em 271 (define-syntax check-em ; internal 305 272 (syntax-rules () 306 273 ((_ form ...) … … 309 276 ...))))) 310 277 311 ;; internal helper 312 (define-syntax show-args 278 (define-syntax show-args ; internal 313 279 (syntax-rules () 314 280 ((_ (name arg ...)) … … 316 282 ((_ arg) arg))) 317 283 318 ;;; (define-test (name . parameters) form . forms) 319 ;;; ---------------------------------------------- 320 ;;; creates a test function 284 #|[ 285 (define-test (name . parameters) form . forms) 286 --- macro *locations* *failures* --- 287 Deprecated! 288 Creates a test function 289 ]|# 321 290 (define-syntax define-test 322 291 (syntax-rules () … … 329 298 ((check-em form . forms) *locations*)))))) 330 299 331 ;;; (compound-test (name) test . tests) 332 ;;; ----------------------------------- 333 ;;; invokes all tests and reports a summary 300 #|[ 301 (compound-test (name) test . tests) 302 --- macro group-on-cdrs *failures* --- 303 Deprecated! 304 Invokes all tests and reports a summary 305 ]|# 334 306 (define-syntax compound-test 335 307 (syntax-rules () 336 308 ((_ (name) test0 test1 ...) 337 309 (begin 310 (writeln "XXX" 'test0 test0 test1 ...) ;;;;; 311 (writeln "YYY" (and? test0 test1 ...)) ;;;;; 338 312 (print "\nTesting " 'name " ...") 339 313 (print "----------------------------") … … 362 336 (exit 1)))))))) 363 337 364 ;;; internal helper from bindings338 ;;; internal helper 365 339 (define (filter ok? lst) 366 340 (let loop ((lst lst) (yes '()) (no '())) … … 383 357 (loop no (cons yes result)))))) 384 358 359 ;;;*failures* 360 ;;; ---------- 361 ;;; Deprecated! 362 ;;; global variable 363 (define *failures* '()) 364 385 365 ;;; *locations* 386 366 ;;; ----------- 387 ;;; dynamic variable 367 ;;; Deprecated! 368 ;;; global variable 388 369 (define *locations* '()) 389 390 ;;; *failures*391 ;;; ----------392 ;;; global variable collecting failure information393 (define *failures* '())394 370 395 371 ;;;;;;; new interface ;;;;;;;;;;; … … 401 377 ; (string=? (symbol->string x) (symbol->string y))) 402 378 403 ;;; (==) 404 ;;; (== x) 405 ;;; (== type? type-equal?) 406 ;;; ---------------------- 407 ;;; generic type equality as curried procedure 379 #|[ 380 (==) 381 (== x) 382 (== type? type-equal?) 383 --- procedure --- 384 Deprecated! 385 Generic type equality as curried procedure 386 ]|# 408 387 (define == 409 388 (let* ((pairs (list (cons pair? (curry equal?)) … … 438 417 ;;; (check* ((var val) ...) xpr ypr . xpr-yprs) ;; internal 439 418 ;;; -------------------------------------------------------------- 419 (define-syntax check* ; internal 420 (ir-macro-transformer 421 (lambda (form inject compare?) 422 (let ((var-vals (cadr form)) 423 (xpr-yprs (cddr form)) 424 (select-failures 425 (lambda (pairs) 426 (let loop ((pairs pairs)) 427 (cond 428 ((null? pairs) '()) 429 ((caar pairs) (loop (cdr pairs))) 430 (else 431 (cons (car pairs) (loop (cdr pairs)))))))) 432 ) 433 `(lambda (verbose?) 434 (letrec ,var-vals 435 (let ((tests '())) 436 ,@(map (lambda (p) 437 `(begin 438 (let ((x ,(car p))) 439 ; protect against functions changing state 440 (when verbose? 441 (print "testing " ',(car p) " ...") 442 (print* "computed: ") (writeln x) 443 (print* "expected: ") (writeln ,(cadr p)) 444 ) 445 (set! tests 446 (cons (cons ((cut equal? <> x) ,(cadr p)) 447 ',(car p)) 448 tests))) 449 ;(cons (cons ((== x) ,(cadr p)) ',(car p)) 450 ; tests))) 451 )) 452 (chop xpr-yprs 2)) 453 (let ((fails (,select-failures (reverse tests)))) 454 (when verbose? 455 (print "Failed test expressions:") 456 (print "------------------------") 457 (if (null? fails) 458 (print "none") 459 (for-each print (map cdr fails)))) 460 (if (null? fails) #t #f))))) 461 )))) 440 462 ;(define-syntax check* 441 463 ; (er-macro-transformer … … 495 517 ; (,%if (,%null? ,%fails) #t #f))))) 496 518 ; )))) 497 (define-syntax check* 498 (ir-macro-transformer 499 (lambda (form inject compare?) 500 (let ((var-vals (cadr form)) 501 (xpr-yprs (cddr form)) 502 (select-failures 503 (lambda (pairs) 504 (let loop ((pairs pairs)) 505 (cond 506 ((null? pairs) '()) 507 ((caar pairs) (loop (cdr pairs))) 508 (else 509 (cons (car pairs) (loop (cdr pairs)))))))) 510 ) 511 `(lambda (verbose?) 512 (letrec ,var-vals 513 (let ((tests '())) 514 ,@(map (lambda (p) 515 `(begin 516 (let ((x ,(car p))) 517 ; protect against functions changing state 518 (when verbose? 519 (print "testing " ',(car p) " ...") 520 (print* "computed: ") (writeln x) 521 (print* "expected: ") (writeln ,(cadr p)) 522 ) 523 (set! tests 524 (cons (cons ((== x) ,(cadr p)) ',(car p)) 525 tests))) 526 )) 527 (chop xpr-yprs 2)) 528 (let ((fails (,select-failures (reverse tests)))) 529 (when verbose? 530 (print "Failed test expressions:") 531 (print "------------------------") 532 (if (null? fails) 533 (print "none") 534 (for-each print (map cdr fails)))) 535 (if (null? fails) #t #f))))) 536 )))) 537 ;;; (check ((var val) ...) xpr ypr . xpr-yprs) 538 ;;; ------------------------------------------ 539 ;;; compare xpr and ypr .... in sequence with == 540 ;;; in the environment defined by var val ... 519 #|[ 520 (check ((var val) ...) xpr ypr . xpr-yprs) 521 --- macro --- 522 Compare xpr and ypr .... in sequence with equal? 523 in the environment defined by var val ... 524 ]|# 541 525 (define-syntax check 542 526 (syntax-rules () … … 544 528 ((check* ((var val) ...) xpr ypr . xpr-yprs) #t)))) 545 529 546 ;;; (define-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 547 ;;; -------------------------------------------------------------- 548 ;;; returns a unary predicate, name?, comparing xpr with ypr .... 549 ;;; and using var val ... within this checks, 550 ;;; verbose? controls the reported summary. 530 #|[ 531 (define-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 532 --- macro --- 533 Deprecated! 534 Returns a unary predicate, name?, comparing xpr with ypr .... 535 and using var val ... within this checks, 536 verbose? controls the reported summary. 537 ]|# 551 538 (define-syntax define-checks 552 539 (ir-macro-transformer … … 572 559 ,@xpr-yprs) ,verbose?))))))))) 573 560 574 ;;; (do-checks (name? verbose? . var-val-pairs) xpr ypr . xpr-yprs) 575 ;;; --------------------------------------------------------------- 576 ;;; returns a unary predicate, name?, comparing xpr with ypr .... 577 ;;; and using var val ... within this checks, 578 ;;; alias to define-checks 561 #|[ 562 (do-checks (name? verbose? . var-val-pairs) xpr ypr . xpr-yprs) 563 --- macro --- 564 Deprecated! 565 Returns a unary predicate, name?, comparing xpr with ypr .... 566 and using var val ... within this checks, 567 alias to define-checks 568 ]|# 579 569 (define-syntax do-checks 580 570 (syntax-rules () … … 583 573 xpr ypr . xpr-ypr-pairs)))) 584 574 585 (define (check-all-proc name . test-name-pairs) ; internal to check-all 586 ; used internally in check-all, must be exported within check-all 575 #|[ 576 (define-tester (name? . var-vals) xpr ypr . xpr-yprs) 577 --- macro --- 578 Returns a thunk predicate, name?, comparing xpr with ypr .... 579 and using var val ... within this tests. 580 The parameter verbose? controls the reported summary, i. e. 581 the side effects. 582 ]|# 583 (define-syntax define-tester 584 (ir-macro-transformer 585 (lambda (form inject compare?) 586 (let ((header (cadr form)) 587 (xpr-yprs (cddr form))) 588 (let ((name (car header)) 589 (var-vals (cdr header))) 590 `(define (,name) 591 (when (verbose?) 592 (print "\nIn " ',name ":") 593 (print* "===" 594 (make-string (string-length 595 (symbol->string ',name)) #\=) 596 "=\n") 597 ) 598 ((check* ,(chop var-vals 2) ,@xpr-yprs) (verbose?)))))))) 599 600 (define (test-all-proc name . test-name-pairs) 601 ; used internally in test-all, must be exported within test-all 587 602 (let loop ((pairs (chop test-name-pairs 2)) (failures '())) 588 603 (cond … … 604 619 (loop (cdr pairs) (cons (cadar pairs) failures)))))) 605 620 606 ;;; (check-all Name check-xpr ....) 607 ;;; ------------------------------- 608 ;;; checks all check-expressions defined with define-checks 609 ;;; producing a list of failures and exiting with 0 or 1 621 #|[ 622 (test-all Name tester ....) 623 --- macro test-all-proc --- 624 invokes all testers defined with define-tester 625 producing a list of failures and exiting with 0 or 1 626 ]|# 627 (define-syntax test-all 628 (er-macro-transformer 629 (lambda (form rename compare?) 630 (let ((name (cadr form)) 631 (tests (cddr form)) 632 (%test-all-proc (rename 'test-all-proc)) 633 (%list (rename 'list)) 634 ) 635 `(,%test-all-proc ',name 636 ,@(apply append 637 (map (lambda (t) `((,t) '(,t))) 638 tests))))))) 639 640 #|[ 641 (check-all Name check-xpr ....) 642 --- macro test-all-proc --- 643 Deprecated! 644 checks all check-expressions defined with define-checks 645 producing a list of failures and exiting with 0 or 1 646 ]|# 610 647 (define-syntax check-all 611 648 (er-macro-transformer … … 613 650 (let ((name (cadr form)) 614 651 (checks (cddr form)) 615 (% check-all-proc (rename 'check-all-proc))652 (%test-all-proc (rename 'test-all-proc)) 616 653 ) 617 `(,% check-all-proc ',name654 `(,%test-all-proc ',name 618 655 ,@(apply append 619 656 (map (lambda (t) `(,t ',t)) 620 657 checks))))))) 621 ) ; simple-tests 622 623 ;(import simple-tests) 624 ; 625 ;(pe '(check ((lst '(0 1 2))) 626 ; (car lst) 627 ; 0 628 ; (cdr lst) 629 ; '(1 2))) 630 ; 631 ;(check ((lst '(0 1 2))) 632 ; (car lst) 633 ; 0 634 ; (cdr lst) 635 ; '(0 1 2)) 636 ; 637 ;(pe '(define-checks (foo verbose? lst '(0 1 2)) 638 ; (car lst) 639 ; 0 640 ; (cdr lst) 641 ; '(1 2))) 642 ;(define-checks (foo verbose? lst '(0 1 2)) 643 ; (car lst) 644 ; 0 645 ; (cdr lst) 646 ; '(1 2 3)) 647 ;(foo #t) 648 ;(ppp (foo #f)) 658 659 #|[ 660 (simple-tests) 661 (simple-tests sym) 662 --- procedure --- 663 documentation procedure 664 ]|# 665 (define simple-tests 666 (let ( 667 (alist '( 668 (verbose? 669 parameter: 670 (verbose? ..) 671 "gets or sets the value of the parameter verbose?" 672 ) 673 (writeln 674 procedure: 675 (writeln xpr ...) 676 "write analog of print, expressions separated by whitespace" 677 ) 678 (and? 679 procedure: 680 (and? . xprs) 681 "non-short-circuited and which executes all side-effects" 682 ) 683 (pe 684 macro: 685 (pe macro-code) 686 "composes pretty-print and expand with additional information" 687 ) 688 (xpr:val 689 macro: 690 (xpr:val xpr ...) 691 "Deprecated!" 692 "Print each xpr quoted in a headline and pretty-print xpr's computed" 693 "value." 694 ) 695 (ppp 696 macro: 697 (ppp xpr ...) 698 "print each xpr quoted in a headline and pretty-print xpr's computed" 699 "value. Alias to xpr:val." 700 ) 701 (ppp* 702 macro: 703 (ppp* {xpr ypr} ...) 704 "Deprecated!" 705 "Print each xpr quoted in a headline and pretty-print xpr's computed" 706 "and expected value, ypr." 707 ) 708 (xpr:val* 709 macro: 710 (xpr:val* {xpr ypr} ...) 711 "Deprecated!" 712 "Print each xpr quoted in a headline and pretty-print xpr's computed" 713 "and expected value, ypr." 714 "Alias to ppp*" 715 ) 716 (ppp** 717 macro: 718 (ppp** ((var val) ...) xpr ypr . other-xpr-ypr-pairs) 719 "Deprecated!" 720 "ppp* wrapped into a let" 721 ) 722 (define-test 723 macro: 724 (define-test (name . parameters) form . forms) 725 "Deprecated!" 726 "Creates a test function" 727 ) 728 (compound-test 729 macro: 730 (compound-test (name) test . tests) 731 "Deprecated!" 732 "Invokes all tests and reports a summary" 733 ) 734 (== 735 procedure: 736 (==) 737 (== x) 738 (== type? type-equal?) 739 "Deprecated!" 740 "Generic type equality as curried procedure" 741 ) 742 (check 743 macro: 744 (check ((var val) ...) xpr ypr . xpr-yprs) 745 "Compare xpr and ypr .... in sequence with equal?" 746 "in the environment defined by var val ..." 747 ) 748 (define-checks 749 macro: 750 (define-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 751 "Deprecated!" 752 "Returns a unary predicate, name?, comparing xpr with ypr ...." 753 "and using var val ... within this checks," 754 "verbose? controls the reported summary." 755 ) 756 (do-checks 757 macro: 758 (do-checks (name? verbose? . var-val-pairs) xpr ypr . xpr-yprs) 759 "Deprecated!" 760 "Returns a unary predicate, name?, comparing xpr with ypr ...." 761 "and using var val ... within this checks," 762 "alias to define-checks" 763 ) 764 (define-tester 765 macro: 766 (define-tester (name? . var-vals) xpr ypr . xpr-yprs) 767 "Returns a thunk predicate, name?, comparing xpr with ypr ...." 768 "and using var val ... within this tests." 769 "The parameter verbose? controls the reported summary, i. e." 770 "the side effects." 771 ) 772 (test-all 773 macro: 774 (test-all Name tester ....) 775 "invokes all testers defined with define-tester" 776 "producing a list of failures and exiting with 0 or 1" 777 ) 778 (check-all 779 macro: 780 (check-all Name check-xpr ....) 781 "Deprecated!" 782 "checks all check-expressions defined with define-checks" 783 "producing a list of failures and exiting with 0 or 1" 784 ) 785 (simple-tests 786 procedure: 787 (simple-tests) 788 (simple-tests sym) 789 "with sym: documentation of exported symbol" 790 "without sym: list of exported symbols" 791 ) 792 )) 793 ) 794 (case-lambda 795 (() (map car alist)) 796 ((sym) 797 (let ((pair (assq sym alist))) 798 (if pair 799 (for-each print (cdr pair)) 800 (print "Choose one of " (map car alist)))))))) 801 ) -
release/5/simple-tests/tags/3.1/tests/run.scm
r39187 r39584 1 1 (import simple-tests) 2 2 3 ;;; old interface 4 5 (define-test (bar n) 6 (positive? n) 7 (even? n)) 8 9 (define-test (foo x y) 10 (< x y) 11 "COMMENT" 12 (bar 4) 13 (odd? 3) 14 (positive? 3)) 15 16 (define-test (++) 17 (= (+ 1 2) 3) 18 (= (+ 1 2 3) 6)) 19 20 (define-test (**) 21 (= (* 1 2) 2) 22 (= (* 1 2 3) 6)) 23 24 (define-test (arithmetic) 25 (++) 26 (**)) 27 28 (define-test (baz) 29 (and? #t #t #t) 30 (and?) 31 (not (and? #t #f #t))) 32 33 34 ;(compound-test (simple-tests) 35 ; (baz) 36 ; (arithmetic?) 37 ; (foo 1 2) 38 ; ) 39 40 ;;; new interface 3 (import (only (chicken base) parameterize print case-lambda)) 41 4 42 5 (newline) 43 (print "check")44 (print "=====")45 (check ((lst '(0 1 2 3)))46 lst47 '(0 1 2 3)48 (car lst)49 050 (cadr lst)51 152 (cddr lst)53 '(2 3)54 )55 6 56 ;;; define-checks is an alias to do-checks 57 (do-checks (bar? verbose? n 5) 58 (positive? n) #t 59 (even? n) #f) 7 (define-test (bar n) (positive? n) (even? n)) 60 8 61 (define-checks (+? verbose?) 62 (+ 1 2) 3 63 (+ 1 2 3) 6) 9 (bar 5) 64 10 65 (define-checks (*? verbose?) 66 (* 1 2) 2 67 (* 1 2 3) 6) 11 (define-test (foo x y) (< x y) "COMMENT" (bar 4) (odd? 3) (positive? 3)) 68 12 69 (define-checks (arithmetic? verbose?) 70 (+? #f) #t 71 (*? #f) #t) 13 (foo 1 2) 72 14 73 (do-checks (baz? verbose?) 74 (and? #t #t #t) #t 75 (and?) #t 76 (and? #t #f #t) #f) 15 (define-test (++) (= (+ 1 2) 3) (= (+ 1 2 3) 6)) 77 16 78 (define-checks (qux? verbose?) 79 ((== "x") "y") #f 80 ((== "x") "x") #t 81 ((== baz?) baz?) #t 82 ((== baz?) bar) #f 83 ((== '()) '()) #t 84 ((== 'x) 'y) #f 85 ((== 'x) 'x) #t 86 ((== #(0 1 2)) #(0 1 2)) #t 87 ((== #(0 1 2)) '(0 1 2)) #f 88 ) 17 (++) 18 19 (define-test (**) (= (* 1 2) 2) (= (* 1 2 3) 6)) 20 21 (**) 22 23 (define-test (arithmetic) (++) (**)) 24 25 (arithmetic) 26 27 (define-test (baz) (and? #t #t #t) (and?) (not (and? #t #f #t))) 28 29 (baz) 30 31 '(compound-test (OLD) (bar 5) (foo 1 2) (++) (**) (arithmetic) (baz)) 32 33 (newline) 34 35 (do-checks (bar? verbose? n 5) (positive? n) #t (even? n) #f) 36 37 (bar?) 38 39 (define-checks (+? verbose?) (+ 1 2) 3 (+ 1 2 3) 6) 40 41 (+?) 42 43 (define-checks (*? verbose?) (* 1 2) 2 (* 1 2 3) 6) 44 45 (*?) 46 47 (define-checks (arithmetic? verbose?) (+? #f) #t (*? #f) #t) 48 49 (arithmetic?) 50 51 (do-checks (baz? verbose?) (and? #t #t #t) #t (and?) #t (and? #t #f #t) #f) 52 53 (baz?) 54 55 (define-checks 56 (qux? verbose?) 57 ((== "x") "y") 58 #f 59 ((== "x") "x") 60 #t 61 ((== baz?) baz?) 62 #t 63 ((== baz?) bar?) 64 #f 65 ((== '()) '()) 66 #t 67 ((== 'x) 'y) 68 #f 69 ((== 'x) 'x) 70 #t 71 ((== #(0 1 2)) #(0 1 2)) 72 #t 73 ((== #(0 1 2)) '(0 1 2)) 74 #f) 75 76 (qux?) 89 77 90 78 (define counter 91 (let ((n 0)) 92 (lambda () 93 (set! n (add1 n)) 94 n))) 79 (let ((n 0)) (case-lambda (() (set! n (add1 n)) n) ((k) (set! n k) n)))) 95 80 96 (define-checks (counter? verbose?) 97 (counter) 1 98 (counter) 2 99 (counter) 3 100 (counter) 4 81 (define-checks 82 (counter? verbose?) 83 (counter 0) 84 0 85 (counter) 86 1 87 (counter) 88 2 89 (counter) 90 3 91 (counter) 92 4) 93 94 (counter?) 95 96 '(check-all NEW (bar?) (*?) (+?) (arithmetic?) (baz?) (qux?) (counter?)) 97 98 (define-tester (Bar? n 5) (positive? n) #t (even? n) #f) 99 100 (define-tester (Plus?) (+ 1 2) 3 (+ 1 2 3) 6) 101 102 (define-tester (Times?) (* 1 2) 2 (* 1 2 3) 6) 103 104 (define-tester 105 (Arithmetic?) 106 (parameterize ((verbose? #f)) (Plus?)) 107 #t 108 (parameterize ((verbose? #f)) (Times?)) 109 #t) 110 111 (define Counter 112 (let ((n 0)) (case-lambda (() (set! n (add1 n)) n) ((k) (set! n k) n)))) 113 114 (define-tester 115 (Counter?) 116 (Counter 0) 117 0 118 (Counter) 119 1 120 (Counter) 121 2 122 (Counter) 123 3 124 (Counter) 125 4 126 (Counter 0) 127 0) 128 129 '(test-all SIMPLE-TESTS Bar? Times? Plus? Arithmetic? Counter?) 130 131 (test-all SIMPLE-TESTS 132 Bar? 133 Plus? 134 Times? 135 Arithmetic? 136 Counter? 101 137 ) 102 103 104 (check-all SIMPLE (bar?) (*?) (+?) (arithmetic?) (baz?) (qux?)105 (counter?)) -
release/5/simple-tests/trunk/simple-tests.egg
r39512 r39584 4 4 (category testing) 5 5 (license "BSD") 6 (version " 2.3.2")6 (version "3.1") 7 7 (author "Juergen Lorenz") 8 (components (extension simple-tests 9 (csc-options "-O3" "-d0")))) 8 (component-options (csc-options "-O3" "-d1")) 9 (components (extension simple-tests))) 10 -
release/5/simple-tests/trunk/simple-tests.scm
r39512 r39584 1 2 ; Author: Juergen Lorenz 3 ; ju (at) jugilo (dot) de 4 ; 5 ; Copyright (c) 2011-2021, Juergen Lorenz 1 ; Copyright (c) 2013-2021 , Juergen Lorenz, ju (at) jugilo (dot) de 6 2 ; All rights reserved. 7 3 ; … … 16 12 ; notice, this list of conditions and the following disclaimer in the 17 13 ; documentation and/or other materials provided with the distribution. 18 ;19 14 ; Neither the name of the author nor the names of its contributors may be 20 15 ; used to endorse or promote products derived from this software without 21 16 ; specific prior written permission. 22 ; 17 ; 23 18 ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 24 19 ; IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED … … 32 27 ; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 28 ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 ; 29 30 35 31 36 32 #|[ 37 33 This is a simple Unit Test Framework inspired by Peter Seibel's 38 34 "Practical Common Lisp" together with some routines which might be 39 useful for debugging. 40 A second test interface is added with version 2.0 41 ]|# 42 35 useful for debugging. It underwent several changes in the maintenance 36 process, most of them are now marked deprecated but are still there in 37 favor of backwards compatibility. 38 39 For the future, it's sufficient to use only the following six routines, 40 the parameter verbose? and the macros pe, ppp, check, make-tester and 41 test-all. 42 43 pe, ppp and check are mostly used in the development phase, make-tester 44 and test-all are the actual test routines to go into tests/run.scm. 45 46 A tester is a nullary predicate which produces a lot of information as 47 side-effects provided the parameter verbose? is true. These testers are 48 invoked in test-all. 49 50 pe is a combination of pretty-print and expand enhanced with additional 51 text; ppp pretty-prints a list of expressions and its values, while 52 check does the same but accompanies these computed values with expected 53 ones, allowing for local variables in the checks. 54 ]|# 43 55 44 56 (module simple-tests ( 45 ; common46 simple-tests57 verbose? 58 writeln 47 59 and? 48 writeln49 60 pe 61 xpr:val 50 62 ppp 51 63 ppp* 64 xpr:val* 52 65 ppp** 53 xpr:val54 xpr:val*66 (define-test *failures* *locations*) 67 (compound-test *failures* group-on-cdrs) 55 68 == 56 ; old interface57 define-test58 (compound-test group-on-cdrs)59 *locations*60 *failures*61 ; new interface62 69 check 63 70 define-checks 64 71 do-checks 65 (check-all check-all-proc) 72 define-tester 73 (test-all test-all-proc) 74 (check-all test-all-proc) 75 simple-tests 66 76 ) 67 77 68 (import scheme (chicken base) (chicken syntax) (chicken pretty-print)) 78 (import scheme 79 (only (chicken base) 80 print case-lambda cut chop exit receive make-parameter) 81 (only (chicken syntax) expand) 82 (only (chicken module) import-for-syntax) 83 (only (chicken pretty-print) pp) 84 ) 69 85 70 86 (import-for-syntax (only (chicken base) chop)) … … 72 88 ;;;;;; Common interface ;;;;;; 73 89 74 ;;; (simple-tests [sym]) 75 ;;; --------------------- 76 ;;; documentation procedure 77 (define simple-tests 78 (let ( 79 (signatures '((simple-tests 80 procedure: 81 (simple-tests sym ..) 82 "documentation procedure") 83 (and? 84 procedure: 85 (and? xpr ...) 86 "Pascal like and procedure") 87 (writeln 88 procedure: 89 (writeln xpr ....) 90 "write analog of print") 91 (pe 92 procedure: 93 (pe macro-code) 94 " composes pretty-print and expand") 95 (ppp 96 macro: 97 (ppp xpr ...) 98 " print each xpr quoted in a headline" 99 "and pretty-print xpr's computed value") 100 (ppp* 101 macro: 102 (ppp* xpr ypr . xpr-yprs) 103 "print each xpr quoted in a headline" 104 "and pretty-print xpr's computed and" 105 "expected value, ypr") 106 (ppp** 107 macro: 108 (ppp** ((var val) ...) xpr ypr . xpr-yprs) 109 "wraps ppp* into a let") 110 (xpr:val 111 macro: 112 (xpr:val xpr ...) 113 "alias to ppp") 114 (xpr:val* 115 macro: 116 (xpr:val* xpr ypr . xpr-yprs) 117 "alias to ppp*") 118 (== 119 procedure: 120 (==) 121 (== x) 122 (== type? type-equal?) 123 "generic type equality as curried procedure:" 124 "the first resets the local database," 125 "the second is the curried equality check" 126 "and the third adds a new equality procedure" 127 "to the local database") 128 129 (define-test 130 macro: 131 (define-test (name . parameters) form . forms) 132 "creates a test function") 133 (compound-test 134 macro: 135 (compound-test (name) test . tests) 136 "checks all tests created with define-test" 137 "and reports a summary of results") 138 139 (check 140 macro: 141 (check ((var val) ...) xpr ypr . xpr-yprs) 142 "compares xpr and ypr .... with == in the" 143 "environment defined by (var val) ...") 144 (define-checks 145 macro: 146 (define-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 147 "returns a unary predicate, name?," 148 "comparing xpr with ypr ...." 149 "and using var val ... within this checks." 150 "verbose? controls the reported results") 151 (do-checks 152 macro: 153 (do-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 154 "alias to define-checks") 155 (check-all 156 macro: 157 (check-all name check-xpr ....) 158 "checks all check-expressions created by do-check" 159 "and reports the results"))) 160 ) 161 (case-lambda 162 (() (map car signatures)) 163 ((sym) 164 (let ((pair (assq sym signatures))) 165 (if pair 166 (for-each print (cdr pair)) 167 (print "Choose one of " (map car signatures)))))))) 168 90 #|[ 91 (verbose? ..) 92 --- parameter --- 93 gets or sets the value of the parameter verbose? 94 ]|# 95 (define verbose? 96 (make-parameter #t 97 (lambda (x) 98 (if (not x) 99 x 100 #t)))) 101 102 #|[ 103 (writeln xpr ...) 104 --- procedure --- 105 write analog of print, expressions separated by whitespace 106 ]|# 169 107 (define (writeln . args) 170 108 (for-each (lambda (a) … … 174 112 (newline)) 175 113 176 ;;; (and? . xprs) 177 ;;; ------------- 178 ;;; non-short-circuited and which executes all side-effects 114 #|[ 115 (and? . xprs) 116 --- procedure --- 117 non-short-circuited and which executes all side-effects 118 ]|# 179 119 (define (and? . xprs) 180 120 (let ((result #t)) … … 183 123 result)) 184 124 185 ;;; (pe macro-code) 186 ;;; --------------- 187 ;;; composes pretty-print and expand 125 #|[ 126 (pe macro-code) 127 --- macro --- 128 composes pretty-print and expand with additional information 129 ]|# 188 130 (define (pe macro-code) 189 (pp (expand macro-code))) 190 191 #|[ 192 The following macro, xpr:val, pretty-prints the literal representation 193 of each of its arguments as well as their respective values. The call 194 to eval-when guarantees, that the whole expression does nothing in 195 compiled code. 196 ]|# 197 198 ;;; (xpr:val xpr ...) 199 ;;; ----------------- 200 ;;; print each xpr quoted in a headline and pretty-print xpr's computed 201 ;;; value. 131 (newline) 132 (print "Macro expansion:") 133 (print "----------------") 134 (pp macro-code) 135 (print "->") 136 (pp (expand macro-code)) 137 (print "----------------") 138 (newline)) 139 140 ;;; The following macro, xpr:val, pretty-prints the literal representation 141 ;;; of each of its arguments as well as their respective values. The call 142 ;;; to eval-when guarantees, that the whole expression does nothing in 143 ;;; compiled code. 144 145 #|[ 146 (xpr:val xpr ...) 147 --- macro --- 148 Deprecated! 149 Print each xpr quoted in a headline and pretty-print xpr's computed 150 value. 151 ]|# 202 152 (define-syntax xpr:val 203 153 (syntax-rules () … … 212 162 (else))))) 213 163 214 ;;; (ppp xpr ...) 215 ;;; ------------- 216 ;;; print each xpr quoted in a headline and pretty-print xpr's computed 217 ;;; value. Alias to xpr:val. 164 #|[ 165 (ppp xpr ...) 166 --- macro --- 167 print each xpr quoted in a headline and pretty-print xpr's computed 168 value. Alias to xpr:val. 169 ]|# 218 170 (define-syntax ppp 219 171 (syntax-rules () 220 172 ((_ xpr ...) 221 (xpr:val xpr ...)))) 173 (cond-expand 174 ((not compiling) 175 (begin (print "Computing " 'xpr " ...") 176 (pp xpr) 177 ) 178 ... 179 ) 180 (else))))) 222 181 223 182 (define-syntax help-ppp* ; internal … … 235 194 )) 236 195 ; 237 ;;;; (ppp* {xpr ypr} ...) 238 ;;; -------------------- 239 ;;; print each xpr quoted in a headline and pretty-print xpr's computed 240 ;;; and expected value, ypr. 196 #|[ 197 (ppp* {xpr ypr} ...) 198 --- macro --- 199 Deprecated! 200 Print each xpr quoted in a headline and pretty-print xpr's computed 201 and expected value, ypr. 202 ]|# 241 203 (define-syntax ppp* 242 204 (syntax-rules () … … 247 209 (else))))) 248 210 249 ;;; (xpr:val* {xpr ypr} ...) 250 ;;; ------------------------ 251 ;;; print each xpr quoted in a headline and pretty-print xpr's computed 252 ;;; and expected value, ypr. 253 ;;; Alias to ppp* 254 (define-syntax xpr:val* 211 #|[ 212 (xpr:val* {xpr ypr} ...) 213 --- macro --- 214 Deprecated! 215 Print each xpr quoted in a headline and pretty-print xpr's computed 216 and expected value, ypr. 217 Alias to ppp* 218 ]|# 219 (define-syntax xpr:val* ; deprecated 255 220 (syntax-rules () 256 221 ((_ . pairs) 257 222 (ppp* . pairs)))) 258 223 259 ;;; (ppp** ((var val) ...) xpr ypr . other xpr-ypr-pairs) 260 ;;; ----------------------------------------------------- 261 ;;; ppp* wrapped into a let 224 #|[ 225 (ppp** ((var val) ...) xpr ypr . other-xpr-ypr-pairs) 226 --- macro --- 227 Deprecated! 228 ppp* wrapped into a let 229 ]|# 262 230 (define-syntax ppp** 263 231 (syntax-rules () … … 268 236 ;;;;;;;; old interface ;;;;;;;;; 269 237 270 ;; helper macro because I don't want to export it 271 (define-syntax disp 238 (define-syntax disp ; internal 272 239 (syntax-rules () 273 240 ((_) … … 277 244 ;;; ------------------------ 278 245 ;;; reports succuss or failure of form and updates failures if necessary 279 (define-syntax report-result 246 (define-syntax report-result ; internal 280 247 (syntax-rules () 281 248 ((_ loc form) … … 299 266 #f))))) 300 267 301 ;;; (check-em . forms) ;; internal268 ;;; (check-em . forms) 302 269 ;;; ------------------ 303 270 ;;; report result of all forms 304 (define-syntax check-em 271 (define-syntax check-em ; internal 305 272 (syntax-rules () 306 273 ((_ form ...) … … 309 276 ...))))) 310 277 311 ;; internal helper 312 (define-syntax show-args 278 (define-syntax show-args ; internal 313 279 (syntax-rules () 314 280 ((_ (name arg ...)) … … 316 282 ((_ arg) arg))) 317 283 318 ;;; (define-test (name . parameters) form . forms) 319 ;;; ---------------------------------------------- 320 ;;; creates a test function 284 #|[ 285 (define-test (name . parameters) form . forms) 286 --- macro *locations* *failures* --- 287 Deprecated! 288 Creates a test function 289 ]|# 321 290 (define-syntax define-test 322 291 (syntax-rules () … … 329 298 ((check-em form . forms) *locations*)))))) 330 299 331 ;;; (compound-test (name) test . tests) 332 ;;; ----------------------------------- 333 ;;; invokes all tests and reports a summary 300 #|[ 301 (compound-test (name) test . tests) 302 --- macro group-on-cdrs *failures* --- 303 Deprecated! 304 Invokes all tests and reports a summary 305 ]|# 334 306 (define-syntax compound-test 335 307 (syntax-rules () 336 308 ((_ (name) test0 test1 ...) 337 309 (begin 310 (writeln "XXX" 'test0 test0 test1 ...) ;;;;; 311 (writeln "YYY" (and? test0 test1 ...)) ;;;;; 338 312 (print "\nTesting " 'name " ...") 339 313 (print "----------------------------") … … 362 336 (exit 1)))))))) 363 337 364 ;;; internal helper from bindings338 ;;; internal helper 365 339 (define (filter ok? lst) 366 340 (let loop ((lst lst) (yes '()) (no '())) … … 383 357 (loop no (cons yes result)))))) 384 358 359 ;;;*failures* 360 ;;; ---------- 361 ;;; Deprecated! 362 ;;; global variable 363 (define *failures* '()) 364 385 365 ;;; *locations* 386 366 ;;; ----------- 387 ;;; dynamic variable 367 ;;; Deprecated! 368 ;;; global variable 388 369 (define *locations* '()) 389 390 ;;; *failures*391 ;;; ----------392 ;;; global variable collecting failure information393 (define *failures* '())394 370 395 371 ;;;;;;; new interface ;;;;;;;;;;; … … 401 377 ; (string=? (symbol->string x) (symbol->string y))) 402 378 403 ;;; (==) 404 ;;; (== x) 405 ;;; (== type? type-equal?) 406 ;;; ---------------------- 407 ;;; generic type equality as curried procedure 379 #|[ 380 (==) 381 (== x) 382 (== type? type-equal?) 383 --- procedure --- 384 Deprecated! 385 Generic type equality as curried procedure 386 ]|# 408 387 (define == 409 388 (let* ((pairs (list (cons pair? (curry equal?)) … … 438 417 ;;; (check* ((var val) ...) xpr ypr . xpr-yprs) ;; internal 439 418 ;;; -------------------------------------------------------------- 419 (define-syntax check* ; internal 420 (ir-macro-transformer 421 (lambda (form inject compare?) 422 (let ((var-vals (cadr form)) 423 (xpr-yprs (cddr form)) 424 (select-failures 425 (lambda (pairs) 426 (let loop ((pairs pairs)) 427 (cond 428 ((null? pairs) '()) 429 ((caar pairs) (loop (cdr pairs))) 430 (else 431 (cons (car pairs) (loop (cdr pairs)))))))) 432 ) 433 `(lambda (verbose?) 434 (letrec ,var-vals 435 (let ((tests '())) 436 ,@(map (lambda (p) 437 `(begin 438 (let ((x ,(car p))) 439 ; protect against functions changing state 440 (when verbose? 441 (print "testing " ',(car p) " ...") 442 (print* "computed: ") (writeln x) 443 (print* "expected: ") (writeln ,(cadr p)) 444 ) 445 (set! tests 446 (cons (cons ((cut equal? <> x) ,(cadr p)) 447 ',(car p)) 448 tests))) 449 ;(cons (cons ((== x) ,(cadr p)) ',(car p)) 450 ; tests))) 451 )) 452 (chop xpr-yprs 2)) 453 (let ((fails (,select-failures (reverse tests)))) 454 (when verbose? 455 (print "Failed test expressions:") 456 (print "------------------------") 457 (if (null? fails) 458 (print "none") 459 (for-each print (map cdr fails)))) 460 (if (null? fails) #t #f))))) 461 )))) 440 462 ;(define-syntax check* 441 463 ; (er-macro-transformer … … 495 517 ; (,%if (,%null? ,%fails) #t #f))))) 496 518 ; )))) 497 (define-syntax check* 498 (ir-macro-transformer 499 (lambda (form inject compare?) 500 (let ((var-vals (cadr form)) 501 (xpr-yprs (cddr form)) 502 (select-failures 503 (lambda (pairs) 504 (let loop ((pairs pairs)) 505 (cond 506 ((null? pairs) '()) 507 ((caar pairs) (loop (cdr pairs))) 508 (else 509 (cons (car pairs) (loop (cdr pairs)))))))) 510 ) 511 `(lambda (verbose?) 512 (letrec ,var-vals 513 (let ((tests '())) 514 ,@(map (lambda (p) 515 `(begin 516 (let ((x ,(car p))) 517 ; protect against functions changing state 518 (when verbose? 519 (print "testing " ',(car p) " ...") 520 (print* "computed: ") (writeln x) 521 (print* "expected: ") (writeln ,(cadr p)) 522 ) 523 (set! tests 524 (cons (cons ((== x) ,(cadr p)) ',(car p)) 525 tests))) 526 )) 527 (chop xpr-yprs 2)) 528 (let ((fails (,select-failures (reverse tests)))) 529 (when verbose? 530 (print "Failed test expressions:") 531 (print "------------------------") 532 (if (null? fails) 533 (print "none") 534 (for-each print (map cdr fails)))) 535 (if (null? fails) #t #f))))) 536 )))) 537 ;;; (check ((var val) ...) xpr ypr . xpr-yprs) 538 ;;; ------------------------------------------ 539 ;;; compare xpr and ypr .... in sequence with == 540 ;;; in the environment defined by var val ... 519 #|[ 520 (check ((var val) ...) xpr ypr . xpr-yprs) 521 --- macro --- 522 Compare xpr and ypr .... in sequence with equal? 523 in the environment defined by var val ... 524 ]|# 541 525 (define-syntax check 542 526 (syntax-rules () … … 544 528 ((check* ((var val) ...) xpr ypr . xpr-yprs) #t)))) 545 529 546 ;;; (define-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 547 ;;; -------------------------------------------------------------- 548 ;;; returns a unary predicate, name?, comparing xpr with ypr .... 549 ;;; and using var val ... within this checks, 550 ;;; verbose? controls the reported summary. 530 #|[ 531 (define-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 532 --- macro --- 533 Deprecated! 534 Returns a unary predicate, name?, comparing xpr with ypr .... 535 and using var val ... within this checks, 536 verbose? controls the reported summary. 537 ]|# 551 538 (define-syntax define-checks 552 539 (ir-macro-transformer … … 572 559 ,@xpr-yprs) ,verbose?))))))))) 573 560 574 ;;; (do-checks (name? verbose? . var-val-pairs) xpr ypr . xpr-yprs) 575 ;;; --------------------------------------------------------------- 576 ;;; returns a unary predicate, name?, comparing xpr with ypr .... 577 ;;; and using var val ... within this checks, 578 ;;; alias to define-checks 561 #|[ 562 (do-checks (name? verbose? . var-val-pairs) xpr ypr . xpr-yprs) 563 --- macro --- 564 Deprecated! 565 Returns a unary predicate, name?, comparing xpr with ypr .... 566 and using var val ... within this checks, 567 alias to define-checks 568 ]|# 579 569 (define-syntax do-checks 580 570 (syntax-rules () … … 583 573 xpr ypr . xpr-ypr-pairs)))) 584 574 585 (define (check-all-proc name . test-name-pairs) ; internal to check-all 586 ; used internally in check-all, must be exported within check-all 575 #|[ 576 (define-tester (name? . var-vals) xpr ypr . xpr-yprs) 577 --- macro --- 578 Returns a thunk predicate, name?, comparing xpr with ypr .... 579 and using var val ... within this tests. 580 The parameter verbose? controls the reported summary, i. e. 581 the side effects. 582 ]|# 583 (define-syntax define-tester 584 (ir-macro-transformer 585 (lambda (form inject compare?) 586 (let ((header (cadr form)) 587 (xpr-yprs (cddr form))) 588 (let ((name (car header)) 589 (var-vals (cdr header))) 590 `(define (,name) 591 (when (verbose?) 592 (print "\nIn " ',name ":") 593 (print* "===" 594 (make-string (string-length 595 (symbol->string ',name)) #\=) 596 "=\n") 597 ) 598 ((check* ,(chop var-vals 2) ,@xpr-yprs) (verbose?)))))))) 599 600 (define (test-all-proc name . test-name-pairs) 601 ; used internally in test-all, must be exported within test-all 587 602 (let loop ((pairs (chop test-name-pairs 2)) (failures '())) 588 603 (cond … … 604 619 (loop (cdr pairs) (cons (cadar pairs) failures)))))) 605 620 606 ;;; (check-all Name check-xpr ....) 607 ;;; ------------------------------- 608 ;;; checks all check-expressions defined with define-checks 609 ;;; producing a list of failures and exiting with 0 or 1 621 #|[ 622 (test-all Name tester ....) 623 --- macro test-all-proc --- 624 invokes all testers defined with define-tester 625 producing a list of failures and exiting with 0 or 1 626 ]|# 627 (define-syntax test-all 628 (er-macro-transformer 629 (lambda (form rename compare?) 630 (let ((name (cadr form)) 631 (tests (cddr form)) 632 (%test-all-proc (rename 'test-all-proc)) 633 (%list (rename 'list)) 634 ) 635 `(,%test-all-proc ',name 636 ,@(apply append 637 (map (lambda (t) `((,t) '(,t))) 638 tests))))))) 639 640 #|[ 641 (check-all Name check-xpr ....) 642 --- macro test-all-proc --- 643 Deprecated! 644 checks all check-expressions defined with define-checks 645 producing a list of failures and exiting with 0 or 1 646 ]|# 610 647 (define-syntax check-all 611 648 (er-macro-transformer … … 613 650 (let ((name (cadr form)) 614 651 (checks (cddr form)) 615 (% check-all-proc (rename 'check-all-proc))652 (%test-all-proc (rename 'test-all-proc)) 616 653 ) 617 `(,% check-all-proc ',name654 `(,%test-all-proc ',name 618 655 ,@(apply append 619 656 (map (lambda (t) `(,t ',t)) 620 657 checks))))))) 621 ) ; simple-tests 622 623 ;(import simple-tests) 624 ; 625 ;(pe '(check ((lst '(0 1 2))) 626 ; (car lst) 627 ; 0 628 ; (cdr lst) 629 ; '(1 2))) 630 ; 631 ;(check ((lst '(0 1 2))) 632 ; (car lst) 633 ; 0 634 ; (cdr lst) 635 ; '(0 1 2)) 636 ; 637 ;(pe '(define-checks (foo verbose? lst '(0 1 2)) 638 ; (car lst) 639 ; 0 640 ; (cdr lst) 641 ; '(1 2))) 642 ;(define-checks (foo verbose? lst '(0 1 2)) 643 ; (car lst) 644 ; 0 645 ; (cdr lst) 646 ; '(1 2 3)) 647 ;(foo #t) 648 ;(ppp (foo #f)) 658 659 #|[ 660 (simple-tests) 661 (simple-tests sym) 662 --- procedure --- 663 documentation procedure 664 ]|# 665 (define simple-tests 666 (let ( 667 (alist '( 668 (verbose? 669 parameter: 670 (verbose? ..) 671 "gets or sets the value of the parameter verbose?" 672 ) 673 (writeln 674 procedure: 675 (writeln xpr ...) 676 "write analog of print, expressions separated by whitespace" 677 ) 678 (and? 679 procedure: 680 (and? . xprs) 681 "non-short-circuited and which executes all side-effects" 682 ) 683 (pe 684 macro: 685 (pe macro-code) 686 "composes pretty-print and expand with additional information" 687 ) 688 (xpr:val 689 macro: 690 (xpr:val xpr ...) 691 "Deprecated!" 692 "Print each xpr quoted in a headline and pretty-print xpr's computed" 693 "value." 694 ) 695 (ppp 696 macro: 697 (ppp xpr ...) 698 "print each xpr quoted in a headline and pretty-print xpr's computed" 699 "value. Alias to xpr:val." 700 ) 701 (ppp* 702 macro: 703 (ppp* {xpr ypr} ...) 704 "Deprecated!" 705 "Print each xpr quoted in a headline and pretty-print xpr's computed" 706 "and expected value, ypr." 707 ) 708 (xpr:val* 709 macro: 710 (xpr:val* {xpr ypr} ...) 711 "Deprecated!" 712 "Print each xpr quoted in a headline and pretty-print xpr's computed" 713 "and expected value, ypr." 714 "Alias to ppp*" 715 ) 716 (ppp** 717 macro: 718 (ppp** ((var val) ...) xpr ypr . other-xpr-ypr-pairs) 719 "Deprecated!" 720 "ppp* wrapped into a let" 721 ) 722 (define-test 723 macro: 724 (define-test (name . parameters) form . forms) 725 "Deprecated!" 726 "Creates a test function" 727 ) 728 (compound-test 729 macro: 730 (compound-test (name) test . tests) 731 "Deprecated!" 732 "Invokes all tests and reports a summary" 733 ) 734 (== 735 procedure: 736 (==) 737 (== x) 738 (== type? type-equal?) 739 "Deprecated!" 740 "Generic type equality as curried procedure" 741 ) 742 (check 743 macro: 744 (check ((var val) ...) xpr ypr . xpr-yprs) 745 "Compare xpr and ypr .... in sequence with equal?" 746 "in the environment defined by var val ..." 747 ) 748 (define-checks 749 macro: 750 (define-checks (name? verbose? . var-vals) xpr ypr . xpr-yprs) 751 "Deprecated!" 752 "Returns a unary predicate, name?, comparing xpr with ypr ...." 753 "and using var val ... within this checks," 754 "verbose? controls the reported summary." 755 ) 756 (do-checks 757 macro: 758 (do-checks (name? verbose? . var-val-pairs) xpr ypr . xpr-yprs) 759 "Deprecated!" 760 "Returns a unary predicate, name?, comparing xpr with ypr ...." 761 "and using var val ... within this checks," 762 "alias to define-checks" 763 ) 764 (define-tester 765 macro: 766 (define-tester (name? . var-vals) xpr ypr . xpr-yprs) 767 "Returns a thunk predicate, name?, comparing xpr with ypr ...." 768 "and using var val ... within this tests." 769 "The parameter verbose? controls the reported summary, i. e." 770 "the side effects." 771 ) 772 (test-all 773 macro: 774 (test-all Name tester ....) 775 "invokes all testers defined with define-tester" 776 "producing a list of failures and exiting with 0 or 1" 777 ) 778 (check-all 779 macro: 780 (check-all Name check-xpr ....) 781 "Deprecated!" 782 "checks all check-expressions defined with define-checks" 783 "producing a list of failures and exiting with 0 or 1" 784 ) 785 (simple-tests 786 procedure: 787 (simple-tests) 788 (simple-tests sym) 789 "with sym: documentation of exported symbol" 790 "without sym: list of exported symbols" 791 ) 792 )) 793 ) 794 (case-lambda 795 (() (map car alist)) 796 ((sym) 797 (let ((pair (assq sym alist))) 798 (if pair 799 (for-each print (cdr pair)) 800 (print "Choose one of " (map car alist)))))))) 801 ) -
release/5/simple-tests/trunk/tests/run.scm
r39187 r39584 1 1 (import simple-tests) 2 2 3 ;;; old interface 4 5 (define-test (bar n) 6 (positive? n) 7 (even? n)) 8 9 (define-test (foo x y) 10 (< x y) 11 "COMMENT" 12 (bar 4) 13 (odd? 3) 14 (positive? 3)) 15 16 (define-test (++) 17 (= (+ 1 2) 3) 18 (= (+ 1 2 3) 6)) 19 20 (define-test (**) 21 (= (* 1 2) 2) 22 (= (* 1 2 3) 6)) 23 24 (define-test (arithmetic) 25 (++) 26 (**)) 27 28 (define-test (baz) 29 (and? #t #t #t) 30 (and?) 31 (not (and? #t #f #t))) 32 33 34 ;(compound-test (simple-tests) 35 ; (baz) 36 ; (arithmetic?) 37 ; (foo 1 2) 38 ; ) 39 40 ;;; new interface 3 (import (only (chicken base) parameterize print case-lambda)) 41 4 42 5 (newline) 43 (print "check")44 (print "=====")45 (check ((lst '(0 1 2 3)))46 lst47 '(0 1 2 3)48 (car lst)49 050 (cadr lst)51 152 (cddr lst)53 '(2 3)54 )55 6 56 ;;; define-checks is an alias to do-checks 57 (do-checks (bar? verbose? n 5) 58 (positive? n) #t 59 (even? n) #f) 7 (define-test (bar n) (positive? n) (even? n)) 60 8 61 (define-checks (+? verbose?) 62 (+ 1 2) 3 63 (+ 1 2 3) 6) 9 (bar 5) 64 10 65 (define-checks (*? verbose?) 66 (* 1 2) 2 67 (* 1 2 3) 6) 11 (define-test (foo x y) (< x y) "COMMENT" (bar 4) (odd? 3) (positive? 3)) 68 12 69 (define-checks (arithmetic? verbose?) 70 (+? #f) #t 71 (*? #f) #t) 13 (foo 1 2) 72 14 73 (do-checks (baz? verbose?) 74 (and? #t #t #t) #t 75 (and?) #t 76 (and? #t #f #t) #f) 15 (define-test (++) (= (+ 1 2) 3) (= (+ 1 2 3) 6)) 77 16 78 (define-checks (qux? verbose?) 79 ((== "x") "y") #f 80 ((== "x") "x") #t 81 ((== baz?) baz?) #t 82 ((== baz?) bar) #f 83 ((== '()) '()) #t 84 ((== 'x) 'y) #f 85 ((== 'x) 'x) #t 86 ((== #(0 1 2)) #(0 1 2)) #t 87 ((== #(0 1 2)) '(0 1 2)) #f 88 ) 17 (++) 18 19 (define-test (**) (= (* 1 2) 2) (= (* 1 2 3) 6)) 20 21 (**) 22 23 (define-test (arithmetic) (++) (**)) 24 25 (arithmetic) 26 27 (define-test (baz) (and? #t #t #t) (and?) (not (and? #t #f #t))) 28 29 (baz) 30 31 '(compound-test (OLD) (bar 5) (foo 1 2) (++) (**) (arithmetic) (baz)) 32 33 (newline) 34 35 (do-checks (bar? verbose? n 5) (positive? n) #t (even? n) #f) 36 37 (bar?) 38 39 (define-checks (+? verbose?) (+ 1 2) 3 (+ 1 2 3) 6) 40 41 (+?) 42 43 (define-checks (*? verbose?) (* 1 2) 2 (* 1 2 3) 6) 44 45 (*?) 46 47 (define-checks (arithmetic? verbose?) (+? #f) #t (*? #f) #t) 48 49 (arithmetic?) 50 51 (do-checks (baz? verbose?) (and? #t #t #t) #t (and?) #t (and? #t #f #t) #f) 52 53 (baz?) 54 55 (define-checks 56 (qux? verbose?) 57 ((== "x") "y") 58 #f 59 ((== "x") "x") 60 #t 61 ((== baz?) baz?) 62 #t 63 ((== baz?) bar?) 64 #f 65 ((== '()) '()) 66 #t 67 ((== 'x) 'y) 68 #f 69 ((== 'x) 'x) 70 #t 71 ((== #(0 1 2)) #(0 1 2)) 72 #t 73 ((== #(0 1 2)) '(0 1 2)) 74 #f) 75 76 (qux?) 89 77 90 78 (define counter 91 (let ((n 0)) 92 (lambda () 93 (set! n (add1 n)) 94 n))) 79 (let ((n 0)) (case-lambda (() (set! n (add1 n)) n) ((k) (set! n k) n)))) 95 80 96 (define-checks (counter? verbose?) 97 (counter) 1 98 (counter) 2 99 (counter) 3 100 (counter) 4 81 (define-checks 82 (counter? verbose?) 83 (counter 0) 84 0 85 (counter) 86 1 87 (counter) 88 2 89 (counter) 90 3 91 (counter) 92 4) 93 94 (counter?) 95 96 '(check-all NEW (bar?) (*?) (+?) (arithmetic?) (baz?) (qux?) (counter?)) 97 98 (define-tester (Bar? n 5) (positive? n) #t (even? n) #f) 99 100 (define-tester (Plus?) (+ 1 2) 3 (+ 1 2 3) 6) 101 102 (define-tester (Times?) (* 1 2) 2 (* 1 2 3) 6) 103 104 (define-tester 105 (Arithmetic?) 106 (parameterize ((verbose? #f)) (Plus?)) 107 #t 108 (parameterize ((verbose? #f)) (Times?)) 109 #t) 110 111 (define Counter 112 (let ((n 0)) (case-lambda (() (set! n (add1 n)) n) ((k) (set! n k) n)))) 113 114 (define-tester 115 (Counter?) 116 (Counter 0) 117 0 118 (Counter) 119 1 120 (Counter) 121 2 122 (Counter) 123 3 124 (Counter) 125 4 126 (Counter 0) 127 0) 128 129 '(test-all SIMPLE-TESTS Bar? Times? Plus? Arithmetic? Counter?) 130 131 (test-all SIMPLE-TESTS 132 Bar? 133 Plus? 134 Times? 135 Arithmetic? 136 Counter? 101 137 ) 102 103 104 (check-all SIMPLE (bar?) (*?) (+?) (arithmetic?) (baz?) (qux?)105 (counter?))
Note: See TracChangeset
for help on using the changeset viewer.