• Stars
    star
    571
  • Rank 78,127 (Top 2 %)
  • Language
    Vim Script
  • Created about 11 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple Vimscript test framework

vader.vim

I use Vader to test Vimscript.

Vader test cases

Vader result

Installation

Use your favorite plugin manager.

  • vim-plug
    1. Add Plug 'junegunn/vader.vim' to .vimrc
    2. Run :PlugInstall

Running Vader tests

  • Vader [file glob ...]
  • Vader! [file glob ...]
    • Exit Vim after running the tests with exit status of 0 or 1
      • vim '+Vader!*' && echo Success || echo Failure
        • (You need to pass --nofork option when using GVim)
    • If the description of Do or Execute block includes FIXME or TODO, the block is recognized as a pending test case and does not affect the exit status.
    • If the environment variable VADER_OUTPUT_FILE is set, the test results will be written to it as well, otherwise they are written to stderr using different methods (depending on Neovim/Vim).

Syntax of .vader file

A Vader file is a flat sequence of blocks each of which starts with the block label, such as Execute:, followed by the content of the block indented by 2 spaces.

  • Given
    • Content to fill the execution buffer
  • Do
    • Normal-mode keystrokes that can span multiple lines
  • Execute
    • Vimscript to execute
  • Then
    • Vimscript to run after Do or Execute block. Used for assertions.
  • Expect
    • Expected result of the preceding Do/Execute block
  • Before
    • Vimscript to run before each test case
  • After
    • Vimscript to run after each test case

If you want to skip 2-space indention, end the block label with a semi-colon instead of a colon.

Basic blocks

Given

The content of a Given block is pasted into the "workbench buffer" for the subsequent Do/Execute blocks. If filetype parameter is given, &filetype of the buffer is set accordingly. It is also used to syntax-highlight the block in .vader file.

Given [filetype] [(comment)]:
  [input text]

Do

The content of a Do block is a sequence of normal-mode keystrokes that can freely span multiple lines. A special key can be written in its name surrounded by angle brackets preceded by a backslash (e.g. \<Enter>).

Do block can be followed by an optional Expect block.

Do [(comment)]:
  [keystrokes]

Execute

The content of an Execute block is plain Vimscript to be executed.

Execute block can also be followed by an optional Expect block.

Execute [(comment)]:
  [vimscript]

In Execute block, the following commands are provided.

  • Assertions
    • Assert <boolean expr>[, message]
    • AssertEqual <expected>, <got>[, message]
    • AssertNotEqual <unexpected>, <got>[, message]
    • AssertThrows <command>
      • This will set g:vader_exception (from v:exception) and g:vader_throwpoint (from v:throwpoint).
  • Other commands
    • Log "Message"
    • Save <name>[, ...]
    • Restore [<name>, ...]

The following syntax helper functions are provided:

  • SyntaxAt: return a string with the name of the syntax group at the following position:

    • SyntaxAt(): current cursor position
    • SyntaxAt(col): current cursor line, at given column
    • SyntaxAt(lnum, col): line and column
  • SyntaxOf(pattern[, nth=1]): return a string with the name of the syntax group at the first character of the nth match of the given pattern. Return '' if there was no match.

The path of the current .vader file can be accessed via g:vader_file.

In addition to plain Vimscript, you can also test Ruby/Python/Perl/Lua interface with Execute block as follows:

Execute [lang] [(comment)]:
  [<lang> code]

See Ruby and Python examples here.

Then

A Then block containing Vimscript can follow a Do or an Execute block. Mostly used for assertions. Can be used in conjunction with an Expect block.

Then [(comment)]:
  [vimscript]

Expect

If an Expect block follows an Execute block or a Do block, the result of the preceding block is compared to the content of the Expect block. Comparison is case-sensitive. filetype parameter is used to syntax-highlight the block.

Expect [filetype] [(comment)]:
  [expected output]

Hooks

Before

The content of a Before block is executed before every following Do/Execute block.

Before [(comment)]:
  [vim script]

After

The content of an After block is executed after every following Do/Execute block.

After [(comment)]:
  [vim script]

Macros

Include

You can include other vader files using Include macro.

Include: setup.vader

# ...

Include: cleanup.vader

Comments

Any line that starts with #, ", =, -, ~, ^, or * without indentation is considered to be a comment and simply ignored.

###################
# Typical comment #
###################

Given (fixture):
================
  Hello

Do (modification):
------------------
* change inner word
  ciw
* to
  World

Expect (result):
~~~~~~~~~~~~~~~~
  World

Example

# Test case
Execute (test assertion):
  %d
  Assert 1 == line('$')

  setf python
  AssertEqual 'python', &filetype

Given ruby (some ruby code):
  def a
    a = 1
    end

Do (indent the block):
  vip=

Expect ruby (indented block):
  def a
    a = 1
  end

Do (indent and shift):
  vip=
  gv>

Expect ruby (indented and shifted):
    def a
      a = 1
    end

