Module: GollyUtils::Testing::RSpecMatchers

Defined in:
lib/golly-utils/testing/rspec/files.rb,
lib/golly-utils/testing/rspec/arrays.rb,
lib/golly-utils/testing/rspec/base.rb

Overview


Instance Method Summary collapse

Instance Method Details

#be_file_with_contents(contents, *extra) ⇒ Object Also known as: be_file_with_content

Checks that a file exists and has expected content.

Examples:

# Specify a single string to for a straight 1:1 comparison.
'version.txt'.should be_file_with_contents "2\n"

# Use regex and the and() method to add multiple expectations
'Gemfile'.should be_file_with_contents(/['"]rspec['"]/).and(/['"]golly-utils['"]/)

# Negative checks can be added too
'Gemfile'.should be_file_with_contents(/gemspec/).and_not(/rubygems/)

With normalisation

# You can specify functions to normalise both the file and expectation.
'version.txt'.should be_file_with_contents("2").when_normalised_with(&:chomp)

# You can add multiple normalisation functions by specifying and() after the first
'stuff.txt'.should be_file_with_contents(/ABC/)
                   .and(/DEF/)
                   .and(/123\n/)
                   .when_normalised_with(&:upcase)
                   .and(&:chomp)


258
259
260
# File 'lib/golly-utils/testing/rspec/files.rb', line 258

def be_file_with_contents(contents, *extra)
  FileWithContents.new.and(contents).and(extra)
end

#equal_array(expected) ⇒ Object

Note:

The order and frequency of elements matters; call sort or uniq first if required.

Passes if an array is the same as the target array.

The advantage of calling this rather than == is that the error messages on failure here are customised for array comparison and will provide much more useful description of why the arrays don't match.

Examples:

%w[a a b].should equal_array %w[a a b]
%w[b a b].should_not equal_array %w[a a b]
files.should equal_array(expected)


78
79
80
81
# File 'lib/golly-utils/testing/rspec/arrays.rb', line 78

def equal_array(expected)
  return be_nil if expected.nil?
  EqualsArray.new(expected)
end

#exist_as_a_dirObject Also known as: exist_as_dir

Passes if a directory exists (relative to the current directory) with a name specified by the target string.

Examples:

'lib'.should exist_as_a_dir
'cache/z01'.should_not exist_as_a_dir


144
145
146
# File 'lib/golly-utils/testing/rspec/files.rb', line 144

def exist_as_a_dir
  ExistAsDir.new
end

#exist_as_a_fileObject Also known as: exist_as_file

Passes if a file exists (relative to the current directory) with a name specified by the target string.

Note: This only passes if a file is found; a directory with the same name will fail.

Examples:

'Gemfile'.should exist_as_a_file
'/tmp/stuff'.should_not exist_as_a_file


102
103
104
# File 'lib/golly-utils/testing/rspec/files.rb', line 102

def exist_as_a_file
  ExistAsFile.new
end