diff --git a/lib/capybara/rspec/compound.rb b/lib/capybara/rspec/compound.rb index 9c4ed5b3..11bf4d51 100644 --- a/lib/capybara/rspec/compound.rb +++ b/lib/capybara/rspec/compound.rb @@ -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 \ No newline at end of file diff --git a/lib/capybara/rspec/features.rb b/lib/capybara/rspec/features.rb index 529f909c..57321073 100644 --- a/lib/capybara/rspec/features.rb +++ b/lib/capybara/rspec/features.rb @@ -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| diff --git a/lib/capybara/rspec/matchers.rb b/lib/capybara/rspec/matchers.rb index de099289..5dc64cdd 100644 --- a/lib/capybara/rspec/matchers.rb +++ b/lib/capybara/rspec/matchers.rb @@ -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 diff --git a/lib/capybara/spec/spec_helper.rb b/lib/capybara/spec/spec_helper.rb index 26dfefcd..f36277e3 100644 --- a/lib/capybara/spec/spec_helper.rb +++ b/lib/capybara/spec/spec_helper.rb @@ -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!