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
-
#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’ }‘.
-
#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.
-
#diffable? ⇒ Boolean
Lets RSpec know these matchers support diffing.
-
#except(regex, replace = '...') ⇒ Object
Enables the ability to do something like: ‘expect(string).to match_fixture(file).except(/d+/).
-
#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.
-
#sanitize? ⇒ Boolean
Returns true if RSpec is configured to sanitize (remove ANSI escape codes) from the actual strings before proceeeding to comparing them.
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
#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
#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
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 |
#expected ⇒ Object
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_message ⇒ Object
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 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
96 97 98 |
# File 'lib/rspec_fixtures/matchers/base.rb', line 96 def fixture_file "#{fixtures_dir}/#{fixture_name}" end |
#fixtures_dir ⇒ Object
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.
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.
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.
78 79 80 |
# File 'lib/rspec_fixtures/matchers/base.rb', line 78 def sanitize? RSpec.configuration.strip_ansi_escape end |