public
Description: BDD for Common Lisp
Homepage:
Clone URL: git://github.com/osteele/cl-spec.git
osteele (author)
Tue Jan 15 07:31:35 -0800 2008
commit  86ec419166158682ab6de9dbe6e4d8458435ddca
tree    98705085f67e3a78d86bbd57d4313892bb1eb478
parent  90633abd1672f695d57edb3e1e06830213ac1a7f
cl-spec / bdd.lisp
100644 169 lines (143 sloc) 5.485 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;;; Author: Yurii Rashkovskii
;;; Source: http://rashkovskii.com/lisp
 
;;; ows 2008-01-05 -- fixed compiler errors and warnings
;;; ows 2008-01-08 -- eval -> funcall, to capture lexical scope
 
(in-package #:cl-spec)
 
;; Utilities
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defmethod obj->string ((s string))
    s)
 
  (defmethod obj->string ((s symbol))
    (string s))
 
  (defun concat-symbol (&rest args)
    (intern (apply #'concatenate 'string
                   (mapcar #'string-upcase (mapcar #'obj->string args)))))
  )
 
(defun respond-to? (o method &rest args)
  (restart-case
      (handler-bind ((undefined-function #'(lambda (c)
                         (declare (ignore c))
                         (invoke-restart 'no)))
             (simple-error #'(lambda (c)
                       (declare (ignore c))
                       (invoke-restart 'no))))
    (let ((sf (symbol-function method))
          (cpl (sb-pcl::class-precedence-list (class-of o))))
      (typecase sf
        (standard-generic-function
         (find t
           (mapcar #'(lambda (klass)
                   (not (null
                     (find-method sf '()
                          (cons klass
                            (mapcar #'(lambda (c) (declare (ignore c)) t) args)) nil))))
               cpl)))
        (function t))))
    (no (&optional v)
      (declare (ignore v))
      nil)))
 
;; Conditions
 
(define-condition expectation-not-met ()
  ((expectation :initarg :expectation :reader expectation)
   (form :initarg :form :reader expectation-form)))
 
;; Expectations
(defclass expectation ()
  ((value-fn :initarg :value-fn :reader value-fn-of)
   (value :accessor value-of)
   (args :initarg :args :reader args-of)))
 
(defclass should (expectation)
  ())
 
(defmethod evaluate ((e expectation))
  (setf (value-of e) (funcall (value-fn-of e))))
 
(defgeneric fulfills? (expectation))
 
(defmethod fulfills? ((e should))
  (flet ((match (matcher-class args)
           (restart-case
           (handler-bind ((simple-error #'(lambda (c)
                                            (declare (ignore c))
                                            (invoke-restart 'fun))))
             (matches? (make-instance matcher-class :args args) e))
           (fun (&optional v)
             ;; This happens when matcher-class doesn't actually name
             ;; a class; e.g. (=> 1 should = 1) instead of
             ;; (=> 0 should be zero)
             (declare (ignore v))
             (apply matcher-class (append (list (evaluate e)) (mapcar #'eval args)))))))
    (with-slots (args) e
      (if (eq (car args) 'not)
          (not (match (cadr args) (cddr args)))
          (match (car args) (cdr args))))))
 
;; Matchers
 
(defclass matcher ()
  ((args :initarg :args :reader args-of)))
 
(defclass be (matcher)
  ())
 
(defmethod initialize-instance :after ((matcher be) &rest initargs)
  (declare (ignore initargs))
  (with-slots (args) matcher
    (when (equal (car args) 'a)
      (pop args))))
 
(defgeneric matches? (matcher expr))
 
(defmethod matches? ((matcher be) expr)
  (with-slots (args) matcher
    (let* ((value (evaluate expr))
           (arguments (cdr args))
           (message-forms (mapcar #'(lambda (suffix)
                                      (concat-symbol (car args) suffix))
                                  '("" "p" "-p" "?"))))
      (when (equal (car arguments) 'of)
        (pop arguments)) ;; am I crazy?
      (setf arguments (mapcar #'eval arguments))
      (some #'(lambda (form)
                (and (respond-to? value form arguments)
                     (apply form value arguments)))
            message-forms))))
 
(defclass raise (matcher)
  ())
 
(defmethod matches? ((matcher raise) e)
  (with-slots (args) matcher
    (let ((class-name (car args)))
      (restart-case
          (handler-bind ((t #'(lambda (c)
                                (setf (value-of e) c)
                                (if (typep c class-name)
                                    (invoke-restart 'raises)
                                    (invoke-restart 'donot)))))
            (evaluate e)
            nil)
        (raises (&optional v)
          (declare (ignore v))
          t)
        (donot (&optional v)
          (declare (ignore v))
          nil)))))
 
;;
(defmacro => (form &rest specification)
  (let ((expectation-class (car specification))
    (args (cdr specification)))
    `(let ((expectation
            (make-instance ',expectation-class
                           :value-fn #'(lambda () ,form)
                           :args ',args)))
       (unless (fulfills? expectation)
         (error (make-instance 'expectation-not-met
         :expectation expectation
         :form ',form)))
       (value-of expectation))))
 
;; Grouping
(defmacro define-with-spec-grouping (name)
  (let ((with-grouping (concat-symbol "with-" name ))
    (spec-groupings (concat-symbol "*spec-" name "s*"))
    (spec-grouping (concat-symbol "*spec-" name "*")))
    `(defmacro ,with-grouping (grouping-name &body body)
       `(progn
      (unless (and (boundp ',',spec-groupings) (listp ,',spec-groupings))
        (defvar ,',spec-groupings nil))
      (let* ((,',spec-groupings (cons ,grouping-name ,',spec-groupings))
         (,',spec-grouping (car ,',spec-groupings)))
        ,@body)))))
 
(define-with-spec-grouping context)
(define-with-spec-grouping aspect)
 
(defmacro specify (name &body body)
  `(let ((*spec-specification* ,name))
     ,@body))