X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?p=clnl;a=blobdiff_plain;f=src%2Ftest%2Fmain.lisp;h=6d3282d6a28cf22d40368708054c6c5810b909c1;hp=19c96670f9b5ed81ad790d68da5d83583e6c392e;hb=89cb3482de1fefc2d8e5f67e1e04a0790e8cd243;hpb=43bbc274299e58d2f4a6e0b05e5366ca5e2900ae diff --git a/src/test/main.lisp b/src/test/main.lisp index 19c9667..6d3282d 100644 --- a/src/test/main.lisp +++ b/src/test/main.lisp @@ -13,17 +13,18 @@ (defun run-tests (tests) (let ((final-result t)) - (loop for test in tests - for result = (run-and-print-test test) - do (setf final-result (and final-result result))) + (loop + :for test :in tests + :for result := (run-and-print-test test) + :do (setf final-result (and final-result result))) final-result)) (defun run-all-tests () - (format t "~%~c[1;33mHere we goooooooo~c[0m~%" #\Esc #\Esc) (run-tests (reverse *tests*))) - + (defun run-tests-matching (match) - (run-tests (remove-if-not (lambda (test-name) (cl-ppcre:scan (format nil "^~A$" match) test-name)) *tests* :key #'car))) + (run-tests + (remove-if-not (lambda (test-name) (cl-ppcre:scan (format nil "^~A$" match) test-name)) *tests* :key #'car))) (defun find-test (name) (or @@ -34,6 +35,9 @@ (defun test-scala-prog (name) (format t "----~%~A~%" (fourth (find-test name)))) (defun test-scala-input (name) (format t "----~%~A~%" (fifth (find-test name)))) +(defun clnl-commands (commands) (if (listp commands) (car commands) commands)) +(defun scala-commands (commands) (if (listp commands) (cadr commands) commands)) + (defmacro defsimpletest (name test-fn debug-fn scala-prog scala-input) `(progn ;(when (find-test ,name) (error "Test with name ~S already exists, abort, abort" ,name)) @@ -43,8 +47,8 @@ (defun checksum= (expected got) (if (stringp expected) - (string= got expected) - (find got expected :test #'string=))) + (string= got expected) + (find got expected :test #'string=))) ; To be used only with the simplest of tests, just a list of commands and a checksum of the ; world after they've been run. @@ -52,11 +56,11 @@ `(defsimpletest (format nil "Simple Command - ~A" ,name) (lambda () - (clnl:boot) + (clnl:boot "resources/empty.nlogo" t) (clnl:run-commands ,commands) (checksum= ,checksum (checksum-world))) (lambda () - (clnl:boot) + (clnl:boot "resources/empty.nlogo" t) (clnl:run-commands ,commands) (format nil "~A~A" (clnl-nvm:export-world) @@ -68,24 +72,161 @@ `(defsimpletest (format nil "Simple Reporter - ~A" ,name) (lambda () - (clnl:boot) + (clnl:boot "resources/empty.nlogo" t) + (and + (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value) + (checksum= ,checksum (checksum-world)))) + (lambda () + (clnl:boot "resources/empty.nlogo" t) + (format nil "~A~%~A~A" + (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) + (clnl-nvm:export-world) + (checksum-world))) + "bin/runcmd.scala" + (format nil "@#$#@#$#@~A" ,reporter))) + +(defmacro defreportertestwithsetup (name setup reporter value checksum) + `(defsimpletest + (format nil "Reporter With Setup - ~A" ,name) + (lambda () + (clnl:boot "resources/empty.nlogo" t) + (clnl:run-commands ,setup) (and - (string= (clnl-nvm:dump-object (clnl:run-reporter ,reporter)) ,value) + (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) ,value) (checksum= ,checksum (checksum-world)))) (lambda () - (clnl:boot) + (clnl:boot "resources/empty.nlogo" t) + (clnl:run-commands ,setup) (format nil "~A~%~A~A" - (clnl-nvm:dump-object (clnl:run-reporter ,reporter)) + (funcall (intern "DUMP-OBJECT" :clnl-nvm) (clnl:run-reporter ,reporter)) (clnl-nvm:export-world) (checksum-world))) - "bin/runreporter.scala" - (format nil "~A~%" ,reporter))) + "bin/runcmd.scala" + (format nil "~A@#$#@#$#@~A" ,setup ,reporter))) + +(defun model-code->nlogo (code) + (format nil + "~A +@#$#@#$#@ +GRAPHICS-WINDOW~%210~%10~%649~%470~%-1~%-1~%13.0~%1~%10~%1~%1~%1~%0~%1~%1~%1~%-1~%1~%-1~%1~%0~%0~%1~%ticks~%30.0~% +@#$#@#$#@ +" + code)) + +(defmacro defmodeltest (name model commands reporter value checksum) + `(defsimpletest + ,name + (lambda () + (let + ((model (with-input-from-string (str ,(model-code->nlogo model)) (clnl-model:read-from-nlogo str)))) + (and + (let + ((callback nil)) + (declaim (sb-ext:muffle-conditions cl:warning)) + (eval (clnl:model->single-form-lisp model :netlogo-callback (lambda (f) (setf callback f)))) + (when ,commands (funcall callback ,commands)) + (and + (or (not ,reporter) (string= (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall callback ,reporter)) ,value)) + (checksum= ,checksum (checksum-world)))) + (let* + ((pkg (make-package (gensym))) + (clnl:*model-package* pkg) + (prev-package *package*)) + (eval + (cons + 'progn + (clnl:model->multi-form-lisp model (intern "BOOT-ME" pkg) + :netlogo-callback-fn (intern "NETLOGO-CALLBACK" pkg)))) + (eval `(in-package ,(package-name prev-package))) + (funcall (symbol-function (intern "BOOT-ME" pkg))) + (when ,commands (funcall (symbol-function (intern "NETLOGO-CALLBACK" pkg)) ,commands)) + (and + (or + (not ,reporter) + (string= + (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall (intern "NETLOGO-CALLBACK" pkg) ,reporter)) + ,value)) + (checksum= ,checksum (checksum-world))))))) + (lambda () + (let + ((callback nil)) + (declaim (sb-ext:muffle-conditions cl:warning)) + (eval + (clnl:model->single-form-lisp + (with-input-from-string (str ,(model-code->nlogo model)) (clnl-model:read-from-nlogo str)) + :netlogo-callback (lambda (f) (setf callback f)))) + (when ,commands (funcall callback ,commands)) + (format nil "~A~A~A" + (if ,reporter (format nil "~A~%" (funcall (intern "DUMP-OBJECT" :clnl-nvm) (funcall callback ,reporter))) "") + (clnl-nvm:export-world) + (checksum-world)))) + "bin/runcmd.scala" + (format nil "~A@#$#@#$#@~A@#$#@#$#@~A" ,commands (or ,reporter "") ,model))) + +(defmacro defmodelcommandtest (name model commands checksum) + `(defmodeltest (format nil "Model Command - ~A" ,name) ,model ,commands nil nil ,checksum)) + +(defmacro defmodelreportertest (name model commands reporter value checksum) + `(defmodeltest (format nil "Model Reporter - ~A" ,name) ,model ,commands ,reporter ,value ,checksum)) + +(defun wait-for-forever () + (loop + :while + (find-if + (lambda (name) (cl-ppcre:scan "Forever button:" name)) + (mapcar #'sb-thread:thread-name (sb-thread:list-all-threads))) + :do (sleep .1))) + +(defmacro defmodelfiletest (name file commands checksum &optional wait-for-forever) + `(defsimpletest + ,(format nil "File Model - ~A" name) + (lambda () + (let + ((model (with-open-file (str ,file) (clnl-model:read-from-nlogo str)))) + (and + (let + ((callback nil)) + (declaim (sb-ext:muffle-conditions cl:warning)) + (eval (clnl:model->single-form-lisp model :netlogo-callback (lambda (f) (setf callback f)))) + (when ,(clnl-commands commands) (funcall callback ,(clnl-commands commands))) + ,(when wait-for-forever `(wait-for-forever)) + (checksum= ,checksum (checksum-world))) + (let* + ((pkg (make-package (gensym))) + (clnl:*model-package* pkg) + (prev-package *package*)) + (eval + (cons + 'progn + (clnl:model->multi-form-lisp model (intern "BOOT-ME" pkg) + :netlogo-callback-fn (intern "NETLOGO-CALLBACK" pkg)))) + (eval `(in-package ,(package-name prev-package))) + (funcall (symbol-function (intern "BOOT-ME" pkg))) + (when ,(clnl-commands commands) + (funcall (symbol-function (intern "NETLOGO-CALLBACK" pkg)) ,(clnl-commands commands))) + ,(when wait-for-forever `(wait-for-forever)) + (checksum= ,checksum (checksum-world)))))) + (lambda () + (let + ((callback nil)) + (declaim (sb-ext:muffle-conditions cl:warning)) + (eval + (clnl:model->single-form-lisp + (with-open-file (str ,file) (clnl-model:read-from-nlogo str)) + :netlogo-callback (lambda (f) (setf callback f)))) + (when ,(clnl-commands commands) (funcall callback ,(clnl-commands commands))) + ,(when wait-for-forever `(wait-for-forever)) + (format nil "~A~A" + (clnl-nvm:export-world) + (checksum-world)))) + "bin/runcmd.scala" + (format nil "~A@#$#@#$#@@#$#@#$#@@#$#@#$#@~A" ,(scala-commands commands) ,file))) (defmacro defviewtest (name commands checksum) `(defsimpletest (format nil "Simple View - ~A" ,name) (lambda () - (clnl:boot) + (clnl:boot "resources/empty55.nlogo") (clnl:run-commands ,commands) (let ((viewsum (checksum-view))) @@ -93,7 +234,7 @@ (format t "~c[1;35m-- For ~A, got ~A but expected ~A~c[0m~%" #\Esc ,name viewsum ,checksum #\Esc)) (checksum= ,checksum (checksum-view)))) (lambda () - (clnl:boot) + (clnl:boot "resources/empty55.nlogo") (clnl:run-commands ,commands) (save-view-to-ppm) (format nil "~A" (checksum-view))) @@ -115,7 +256,11 @@ (defun save-view-to-ppm () (let ((height 143) (width 143)) ; hardcoded in interface, hardcoded here, cry for me - (with-open-file (str "cl.ppm" :direction :output :if-exists :supersede :if-does-not-exist :create :element-type '(unsigned-byte 8)) + (with-open-file (str "cl.ppm" + :direction :output + :if-exists :supersede + :if-does-not-exist :create + :element-type '(unsigned-byte 8)) (write-sequence (map 'vector #'char-code (format nil "P6~%")) str) (write-sequence (map 'vector #'char-code (format nil "143 143~%")) str) (write-sequence (map 'vector #'char-code (format nil "255~%")) str) @@ -128,6 +273,12 @@ (write-byte (aref image-data (+ 2 (* 4 (+ (* (- (1- height) i) width) j)))) str))))))) (defun run () - (loop for str = (progn (format t "> ") (force-output) (read-line)) - while str - do (progn (asdf:load-system :clnl-test) (run-tests-matching str)))) + (loop + :for str := (progn (format t "> ") (force-output) (read-line)) + :while str + :do + (handler-case + (progn + (asdf:load-system :clnl-test) + (run-tests-matching str)) + (error (e) (format t "Ok, something went wrong: ~A~%" e)))))