d3acdc3e3f78531286b80b38ebf695bd7b806eb8
[cl-mock.git] / tests / functions.lisp
1 ;;; -*- mode: lisp; syntax: common-lisp; coding: utf-8-unix; package: cl-mock-tests; -*-
2
3 (in-package #:cl-mock-tests)
4
5 (def-test dflet.calls-binding ()
6   (dflet ((foo () 23))
7     (is (eql 23 (foo)))))
8
9 (def-test dflet.notinline.works ()
10   (declare (notinline foo bar))
11   (defun foo () 23)
12   (dflet ((foo () 42))
13     (is (eql 42 (foo)))))
14
15 (def-test dflet.simple-mock ()
16   (defun foo (&optional (string "Hello, World!"))
17     (1+ (bar string)))
18   (defun bar (string)
19     (length string))
20   (dflet ((bar (string)
21             (cond
22               ((equalp string "Hello, World!")
23                42))))
24     (is (eql 43 (foo)))
25     (is (eql 43 (foo "HELLO, WORLD!")))))
26
27 (def-test dflet.package-locks ()
28   "Either we can rebind LIST, or an error occurs and the binding is not
29 modified."
30   (let ((list #'list))
31     (handler-case (dflet ((list ()))
32                     (is (eql 42 (list))))
33       (error ()
34         (is (eq #'list list))))))
35
36 (def-test dflet.package-locks.order.1 ()
37   (defun foo ()
38     23)
39   (let ((list #'list)
40         (foo #'foo))
41     (handler-case (dflet
42                       ((foo () 13)
43                        (list () 42))
44                     (is (eql 42 (list)))
45                     (is (eql 13 (foo))))
46       (error ()
47         (is (eq #'list list))
48         (is (eq #'foo foo))))))
49
50 (def-test dflet.package-locks.order.2 ()
51   (defun foo ()
52     23)
53   (let ((list #'list)
54         (foo #'foo))
55     (handler-case (dflet
56                       ((list () 42)
57                        (foo () 13))
58                     (is (eql 42 (list)))
59                     (is (eql 13 (foo))))
60       (error ()
61         (is (eq #'list list))
62         (is (eq #'foo foo))))))