diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/examples.lisp | 124 | ||||
-rw-r--r-- | examples/legacy-examples.lisp | 72 |
2 files changed, 196 insertions, 0 deletions
diff --git a/examples/examples.lisp b/examples/examples.lisp new file mode 100644 index 0000000..a188f2f --- /dev/null +++ b/examples/examples.lisp @@ -0,0 +1,124 @@ +(defpackage :testiere.examples + (:use #:cl #:testiere)) + +(defpackage :dummy + (:use #:cl)) + +(in-package :testiere.examples) + +;;; Turn Testiere On. +(testiere-on) + +;;; BASIC TESTS + +(defun add3 (x y z) + "Adds three numbers" + #+testiere + (:tests + (= 6 (add3 1 2 3)) + (:is (evenp (add3 2 2 2))) + (:fails (add3)) + (:fails (add3 1 2 "oh no"))) + (+ x y z)) + +;;; Using external tests + +(defun dummy::test-add10 (n) + "Tests add10 in the same way N times. Obviously useless. We define +this in a separate package to give you an idea that you can embed +tests that aren't part of the package you're testing." + (loop :repeat n :do + (assert (= 13 (add10 3))))) + +(defun add10 (x) + "Adds 10 to X" + #+testiere + (:tests + (:funcall 'dummy::test-add10 1)) + (+ x 10)) + +;;; Adding some context to tests with :LET + +(defvar *count*) + +(defun increment-count (&optional (amount 1)) + "Increments *COUNT* by AMOUNT" + #+testiere + (:tests + (:let ((*count* 5)) + (:funcall #'increment-count) + (= *count* 6) + (:funcall #'increment-count 4) + (= *count* 10)) + (:let ((*count* -10)) + (= (increment-count) -9))) + (incf *count* amount)) + +;;; Stubbing functions with :WITH-DEFUNS + +(defun dummy::make-drakma-request (url) + "Assume this actually makes an HTTP request using drakma" + ) + +(defun test-count-words-in-response () + (assert (= 3 (count-words-in-response "blah")))) + +(defun count-words-in-response (url) + "Fetches a url and counts the words in the response." + #+testiere + (:tests + (:with-defuns + ((dummy::make-drakma-request (url) + (declare (values (simple-array character))) + "Hello there dudes")) + (= 3 (count-words-in-response "dummy-url")) + (:funcall 'test-count-words-in-response))) + (loop + :with resp string := (dummy::make-drakma-request url) + :with in-word? := nil + :for char :across resp + :when (and in-word? (not (alphanumericp char))) + :count 1 :into wc + :and :do (setf in-word? nil) + :when (alphanumericp char) + :do (setf in-word? t) + :finally (return + (if (alphanumericp char) (1+ wc) wc)))) + +;;; Testing Classes + +(defclass point () + ((x + :accessor px + :initform 0 + :initarg :x) + (y + :accessor py + :initform 0 + :initarg :y)) + #+testiere + (:tests + (:let ((pt (make-instance 'point :x 10 :y 20))) + (= 20 (py pt)) + (= 10 (px pt)) + (:is (< (px pt) (py pt)))))) + +;;; Testing Structs + +(defstruct pt + x y + #+testiere + (:tests + (:let ((pt (make-pt :x 10 :y 20))) + (= 20 (pt-y pt)) + (:is (< (pt-x pt) (pt-y pt)))))) + +;;; Testing Types + +(deftype optional-int () + #+testiere + (:tests + (:is (typep nil 'optional-int)) + (:is (typep 10 'optional-int)) + (:is (not (typep "foo" 'optional-int)))) + '(or integer null)) diff --git a/examples/legacy-examples.lisp b/examples/legacy-examples.lisp new file mode 100644 index 0000000..b515b98 --- /dev/null +++ b/examples/legacy-examples.lisp @@ -0,0 +1,72 @@ +(defpackage :testiere.examples + (:use #:cl) + (:import-from #:testiere + #:defun/t + #:with-stubs)) + +(in-package :testiere.examples) + +(defun test-fibble () + (assert (= 13 (fibble 1 2)))) + +(defun/t fibble (x y &key (z 10)) + "Hey, a docstring." + :tests + (:program test-fibble) + :end + (+ x y z)) + +(defvar *count*) + +(defun/t increment-count () + "Increments the *count* variable." + :tests + (:let ((*count* 4)) + (:afterp () (lambda () (= *count* 5))) + (= () 6) + (:outputp () (lambda (x) (= x 7)))) + :end + (incf *count*)) + +(defun/t other-increment-count (&optional (amount 1)) + "Also increments the *count* variable by an optional amount." + :tests + (:let ((*count* 10)) + (= () 11)) + (:let ((*count* 0)) + (= (10) 10) + (:afterp (2) (lambda () (= *count* 12)))) + :end + (incf *count* amount)) + + +(defun just-a-function () + (print "Just a function.")) + +(defun/t call-just-a-function () + "Calls JUST-A-FUNCTION." + :tests + (:with-stubs ((just-a-function () (print "TEMP JUST-A-FUNCTION."))) + (equal () "TEMP JUST-A-FUNCTION.")) + :end + (just-a-function)) + + +(defun make-drakma-request () + "Make an HTTP request using DRAKMA" + ;; implemented elsewhere + ) + +(defun test-url-word-counter () + (with-stubs + ((make-drakma-request () "one two three four five six seven")) + (assert (= (count-words) 7)))) + +(defun/t count-words () + "Fetches a url and counts now many words the page contains." + :tests + (:program test-url-word-counter) + :end + (let ((fetched + (make-drakma-request))) + (1+ (count #\space fetched)))) |