Skip to content

Commit

Permalink
Remove support for Ruby v1.9
Browse files Browse the repository at this point in the history
Closes #552.
  • Loading branch information
floehopper committed Sep 25, 2022
2 parents 3b369e9 + f38be1d commit 26c4ae1
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 191 deletions.
7 changes: 0 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ jobs:
steps:
- checkout
- run: ruby --version
- when:
condition:
equal: [ "ruby:1.9", << parameters.docker-image >>]
steps:
- run: gem update --system 2.7.8
- run: gem install bundler -v1.17.3
- when:
condition:
equal: [ "jruby:latest", << parameters.docker-image >>]
Expand Down Expand Up @@ -76,7 +70,6 @@ workflows:
matrix:
parameters:
docker-image:
- ruby:1.9
- ruby:2.0
- ruby:2.1
- ruby:2.2
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.2 # closest to required_ruby_version of '>= 1.9'
TargetRubyVersion: 2.2 # closest to required_ruby_version of '>= 2.0'

# Even the reference in the documentation suggests that you should prefer
# `alias_method` vs `alias`, so I don't understand why that isn't the default.
Expand Down
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ source 'https://rubygems.org'
gemspec

# rubocop:disable Bundler/DuplicatedGem
if RUBY_VERSION < '2'
gem 'rake', '~> 12.2.1'
elsif RUBY_VERSION < '2.2'
if RUBY_VERSION < '2.2'
gem 'rake', '~> 12.3.3'
else
gem 'rake'
Expand Down
7 changes: 0 additions & 7 deletions lib/mocha/ruby_version.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
require 'mocha/deprecation'

module Mocha
RUBY_V2_PLUS = Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2')

unless RUBY_V2_PLUS
Mocha::Deprecation.warning(
'Versions of Ruby earlier than v2.0 will not be supported in future versions of Mocha.'
)
end
end
40 changes: 1 addition & 39 deletions lib/mocha/stubbed_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def stub

def unstub
remove_new_method
restore_original_method
mock.unstub(method_name.to_sym)
return if mock.any_expectations?
reset_mocha
Expand All @@ -37,20 +36,7 @@ def reset_mocha
def hide_original_method
return unless original_method_owner.__method_exists__?(method_name)
store_original_method_visibility
if use_prepended_module_for_stub_method?
use_prepended_module_for_stub_method
else
begin
store_original_method
# rubocop:disable Lint/HandleExceptions
rescue NameError
# deal with nasties like ActiveRecord::Associations::AssociationProxy
end
# rubocop:enable Lint/HandleExceptions
if stub_method_overwrites_original_method?
remove_original_method_from_stubbee
end
end
use_prepended_module_for_stub_method
end

def define_new_method
Expand All @@ -66,18 +52,6 @@ def remove_new_method
stub_method_owner.send(:remove_method, method_name)
end

def store_original_method
@original_method = stubbee_method(method_name)
end

def restore_original_method
return if use_prepended_module_for_stub_method?
if stub_method_overwrites_original_method?
original_method_owner.send(:define_method, method_name, @original_method)
end
retain_original_visibility(original_method_owner)
end

def matches?(other)
return false unless other.class == self.class
(stubbee.object_id == other.stubbee.object_id) && (method_name == other.method_name)
Expand All @@ -100,18 +74,6 @@ def store_original_method_visibility
@original_visibility = original_method_owner.__method_visibility__(method_name)
end

def stub_method_overwrites_original_method?
@original_method && @original_method.owner == original_method_owner
end

def remove_original_method_from_stubbee
original_method_owner.send(:remove_method, method_name)
end

def use_prepended_module_for_stub_method?
RUBY_V2_PLUS
end

def use_prepended_module_for_stub_method
@stub_method_owner = PrependedModule.new
original_method_owner.__send__ :prepend, @stub_method_owner
Expand Down
2 changes: 1 addition & 1 deletion mocha.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.name = 'mocha'
s.version = Mocha::VERSION
s.licenses = ['MIT', 'BSD-2-Clause']
s.required_ruby_version = '>= 1.9'
s.required_ruby_version = '>= 2.0'

s.authors = ['James Mead']
s.description = 'Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.'
Expand Down
92 changes: 44 additions & 48 deletions test/acceptance/prepend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,75 +12,71 @@ def teardown
teardown_acceptance_test
end

if Mocha::RUBY_V2_PLUS
module Mod1
def my_method
super + ' World'
end
end

module Mod1
def my_method
super + ' World'
end
module Mod2
def my_method
super + ' Wide'
end
end

module Mod2
def my_method
super + ' Wide'
end
class Klass1
prepend Mod1
prepend Mod2

def my_method
'Hello'
end
end

class Klass1
class Klass2
class << self
prepend Mod1
prepend Mod2

def my_method
'Hello'
end
end
end

class Klass2
class << self
prepend Mod1
prepend Mod2

def my_method
'Hello'
end
end
end

