Module: Vimrunner::Testing

Defined in:
lib/vimrunner/testing.rb,
lib/vimrunner/rspec.rb

Overview

Public: Provides some utility helpers to assist in using Vimrunner for testing purposes.

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



29
30
31
# File 'lib/vimrunner/rspec.rb', line 29

def instance
  @instance
end

Instance Method Details

#normalize_string_indent(string) ⇒ Object

Public: Normalizes a string’s indentation whitespace, so that heredocs can be used more easily for testing.

Example

foo = normalize_string_indent(<<-EOF)
  def foo
    bar
  end
EOF

In this case, the raw string would have a large chunk of indentation in the beginning due to its location within the code. The helper removes all whitespace in the beginning by taking the one of the first line.

Note: #scan and #chop are being used instead of #split to avoid discarding empty lines.

string - a String, usually defined using a heredoc

Returns a String with reduced indentation.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vimrunner/testing.rb', line 44

def normalize_string_indent(string)
  if string.end_with?("\n")
    lines      = string.scan(/.*\n/).map(&:chop)
    whitespace = lines.grep(/\S/).first.scan(/^\s*/).first
  else
    lines      = [string]
    whitespace = string.scan(/^\s*/).first
  end

  lines.map do |line|
    line.gsub(/^#{whitespace}/, '') if line =~ /\S/
  end.join("\n")
end

#vimObject



32
33
34
# File 'lib/vimrunner/rspec.rb', line 32

def vim
  Testing.instance ||= Vimrunner::RSpec.configuration.start_vim_method.call
end

#write_file(filename, string) ⇒ Object

Public: Writes the given string to the file identified by “filename”.

Uses #normalize_string_indent to ensure consistent indentation when given a heredoc, and takes care to write it in the same way that Vim would.

filename - a String, the name of the file to write string - a String, the contents of the file

Returns nothing.



18
19
20
21
# File 'lib/vimrunner/testing.rb', line 18

def write_file(filename, string)
  string = normalize_string_indent(string)
  File.open(filename, 'w') { |f| f.write(string + "\n") }
end