Class: RSpecFixtures::Matchers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_fixtures/matchers/base.rb

Overview

A base matcher for fixture approvals

Direct Known Subclasses

MatchFixture, OutputFixture, RaiseFixture

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixture_name = nil) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/rspec_fixtures/matchers/base.rb', line 8

def initialize(fixture_name=nil)
  @before = nil
  @fixture_name = fixture_name
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



6
7
8
# File 'lib/rspec_fixtures/matchers/base.rb', line 6

def actual
  @actual
end

#actual_distanceObject (readonly)

Returns the value of attribute actual_distance.



6
7
8
# File 'lib/rspec_fixtures/matchers/base.rb', line 6

def actual_distance
  @actual_distance
end

#distanceObject (readonly)

Returns the value of attribute distance.



6
7
8
# File 'lib/rspec_fixtures/matchers/base.rb', line 6

def distance
  @distance
end

#fixture_nameObject (readonly)

Returns the value of attribute fixture_name.



6
7
8
# File 'lib/rspec_fixtures/matchers/base.rb', line 6

def fixture_name
  @fixture_name
end

Instance Method Details

#before(proc) ⇒ Object

Enables the ability to adjust the actual string before checking for matches: ‘expect(a).to match_fixture(f).before ->(actual) { actual.gsub /one/, ’two’ }‘



47
48
49
50
51
# File 'lib/rspec_fixtures/matchers/base.rb', line 47

def before(proc)
  @before ||= []
  @before << proc
  self
end

#diff(distance) ⇒ Object

Enables the ability to do something like: ‘expect(string).to match_fixture(file).diff(10) The distance argument is the max allowed Levenshtein Distance.



31
32
33
34
# File 'lib/rspec_fixtures/matchers/base.rb', line 31

def diff(distance)
  @distance = distance
  self
end

#diffable?Boolean

Lets RSpec know these matchers support diffing

Returns:

  • (Boolean)


72
73
74
# File 'lib/rspec_fixtures/matchers/base.rb', line 72

def diffable?
  true
end

#except(regex, replace = '...') ⇒ Object

Enables the ability to do something like: ‘expect(string).to match_fixture(file).except(/d+/)



38
39
40
41
42
# File 'lib/rspec_fixtures/matchers/base.rb', line 38

def except(regex, replace = '...')
  before ->(str) do
    str.gsub regex, replace
  end
end

#expectedObject

Returns the expected value, from a fixture file



54
55
56
# File 'lib/rspec_fixtures/matchers/base.rb', line 54

def expected
  @expected ||= expected!
end

#failure_messageObject

Called by RSpec when there is a failure



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rspec_fixtures/matchers/base.rb', line 59

def failure_message
  return "actual string is empty" if actual.empty?

  result = "expected: #{actual}\nto match: #{expected}"
  
  if distance
    result = "#{result}\n(actual distance is #{actual_distance} instead of the expected #{distance})"
  end

  result
end

#fixture_fileObject

Returns the path to the fixture file



96
97
98
# File 'lib/rspec_fixtures/matchers/base.rb', line 96

def fixture_file
  "#{fixtures_dir}/#{fixture_name}"
end

#fixtures_dirObject

Returns the path to the fixtures directory. Default: ‘spec/fixtures`



91
92
93
# File 'lib/rspec_fixtures/matchers/base.rb', line 91

def fixtures_dir
  RSpec.configuration.fixtures_path
end

#interactive?Boolean

Returns true if RSpec is configured to allow interactivity. By default, interactivity is enabled unless the environment variable ‘CI` is set.

Returns:

  • (Boolean)


85
86
87
# File 'lib/rspec_fixtures/matchers/base.rb', line 85

def interactive?
  RSpec.configuration.interactive_fixtures
end

#matches?(actual) ⇒ Boolean

Called by RSpec. This will be overridden by child matchers.

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec_fixtures/matchers/base.rb', line 14

def matches?(actual)
  @actual ||= actual
  return false if @actual.empty?

  @actual = sanitize @actual
  success = strings_match?

  if success or !interactive?
    success
  else
    approve_fixture
  end
end

#sanitize?Boolean

Returns true if RSpec is configured to sanitize (remove ANSI escape codes) from the actual strings before proceeeding to comparing them.

Returns:

  • (Boolean)


78
79
80
# File 'lib/rspec_fixtures/matchers/base.rb', line 78

def sanitize?
  RSpec.configuration.strip_ansi_escape
end