Some ideas about rebinding methods.
[cl-mock.git] / tests / mock.lisp
1 ;; -*- mode: lisp; syntax: common-lisp; coding: utf-8-unix; package: cl-mock-tests; -*-
2
3 (in-package #:cl-mock-tests)
4 \f
5 (in-suite cl-mock)
6
7 (def-test call-with-mocks.empty ()
8   (is (eq T (with-mocks () T))))
9
10 (def-test call-with-mocks.discards-values ()
11   (is (equal
12        '(1 2 3)
13        (multiple-value-list
14         (with-mocks ()
15           (values 1 2 3))))))
16
17 (declaim (notinline foo/simple))
18 (defun foo/simple ()
19   (fail "original function binding ~A was called" 'foo/simple))
20
21 (def-test call-with-mocks.simple ()
22   (with-mocks ()
23     (register-mock 'foo/simple)
24     (foo/simple)
25     (pass)))
26
27 (declaim (notinline foo bar))
28 (defun foo () 'foo)
29 (defun bar () 'bar)
30
31 (def-test call-with-mocks.default-values ()
32   (with-mocks ()
33     (register-mock 'foo)
34     (is (null (multiple-value-list (foo))))))
35
36 (def-test if-called.simple ()
37   (with-mocks ()
38     (if-called 'foo (constantly 42))
39     (is (eql 42 (foo)))))
40
41 (def-test invocations.length ()
42   (with-mocks ()
43     (register-mock 'foo)
44     (dotimes (i 3) (foo))
45     (is (eql 3 (length (invocations))))))
46
47 (def-test invocations.arguments ()
48   (with-mocks ()
49     (register-mock 'foo)
50     (let ((sym (gensym)))
51       (foo sym)
52       (is (equal `((foo ,sym)) (invocations))))))
53
54 (def-test invocations.name ()
55   (with-mocks ()
56     (register-mock 'foo)
57     (register-mock 'bar)
58     (foo)
59     (bar)
60     (is (equal '((foo)) (invocations 'foo)))))
61
62 (def-test call-with-mocks.method.default-values ()
63   (with-mocks ()
64     (register-mock 'baz :method '(NIL (list)))
65     (is (null (multiple-value-list (baz NIL))))))
66
67 (def-test invocations.method.simple ()
68   (with-mocks ()
69     (register-mock 'baz :method '(NIL (list)))
70     (is (typep #'baz 'generic-function))
71     (baz (list 1 2 3))
72     (is (equal '((baz (1 2 3))) (invocations)))))
73
74 (def-test call-with-mocks.method.autodetection ()
75   (with-mocks ()
76     (register-mock 'baz)
77     (is (typep #'baz 'generic-function))
78     (baz (list 1 2 3))
79     (is (equal '((baz (1 2 3))) (invocations)))))