Given c (C file):
  int i = 0;

Execute (syntax is good):
  AssertEqual SyntaxAt(2), 'cType'
  AssertEqual SyntaxOf('0'), 'cNumber'

Setting up isolated testing environment

When you test a plugin, it's generally a good idea to setup a testing environment that is isolated from the other plugins and settings irrelevant to the test. The simplest way to achieve this is to start Vim with a mini .vimrc as follows:

vim -Nu <(cat << EOF
filetype off
set rtp+=~/.vim/bundle/vader.vim
set rtp+=~/.vim/bundle/vim-markdown
set rtp+=~/.vim/bundle/vim-markdown/after
filetype plugin indent on
syntax enable
EOF) +Vader*

Travis CI integration

To make your project tested on Travis CI, you need to add .travis.yml to your project root. For most plugins the following example should suffice.

language: vim

before_script: |
  git clone https://github.com/junegunn/vader.vim.git

script: |
  vim -Nu <(cat << VIMRC
  filetype off
  set rtp+=vader.vim
  set rtp+=.
  set rtp+=after
  filetype plugin indent on
  syntax enable
  VIMRC) -c 'Vader! test/*' > /dev/null

(Note that vim is not a valid language for Travis CI. It just sets up Ruby execution environment instead as the default.)

Examples

Projects using Vader

See the wiki page.

Known issues

feedkeys() cannot be tested

The keystrokes given to the feedkeys() function are consumed only after Vader finishes executing the content of the Do/Execute block. Take the following case as an example:

Do (Test feedkeys() function):
  i123
  \<C-O>:call feedkeys('456')\<CR>
  789

Expect (Wrong!):
  123456789

You may have expected 123456789, but the result is 123789456. Unfortunately I have yet to find a workaround for this problem. Please let me know if you find one.

Some events may not be triggered

It is reported that CursorMoved event is not triggered inside a Do block. If you need to test a feature that involves autocommands on CursorMoved event, you have to manually invoke it in the middle of the block using :doautocmd.

Do (Using doautocmd):
  jjj
  :doautocmd CursorMoved\<CR>

Search history may not be correctly updated

This is likely a bug of Vim itself. For some reason, search history is not correctly updated when searches are performed inside a Do block. The following test scenario fails due to this problem.

Execute (Clear search history):
  for _ in range(&history)
    call histdel('/', -1)
  endfor

Given (Search and destroy):
  I'm a street walking cheetah with a heart full of napalm
  I'm a runaway son of the nuclear A-bomb
  I'm a world's forgotten boy
  The one who searches and destroys

Do (Searches):
  /street\<CR>
  /walking\<CR>
  /cheetah\<CR>
  /runaway\<CR>
  /search\<CR>

Execute (Assertions):
  Log string(map(range(1, &history), 'histget("/", - v:val)'))
  AssertEqual 'runaway', histget('/', -2)
  AssertEqual 'search', histget('/', -1)

The result is given as follows:

