Class: RSpecFixtures::Matchers::Base
- Inherits:
-
Object
- Object
- RSpecFixtures::Matchers::Base
- Defined in:
- lib/rspec_fixtures/matchers/base.rb
Overview
A base matcher for fixture approvals
Direct Known Subclasses
Instance Attribute Summary collapse
-
#actual ⇒ Object
readonly
Returns the value of attribute actual.
-
#actual_distance ⇒ Object
readonly
Returns the value of attribute actual_distance.
-
#distance ⇒ Object
readonly
Returns the value of attribute distance.
-
#fixture_name ⇒ Object
readonly
Returns the value of attribute fixture_name.
Instance Method Summary collapse
-
#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.
-
#diffable? ⇒ Boolean
Lets RSpec know these matchers support diffing.
-
#expected ⇒ Object
Returns the expected value, from a fixture file.
-
#failure_message ⇒ Object
Called by RSpec when there is a failure.
-
#fixture_file ⇒ Object
Returns the path to the fixture file.
-
#fixtures_dir ⇒ Object
Returns the path to the fixtures directory.
-
#initialize(fixture_name = nil) ⇒ Base
constructor
A new instance of Base.
-
#interactive? ⇒ Boolean
Returns true if RSpec is configured to allow interactivity.
-
#matches?(actual) ⇒ Boolean
Called by RSpec.
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
#actual ⇒ Object (readonly)
Returns the value of attribute actual.
6 7 8 |
# File 'lib/rspec_fixtures/matchers/base.rb', line 6 def actual @actual end |
#actual_distance ⇒ Object (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 |
#distance ⇒ Object (readonly)
Returns the value of attribute distance.
6 7 8 |
# File 'lib/rspec_fixtures/matchers/base.rb', line 6 def distance @distance end |
#fixture_name ⇒ Object (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
53 54 55 |
# File 'lib/rspec_fixtures/matchers/base.rb', line 53 def diffable? true end |
#expected ⇒ Object
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_message ⇒ Object
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 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_file ⇒ Object
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_dir ⇒ Object
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.
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.
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 |