| 1 |
;;;; gen-test-spec.lisp --- Test against the specs |
| 2 |
|
| 3 |
;;; Copyright (C) 2011, 2012 Kan-Ru Chen |
| 4 |
|
| 5 |
;;; Author: Kan-Ru Chen <[email protected]> |
| 6 |
|
| 7 |
;;; Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 |
;;; this software and associated documentation files (the "Software"), to deal in |
| 9 |
;;; the Software without restriction, including without limitation the rights to |
| 10 |
;;; use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 11 |
;;; of the Software, and to permit persons to whom the Software is furnished to do |
| 12 |
;;; so, subject to the following conditions: |
| 13 |
|
| 14 |
;;; The above copyright notice and this permission notice shall be included in all |
| 15 |
;;; copies or substantial portions of the Software. |
| 16 |
|
| 17 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
;;; SOFTWARE. |
| 24 |
|
| 25 |
;;;; Commentary: |
| 26 |
|
| 27 |
;;;; Code: |
| 28 |
|
| 29 |
(in-package :cl-user) |
| 30 |
(require :alexandria) |
| 31 |
(require :cl-fad) |
| 32 |
(require :cl-json) |
| 33 |
|
| 34 |
(defparameter *spec-directory* |
| 35 |
(make-pathname |
| 36 |
:directory |
| 37 |
(append (pathname-directory |
| 38 |
#.(or *load-truename* *compile-file-truename*)) |
| 39 |
'("spec" "specs")))) |
| 40 |
|
| 41 |
(defun utf8-json-decode (pathname) |
| 42 |
(with-open-file (stream pathname |
| 43 |
:direction :input) |
| 44 |
(let ((json:*json-array-type* 'vector)) |
| 45 |
(json:decode-json-from-source stream)))) |
| 46 |
|
| 47 |
(defun json-file-p (pathname) |
| 48 |
(string= "json" (pathname-type pathname))) |
| 49 |
|
| 50 |
(defun all-specs () |
| 51 |
(let (specs) |
| 52 |
(uiop:collect-sub*directories |
| 53 |
*spec-directory* |
| 54 |
(constantly t) |
| 55 |
(complement #'uiop:hidden-pathname-p) |
| 56 |
(lambda (dir) |
| 57 |
(mapc (lambda (file) |
| 58 |
(when (json-file-p file) |
| 59 |
(push (utf8-json-decode file) specs))) |
| 60 |
(uiop:directory-files dir)))) |
| 61 |
specs)) |
| 62 |
|
| 63 |
(defmacro with-test ((test) &body body) |
| 64 |
`(let ((name (alexandria:assoc-value ,test :name)) |
| 65 |
(template (alexandria:assoc-value ,test :template)) |
| 66 |
(data (alexandria:assoc-value ,test :data)) |
| 67 |
(expected (alexandria:assoc-value ,test :expected)) |
| 68 |
(desc (alexandria:assoc-value ,test :desc)) |
| 69 |
(partials (alexandria:assoc-value ,test :partials))) |
| 70 |
,@body)) |
| 71 |
|
| 72 |
(defmacro with-test-in-specs ((test) specs &body body) |
| 73 |
(alexandria:with-gensyms (spec) |
| 74 |
`(loop for ,spec in ,specs |
| 75 |
do (loop for ,test across (alexandria:assoc-value ,spec :tests) |
| 76 |
do (progn ,@body))))) |
| 77 |
|
| 78 |
;; Generate test file |
| 79 |
|
| 80 |
(let ((*print-case* :downcase)) |
| 81 |
(pprint '(in-package :mustache-test)) |
| 82 |
(pprint |
| 83 |
`(deftest spec |
| 84 |
,@(let (tests) |
| 85 |
(with-test-in-specs (test) |
| 86 |
(all-specs) |
| 87 |
(with-test (test) |
| 88 |
(push |
| 89 |
`(is (mustache:render* |
| 90 |
,template |
| 91 |
(mustache:make-context :data ',data :partials ',partials)) |
| 92 |
,expected |
| 93 |
(format nil "~A :: ~A" ,name ,desc)) |
| 94 |
tests))) |
| 95 |
tests) |
| 96 |
(finalize)))) |
| 97 |
|
| 98 |
;;; gen-test-spec.lisp ends here |
| 99 |
|
| 100 |
;;; Local Variables: |
| 101 |
;;; mode: lisp |
| 102 |
;;; End: |