Overhaul and version bump.
[cl-mock.git] / tests / mock.lisp
diff --git a/tests/mock.lisp b/tests/mock.lisp
new file mode 100644 (file)
index 0000000..c935e8e
--- /dev/null
@@ -0,0 +1,60 @@
+;; -*- mode: lisp; syntax: common-lisp; coding: utf-8-unix; package: cl-mock-tests; -*-
+
+(in-package #:cl-mock-tests)
+\f
+(in-suite cl-mock)
+
+(def-test call-with-mocks.empty ()
+  (is (eq T (with-mocks () T))))
+
+(def-test call-with-mocks.discards-values ()
+  (is (equal
+       '(1 2 3)
+       (multiple-value-list
+        (with-mocks ()
+          (values 1 2 3))))))
+
+(declaim (notinline foo/simple))
+(defun foo/simple ()
+  (fail "original function binding ~A was called" 'foo/simple))
+
+(def-test call-with-mocks.simple ()
+  (with-mocks ()
+    (register-mock 'foo/simple)
+    (foo/simple)
+    (pass)))
+
+(declaim (notinline foo bar))
+(defun foo () 'foo)
+(defun bar () 'bar)
+
+(def-test call-with-mocks.default-values ()
+  (with-mocks ()
+    (register-mock 'foo)
+    (is (null (multiple-value-list (foo))))))
+
+(def-test if-called.simple ()
+  (with-mocks ()
+    (if-called 'foo (constantly 42))
+    (is (eql 42 (foo)))))
+
+(def-test invocations.length ()
+  (with-mocks ()
+    (register-mock 'foo)
+    (dotimes (i 3) (foo))
+    (is (eql 3 (length (invocations))))))
+
+(def-test invocations.arguments ()
+  (with-mocks ()
+    (register-mock 'foo)
+    (let ((sym (gensym)))
+      (foo sym)
+      (is (equal `((foo ,sym)) (invocations))))))
+
+(def-test invocations.name ()
+  (with-mocks ()
+    (register-mock 'foo)
+    (register-mock 'bar)
+    (foo)
+    (bar)
+    (is (equal `((foo)) (invocations 'foo)))))