OdonataResearchLLC / lisp-unit

A Test Framework for Common Lisp in the style of JUnit, designed and implemented with simplicity of use in mind.
108 stars 20 forks source link

Running tests is destroying the test-code of my tests #19

Closed mck- closed 11 years ago

mck- commented 11 years ago

Which results in tests passing the first time, and failing the next..

(defun aval (key alist)
  "Given alist and key, return value"
  (cdr (assoc key alist :test #'equal)))

(defmacro val-reversed (alist &rest keys)
  "Given an alist, and a list of keys, retrieve value dot-notation style (reversed)"
  (if (null keys) alist
      `(aval ,(car keys) (val-reversed ,alist ,@(cdr keys)))))

(defmacro val (alist &rest keys)
  "Given an alist, and a list of keys, retrieve value dot-notation style"
  `(val-reversed ,alist ,@(nreverse keys)))

(define-test alist-highest-occurence
  (assert-equal 1
                (val '(("this" (NEXT . 1)))
                     "this" 'next)))

LISP-UNIT> (test-code 'alist-highest-occurence)
((ASSERT-EQUAL 1 (VAL '(("this" (NEXT . 1))) "this" 'NEXT)))

LISP-UNIT> (run-tests)
Unit Test Summary
 | 1 assertions total
 | 1 passed
 | 0 failed
 | 0 execution errors
 | 0 missing tests

#<TEST-RESULTS-DB Total(1) Passed(1) Failed(0) Errors(0)>

LISP-UNIT> (test-code 'alist-highest-occurence)
((ASSERT-EQUAL 1 (VAL '(("this" (NEXT . 1))) "this")))

LISP-UNIT> (run-tests)
Unit Test Summary
 | 1 assertions total
 | 0 passed
 | 1 failed
 | 0 execution errors
 | 0 missing tests

#<TEST-RESULTS-DB Total(1) Passed(0) Failed(1) Errors(0)>

LISP-UNIT> 
markcox80 commented 11 years ago

Your use of NREVERSE is undefined according to [1].

"The consequences are undefined if a macro function destructively modifies any part of its form argument."

[1] http://www.lispworks.com/documentation/HyperSpec/Body/03_ababb.htm

mck- commented 11 years ago

Thanks!!

Sent from my iPhone

On 2013-09-23, at 6:13 PM, Mark Cox notifications@github.com wrote:

Your use of NREVERSE is undefined according to [1].

"The consequences are undefined if a macro function destructively modifies any part of its form argument."

[1] http://www.lispworks.com/documentation/HyperSpec/Body/03_ababb.htm

— Reply to this email directly or view it on GitHub.

ThomasHermann commented 11 years ago

While the use of NREVERSE in a macro is problematic, I think there is also an issue with destructively modifying the literal ALIST. I don't have time tonight to work out an example, but I've run into problems with unit tests where a literal object is modified. I'll probably have time to work out the example tomorrow.

ThomasHermann commented 11 years ago

The following code works without error, even with the literal alist. I could not find a bug related to this in lisp-unit and am closing this issue.

(defun aval (key alist)
  "Given alist and key, return value"
  (cdr (assoc key alist :test #'equal)))

(defun val-reversed (alist &rest keys)
  "Given an alist, and a list of keys, retrieve value dot-notation style (reversed)"
  (if (null keys)
      alist
      (aval (first keys) (apply #'val-reversed alist (rest keys)))))

(defun val (alist &rest keys)
  "Given an alist, and a list of keys, retrieve value dot-notation style."
  (apply #'val-reversed alist (reverse keys)))

(define-test alist-highest-occurence
  (assert-equal
   1 (val '(("this" (NEXT . 1))) "this" 'next)))
mck- commented 11 years ago

Nice, no macros! Thanks :)