Starting Vader: 1 suite(s), 3 case(s)
  Starting Vader: /Users/jg/.vim/plugged/vader.vim/search-and-destroy.vader
    (1/3) [EXECUTE] Clear search history
    (2/3) [  GIVEN] Search and destroy
    (2/3) [     DO] Searches
    (3/3) [  GIVEN] Search and destroy
    (3/3) [EXECUTE] Assertions
      > ['search', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    (3/3) [EXECUTE] (X) Assertion failure: 'runaway' != ''
  Success/Total: 2/3
Success/Total: 2/3 (assertions: 0/1)
Elapsed time: 0.36 sec.

License

MIT

More Repositories

1

fzf

🌸 A command-line fuzzy finder
Go
55,523
star
2

vim-plug

🌺 Minimalist Vim Plugin Manager
Vim Script
33,335
star
3

fzf.vim

fzf ❀️ vim
Vim Script
9,178
star
4

goyo.vim

🌷 Distraction-free writing in Vim
Vim Script
4,404
star
5

vim-easy-align

🌻 A Vim alignment plugin
Vim Script
3,981
star
6

limelight.vim

πŸ”¦ All the world's indeed a stage and we are merely players
Vim Script
2,290
star
7

redis-stat

(UNMAINTAINED) A real-time Redis monitoring tool
Ruby
2,014
star
8

seoul256.vim

🌳 Low-contrast Vim color scheme based on Seoul Colors
Vim Script
1,604
star
9

gv.vim

A git commit browser in Vim
Vim Script
1,286
star
10

vim-peekaboo

πŸ‘€ " / @ / CTRL-R
Vim Script
1,102
star
11

vim-emoji

πŸ˜ƒ Emoji in Vim
Vim Script
606
star
12

vim-github-dashboard

:octocat: Browse GitHub events in Vim
Vim Script
481
star
13

vim-slash

Enhancing in-buffer search experience
Vim Script
322
star
14

fzf-git.sh

bash and zsh key bindings for Git objects, powered by fzf
Shell
280
star
15

vim-journal

πŸ“
Vim Script
259
star
16

myvim

🍱 Script to create a portable bundle of Vim environment
Shell
252
star
17

heytmux

Tmux scripting made easy
Ruby
139
star
18

vim-xmark

Live markdown preview for Vim on macOS (UNMAINTAINED; see https://github.com/iamcco/markdown-preview.nvim)
CSS
135
star
19

fzf-bin

117
star
20

vim-after-object

πŸ‘‰ Target text *after* the designated characters
Vim Script
112
star
21

vim-oblique

DEPRECATED Improved /-search (experimental)
Vim Script
83
star
22

vim-startuptime-benchmark

Outdated information
Vim Script
69
star
23

tmux-fzf-url

πŸš€ Quickly open urls on screen from your browser!
Shell
45
star
24

perlin_noise

Perlin noise generator in Ruby
Ruby
43
star
25

gimchi

Gimchi reads Korean.
Ruby
40
star
26

hbase-jruby

A JRuby binding for HBase
Ruby
38
star
27

tmux-fzf-maccy

Tmux plugin for Maccy and fzf integration
Shell
34
star
28

blsd

List directories in breadth-first order
Go
33
star
29

mvmv

Simple batch renaming script (ruby)
Ruby
27
star
30

vim-fnr

🎭 Find-N-Replace helper free of regular expressions
Vim Script
25
star
31

jdbc-helper

Deprecated. Use Sequel.
Ruby
19
star
32

jruby-daemon-template

Turn a JRuby script into an init-d style daemon
Shell
18
star
33

tabularize

Formatting tabular data with paddings
Ruby
18
star
34

mini-file-server

A simple, insecure file server
Clojure
17
star
35

ansi256

A Rubygem for colorizing text with 256-color ANSI codes
Ruby
16
star
36

vim-pseudocl

Pseudo-command-line (experimental)
Vim Script
16
star
37

insensitive_hash

DEPRECATED: Use hashie
Ruby
15
star
38

pipe-logger

Log rotation of stdout & stderr
Ruby
14
star
39

grouper

A simple batch processing facility
Clojure
12
star
40

vim-redis

Experimental Redis plugin for Vim
Vim Script
12
star
41

lq

A simple HTTP server for queuing lines of text
Clojure
11
star
42

clj-inspector

Inspector helps debugging Clojure programs
Clojure
11
star
43

parallelize

Simple multi-threading for Ruby
Ruby
10
star
44

tre-ruby

Approximate regular expression matching using TRE
Ruby
10
star
45

treely

Library for generating tree diagram of nested data structure
Clojure
9
star
46

junegunn

8
star
47

coffee-processing

Helps writing Processing.js sketches in CoffeeScript
Ruby
8
star
48

ssh-copy-id.rb

Unmaintained
Ruby
7
star
49

agl

List files and directories using ag
Ruby
7
star
50

si

Human-readable numbers with SI prefix (metric prefix)
Ruby
6
star
51

evented-servers

experiments with libev
C
6
star
52

vim-ruby-x

if_ruby helper
Vim Script
6
star
53

microbe

A simple micro benchmark helper for Clojure
Clojure
5
star
54

vim-cfr

Decompile Java class files using CFR
Vim Script
5
star
55

each_line_reverse

Read lines of a file in reverse order
Ruby
4
star
56

shorten

Number shortener
Ruby
4
star
57

proco

experimental: a lightweight asynchronous task executor designed for efficient batch processing
Ruby
4
star
58

lps

lps: rate-controlled loop execution
Ruby
3
star
59

SuperCSV

A fork of SuperCSV project with a few fixes
Java
2
star
60

colored-not

Toggles methods from colored gem
Ruby
2
star
61

img2xterm-clj

img2xterm rewritten in clojure (for no good reason)
Clojure
2
star
62

jrubysql

SQL client for any JDBC-compliant database.
Ruby
2
star
63

linux-scripts

Ruby
2
star
64

zipfian

Zipfian distribution in Ruby
Ruby
2
star
65

rcron

A simple cron-like scheduler for Ruby
Ruby
2
star
66

option_initializer

Object construction with method chaining
Ruby
2
star
67

quote_unquote

Wraps (and unwraps) strings with quotes
Ruby
1
star
68

maven_dependency

A Ruby gem to resolve maven dependencies
Ruby
1
star
69

coffee-processing-live

coffee-processing demo app
JavaScript
1
star
70

tweet-backup-ruby

Ruby script for backing up tweets
Ruby
1
star
71

i

1
star
72

hbase-client-dep

Makefile
1
star
73

each_sql

Enumerate each SQL statement in SQL scripts
Ruby
1
star
74

srsly

SRSLY? NO!
Ruby
1
star
75

omniauth-nate

OmniAuth strategy for Nate.com (Nate/Cyworld)
Ruby
1
star
76

oxm

A tiny Object-XML-Mapper for Ruby
Ruby
1
star