Don't depend on RSpec version check

This commit is contained in:
Thomas Walpole 2018-06-08 15:25:47 -07:00
parent ac86d0be92
commit 3634121afe
4 changed files with 79 additions and 83 deletions

View File

@ -1,92 +1,94 @@
# frozen_string_literal: true
module Capybara
module RSpecMatchers
module Compound
include ::RSpec::Matchers::Composable
if defined?(::RSpec::Expectations::Version)
module Capybara
module RSpecMatchers
module Compound
include ::RSpec::Matchers::Composable
def and(matcher)
And.new(self, matcher)
end
def and_then(matcher)
::RSpec::Matchers::BuiltIn::Compound::And.new(self, matcher)
end
def or(matcher)
Or.new(self, matcher)
end
class CapybaraEvaluator
def initialize(actual)
@actual = actual
@match_results = Hash.new { |h, matcher| h[matcher] = matcher.matches?(@actual) }
def and(matcher)
And.new(self, matcher)
end
def matcher_matches?(matcher)
@match_results[matcher]
def and_then(matcher)
::RSpec::Matchers::BuiltIn::Compound::And.new(self, matcher)
end
def reset
@match_results.clear
def or(matcher)
Or.new(self, matcher)
end
end
class And < ::RSpec::Matchers::BuiltIn::Compound::And
private
class CapybaraEvaluator
def initialize(actual)
@actual = actual
@match_results = Hash.new { |h, matcher| h[matcher] = matcher.matches?(@actual) }
end
def match(_expected, actual)
@evaluator = CapybaraEvaluator.new(actual)
syncer = sync_element(actual)
begin
syncer.synchronize do
@evaluator.reset
raise ::Capybara::ElementNotFound unless [matcher_1_matches?, matcher_2_matches?].all?
true
def matcher_matches?(matcher)
@match_results[matcher]
end
def reset
@match_results.clear
end
end
class And < ::RSpec::Matchers::BuiltIn::Compound::And
private
def match(_expected, actual)
@evaluator = CapybaraEvaluator.new(actual)
syncer = sync_element(actual)
begin
syncer.synchronize do
@evaluator.reset
raise ::Capybara::ElementNotFound unless [matcher_1_matches?, matcher_2_matches?].all?
true
end
rescue StandardError
false
end
rescue StandardError
false
end
end
def sync_element(el)
if el.respond_to? :synchronize
el
elsif el.respond_to? :current_scope
el.current_scope
else
Capybara.string(el)
end
end
end
class Or < ::RSpec::Matchers::BuiltIn::Compound::Or
private
def match(_expected, actual)
@evaluator = CapybaraEvaluator.new(actual)
syncer = sync_element(actual)
begin
syncer.synchronize do
@evaluator.reset
raise ::Capybara::ElementNotFound unless [matcher_1_matches?, matcher_2_matches?].any?
true
def sync_element(el)
if el.respond_to? :synchronize
el
elsif el.respond_to? :current_scope
el.current_scope
else
Capybara.string(el)
end
rescue StandardError
false
end
end
def sync_element(el)
if el.respond_to? :synchronize
el
elsif el.respond_to? :current_scope
el.current_scope
else
Capybara.string(el)
class Or < ::RSpec::Matchers::BuiltIn::Compound::Or
private
def match(_expected, actual)
@evaluator = CapybaraEvaluator.new(actual)
syncer = sync_element(actual)
begin
syncer.synchronize do
@evaluator.reset
raise ::Capybara::ElementNotFound unless [matcher_1_matches?, matcher_2_matches?].any?
true
end
rescue StandardError
false
end
end
def sync_element(el)
if el.respond_to? :synchronize
el
elsif el.respond_to? :current_scope
el.current_scope
else
Capybara.string(el)
end
end
end
end
end
end
end
end

View File

@ -9,10 +9,8 @@ RSpec.shared_context "Capybara Features", capybara_feature: true do
end
# ensure shared_context is included if default shared_context_metadata_behavior is changed
if RSpec::Core::Version::STRING.to_f >= 3.5
RSpec.configure do |config|
config.include_context "Capybara Features", capybara_feature: true
end
RSpec.configure do |config|
config.include_context "Capybara Features", capybara_feature: true if config.respond_to?(:include_context)
end
RSpec.configure do |config|

View File

@ -1,12 +1,11 @@
# frozen_string_literal: true
require 'capybara/rspec/compound'
module Capybara
module RSpecMatchers
class Matcher
if defined?(::RSpec::Expectations::Version)
require 'capybara/rspec/compound'
include ::Capybara::RSpecMatchers::Compound
end
include ::Capybara::RSpecMatchers::Compound if defined?(::Capybara::RSpecMatchers::Compound)
attr_reader :failure_message, :failure_message_when_negated
@ -199,10 +198,7 @@ module Capybara
end
class NegatedMatcher
if defined?(::RSpec::Expectations::Version)
require 'capybara/rspec/compound'
include ::Capybara::RSpecMatchers::Compound
end
include ::Capybara::RSpecMatchers::Compound if defined?(::Capybara::RSpecMatchers::Compound)
def initialize(matcher)
@matcher = matcher

View File

@ -15,7 +15,7 @@ module Capybara
config.before { Capybara::SpecHelper.reset! }
config.after { Capybara::SpecHelper.reset! }
# Test in 3.5+ where metadata doesn't autotrigger shared context inclusion - will be only behavior in RSpec 4
config.shared_context_metadata_behavior = :apply_to_host_groups if RSpec::Core::Version::STRING.to_f >= 3.5
config.shared_context_metadata_behavior = :apply_to_host_groups if config.respond_to?(:shared_context_metadata_behavior=)
end
def reset!