-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrubyblock_test.vim
308 lines (247 loc) · 7.86 KB
/
rubyblock_test.vim
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
silent filetype indent plugin on
runtime! macros/matchit.vim
runtime! plugin/textobj/*.vim
set visualbell
function! SelectInsideFrom(number, position)
execute "normal ".a:number."G".a:position
execute "normal v\<Plug>(textobj-rubyblock-i)\<Esc>"
return [a:number, line("'<"), line("'>")]
endfunction
function! SelectAroundFrom(number, position)
execute "normal ".a:number."G".a:position
execute "normal v\<Plug>(textobj-rubyblock-a)\<Esc>"
return [a:number, line("'<"), line("'>")]
endfunction
function! SelectedRange()
return [line("'<"), line("'>")]
endfunction
describe 'rubyblock'
it 'should set a global variable'
Expect exists('g:loaded_textobj_rubyblock') ==# 1
end
end
describe 'default'
it 'should create named key maps'
for _ in ['<Plug>(textobj-rubyblock-a)', '<Plug>(textobj-rubyblock-i)']
execute "Expect maparg(".string(_).", 'c') == ''"
execute "Expect maparg(".string(_).", 'i') == ''"
execute "Expect maparg(".string(_).", 'n') == ''"
execute "Expect maparg(".string(_).", 'o') != ''"
execute "Expect maparg(".string(_).", 'v') != ''"
endfor
end
it 'should be set up mappings for visual and operator-pending modes only'
Expect maparg('ar', 'c') ==# ''
Expect maparg('ar', 'i') ==# ''
Expect maparg('ar', 'n') ==# ''
Expect maparg('ar', 'o') ==# '<Plug>(textobj-rubyblock-a)'
Expect maparg('ar', 'v') ==# '<Plug>(textobj-rubyblock-a)'
Expect maparg('ir', 'c') ==# ''
Expect maparg('ir', 'i') ==# ''
Expect maparg('ir', 'n') ==# ''
Expect maparg('ir', 'o') ==# '<Plug>(textobj-rubyblock-i)'
Expect maparg('ir', 'v') ==# '<Plug>(textobj-rubyblock-i)'
end
end
describe '<Plug>(textobj-rubyblock-i)'
before
silent tabnew t/samples/class.rb
end
after
silent tabclose
end
it 'selects inside of a class'
Expect SelectInsideFrom(1, '^') ==# [1, 2, 2]
end
end
describe '<Plug>(textobj-rubyblock-a)'
before
silent tabnew t/samples/class.rb
end
after
silent tabclose
end
it 'selects all of a class'
Expect SelectAroundFrom(1, '^') ==# [1, 1, 3]
end
end
describe 'nested while and unless blocks'
before
silent tabnew t/examples.rb
end
after
silent tabclose
end
it 'ignores nested while and unless blocks'
Expect SelectAroundFrom(69, '^') ==# [69, 68, 75]
Expect SelectAroundFrom(78, '^') ==# [78, 77, 83]
end
end
describe 'g:textobj_rubyblock_mids'
before
silent tabnew t/examples.rb
end
after
silent tabclose
end
context '1'
it 'selects only between mids'
let g:textobj_rubyblock_mids = 1
Expect SelectInsideFrom(87, '^') ==# [87, 87, 88]
end
end
context '0'
it 'ignores mids'
let g:textobj_rubyblock_mids = 0
Expect SelectInsideFrom(87, '^') ==# [87, 87, 90]
end
end
end
describe '<Plug>(textobj-rubyblock-i)'
before
silent tabnew t/samples/commented-end.rb
end
after
silent tabclose
end
it 'ignores "end" keyword inside of a comment'
execute "normal v\<Plug>(textobj-rubyblock-i)\<Esc>"
TODO
Expect SelectInsideFrom(1, '^') ==# [1, 2, 2]
end
end
describe 'if/else blocks'
before
silent tabnew t/samples/if-else.rb
end
after
silent tabclose
end
it 'ignores nested if/else block'
for number in [1,2,8]
Expect SelectInsideFrom(number, '^') ==# [number, 2, 7]
Expect SelectInsideFrom(number, '0') ==# [number, 2, 7]
Expect SelectAroundFrom(number, '0') ==# [number, 1, 8]
endfor
for number in [1,2,7,8]
Expect SelectAroundFrom(number, '^') ==# [number, 1, 8]
endfor
" this behaviour (already tested in loop) is unintuitive!
Expect SelectAroundFrom(7, '^') ==# [7, 1, 8]
end
it 'selects nested if/else block'
for number in [3,4,5,6,7]
Expect SelectInsideFrom(number, '^') ==# [number, 4, 6]
endfor
for number in [3,4,5,6]
Expect SelectAroundFrom(number, '^') ==# [number, 3, 7]
endfor
Expect SelectAroundFrom(7, '0') ==# [7, 3, 7]
end
end
describe 'nested blocks: (module > class > def > do)'
before
silent tabnew t/samples/nested-blocks.rb
end
after
silent tabclose
end
it 'selects around the module'
Expect SelectAroundFrom(1, '^') ==# [1, 1, 9]
Expect SelectAroundFrom(1, '$') ==# [1, 1, 9]
Expect SelectAroundFrom(9, '^') ==# [9, 1, 9]
Expect SelectAroundFrom(9, '$') ==# [9, 1, 9]
" FIXME: these cases may not be intuitive
Expect SelectAroundFrom(2, '0') ==# [2, 1, 9]
Expect SelectAroundFrom(8, '^') ==# [8, 1, 9]
end
it 'selects inside the module'
Expect SelectInsideFrom(1, '^') ==# [1, 2, 8]
Expect SelectInsideFrom(1, '$') ==# [1, 2, 8]
Expect SelectInsideFrom(9, '^') ==# [9, 2, 8]
Expect SelectInsideFrom(9, '$') ==# [9, 2, 8]
Expect SelectInsideFrom(2, '0') ==# [2, 2, 8]
" inconsistent?
Expect SelectInsideFrom(8, '$') ==# [8, 2, 8]
Expect SelectInsideFrom(8, '^') ==# [8, 3, 7]
end
it 'selects around the class'
Expect SelectAroundFrom(2, '^') ==# [2, 2, 8]
Expect SelectAroundFrom(3, '0') ==# [3, 2, 8]
Expect SelectAroundFrom(8, '0') ==# [8, 2, 8]
" FIXME: it feels as though this should work (but it doesn't):
" Expect SelectAroundFrom(8, '^') ==# [8, 2, 8]
end
it 'selects inside the class'
Expect SelectInsideFrom(2, '^') ==# [2, 3, 7]
Expect SelectInsideFrom(3, '0') ==# [3, 3, 7]
Expect SelectInsideFrom(7, '$') ==# [7, 3, 7]
" FIXME: it feels as though this should work (but it doesn't):
" Expect SelectInsideFrom(7, '^') ==# [7, 3, 7]
end
it 'selects around the `[].each do` block'
Expect SelectAroundFrom(4, 'fd') ==# [4, 4, 6]
Expect SelectAroundFrom(4, '$') ==# [4, 4, 6]
Expect SelectAroundFrom(5, '0') ==# [5, 4, 6]
Expect SelectAroundFrom(6, '0') ==# [6, 4, 6]
" FIXME: it feels as though this should work (but it doesn't):
" Expect SelectAroundFrom(4, '^') ==# [4, 4, 6]
end
it 'selects inside the `[].each do` block'
Expect SelectInsideFrom(4, 'fd') ==# [4, 5, 5]
Expect SelectInsideFrom(4, '$') ==# [4, 5, 5]
Expect SelectInsideFrom(5, '$') ==# [5, 5, 5]
Expect SelectInsideFrom(6, '0') ==# [6, 5, 5]
" FIXME: it feels as though this should work (but it doesn't):
" Expect SelectInsideFrom(4, '^') ==# [4, 5, 5]
end
it 'repeating `ar` expands the selection'
normal 5G
execute "normal v\<Plug>(textobj-rubyblock-i)\<Esc>"
Expect SelectedRange() ==# [5,5]
execute "normal v\<Plug>(textobj-rubyblock-a)\<Esc>"
Expect SelectedRange() ==# [4,6]
execute "normal v\<Plug>(textobj-rubyblock-a)\<Esc>"
Expect SelectedRange() ==# [3,7]
execute "normal v\<Plug>(textobj-rubyblock-a)\<Esc>"
Expect SelectedRange() ==# [2,8]
execute "normal v\<Plug>(textobj-rubyblock-a)\<Esc>"
Expect SelectedRange() ==# [1,9]
end
it 'repeating `ir` contracts the selection'
normal gg
execute "normal v\<Plug>(textobj-rubyblock-a)\<Esc>"
Expect SelectedRange() ==# [1,9]
execute "normal v\<Plug>(textobj-rubyblock-i)\<Esc>"
Expect SelectedRange() ==# [2,8]
execute "normal v\<Plug>(textobj-rubyblock-i)\<Esc>"
Expect SelectedRange() ==# [3,7]
execute "normal v\<Plug>(textobj-rubyblock-i)\<Esc>"
Expect SelectedRange() ==# [4,6]
execute "normal v\<Plug>(textobj-rubyblock-i)\<Esc>"
Expect SelectedRange() ==# [5,5]
end
end
describe 'rubyblocks with a method call'
after
silent tabclose
end
it 'handles `end.max` style method invocations'
silent tabnew t/samples/map-dot-max.rb
Expect SelectInsideFrom(2, '^') ==# [2, 2, 2]
Expect SelectAroundFrom(2, '^') ==# [2, 1, 3]
end
end
describe 'oneline conditionals'
before
silent tabnew t/samples/oneline-conditionals.rb
end
after
silent tabclose
end
it 'is not confused by trailing if/unless statements'
TODO
Expect SelectInsideFrom(2, '^') ==# [2, 2, 5]
Expect SelectAroundFrom(1, '^') ==# [1, 1, 6]
end
end