-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstate-tests.el
More file actions
209 lines (178 loc) · 7.13 KB
/
Copy pathstate-tests.el
File metadata and controls
209 lines (178 loc) · 7.13 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;;;; (executable-interpret (format "emacs -batch -L %s -l %s" (file-name-directory (locate-library "test-simple.elc")) buffer-file-name))
(require 'test-simple)
(test-simple-start)
(assert-t (load-file "./state.el") "Can't load ./state.el")
(note "state-define-state")
(assert-equal '(define-key state-prefix-map (kbd "0")
(lambda ()
"Switch to state `test'"
(interactive)
(state--do-switch "0")))
(macroexpand '(state-define-state test
:key "0"
:in fake)))
(defmacro setq-state (var &rest args)
"[Test helper]Define state and set it to VAR"
`(progn
(state-define-state ,var ,@args)
(setq ,var (state--get-state-by-name ',var))))
(put 'setq-state 'lisp-indent-function 1)
(setq-state in-directory
:key "a"
:in "~/.emacs.d/")
(setq-state in-file
:key "b"
:in "~/.emacs.d/init.el")
(setq-state switch-file
:key "c"
:switch "~/.emacs.d/init.el")
(setq-state switch-buf
:key "d"
:switch "*scratch*")
(setq-state in-sexp
:key "e"
:in (ignore 1))
(setq-state in-and-switch
:key "f"
:in "in"
:switch "switch")
(fset 'func-in 'ignore)
(setq-state create-in-exist-switch-before
:key "g"
:create func-create
:in func-in
:exist func-exist
:switch func-switch
:before func-before)
(note "state-define-state:state-key")
(assert-raises error
(state-define-state no-key :switch func-switch)
"no key")
(assert-equal "a" (state-key in-directory))
(note "state-define-state:state-create")
(assert-equal 'func-create (state-create create-in-exist-switch-before))
(assert-equal '(state--create-in-directory "~/.emacs.d/")
(state-create in-directory))
(assert-equal '(state--create-in-file "~/.emacs.d/init.el")
(state-create in-file))
(assert-equal '(state--create-switch-file "~/.emacs.d/init.el")
(state-create switch-file))
(assert-equal '(state--create-switch-buffer "*scratch*")
(state-create switch-buf))
(assert-equal '(state--create-switch-buffer "switch")
(state-create in-and-switch))
(note "state-define-state:state-in")
(assert-equal 'func-in (state-in create-in-exist-switch-before))
(assert-equal '(state--in-in-file "~/.emacs.d/")
(state-in in-directory))
(assert-equal '(state--in-switch-file "~/.emacs.d/init.el")
(state-in switch-file))
(assert-equal '(state--in-switch-buffer "*scratch*")
(state-in switch-buf))
(assert-equal '(state--in-in-file "in")
(state-in in-and-switch))
(assert-raises error
(state-define-state err
:key "E"
:switch func)
"no in")
(note "state-define-state:state-exist")
(assert-equal 'func-exist (state-exist create-in-exist-switch-before))
(assert-equal '(state--exist-in-file "~/.emacs.d/")
(state-exist in-directory))
(assert-equal '(state--exist-switch-buffer "*scratch*")
(state-exist switch-buf))
(assert-equal '(state--exist-in-file "in")
(state-exist in-and-switch))
(note "state-define-state:state-switch")
(assert-equal 'func-switch (state-switch create-in-exist-switch-before))
(assert-equal '(state--switch-in-file "~/.emacs.d/" 'in-directory)
(state-switch in-directory))
(assert-equal '(state--switch-switch-file "~/.emacs.d/init.el")
(state-switch switch-file))
(assert-equal '(state--switch-switch-buffer "*scratch*")
(state-switch switch-buf))
(assert-equal '(state--switch-default 'in-sexp)
(state-switch in-sexp))
(assert-equal '(state--switch-switch-buffer "switch")
(state-switch in-and-switch))
(note "state-define-state:state-before")
(assert-equal 'func-before (state-before create-in-exist-switch-before))
(assert-equal '(state--before-default 'in-directory)
(state-before in-directory))
(note "priority:bound")
(state-define-state 1 :key "C-a" :in "a" :bound 1 :priority 10)
(state-define-state 2 :key "C-a" :in "a" :bound 1)
(assert-equal '(2) (mapcar 'state-name (state--select-states "C-a" 'default)))
(state-define-state 3 :key "C-b" :in "a" :bound 1 :priority 10)
(state-define-state 4 :key "C-b" :in "a" :bound 1 :priority 5)
(state-define-state 5 :key "C-b" :in "a" :bound 1 :priority 7)
(assert-equal '(4) (mapcar 'state-name (state--select-states "C-b" 'default)))
(state-define-state 6 :key "C-c" :in "a" :bound 1 :priority 10)
(state-define-state 7 :key "C-c" :in "a" :bound 1 :priority 5)
(state-define-state 8 :key "C-c" :in "a" :bound 1 :priority 5)
(assert-equal '(7 8) (sort (mapcar 'state-name (state--select-states "C-c" 'default)) '<))
(note "priority:unbound")
(state-define-state 9 :key "C-d" :in "a" :priority 10)
(state-define-state 10 :key "C-d" :in "a")
(assert-equal '(9 10) (sort (mapcar 'state-name (state--select-states "C-d" 'default)) '<))
(note "priority:bound and unbound")
(state-define-state 11 :key "C-d" :in "a" :bound 1 :priority 9999)
(assert-equal '(11) (sort (mapcar 'state-name (state--select-states "C-d" 'default)) '<))
(note "state--get-state-in")
(set-buffer "*scratch*")
(assert-equal switch-buf (state--get-state-in))
(note "state--do-switch: new state")
(setq-state another-switch-buf
:key "A"
:switch "*another-scratch*")
(setq scratch-winconf (current-window-configuration))
(state--do-switch "A")
(assert-equal another-switch-buf (state--get-state-in))
;; save previous state to origin
(assert-equal 'switch-buf (state-origin another-switch-buf))
;; save window configuration before switching state
(assert-equal scratch-winconf (state-current switch-buf))
(note "state--do-switch: same state (switch to previous state)")
(state--do-switch "A")
(assert-equal switch-buf (state--get-state-in))
(note "state--do-switch: switch back")
(state--do-switch "A")
(assert-equal another-switch-buf (state--get-state-in))
(note "state--do-switch: call flow")
(setq-state exist-test-state
:key "B"
:exist (progn (push 'Bexist flow) t)
:switch (push 'Bswitch flow)
:before (push 'Bbefore flow)
:create (push 'Bcreate flow)
:in (push 'Bin flow)
:switch (push 'Bswitch flow))
(setq-state not-exist-in-state
:key "C"
:exist (progn (push 'Cexist flow) nil)
:switch (push 'Cswitch flow)
:before (push 'Cbefore flow)
:create (push 'Ccreate flow)
:in (progn (push 'Cin flow) t)
:switch (push 'Cswitch flow))
(setq-state not-exist-not-in-state
:key "D"
:exist (progn (push 'Dexist flow) nil)
:switch (push 'Dswitch flow)
:before (push 'Dbefore flow)
:create (push 'Dcreate flow)
:in (progn (push 'Din flow) nil)
:switch (push 'Dswitch flow))
(cl-letf (((symbol-function 'state--get-state-in)
(lambda () state--default-state)))
(let (flow) ;
(state--do-switch (state-key exist-test-state))
(assert-equal '(Bexist Bswitch Bbefore) (reverse flow)))
(let (flow)
(state--do-switch (state-key not-exist-in-state))
(assert-equal '(Cexist Ccreate Cin Cbefore) (reverse flow)))
(let (flow)
(state--do-switch (state-key not-exist-not-in-state))
(assert-equal '(Dexist Dcreate Din Dswitch Dbefore) (reverse flow))))
(end-tests)