Skip to content

Commit

Permalink
Merge pull request #2116 from deivid-rodriguez/workaround_jruby_prepe…
Browse files Browse the repository at this point in the history
…nd_issue

Workaround jruby prepend issue
  • Loading branch information
twalpole committed Oct 19, 2018
2 parents 083703d + 5b235eb commit 057b552
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
- docker
rvm:
- 2.5.1
- jruby-9.1.16.0
- jruby-9.2.0.0
gemfile:
- Gemfile
env:
Expand Down
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Version 3.10.0
Release date: unreleased

### Fixed

* Workaround installation of rspec matcher proxies under jruby by reverting to the old solution not using prepend, so jruby bugs are not hit - Issue #2115

### Added

* :class filter can now check for class names starting with !
Expand Down
69 changes: 47 additions & 22 deletions lib/capybara/rspec/matcher_proxies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,68 @@ def within(*args)
end
end

module DSLRSpecProxyInstaller
module ClassMethods
def included(base)
if defined?(::RSpec::Matchers)
base.include(::Capybara::RSpecMatcherProxies) if base.include?(::RSpec::Matchers)
if RUBY_ENGINE == "jruby"
module DSL
class <<self
remove_method :included

def included(base)
warn "including Capybara::DSL in the global scope is not recommended!" if base == Object

if defined?(::RSpec::Matchers) && base.include?(::RSpec::Matchers)
base.send(:include, ::Capybara::RSpecMatcherProxies)
end

super
end
super
end
end
else
module DSLRSpecProxyInstaller
module ClassMethods
def included(base)
if defined?(::RSpec::Matchers)
base.include(::Capybara::RSpecMatcherProxies) if base.include?(::RSpec::Matchers)
end
super
end
end

def self.prepended(base)
class <<base
prepend ClassMethods
def self.prepended(base)
class <<base
prepend ClassMethods
end
end
end
end

module RSpecMatcherProxyInstaller
module ClassMethods
def included(base)
base.include(::Capybara::RSpecMatcherProxies) if base.include?(::Capybara::DSL)
super
module RSpecMatcherProxyInstaller
module ClassMethods
def included(base)
base.include(::Capybara::RSpecMatcherProxies) if base.include?(::Capybara::DSL)
super
end
end
end

def self.prepended(base)
class <<base
prepend ClassMethods
def self.prepended(base)
class <<base
prepend ClassMethods
end
end
end
end

DSL.prepend ::Capybara::DSLRSpecProxyInstaller
DSL.prepend ::Capybara::DSLRSpecProxyInstaller
end
end

if defined?(::RSpec::Matchers)
module ::RSpec::Matchers
prepend ::Capybara::RSpecMatcherProxyInstaller
if RUBY_ENGINE == "jruby"
def self.included(base)
base.send(:include, ::Capybara::RSpecMatcherProxies) if base.include?(::Capybara::DSL)
super
end
else
prepend ::Capybara::RSpecMatcherProxyInstaller
end
end
end
20 changes: 20 additions & 0 deletions spec/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ class DSLMatchersTest
expect(@test_class_instance.find(:css, 'span.number').text.to_i).to @test_class_instance.within(1).of(41)
end
end

context 'when `match_when_negated` is not defined in a matcher' do
before do
RSpec::Matchers.define :only_match_matcher do |expected|
match do |actual|
!(actual ^ expected)
end
end
end

it "can be called with `not_to`" do
# This test is for a bug in jruby where `super` isn't defined correctly - https://github.com/jruby/jruby/issues/4678
# Reported in https://github.com/teamcapybara/capybara/issues/2115
@test_class_instance.instance_eval do
expect do
expect(true).not_to only_match_matcher(false)
end.not_to raise_error
end
end
end
end

it 'should not include Capybara' do
Expand Down

0 comments on commit 057b552

Please sign in to comment.