-
Notifications
You must be signed in to change notification settings - Fork 1
/
el-spy.el
147 lines (125 loc) · 4.18 KB
/
el-spy.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
;;; el-spy.el --- Mocking framework for Emacs lisp. It also support spy, proxy.
;;-------------------------------------------------------------------
;;
;; Copyright (C) 2012 Yuuki Arisawa
;;
;; This file is NOT part of Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;
;;-------------------------------------------------------------------
;; Author: Yuuki Arisawa <[email protected]>
;; URL: https://github.com/uk-ar/el-spy
;; Created: 24 Sep 20
;; Version: 0.1
;; Keywords: test
;;; Commentary:
;; ######## Compatibility ########################################
;;
;; Works with Emacs-24.2
;; ######## Quick start ########################################
;;
;; Add to your ~/.emacs
;;
;; (require 'el-spy)
;;; Code: section
(require 'advice)
(eval-when-compile (require 'cl))
;;for should in el-spy:make-args-keylist
(require 'ert)
(defun el-spy:setup-mock (funcsym)
(el-spy:put-args funcsym nil)
(when (fboundp funcsym)
(put funcsym 'el-spy:original-func (symbol-function funcsym))))
(defun el-spy:get-original-func (symbol)
(get symbol 'el-spy:original-func))
(defun el-spy:teardown-mock (funcsym)
(el-spy:put-args funcsym nil)
(let ((func (el-spy:get-original-func funcsym)))
(if func
(ad-safe-fset funcsym func)
(fmakunbound funcsym))))
(defmacro with-el-spy (&rest body)
(declare (indent 0) (debug t))
`(progn
;; macrolet
(letf (((symbol-function 'defmock) (symbol-function 'el-spy:defmock)))
(let ((el-spy:original-func nil))
(unwind-protect
(progn ,@body)
(mapc #'el-spy:teardown-mock el-spy:original-func)
)))))
(defalias 'with-mock2 'with-el-spy)
;; user level
(defun el-spy:get-args (symbol)
(get symbol 'el-spy:args))
(defalias 'el-spy:args-for-call 'el-spy:get-args)
(defun el-spy:put-args (symbol args)
(put symbol 'el-spy:args args))
(defun el-spy:append-args (symbol arglist)
(el-spy:put-args
symbol
(append (get symbol 'el-spy:args) arglist)))
(defun el-spy:not-called (symbol)
(eq (length (el-spy:get-args symbol)) 0))
(defun el-spy:called-count (symbol)
(length (el-spy:get-args symbol)))
(defvar el-spy:func-name nil)
(defmacro el-spy:defmock (symbol arglist &rest body)
(declare (indent defun) (debug t))
`(progn
(unless (boundp 'el-spy:original-func)
(error "not in with-mock"))
(push ',symbol el-spy:original-func)
(el-spy:setup-mock ',symbol)
(ad-safe-fset
',symbol
(lambda ,arglist
(setq el-spy:func-name ',symbol)
(el-spy:append-args ',symbol (list (mapcar 'symbol-value ',arglist)))
;; Even if body include interactive, it works well.
;; Undocumented behavior?
,@body)
)))
;; user level
(defmacro defproxy (symbol arglist &rest body)
;; todo
)
;; user level
(defun el-spy:make-returns-keylist (list default)
(append
(let ((i 0))
(mapcar (lambda (elem) (list (incf i) elem)) list))
`((t ,default))))
(defun el-spy:make-args-keylist (arg list default)
(el-spy:make-returns-keylist
(mapcar (lambda (elem) (list 'should `(equal ,arg ,elem))) list) 6)
)
;; user level
(defmacro el-spy:returns (list default)
`(case (el-spy:called-count el-spy:func-name)
,@(el-spy:make-returns-keylist list default)))
;; user level
(defmacro el-spy:args (symbol list default)
`(case (el-spy:called-count el-spy:func-name)
,@(el-spy:make-args-keylist symbol list default)))
;; el-mock limitation
;; (with-mock
;; (mock (test1) :times 0)
;; (call-interactively 'test1);; error
;; )
(provide 'el-spy)
;;; el-spy.el ends here