Skip to content

Commit

Permalink
Correct the macro expansion assertion to number GENSYMS from 1.
Browse files Browse the repository at this point in the history
Improve the comparison of the expanded forms.
  • Loading branch information
Thomas M. Hermann committed Feb 8, 2013
1 parent 97320b2 commit 43d869f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
15 changes: 15 additions & 0 deletions internal-test/example-tests.lisp
Expand Up @@ -57,6 +57,21 @@
(dotimes (i 5)
(assert-equal i (my-sqrt (* i i)) i)))

;;; Macro

(defmacro my-macro (arg1 arg2)
(let ((g1 (gensym))
(g2 (gensym)))
`(let ((,g1 ,arg1)
(,g2 ,arg2))
"Start"
(+ ,g1 ,g2 3))))

(define-test test-macro
(assert-expands
(let ((#:G1 A) (#:G2 B)) "Start" (+ #:G1 #:G2 3))
(my-macro a b)))

;;; Tags

(defun add-integer (integer1 integer2)
Expand Down
34 changes: 27 additions & 7 deletions lisp-unit.lisp
Expand Up @@ -332,7 +332,7 @@ assertion.")
"Assert whether form expands to expansion."
`(expand-assert :macro ,form
(expand-macro-form ,form nil)
,expansion ,extras))
',expansion ,extras))

(defmacro assert-false (form &rest extras)
"Assert whether the form is false."
Expand Down Expand Up @@ -375,7 +375,8 @@ assertion.")

(defmacro expand-macro-form (form env)
"Expand the macro form once."
`(macroexpand-1 ',form ,env))
`(let ((*gensym-counter* 1))
(macroexpand-1 ',form ,env)))

(defmacro expand-extras (extras)
"Expand extra forms."
Expand Down Expand Up @@ -433,11 +434,28 @@ assertion.")
(:documentation
"Result of a failed macro expansion assertion."))

;;; FIXME: Review the internal tests for macros.
(defun %expansion-equal (form1 form2)
"Descend into the forms checking for equality."
(let ((item1 (first form1))
(item2 (first form2)))
(cond
((and (null item1) (null item2)))
((and (listp item1) (listp item2))
(and
(%expansion-equal item1 item2)
(%expansion-equal (rest form1) (rest form2))))
((and (symbolp item1) (symbolp item2))
(and
(string= (symbol-name item1) (symbol-name item2))
(%expansion-equal (rest form1) (rest form2))))
(t (and
(equal item1 item2)
(%expansion-equal (rest form1) (rest form2)))))))

(defun macro-result (test expected actual)
"Return the result of a macro assertion."
(declare (ignore test))
(equal (car actual) (car expected)))
(%expansion-equal (first expected) (first actual)))

(defclass boolean-result (failure-result)
()
Expand Down Expand Up @@ -517,9 +535,11 @@ assertion.")
(exerr
:initarg :exerr
:reader exerr)
(run-time :initarg :run-time
:reader run-time
:documentation "run time measured in internal time units"))
(run-time
:initarg :run-time
:reader run-time
:documentation
"Test run time measured in internal time units"))
(:default-initargs :exerr nil)
(:documentation
"Store the results of the unit test."))
Expand Down

0 comments on commit 43d869f

Please sign in to comment.