Fix tests for new return values.
[cl-mock.git] / tests / facade.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 (equal '(T) (call-with-mocks
9                    (make-mock-bindings)
10                    (constantly T)))))
11
12 (def-test call-with-mocks.discards-values ()
13   (is (equal
14        '((1 2 3) NIL)
15        (multiple-value-list
16         (call-with-mocks
17          (make-mock-bindings)
18          (lambda ()
19            (values 1 2 3)))))))
20
21 (def-test call-with-mocks.simple ()
22   (declare (notinline foo))
23   (defun foo ()
24     (fail "original function binding ~A was called" 'foo))
25   (let ((mock-bindings (make-mock-bindings)))
26     (register-mock mock-bindings 'foo)
27     (call-with-mocks
28      mock-bindings
29      (lambda ()
30        (foo)
31        (pass)))))
32
33 (def-test call-with-mocks.default-values ()
34   (declare (notinline foo))
35   (defun foo () 'foo)
36   (let ((mock-bindings (make-mock-bindings)))
37     (register-mock mock-bindings 'foo)
38     (call-with-mocks
39      mock-bindings
40      (lambda ()
41        (is (null (multiple-value-list (foo))))))))
42
43 (def-test if-called.simple ()
44   (declare (notinline foo))
45   (defun foo () 'foo)
46   (let ((mock-bindings (make-mock-bindings)))
47     (if-called mock-bindings 'foo (constantly T) (constantly 42))
48     (call-with-mocks
49      mock-bindings
50      (lambda ()
51        (is (eql 42 (foo)))))))
52
53 (def-test call-with-mocks.invocations ()
54   (declare (notinline foo))
55   (defun foo () 'foo)
56   (let ((mock-bindings (make-mock-bindings)))
57     (register-mock mock-bindings 'foo)
58     (is (equal
59          '(NIL ((foo 1) (foo 2) (foo 3)))
60          (multiple-value-list
61           (call-with-mocks
62            mock-bindings
63            (lambda ()
64              (foo 1)
65              (foo 2)
66              (foo 3))))))))
67
68 (def-test when-called.simple ()
69   (declare (notinline foo))
70   (defun foo () 'foo)
71   (let ((mock-bindings (make-mock-bindings)))
72     (when-called mock-bindings foo 42)
73     (when-called mock-bindings foo 23)
74     (call-with-mocks
75      mock-bindings
76      (lambda ()
77        (is (eql 42 (foo)))))))
78
79 (def-test when-called.literal ()
80   (declare (notinline foo))
81   (defun foo () 'foo)
82   (let ((mock-bindings (make-mock-bindings)))
83     (when-called mock-bindings (foo 1) 2)
84     (when-called mock-bindings (foo 2) 3)
85     (when-called mock-bindings foo 42)
86     (call-with-mocks
87      mock-bindings
88      (lambda ()
89        (is (eql 2 (foo 1)))
90        (is (eql 2 (foo 1)))
91        (is (eql 3 (foo 2)))
92        (is (eql 3 (foo 2)))
93        (is (eql 42 (foo)))
94        (is (eql 42 (foo 'foo)))))))
95
96 (def-test when-called.times ()
97   (declare (notinline foo))
98   (defun foo () 'foo)
99   (let ((mock-bindings (make-mock-bindings)))
100     (when-called mock-bindings foo 1 2 3)
101     (call-with-mocks
102      mock-bindings
103      (lambda ()
104        (is (eql 1 (foo)))
105        (is (eql 2 (foo)))
106        (is (eql 3 (foo)))
107        (is (eql 3 (foo)))))))
108
109 (def-test when-called.call-previous ()
110   (declare (notinline foo))
111   (defun foo () 'foo)
112   (let ((mock-bindings (make-mock-bindings)))
113     (when-called mock-bindings foo 3 (call-previous))
114     (call-with-mocks
115      mock-bindings
116      (lambda ()
117        (is (eql 3 (foo)))
118        (is (eq 'foo (foo)))))))