def test_stubbing_any_instance_with_multiple_prepended_methods
assert_snapshot_unchanged(Klass1) do
test_result = run_as_test do
Klass1.any_instance.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', Klass1.new.my_method
end
assert_passed(test_result)
def test_stubbing_any_instance_with_multiple_prepended_methods
assert_snapshot_unchanged(Klass1) do
test_result = run_as_test do
Klass1.any_instance.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', Klass1.new.my_method
end
assert_equal 'Hello World Wide', Klass1.new.my_method
assert_passed(test_result)
end
assert_equal 'Hello World Wide', Klass1.new.my_method
end

def test_stubbing_instance_with_multiple_prepended_methods
object = Klass1.new
def test_stubbing_instance_with_multiple_prepended_methods
object = Klass1.new

assert_snapshot_unchanged(object) do
test_result = run_as_test do
object.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', object.my_method
assert_equal 'Hello World Wide', Klass1.new.my_method
end
assert_passed(test_result)
assert_snapshot_unchanged(object) do
test_result = run_as_test do
object.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', object.my_method
assert_equal 'Hello World Wide', Klass1.new.my_method
end
assert_equal 'Hello World Wide', object.my_method
assert_passed(test_result)
end
assert_equal 'Hello World Wide', object.my_method
end

def test_stubbing_a_prepended_class_method
assert_snapshot_unchanged(Klass2) do
test_result = run_as_test do
Klass2.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', Klass2.my_method
end
assert_passed(test_result)
def test_stubbing_a_prepended_class_method
assert_snapshot_unchanged(Klass2) do
test_result = run_as_test do
Klass2.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', Klass2.my_method
end
assert_equal 'Hello World Wide', Klass2.my_method
assert_passed(test_result)
end

assert_equal 'Hello World Wide', Klass2.my_method
end
end
45 changes: 2 additions & 43 deletions test/unit/any_instance_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ def class_with_method(method, result = nil)
end
end

unless RUBY_V2_PLUS
def test_should_hide_original_method
klass = class_with_method(:method_x)
method = AnyInstanceMethod.new(klass, :method_x)

method.hide_original_method

assert_equal false, klass.method_defined?(:method_x)
end
end

def test_should_not_raise_error_hiding_method_that_isnt_defined
klass = class_with_method(:irrelevant)
method = AnyInstanceMethod.new(klass, :method_x)
Expand Down Expand Up @@ -66,7 +55,7 @@ def test_should_include_the_filename_and_line_number_in_exceptions
method.define_new_method

expected_filename = 'stubbed_method.rb'
expected_line_number = 60
expected_line_number = 46

exception = assert_raises(Exception) { klass.new.method_x }
matching_line = exception.backtrace.find do |line|
Expand All @@ -77,38 +66,26 @@ def test_should_include_the_filename_and_line_number_in_exceptions
assert_not_nil matching_line, "Expected to find #{expected_filename}:#{expected_line_number} in the backtrace:\n #{exception.backtrace.join("\n")}"
end

def test_should_restore_original_method
def test_remove_new_method_restores_original_method
klass = class_with_method(:method_x, :original_result)
method = AnyInstanceMethod.new(klass, :method_x)

method.hide_original_method
method.define_new_method
method.remove_new_method
method.restore_original_method

instance = klass.new
assert instance.respond_to?(:method_x)
assert_equal :original_result, instance.method_x
end

def test_should_not_restore_original_method_if_none_was_defined_in_first_place
klass = class_with_method(:method_x, :new_result)
method = AnyInstanceMethod.new(klass, :method_x)

method.restore_original_method

instance = klass.new
assert_equal :new_result, instance.method_x
end

def test_should_call_remove_new_method
klass = class_with_method(:method_x)
any_instance = build_mock
any_instance_mocha = build_mock
any_instance.stubs(:mocha).returns(any_instance_mocha)
define_instance_method(klass, :any_instance) { any_instance }
method = AnyInstanceMethod.new(klass, :method_x)
replace_instance_method(method, :restore_original_method) {}
replace_instance_method(method, :reset_mocha) {}
define_instance_accessor(method, :remove_called)
replace_instance_method(method, :remove_new_method) { self.remove_called = true }
Expand All @@ -118,30 +95,12 @@ def test_should_call_remove_new_method
assert method.remove_called
end

def test_should_call_restore_original_method
klass = class_with_method(:method_x)
any_instance = build_mock
any_instance_mocha = build_mock
any_instance.stubs(:mocha).returns(any_instance_mocha)
define_instance_method(klass, :any_instance) { any_instance }
method = AnyInstanceMethod.new(klass, :method_x)
replace_instance_method(method, :remove_new_method) {}
replace_instance_method(method, :reset_mocha) {}
define_instance_accessor(method, :restore_called)
replace_instance_method(method, :restore_original_method) { self.restore_called = true }

method.unstub

assert method.restore_called
end

def test_should_call_mock_unstub
klass = class_with_method(:method_x)

method = AnyInstanceMethod.new(klass, :method_x)

replace_instance_method(method, :remove_new_method) {}
replace_instance_method(method, :restore_original_method) {}
mocha = Class.new do
class << self
attr_accessor :unstub_method
Expand Down
Loading

0 comments on commit 26c4ae1

Please sign in to comment.