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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixture_name = nil) ⇒ Base

Returns a new instance of Base.



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

def initialize(fixture_name=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

#diff(distance) ⇒ Object

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



29
30
31
32
# File 'lib/rspec_fixtures/matchers/base.rb', line 29

def diff(distance)
  @distance = distance
  self
end

#diffable?Boolean

Lets RSpec know these matchers support diffing

Returns:

  • (Boolean)


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

def diffable?
  true
end

#expectedObject

Returns the expected value, from a fixture file



35
36
37
# File 'lib/rspec_fixtures/matchers/base.rb', line 35

def expected
  @expected ||= expected!
end

#failure_messageObject

Called by RSpec when there is a failure



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rspec_fixtures/matchers/base.rb', line 40

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



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

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

#fixtures_dirObject

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



66
67
68
# File 'lib/rspec_fixtures/matchers/base.rb', line 66

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)


60
61
62
# File 'lib/rspec_fixtures/matchers/base.rb', line 60

def interactive?
  RSpec.configuration.interactive_fixtures
end

#matches?(actual) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

  success = strings_match?

  if success or !interactive?
    success
  else
    approve_fixture
  end
end