vimrunner 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -66,6 +66,55 @@ you can control Vim. For a full list of methods you can invoke on the remote
66
66
  Vim instance, check out the [`Client`
67
67
  documentation](http://rubydoc.info/gems/vimrunner/Vimrunner/Client).
68
68
 
69
+ ## Testing
70
+
71
+ If you're using Vimrunner for testing vim plugins, take a look at the
72
+ documentation for the
73
+ [Vimrunner::Testing](http://rubydoc.info/gems/vimrunner/Vimrunner/Testing)
74
+ module. It contains a few simple helpers that may make it a bit easier to write
75
+ regression tests in rspec. With them, it could work something like this:
76
+
77
+ ``` ruby
78
+ require 'spec_helper'
79
+ require 'vimrunner/testing'
80
+
81
+ describe "My Vim plugin" do
82
+ let(:vim) { some_instance_of_vim }
83
+
84
+ around :each do |example|
85
+ # needed only once for any Vim instance:
86
+ vim.add_plugin(File.expand_path('../my_plugin_path'), 'plugin/my_plugin.vim')
87
+
88
+ # ensure a clean temporary directory for each test:
89
+ Vimrunner::Testing.tmpdir(vim) do
90
+ example.call
91
+ end
92
+ end
93
+
94
+ specify "some behaviour" do
95
+ Vimrunner::Testing.write_file('test.rb', <<-EOF)
96
+ def foo
97
+ bar
98
+ end
99
+ EOF
100
+
101
+ vim.edit 'test.rb'
102
+ do_plugin_related_stuff_with(vim)
103
+ vim.write
104
+
105
+ IO.read('test.rb').should eq Vimrunner::Testing.normalize_string_indent(<<-EOF)
106
+ def bar
107
+ foo
108
+ end
109
+ EOF
110
+ end
111
+ end
112
+ ```
113
+
114
+ It's possible to make this a lot more concise by including
115
+ `Vimrunner::Testing`, by making your own helper methods that wrap common
116
+ behaviour, by extracting some code to `spec_helper.rb`, and so on.
117
+
69
118
  ## Requirements
70
119
 
71
120
  Vim needs to be compiled with `+clientserver`. This should be available with
@@ -0,0 +1,80 @@
1
+ require 'tmpdir'
2
+
3
+ module Vimrunner
4
+
5
+ # Public: Provides some utility helpers to assist in using Vimrunner for
6
+ # testing purposes.
7
+ module Testing
8
+
9
+ # Public: Within the given block, switches to a temporary directory for
10
+ # isolation purposes.
11
+ #
12
+ # Example:
13
+ #
14
+ # tmpdir(vim) do
15
+ # puts vim.command('pwd')
16
+ # end
17
+ #
18
+ # vim - a Vimrunner::Client instance
19
+ #
20
+ # Returns nothing.
21
+ # Yields nothing.
22
+ def tmpdir(vim)
23
+ Dir.mktmpdir do |dir|
24
+ Dir.chdir(dir) do
25
+ vim.command("cd #{dir}")
26
+ yield
27
+ end
28
+ end
29
+ end
30
+
31
+ # Public: Writes the given string to the file identified by "filename".
32
+ #
33
+ # Uses #normalize_string_indent to ensure consistent indentation when given
34
+ # a heredoc, and takes care to write it in the same way that Vim would.
35
+ #
36
+ # filename - a String, the name of the file to write
37
+ # string - a String, the contents of the file
38
+ #
39
+ # Returns nothing.
40
+ def write_file(filename, string)
41
+ string = normalize_string_indent(string)
42
+ File.open(filename, 'w') { |f| f.write(string + "\n") }
43
+ end
44
+
45
+ # Public: Normalizes a string's indentation whitespace, so that heredocs
46
+ # can be used more easily for testing.
47
+ #
48
+ # Example
49
+ #
50
+ # foo = normalize_string_indent(<<-EOF)
51
+ # def foo
52
+ # bar
53
+ # end
54
+ # EOF
55
+ #
56
+ # In this case, the raw string would have a large chunk of indentation in
57
+ # the beginning due to its location within the code. The helper removes all
58
+ # whitespace in the beginning by taking the one of the first line.
59
+ #
60
+ # Note: #scan and #chop are being used instead of #split to avoid
61
+ # discarding empty lines.
62
+ #
63
+ # string - a String, usually defined using a heredoc
64
+ #
65
+ # Returns a String with reduced indentation.
66
+ def normalize_string_indent(string)
67
+ if string.end_with?("\n")
68
+ lines = string.scan(/.*\n/).map(&:chop)
69
+ whitespace = lines.grep(/\S/).first.scan(/^\s*/).first
70
+ else
71
+ lines = [string]
72
+ whitespace = string.scan(/^\s*/).first
73
+ end
74
+
75
+ lines.map do |line|
76
+ line.gsub(/^#{whitespace}/, '') if line =~ /\S/
77
+ end.join("\n")
78
+ end
79
+ end
80
+ end
@@ -1,3 +1,3 @@
1
1
  module Vimrunner
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vimrunner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-06 00:00:00.000000000 Z
12
+ date: 2012-12-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -85,12 +85,13 @@ executables:
85
85
  extensions: []
86
86
  extra_rdoc_files: []
87
87
  files:
88
- - lib/vimrunner/server.rb
89
- - lib/vimrunner/platform.rb
90
- - lib/vimrunner/version.rb
88
+ - lib/vimrunner.rb
91
89
  - lib/vimrunner/client.rb
90
+ - lib/vimrunner/testing.rb
92
91
  - lib/vimrunner/errors.rb
93
- - lib/vimrunner.rb
92
+ - lib/vimrunner/version.rb
93
+ - lib/vimrunner/server.rb
94
+ - lib/vimrunner/platform.rb
94
95
  - vim/vimrc
95
96
  - bin/vimrunner
96
97
  - LICENSE
@@ -109,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
110
  version: '0'
110
111
  segments:
111
112
  - 0
112
- hash: 1355458872230700164
113
+ hash: 1021095784452390189
113
114
  required_rubygems_version: !ruby/object:Gem::Requirement
114
115
  none: false
115
116
  requirements: