4c375c605bfa4eb6ad791847a628892f230f506f
[clnl] / src / test / main.lisp
1 (in-package #:cl-nl-test)
2
3 (defparameter *tests* nil)
4
5 (defun run-and-print-test (test)
6  (let
7   ((green (format nil "~c[1;32m" #\Esc))
8    (red (format nil "~c[1;31m" #\Esc))
9    (result (funcall (cadr test))))
10   (format t "~A- ~S ~A~c[0m~%" (if result green red) (car test) (if result "passed" "failed") #\Esc)
11   result))
12
13 (defun run-tests (tests)
14  (let
15   ((final-result t))
16   (loop for test in tests
17         for result = (run-and-print-test test)
18         do (setf final-result (and final-result result)))
19   final-result))
20
21 (defun run-all-tests ()
22  (format t "~%Here we goooooooo~%")
23  (run-tests (reverse *tests*)))
24   
25 (defun run-tests-matching (match)
26  (run-tests (remove-if-not (lambda (test-name) (cl-ppcre:scan (format nil "^~A$" match) test-name)) *tests* :key #'car)))
27
28 (defun find-test (name)
29  (or
30   (find name *tests* :test #'string= :key #'car)
31   (error "Couldn't find test with name: ~A" name)))
32
33 (defun test-debug (name) (format t "----~%~A~%" (funcall (third (find-test name)))))
34 (defun test-scala-prog (name) (format t "----~%~A~%" (fourth (find-test name))))
35 (defun test-scala-input (name) (format t "----~%~A~%" (fifth (find-test name))))
36
37 (defmacro defsimpletest (name test-fn debug-fn scala-prog scala-input)
38  `(progn
39    ;(when (find-test ,name) (error "Test with name ~S already exists, abort, abort" ,name))
40    (push
41     (list ,name ,test-fn ,debug-fn ,scala-prog ,scala-input)
42     *tests*)))
43
44 ; To be used only with the simplest of tests, just a list of commands and a checksum of the
45 ; world after they've been run.
46 (defmacro defsimplecommandtest (name commands checksum)
47  `(defsimpletest
48    ,name
49    (lambda ()
50     (cl-nl:boot)
51     (cl-nl:run-commands ,commands)
52     (string= ,checksum (checksum-world)))
53    (lambda ()
54     (cl-nl:boot)
55     (cl-nl:run-commands ,commands)
56     (format nil "~A~A"
57      (cl-nl.nvm:export-world)
58      (checksum-world)))
59    "bin/runcmd.scala"
60    (format nil "~A~%" ,commands)))
61
62 (defmacro defsimplereportertest (name reporter value checksum)
63  `(defsimpletest
64    ,name
65    (lambda ()
66     (cl-nl:boot)
67     (and
68      (string= (cl-nl.nvm:dump-object (cl-nl:run-reporter ,reporter)) ,value)
69      (string= ,checksum (checksum-world))))
70    (lambda ()
71     (cl-nl:boot)
72     (format nil "~A~%~A~A"
73      (cl-nl.nvm:dump-object (cl-nl:run-reporter ,reporter))
74      (cl-nl.nvm:export-world)
75      (checksum-world)))
76    "bin/runreporter.scala"
77    (format nil "~A~%" ,reporter)))
78
79 (defun checksum-world ()
80  (format nil "~{~2,'0X~}"
81   (map 'list #'identity
82    (ironclad:digest-sequence
83     :sha1
84     (map '(vector (unsigned-byte 8)) #'char-code (cl-nl.nvm:export-world))))))
85
86 (defun run ()
87  (loop for str = (progn (format t "> ") (force-output) (read-line))
88        while str
89        do (progn (asdf:load-system :cl-nl-test) (run-tests-matching str))))