/
vspec.txt
614 lines (470 loc) · 20.5 KB
/
vspec.txt
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
*vspec.txt* Testing framework for Vim script
Version 1.4.0
Script ID: 3012
Copyright (C) 2009-2014 Kana Natsuno <http://whileimautomaton.net/>
License: MIT license {{{
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}}}
CONTENTS *vspec-contents*
Introduction |vspec-introduction|
Interface |vspec-interface|
External Commands |vspec-external-commands|
Commands |vspec-commands|
Functions |vspec-functions|
Examples |vspec-examples|
FAQ |vspec-faq|
Bugs |vspec-bugs|
Changelog |vspec-changelog|
==============================================================================
INTRODUCTION *vspec-introduction*
*vspec* is a testing framework for Vim script. It consists of:
* A driver script to run a test suite,
* A Vim plugin to write tests in a format which resembles RSpec,
* Additional syntax/indent files for Vim script to write tests.
Since vspec is a part of a system to automated tests for Vim script,
it requires several steps to set up an environment to easily run tests.
See the following article: http://whileimautomaton.net/2013/02/13211500
Requirements:
- Vim 7.3 or later
Latest version:
http://github.com/kana/vim-vspec
Document in HTML format:
http://vim-doc.heroku.com/view?https://raw.github.com/kana/vim-vspec/master/doc/vspec.txt
==============================================================================
INTERFACE *vspec-interface*
------------------------------------------------------------------------------
EXTERNAL COMMANDS *vspec-external-commands*
*bin/vspec*
bin/vspec [{non-standard-runtimepath} ...] {input-script}
Utility command to test Vim script with vspec.
{input-script} is the path to a file which is a test
script written with vspec.
{non-standard-runtimepath} is the path to a directory
to use as a part of 'runtimepath' while executing
{input-script}.
Examples: >
$ ./bin/vspec . test/context.input
------------------------------------------------------------------------------
COMMANDS *vspec-commands*
:describe {subject} *:describe*
:end
Define a set of examples about {subject}. {subject}
is a string to describe the subject of the examples.
:describe can be nested. It is useful to group
examples by their category. For example: >
describe 'textobj-function'
describe 'af'
it 'targets a whole function'
...
end
end
describe 'if'
it 'targets the inner code of a function'
...
end
end
end
:context {subject} *:context*
:end
Define a set of examples about {subject}. This is an
alias of |:describe|. It is useful to make nested
groups more readable. For example: >
describe 'textobj-function'
context 'with JavaScript'
it 'targets a function'
...
end
end
context 'with Ruby'
it 'targets a method'
...
end
end
end
:it {example} *:it*
:end
Define an example. {example} is a string to describe
the example. An |:it| block must be written in
a |:describe| block.
Examples: >
describe 'comparison operators'
it 'should match case-sensitively'
Expect 'abc' =~# 'a'
Expect 'abc' !~# 'A'
end
end
:Expect {actual} {matcher} {expected} *:Expect*
Describe an expectation - test whether {actual} value
matches with {expected} value, in a sense of
{matcher}.
{actual} and {expected} is an arbitrary Vim script
|expression| with the following limitation:
- Comparing operators, such as ==, !~, etc, cannot be
used in {actual} and {expected}. See |expr4| for
the list of comparing operators in Vim script.
{matcher} is a comparing operator, such as ==, !~,
etc, which is described in |expr4|.
You have to insert one or more spaces between
{actual}, {matcher} and {expected}.
Examples: >
:Expect 'abc' =~# 'A'
" ==> good
:Expect 'abc' =~? 'A'
" ==> bad
:Expect {} == {}
" ==> good
:Expect {} isnot {}
" ==> bad
:Expect {actual} {custom-matcher} [{arg}, ...] *vspec-custom-matcher*
If {custom-matcher} starts with "to", :Expect acts in
another mode. {custom-matcher} is treated as an alias
of a function which is called "custom matcher".
The custom matcher will be called with {actual} and
{arg}s. It must return true if {actual} value is
valid, otherwise it must return false.
See also:
- |vspec-predefined-custom-matchers|
- |vspec-custom-matcher-examples|
- |vspec#customize_matcher()|
*vspec-predefined-custom-matchers*
:Expect {actual} to_be_true *vspec-to_be_true*
:Expect {actual} to_be_false *vspec-to_be_false*
Test whether {actual} is evaluated to true/false.
:Expect {actual} toBeTrue
:Expect {actual} toBeFalse
Deprecated. Old names of to_be_true/to_be_false.
*vspec-to_throw*
:Expect expr { {expression} } to_throw [{regexp}]
Test whether a given {expression} throws an exception
or not. This expectation succeeds if an exception is
thrown and |v:exception| is matched to {regexp}.
Otherwise, this expectation fails.
{regexp} is optional. In this case, this expectation
succeeds if an exception is thrown. The content of
|v:exception| does not matter.
Note that you must wrap {expression} with "expr {" and
"}" to denote the {expression} might fail.
:Expect {actual} not {matcher} {expected} *:Expect-not*
:Expect {actual} not {custom-matcher} [{arg}, ...]
Like |:Expect|, but test whether {actual} value does
NOT match with {expected} value, in a sense of
{matcher} or {custom-matcher}.
:before *:before*
:end
:after *:after*
:end
Define a code block which is run before/after each
example defined by |:it|.
Note that:
- Only one :before/:after block can be written in each
|:describe| block.
- :before/:after blocks cannot be defined for
|:describe| blocks.
- :before/:after blocks cannot be defined for
a specific |:it| block.
These limitations might be resolved later.
:ResetContext *:ResetContext*
Reset the current context of a script with one saved
by |:SaveContext|.
To use this feature, you have to tell "scope" hint to
vspec with |vspec#hint()|.
:SaveContext *:SaveContext*
Save the current context of a script, i.e., an
snapshot of script-local variables defined in the
script. See also |:ResetContext|.
:SaveContext is automatically executed whenever
|vspec#hint()| is called with "scope" hint.
To use this feature, you have to tell "scope" hint to
vspec with |vspec#hint()|.
:TODO *:TODO*
Represent that the current example is not written yet.
The current example will be always failed.
:SKIP {message} *:SKIP*
Skip the current example.
The current example will be always succeeded.
{message} is a string which will be displayed as an
additional message to provide a hint about the skipped
example. Note that single-quoted strings are only
supported at the moment.
------------------------------------------------------------------------------
FUNCTIONS *vspec-functions*
Call(...) *Call()*
Alias of |vspec#call()|.
Ref(...) *Ref()*
Alias of |vspec#ref()|.
Set(...) *Set()*
Alias of |vspec#set()|.
vspec#call({funcname}, [{arg}, ...]) *vspec#call()*
Like |call()|, but vspec#call() can call
a script-local function defined in a script to be
tested.
{funcname} is a string which represents the name of
a function. If {funcname} starts with 's:', it calls
a script-local function defined in a script to be
tested. To use this feature, you have to tell
"sid" hint to vspec with |vspec#hint()|.
{arg} is an arbitrary value which is given to the
function corresponding to o{funcname}.
vspec#customize_matcher({alias}, {matcher}) *vspec#customize_matcher()*
Register {matcher} as a |vspec-custom-matcher| with
a given {alias}. {alias} should be snake_case.
See also |vspec-custom-matcher-examples|.
{matcher} is a dictionary with the following items:
"match" (required)
A |Funcref| to determine whether {actual}
value matches to {expected} value. It takes
1 or more arguments. The first argument is
{actual} value given to |:Expect|, and the
rest of arguments are arbitrary {expected}
values. It returns true if {actual} value is
matched to {expected} value, or false
otherwise.
"failure_message_for_should" (optional)
A |Funcref| to generate user friendly message
for failed match with |:Expect|. It takes
arguments the same as "match", and it returns
a string or a list of strings to describe
a failure.
"failure_message_for_should_not" (optional)
Like "failure_message_for_should", but it is
used to generate failure message for
|:Expect-not|.
vspec#customize_matcher({alias}, {function}) *vspec#customize_matcher()-old*
Deprecated. Use |vspec#customize_matcher()| instead.
This style is remiained for backward compatibility.
vspec#hint({info}) *vspec#hint()*
Tell vspec "hint" information to use useful API to
test a Vim script.
{info} is a dictionary. Each key is a string. The
detail of this dictionary is as follows:
"sid" (optional)
A string which is evaluated to <SID> of
a script to be tested.
If this value is given, you can use
|vspec#call()|.
"scope" (optional)
A string which is evaluated to the dictionary
corresponding to s: (= the scope of
script-local variables) for a script.
If this value is given, you can use
|vspec#ref()| and |vspec#set()|.
vspec#pretty_string({value}) *vspec#pretty_string()*
Similar to |string()|, but use "string" notation
rather than 'string' notation to encode {value} to
a string. Its result consists of only printable
characters, so that it is safe to |:echo|. But it can
be decoded to the original {value} with |eval()|,
unlike |strtrans()|.
vspec#ref({varname}) *vspec#ref()*
Return the value of a variable.
{varname} is a string which represents the name of
a script-local variable defined in a script to be
tested. The name must start with 's:'. To use this
feature, you have to tell vspec to "scope" hint with
|vspec#hint()|.
vspec#set({varname}, {value}) *vspec#set()*
Assign {value} to a variable. See |vspec#ref()| for
the details of {varname}.
vspec#test({specfile-path}) *vspec#test()*
|:source| {specfile-path}, test examples described in
the file, then output results of the exmaples.
Output format is Test Anything Protocol version 12.
See also http://testanything.org/ for the details.
==============================================================================
EXAMPLES *vspec-examples*
See also files in the "t" directory in a source tree of this plugin:
https://github.com/kana/vim-vspec/tree/master/t
CUSTOM MATCHERS *vspec-custom-matcher-examples*
(a) A simple matcher which checks only an actual value: >
function! ToBeTrue(actual_value)
return (type(a:actual_value) == type(0)
\ ? a:actual_value
\ : !!0)
endfunction
call vspec#customize_matcher(
\ 'to_be_true',
\ function('ToBeTrue')
\ )
:Expect 123 to_be_true
" ===> good
:Expect [123] to_be_true
" ===> bad
(b) A matcher which takes arguments about an expected value: >
function! ToBeBetween(actual, expected_min, expected_max)
return a:expected_min <= a:actual && a:actual <= a:expected_max
endfunction
call vspec#customize_matcher(
\ 'to_be_between',
\ function('ToBeBetween')
\ )
:Expect 15 to_be_between 10, 20
" ===> good
:Expect 42 to_be_between 10, 20
" ===> bad
==============================================================================
FAQ *vspec-faq*
(a) ":read file" in an empty buffer doesn't keep an empty line.
|bin/vspec| invokes a Vim process in |Ex-mode| to automate running
a test script. Most commands works the same as interactive, non-Ex
mode Vim. However, a few commands work differently for POSIX
compatibility. ":read file" is the most notable example. See
|new-posix| for the details.
(b) Autoload variables must be prefixed with g: in :it/:before/:after blocks.
Code in |:it|, |:before| and |:after| blocks are evaluated the same as
code in functions. Inside functions, variables without any scope
prefix are treated as local variables. Since |autoload| variables are
just global variables which have special characters in their names,
the |g:| scope prefix is required to refer autoload variables.
(c) To emulate user input, use |:normal| instead of |feedkeys()|.
For example, omni completion can be activated by:
>
execute "normal! a\<C-x>\<C-o>"
<
But
>
call feedkeys("a\<C-x>\<C-o")
<
does nothing. Because feedkeys() only pushes characters into the
input queue. When Vim becomes idle and the input queue is not empty,
Vim will take a character from the input queue and processes it as if
user typed the character. But Vim will never be idle while running
a test script, because Vim is always busy to execute each line in the
test script.
(d) To respond to |input()|, use |:normal|.
Suppose that you define <Plug>InteractiveCommand which calls input(),
and you want to write a test for it. Use :normal as follows to queue
a response to the input() from <Plug>InteractiveCommand:
>
execute "normal \<Plug>InteractiveCommand{response}\<CR>"
<
Technically you can use also feedkeys() to queue a response.
But resulting code is hard read, because the order of code is
scrambled -- you have to feedkeys() before :normal.
(e) plugin/*.vim must be explicitly |:source|d.
This is an intentional behavior to set up configuration variables, key
mappings, etc before loading plugin/*.vim.
Several plugins behave differently depending on the current
configuration. Some plugins define key mappings based on their
configuration variables. Some plugins try not to override
user-defined key mappings.
If plugin/*.vim are automatically :source'd, we have to provide a way
to emulate user's vimrc to reproduce such situations. It makes things
complicated.
==============================================================================
BUGS *vspec-bugs*
- See http://github.com/kana/vim-vspec/issues for known issues and planned
features.
==============================================================================
CHANGELOG *vspec-changelog*
1.4.0 2014-04-12T15:19:32+09:00 *vspec-changelog-1.4.0*
- Support nested |:describe|.
- Support |:context| as an alias of |:describe|.
1.3.1 2014-03-26T21:51:39+09:00 *vspec-changelog-1.3.1*
- When an unexpected error happens while running a test script,
vspec showed |v:exception| and |v:throwpoint| about the error.
But v:throwpoint includes also an internal call stack of vspec.
It's somewhat confusing for users to grasp where the error happens.
Now vspec shows v:throwpoint without the internal call stack.
- Now it's safe to use the same description for multiple |:it| within
a |:describe| block. In old versions, such :it blocks are not
properly executed. All but the last :it block will never be
executed, and only the last :it block will be executed many times.
Though it is recommended to write a distinct description for each
:it block, the old behavior is surprising for users, and it is
annoying when writing one-time tests to check nontrivial behavior.
- Now it's safe to use combining characters such as U+0300 (grave
accent) in :it descriptions. In old versions use of such characters
causes unexpected errors or results depending on 'encoding'.
1.3.0 2014-03-16T18:28:33+09:00 *vspec-changelog-1.3.0*
- Add |vspec#pretty_string()|.
- Fix to properly show special characters in failure messages. In old
versions, special characters especially line feeds are rendered as
is, so that resulting messages are not in valid TAP format.
1.2.0 2014-02-13T23:12:36+09:00 *vspec-changelog-1.2.0*
- Add "to_throw" matcher to test whether an exception is thrown or
not. See |vspec-to_throw| for the details.
1.1.4 2014-02-08T20:28:39+09:00 *vspec-changelog-1.1.4*
- Add more notes on common pitfalls to write tests. See |vspec-faq|.
- Fix a bug that |:it| fails if {example} starts with a number.
- Fix to generate a proper failure message for TAP harness if an
unexpected error happens outside |:it| blocks. In old versions,
vspec generates only a diagnostic message.
- Fix to correctly show multiline error messages, especially for
multiline tracebacks given from Python.
1.1.3 2014-02-02T20:18:47+09:00 *vspec-changelog-1.1.3*
- Accept double-quoted strings in |:describe| and |:it|.
- Add a known issue on |:read|. See |vspec-faq|.
1.1.2 2013-06-11T22:30:06+09:00 *vspec-changelog-1.1.2*
- Fix |bin/vspec| to output lines with LF, not CR LF.
- Add |vspec-custom-matcher-examples|.
1.1.1 2013-06-10T22:30:31+09:00 *vspec-changelog-1.1.1*
- Fix |bin/vspec| to work properly when
- No {non-standard-runtimepath} is given.
- Any {non-standard-runtimepath} contains spaces.
1.1.0 2012-10-29T22:19:45+09:00 *vspec-changelog-1.1.0*
- |:Expect-not|:
- Show proper messages for failed expectations.
- |vspec-custom-matcher|:
- Change the naming guideline.
- Use snake_case instead of camelCase.
- Add new aliases for predefined custom matcheres.
- Old aliases are still supported, but deprecated.
- Support custom failure message.
- See |vspec#customize_matcher()| for the details.
- It was not possible to show meaningful messages for failed
expectations using custom mathcers with old versions.
- Change the syntax to register new matcher.
- Old syntax is still supported, but deprecated.
- Fix to properly support custom matchers with 2 or more arguments.
1.0.2 2012-02-12T21:02:50+09:00 *vspec-changelog-1.0.2*
- |bin/vspec|:
- Remove user's |after-directory| from 'runtimepath' to avoid
unexpected interference to run tests.
- Remove the output adjustment for the current directory.
- |:Expect|: Fix to parse expressions including '"' and '|' correctly.
- Add missing syntax highlighting for |:SKIP| and |:TODO|.
1.0.1 2012-02-10T23:00:35+09:00 *vspec-changelog-1.0.1*
- Fix not to install test scripts.
1.0.0 2012-01-31T00:32:15+09:00 *vspec-changelog-1.0.0*
- Completely rewritten. No compatibility with old versions.
- Output format is changed to Test Anything Protocol version 12.
- The style to write examples is changed.
See also |vspec-examples| for the details.
- Change the order of arguments to |bin/vspec|.
- Change the syntax for |vspec-custom-matcher| to be extensible and
readable.
- Add |:Expect-not| for counter examples.
- Add |:TODO| for incomplete examples.
- Add |:SKIP| for environment-specific examples.
- Add |:before| blocks and |:after| blocks.
0.0.4 2010-04-04T20:59:35+09:00 *vspec-changelog-0.0.4*
- Add additional syntax file for |vspec-commands|.
- |:Should|: Provide expression completion for convenience.
- Improve minor stuffs.
0.0.3 2010-03-12T21:43:12+09:00 *vspec-changelog-0.0.3*
- Add test driver |bin/vspec|.
0.0.2 2010-02-20T23:11:03+09:00 *vspec-changelog-0.0.2*
- Add |vspec-custom-matcher|.
- Fix typos in the document.
0.0.1 2009-11-18T20:44:13+09:00 *vspec-changelog-0.0.1*
- Test driver: Fix not to read/write viminfo file for reproducibility
and to avoid overwrite user's viminfo unexpectedly.
0.0.0 2009-10-12T10:51:26+09:00 *vspec-changelog-0.0.0*
- Initial version.
==============================================================================
vim:tw=78:ts=8:ft=help:norl:fen:fdl=0:fdm=marker: