[DRE-commits] [ruby-mocha] 01/11: New upstream version 1.2.1
Cédric Boutillier
boutil at moszumanska.debian.org
Thu Jul 6 22:58:31 UTC 2017
This is an automated email from the git hooks/post-receive script.
boutil pushed a commit to branch master
in repository ruby-mocha.
commit bbbbcce109bf6e9985469ba00a8a8fcb046984df
Author: Cédric Boutillier <boutil at debian.org>
Date: Thu Jul 6 23:50:28 2017 +0200
New upstream version 1.2.1
---
README.md | 94 +++++-
RELEASE.md | 21 ++
Rakefile | 20 +-
bin/build-matrix | 1 -
checksums.yaml.gz | Bin 270 -> 0 bytes
gemfiles/Gemfile.test-unit.latest | 6 +-
lib/mocha/any_instance_method.rb | 46 +--
lib/mocha/api.rb | 4 +-
lib/mocha/class_method.rb | 66 ++--
lib/mocha/class_methods.rb | 1 +
lib/mocha/integration/monkey_patcher.rb | 18 +-
lib/mocha/mockery.rb | 3 +-
lib/mocha/parameter_matchers/equals.rb | 2 +-
lib/mocha/parameter_matchers/includes.rb | 47 ++-
lib/mocha/ruby_version.rb | 4 +
lib/mocha/version.rb | 2 +-
metadata.yml | 365 ---------------------
mocha.gemspec | 40 ++-
test/acceptance/issue_272_test.rb | 52 +++
test/acceptance/prepend_test.rb | 3 +-
test/acceptance/stub_any_instance_method_test.rb | 58 +++-
.../stub_class_method_defined_on_class_test.rb | 6 +-
.../stub_instance_method_defined_on_class_test.rb | 6 +-
.../stub_instance_method_defined_on_module_test.rb | 3 +
...ub_method_defined_on_module_and_aliased_test.rb | 39 +++
test/acceptance/stubbing_nil_test.rb | 68 ++--
...ubbing_non_existent_any_instance_method_test.rb | 13 +
test/assertions.rb | 10 +-
test/minitest_result.rb | 4 +-
test/test_runner.rb | 2 +-
test/unit/any_instance_method_test.rb | 2 +
test/unit/class_method_test.rb | 2 +
test/unit/mock_test.rb | 3 +-
test/unit/object_inspect_test.rb | 3 +-
test/unit/parameter_matchers/includes_test.rb | 43 +++
35 files changed, 538 insertions(+), 519 deletions(-)
diff --git a/README.md b/README.md
index 3c0f39c..da1a0b4 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
-## Mocha [](https://secure.travis-ci.org/freerange/mocha) [](http://badge.fury.io/rb/mocha)
+## Mocha [](https://secure.travis-ci.org/freerange/mocha) [](http://badge.fury.io/rb/mocha) [](#backers) [](#sponsors)
+
### Description
@@ -75,6 +76,22 @@ gem 'mocha'
require 'mocha/mini_test'
```
+##### RSpec
+
+Assuming you are using the `rspec-rails` gem:
+
+```ruby
+# Gemfile in Rails app
+gem 'mocha'
+
+# Within `spec/spec_helper.rb`
+RSpec.configure do |config|
+ config.mock_with :mocha
+end
+```
+
+Note: There is no need to use a require statement to setup Mocha; RSpec does this itself.
+
#### Rails Plugin
Install the Rails plugin...
@@ -90,8 +107,10 @@ Note: As of version 0.9.8, the Mocha plugin is not automatically setup at plugin
require 'mocha/mini_test'
```
-#### Know Issues
+#### Known Issues
+* In Mocha v1.2.0 there is a scenario where stubbing a class method originally defined in a module hangs the Ruby interpreter due to [a bug in Ruby v2.3.1](https://bugs.ruby-lang.org/issues/12832). See #272. This was fixed in Mocha v1.2.1.
+* Stubbing an aliased class method, where the original method is defined in a module that's used to `extend` the class doesn't work in Ruby 1.8.x. See stub_method_defined_on_module_and_aliased_test.rb for an example of this behaviour.
* 0.13.x versions cause a harmless, but annoying, deprecation warning when used with Rails 3.2.0-3.2.12, 3.1.0-3.1.10 & 3.0.0-3.0.19.
* 0.11.x versions don't work with Rails 3.2.13 (`TypeError: superclass mismatch for class ExpectationError`). See #115.
* Versions 0.10.2, 0.10.3 & 0.11.0 of the Mocha gem were broken. Please do not use these versions.
@@ -193,7 +212,7 @@ class Order
def find_all
# Database.connection.execute('select * from orders...
end
-
+
def number_shipped_since(date)
find_all.select { |order| order.shipped_on > date }.length
end
@@ -262,6 +281,75 @@ See the [documentation](http://gofreerange.com/mocha/docs/Mocha/Mock.html) for `
See this [list of contributors](https://github.com/freerange/mocha/graphs/contributors).
+###Backers
+
+Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/mocha#backer)]
+
+<a href="https://opencollective.com/mocha/backer/0/website" target="_blank"><img src="https://opencollective.com/mocha/backer/0/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/1/website" target="_blank"><img src="https://opencollective.com/mocha/backer/1/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/2/website" target="_blank"><img src="https://opencollective.com/mocha/backer/2/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/3/website" target="_blank"><img src="https://opencollective.com/mocha/backer/3/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/4/website" target="_blank"><img src="https://opencollective.com/mocha/backer/4/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/5/website" target="_blank"><img src="https://opencollective.com/mocha/backer/5/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/6/website" target="_blank"><img src="https://opencollective.com/mocha/backer/6/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/7/website" target="_blank"><img src="https://opencollective.com/mocha/backer/7/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/8/website" target="_blank"><img src="https://opencollective.com/mocha/backer/8/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/9/website" target="_blank"><img src="https://opencollective.com/mocha/backer/9/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/10/website" target="_blank"><img src="https://opencollective.com/mocha/backer/10/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/11/website" target="_blank"><img src="https://opencollective.com/mocha/backer/11/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/12/website" target="_blank"><img src="https://opencollective.com/mocha/backer/12/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/13/website" target="_blank"><img src="https://opencollective.com/mocha/backer/13/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/14/website" target="_blank"><img src="https://opencollective.com/mocha/backer/14/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/15/website" target="_blank"><img src="https://opencollective.com/mocha/backer/15/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/16/website" target="_blank"><img src="https://opencollective.com/mocha/backer/16/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/17/website" target="_blank"><img src="https://opencollective.com/mocha/backer/17/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/18/website" target="_blank"><img src="https://opencollective.com/mocha/backer/18/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/19/website" target="_blank"><img src="https://opencollective.com/mocha/backer/19/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/20/website" target="_blank"><img src="https://opencollective.com/mocha/backer/20/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/21/website" target="_blank"><img src="https://opencollective.com/mocha/backer/21/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/22/website" target="_blank"><img src="https://opencollective.com/mocha/backer/22/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/23/website" target="_blank"><img src="https://opencollective.com/mocha/backer/23/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/24/website" target="_blank"><img src="https://opencollective.com/mocha/backer/24/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/25/website" target="_blank"><img src="https://opencollective.com/mocha/backer/25/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/26/website" target="_blank"><img src="https://opencollective.com/mocha/backer/26/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/27/website" target="_blank"><img src="https://opencollective.com/mocha/backer/27/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/28/website" target="_blank"><img src="https://opencollective.com/mocha/backer/28/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/backer/29/website" target="_blank"><img src="https://opencollective.com/mocha/backer/29/avatar.svg"></a>
+
+### Sponsors
+Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/mocha#sponsor)]
+
+<a href="https://opencollective.com/mocha/sponsor/0/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/0/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/1/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/1/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/2/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/2/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/3/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/3/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/4/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/4/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/5/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/5/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/6/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/6/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/7/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/7/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/8/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/8/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/9/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/9/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/10/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/10/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/11/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/11/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/12/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/12/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/13/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/13/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/14/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/14/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/15/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/15/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/16/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/16/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/17/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/17/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/18/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/18/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/19/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/19/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/20/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/20/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/21/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/21/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/22/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/22/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/23/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/23/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/24/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/24/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/25/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/25/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/26/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/26/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/27/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/27/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/28/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/28/avatar.svg"></a>
+<a href="https://opencollective.com/mocha/sponsor/29/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/29/avatar.svg"></a>
+
### Translations
* [Serbo-Croatian](http://science.webhostinggeeks.com/mocha) by [WHG Team](http://webhostinggeeks.com/). (may be out-of-date)
diff --git a/RELEASE.md b/RELEASE.md
index 679398c..9fa676a 100644
--- a/RELEASE.md
+++ b/RELEASE.md
@@ -1,5 +1,26 @@
# Release Notes
+## 1.2.1
+
+* Fixed #272. Workaround Ruby bug 12832 which caused interpreter to hang. See https://bugs.ruby-lang.org/issues/12832. Thanks to @chrisroos & @petems (6f1c8b9b, #273).
+
+## 1.2.0
+
+* Always use prepended module to stub class & instance methods for Ruby v2+ - thanks to @grosser & @chrisroos (43d56671, #244)
+* Always use prepended module to stub AnyInstance methods in Ruby v2+ - thanks to @chrisroos (#262)
+* Always set visibility of stub method to match stubbed method on included module - thanks to @grosser & @chrisroos (e87c03b0, #248)
+* Always set visibility to stub method to match stubbed method on superclass - thanks to @chrisroos (38d902ad)
+* Allow stubbing of method to which any instance responds (#200)
+* Allow `includes` matcher to take matcher arguments - thanks to @lazyatom (#217)
+* Avoid exception in older version of Rubygems - thanks to @chrisroos (78d930a7)
+* Add licenses to gemspec as requested by @coreyhaines (#201)
+* Fix typo in README - thanks to @jaredbeck (6119460d)
+* Added section about using Mocha with RSpec & Rails to README (#221)
+* Fix documentation for Mocha::API#stub method - thanks to @raeno (599b1dcd)
+* Added backers and sponsors from OpenCollective - thanks to @piamancini (#253)
+* Fix typo in docs for equals - thanks to @alexcoco (#254)
+* Add known issue for Ruby v1.8 to README - thanks to @chrisroos (2c642096)
+
## 1.1.0
* Set visibility of any instance stub method.
diff --git a/Rakefile b/Rakefile
index ca0124c..9d4c25a 100644
--- a/Rakefile
+++ b/Rakefile
@@ -2,6 +2,8 @@ require "bundler"
Bundler::GemHelper.install_tasks
require "bundler/setup"
+MOCHA_DOCS_HOST = ENV['MOCHA_DOCS_HOST'] || 'gofreerange.com'
+
require 'rake/testtask'
desc "Run all tests"
@@ -87,10 +89,18 @@ end
def benchmark_test_case(klass, iterations)
require 'benchmark'
+ require 'mocha/detection/mini_test'
if defined?(MiniTest)
- MiniTest::Unit.output = StringIO.new
- Benchmark.realtime { iterations.times { |i| MiniTest::Unit.new.run([klass]) } }
+ minitest_version = Gem::Version.new(Mocha::Detection::MiniTest.version)
+ if Gem::Requirement.new('>= 5.0.0').satisfied_by?(minitest_version)
+ result = Benchmark.realtime { iterations.times { |i| klass.run(MiniTest::CompositeReporter.new) } }
+ MiniTest::Runnable.runnables.delete(klass)
+ result
+ else
+ MiniTest::Unit.output = StringIO.new
+ Benchmark.realtime { iterations.times { |i| MiniTest::Unit.new.run([klass]) } }
+ end
else
load 'test/unit/ui/console/testrunner.rb' unless defined?(Test::Unit::UI::Console::TestRunner)
unless $silent_option
@@ -105,7 +115,7 @@ def benchmark_test_case(klass, iterations)
end
end
-unless ENV["MOCHA_NO_DOCS"]
+if ENV["MOCHA_GENERATE_DOCS"]
require 'yard'
desc 'Remove generated documentation'
@@ -127,10 +137,10 @@ unless ENV["MOCHA_NO_DOCS"]
desc "Generate documentation"
task 'generate_docs' => ['clobber_yardoc', 'yardoc']
- desc "Publish docs to gofreerange.com/docs/mocha"
+ desc "Publish docs to #{MOCHA_DOCS_HOST}/docs/mocha"
task 'publish_docs' => 'generate_docs' do
path = "/home/freerange/docs/mocha"
- system %{ssh gofreerange.com "sudo rm -fr #{path} && mkdir -p #{path}" && scp -r doc/* gofreerange.com:#{path}}
+ system %{ssh #{MOCHA_DOCS_HOST} "sudo rm -fr #{path} && mkdir -p #{path}" && scp -r doc/* #{MOCHA_DOCS_HOST}:#{path}}
end
end
diff --git a/bin/build-matrix b/bin/build-matrix
index fbc8d30..386932e 100755
--- a/bin/build-matrix
+++ b/bin/build-matrix
@@ -33,7 +33,6 @@ def run(ruby_version, gemfile, task = "test")
ENV["RBENV_VERSION"] = ruby_version
ENV["BUNDLE_GEMFILE"] = gemfile
ENV["MOCHA_OPTIONS"] = "debug"
- ENV["MOCHA_NO_DOCS"] = "true"
reset_bundle
execute(
with_rbenv("bundle install --gemfile=#{gemfile}"),
diff --git a/checksums.yaml.gz b/checksums.yaml.gz
deleted file mode 100644
index 2777d56..0000000
Binary files a/checksums.yaml.gz and /dev/null differ
diff --git a/gemfiles/Gemfile.test-unit.latest b/gemfiles/Gemfile.test-unit.latest
index 32b9119..b460292 100644
--- a/gemfiles/Gemfile.test-unit.latest
+++ b/gemfiles/Gemfile.test-unit.latest
@@ -3,5 +3,9 @@ source 'https://rubygems.org'
gemspec :path=>"../"
group :development do
- gem "test-unit"
+ if RUBY_VERSION < '1.9'
+ gem "test-unit", "~> 2"
+ else
+ gem "test-unit"
+ end
end
diff --git a/lib/mocha/any_instance_method.rb b/lib/mocha/any_instance_method.rb
index eec1528..305ee2e 100644
--- a/lib/mocha/any_instance_method.rb
+++ b/lib/mocha/any_instance_method.rb
@@ -1,3 +1,4 @@
+require 'mocha/ruby_version'
require 'mocha/class_method'
module Mocha
@@ -13,20 +14,17 @@ module Mocha
end
def hide_original_method
- if method_exists?(method)
+ if @original_visibility = method_visibility(method)
begin
@original_method = stubbee.instance_method(method)
- if @original_method && @original_method.owner == stubbee
- @original_visibility = :public
- if stubbee.protected_instance_methods.include?(method)
- @original_visibility = :protected
- elsif stubbee.private_instance_methods.include?(method)
- @original_visibility = :private
+ if RUBY_V2_PLUS
+ @definition_target = PrependedModule.new
+ stubbee.__send__ :prepend, @definition_target
+ else
+ if @original_method && @original_method.owner == stubbee
+ stubbee.send(:remove_method, method)
end
- stubbee.send(:remove_method, method)
end
-
- include_prepended_module if RUBY_VERSION >= '2.0'
rescue NameError
# deal with nasties like ActiveRecord::Associations::AssociationProxy
end
@@ -49,32 +47,22 @@ module Mocha
end
def restore_original_method
- if @original_method && @original_method.owner == stubbee
- stubbee.send(:define_method, method, @original_method)
- Module.instance_method(@original_visibility).bind(stubbee).call(method)
+ unless RUBY_V2_PLUS
+ if @original_method && @original_method.owner == stubbee
+ stubbee.send(:define_method, method, @original_method)
+ Module.instance_method(@original_visibility).bind(stubbee).call(method)
+ end
end
end
- def method_exists?(method)
- return true if stubbee.public_instance_methods(false).include?(method)
- return true if stubbee.protected_instance_methods(false).include?(method)
- return true if stubbee.private_instance_methods(false).include?(method)
- return false
+ def method_visibility(method)
+ (stubbee.public_instance_methods(true).include?(method) && :public) ||
+ (stubbee.protected_instance_methods(true).include?(method) && :protected) ||
+ (stubbee.private_instance_methods(true).include?(method) && :private)
end
private
- def include_prepended_module
- possible_prepended_modules = stubbee.ancestors.take_while do |mod|
- !(Class === mod)
- end
-
- if possible_prepended_modules.any?
- @definition_target = PrependedModule.new
- stubbee.__send__ :prepend, @definition_target
- end
- end
-
def definition_target
@definition_target ||= stubbee
end
diff --git a/lib/mocha/api.rb b/lib/mocha/api.rb
index 51392df..745c1eb 100644
--- a/lib/mocha/api.rb
+++ b/lib/mocha/api.rb
@@ -70,7 +70,7 @@ module Mocha
#
# @example Using stubbed_methods_vs_return_values Hash to setup stubbed methods.
# def test_motor_starts_and_stops
- # motor = mock('motor', :start => true, :stop => true)
+ # motor = stub('motor', :start => true, :stop => true)
# assert motor.start
# assert motor.stop
# # an error will not be raised even if either Motor#start or Motor#stop has not been called
@@ -78,7 +78,7 @@ module Mocha
#
# @example Using the optional block to setup expectations & stubbed methods.
# def test_motor_starts_and_stops
- # motor = mock('motor') do
+ # motor = stub('motor') do
# expects(:start).with(100.rpm).returns(true)
# stubs(:stop).returns(true)
# end
diff --git a/lib/mocha/class_method.rb b/lib/mocha/class_method.rb
index b2d7fdf..bc7d20e 100644
--- a/lib/mocha/class_method.rb
+++ b/lib/mocha/class_method.rb
@@ -1,3 +1,4 @@
+require 'mocha/ruby_version'
require 'metaclass'
module Mocha
@@ -11,7 +12,7 @@ module Mocha
def initialize(stubbee, method)
@stubbee = stubbee
@original_method, @original_visibility = nil, nil
- @method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
+ @method = PRE_RUBY_V19 ? method.to_s : method.to_sym
end
def stub
@@ -37,20 +38,17 @@ module Mocha
end
def hide_original_method
- if method_exists?(method)
+ if @original_visibility = method_visibility(method)
begin
- @original_method = stubbee._method(method)
- @original_visibility = :public
- if stubbee.__metaclass__.protected_instance_methods.include?(method)
- @original_visibility = :protected
- elsif stubbee.__metaclass__.private_instance_methods.include?(method)
- @original_visibility = :private
+ if RUBY_V2_PLUS
+ @definition_target = PrependedModule.new
+ stubbee.__metaclass__.__send__ :prepend, @definition_target
+ else
+ @original_method = stubbee._method(method)
+ if @original_method && @original_method.owner == stubbee.__metaclass__
+ stubbee.__metaclass__.send(:remove_method, method)
+ end
end
- if @original_method && @original_method.owner == stubbee.__metaclass__
- stubbee.__metaclass__.send(:remove_method, method)
- end
-
- include_prepended_module if RUBY_VERSION >= '2.0'
rescue NameError
# deal with nasties like ActiveRecord::Associations::AssociationProxy
end
@@ -73,18 +71,20 @@ module Mocha
end
def restore_original_method
- if @original_method && @original_method.owner == stubbee.__metaclass__
- if RUBY_VERSION < '1.9'
- original_method = @original_method
- stubbee.__metaclass__.send(:define_method, method) do |*args, &block|
- original_method.call(*args, &block)
+ unless RUBY_V2_PLUS
+ if @original_method && @original_method.owner == stubbee.__metaclass__
+ if PRE_RUBY_V19
+ original_method = @original_method
+ stubbee.__metaclass__.send(:define_method, method) do |*args, &block|
+ original_method.call(*args, &block)
+ end
+ else
+ stubbee.__metaclass__.send(:define_method, method, @original_method)
end
- else
- stubbee.__metaclass__.send(:define_method, method, @original_method)
end
- end
- if @original_visibility
- Module.instance_method(@original_visibility).bind(stubbee.__metaclass__).call(method)
+ if @original_visibility
+ Module.instance_method(@original_visibility).bind(stubbee.__metaclass__).call(method)
+ end
end
end
@@ -99,25 +99,17 @@ module Mocha
"#{stubbee}.#{method}"
end
- def method_exists?(method)
+ def method_visibility(method)
symbol = method.to_sym
- __metaclass__ = stubbee.__metaclass__
- __metaclass__.public_method_defined?(symbol) || __metaclass__.protected_method_defined?(symbol) || __metaclass__.private_method_defined?(symbol)
+ metaclass = stubbee.__metaclass__
+
+ (metaclass.public_method_defined?(symbol) && :public) ||
+ (metaclass.protected_method_defined?(symbol) && :protected) ||
+ (metaclass.private_method_defined?(symbol) && :private)
end
private
- def include_prepended_module
- possible_prepended_modules = stubbee.__metaclass__.ancestors.take_while do |mod|
- !(Class === mod)
- end
-
- if possible_prepended_modules.any?
- @definition_target = PrependedModule.new
- stubbee.__metaclass__.__send__ :prepend, @definition_target
- end
- end
-
def definition_target
@definition_target ||= stubbee.__metaclass__
end
diff --git a/lib/mocha/class_methods.rb b/lib/mocha/class_methods.rb
index 3bb10e4..3419de9 100644
--- a/lib/mocha/class_methods.rb
+++ b/lib/mocha/class_methods.rb
@@ -34,6 +34,7 @@ module Mocha
def method_exists?(method, include_public_methods = true)
if include_public_methods
return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
+ return true if @stubba_object.allocate.respond_to?(method.to_sym)
end
return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
diff --git a/lib/mocha/integration/monkey_patcher.rb b/lib/mocha/integration/monkey_patcher.rb
index da60e07..e0905e1 100644
--- a/lib/mocha/integration/monkey_patcher.rb
+++ b/lib/mocha/integration/monkey_patcher.rb
@@ -4,13 +4,21 @@ module Mocha
module Integration
module MonkeyPatcher
def self.apply(mod, run_method_patch)
- unless mod < Mocha::API
+ if mod < Mocha::API
+ Debug.puts "Mocha::API already included in #{mod}"
+ else
mod.send(:include, Mocha::API)
end
- unless mod.method_defined?(:run_before_mocha)
- mod.send(:alias_method, :run_before_mocha, :run)
- mod.send(:remove_method, :run)
- mod.send(:include, run_method_patch)
+ if mod.method_defined?(:run_before_mocha)
+ Debug.puts "#{mod}#run_before_mocha method already defined"
+ else
+ if mod.method_defined?(:run)
+ mod.send(:alias_method, :run_before_mocha, :run)
+ mod.send(:remove_method, :run)
+ mod.send(:include, run_method_patch)
+ else
+ raise "Unable to monkey-patch #{mod}, because it does not define a `#run` method"
+ end
end
end
end
diff --git a/lib/mocha/mockery.rb b/lib/mocha/mockery.rb
index 57a8436..9d7a0b2 100644
--- a/lib/mocha/mockery.rb
+++ b/lib/mocha/mockery.rb
@@ -1,3 +1,4 @@
+require 'mocha/ruby_version'
require 'mocha/central'
require 'mocha/mock'
require 'mocha/names'
@@ -97,7 +98,7 @@ module Mocha
end
def on_stubbing(object, method)
- method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
+ method = PRE_RUBY_V19 ? method.to_s : method.to_sym
unless Mocha::Configuration.allow?(:stubbing_non_existent_method)
unless object.method_exists?(method, include_public_methods = true)
on_stubbing_non_existent_method(object, method)
diff --git a/lib/mocha/parameter_matchers/equals.rb b/lib/mocha/parameter_matchers/equals.rb
index e822718..ad6cf04 100644
--- a/lib/mocha/parameter_matchers/equals.rb
+++ b/lib/mocha/parameter_matchers/equals.rb
@@ -22,7 +22,7 @@ module Mocha
# object = mock()
# object.expects(:method_1).with(equals(2))
# object.method_1(3)
- # # error raised, because method_1 was not called with an +Object+ that equals 3
+ # # error raised, because method_1 was not called with an +Object+ that equals 2
def equals(value)
Equals.new(value)
end
diff --git a/lib/mocha/parameter_matchers/includes.rb b/lib/mocha/parameter_matchers/includes.rb
index ab668a7..12f406a 100644
--- a/lib/mocha/parameter_matchers/includes.rb
+++ b/lib/mocha/parameter_matchers/includes.rb
@@ -22,6 +22,46 @@ module Mocha
# @example Actual parameter does not include all items.
# object.method_1(['foo', 'baz'])
# # error raised, because ['foo', 'baz'] does not include 'bar'.
+ #
+ # @example Actual parameter includes item which matches nested matcher.
+ # object = mock()
+ # object.expects(:method_1).with(includes(has_key(:key)))
+ # object.method_1(['foo', 'bar', {:key => 'baz'}])
+ # # no error raised
+ #
+ # @example Actual parameter does not include item matching nested matcher.
+ # object.method_1(['foo', 'bar', {:other_key => 'baz'}])
+ # # error raised, because no element matches `has_key(:key)` matcher
+ #
+ # @example Actual parameter is a String including substring.
+ # object = mock()
+ # object.expects(:method_1).with(includes('bar'))
+ # object.method_1('foobarbaz')
+ # # no error raised
+ #
+ # @example Actual parameter is a String not including substring.
+ # object.method_1('foobaz')
+ # # error raised, because 'foobaz' does not include 'bar'
+ #
+ # @example Actual parameter is a Hash including the given key.
+ # object = mock()
+ # object.expects(:method_1).with(includes(:bar))
+ # object.method_1({:foo => 1, :bar => 2})
+ # # no error raised
+ #
+ # @example Actual parameter is a Hash without the given key.
+ # object.method_1({:foo => 1, :baz => 2})
+ # # error raised, because hash does not include key 'bar'
+ #
+ # @example Actual parameter is a Hash with a key matching the given matcher.
+ # object = mock()
+ # object.expects(:method_1).with(includes(regexp_matches(/ar/)))
+ # object.method_1({'foo' => 1, 'bar' => 2})
+ # # no error raised
+ #
+ # @example Actual parameter is a Hash no key matching the given matcher.
+ # object.method_1({'foo' => 1, 'baz' => 3})
+ # # error raised, because hash does not include a key matching /ar/
def includes(*items)
Includes.new(*items)
end
@@ -38,9 +78,12 @@ module Mocha
def matches?(available_parameters)
parameter = available_parameters.shift
return false unless parameter.respond_to?(:include?)
-
if @items.size == 1
- return parameter.include?(@items.first)
+ if parameter.respond_to?(:any?) && !parameter.is_a?(String)
+ return parameter.any? { |(p,_)| @items.first.to_matcher.matches?([p]) }
+ else
+ return parameter.include?(@items.first)
+ end
else
includes_matchers = @items.map { |item| Includes.new(item) }
AllOf.new(*includes_matchers).matches?([parameter])
diff --git a/lib/mocha/ruby_version.rb b/lib/mocha/ruby_version.rb
new file mode 100644
index 0000000..c5c672c
--- /dev/null
+++ b/lib/mocha/ruby_version.rb
@@ -0,0 +1,4 @@
+module Mocha
+ PRE_RUBY_V19 = Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('1.9')
+ RUBY_V2_PLUS = Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2')
+end
diff --git a/lib/mocha/version.rb b/lib/mocha/version.rb
index fc0c829..0c3ed19 100644
--- a/lib/mocha/version.rb
+++ b/lib/mocha/version.rb
@@ -1,3 +1,3 @@
module Mocha
- VERSION = "1.1.0"
+ VERSION = "1.2.1"
end
diff --git a/metadata.yml b/metadata.yml
deleted file mode 100644
index c9f9f0a..0000000
--- a/metadata.yml
+++ /dev/null
@@ -1,365 +0,0 @@
---- !ruby/object:Gem::Specification
-name: mocha
-version: !ruby/object:Gem::Version
- version: 1.1.0
-platform: ruby
-authors:
-- James Mead
-autorequire:
-bindir: bin
-cert_chain: []
-date: 2014-05-13 00:00:00.000000000 Z
-dependencies:
-- !ruby/object:Gem::Dependency
- name: metaclass
- requirement: !ruby/object:Gem::Requirement
- requirements:
- - - ~>
- - !ruby/object:Gem::Version
- version: 0.0.1
- type: :runtime
- prerelease: false
- version_requirements: !ruby/object:Gem::Requirement
- requirements:
- - - ~>
- - !ruby/object:Gem::Version
- version: 0.0.1
-- !ruby/object:Gem::Dependency
- name: rake
- requirement: !ruby/object:Gem::Requirement
- requirements:
- - - '>='
- - !ruby/object:Gem::Version
- version: '0'
- type: :development
- prerelease: false
- version_requirements: !ruby/object:Gem::Requirement
- requirements:
- - - '>='
- - !ruby/object:Gem::Version
- version: '0'
-- !ruby/object:Gem::Dependency
- name: introspection
- requirement: !ruby/object:Gem::Requirement
- requirements:
- - - ~>
- - !ruby/object:Gem::Version
- version: 0.0.1
- type: :development
- prerelease: false
- version_requirements: !ruby/object:Gem::Requirement
- requirements:
- - - ~>
- - !ruby/object:Gem::Version
- version: 0.0.1
-- !ruby/object:Gem::Dependency
- name: yard
- requirement: !ruby/object:Gem::Requirement
- requirements:
- - - '>='
- - !ruby/object:Gem::Version
- version: '0'
- type: :development
- prerelease: false
- version_requirements: !ruby/object:Gem::Requirement
- requirements:
- - - '>='
- - !ruby/object:Gem::Version
- version: '0'
-- !ruby/object:Gem::Dependency
- name: redcarpet
- requirement: !ruby/object:Gem::Requirement
- requirements:
- - - ~>
- - !ruby/object:Gem::Version
- version: '1'
- type: :development
- prerelease: false
- version_requirements: !ruby/object:Gem::Requirement
- requirements:
- - - ~>
- - !ruby/object:Gem::Version
- version: '1'
-description: Mocking and stubbing library with JMock/SchMock syntax, which allows
- mocking and stubbing of methods on real (non-mock) classes.
-email: mocha-developer at googlegroups.com
-executables: []
-extensions: []
-extra_rdoc_files: []
-files:
-- .gemtest
-- .yardopts
-- CONTRIBUTING.md
-- COPYING.md
-- Gemfile
-- MIT-LICENSE.md
-- README.md
-- RELEASE.md
-- Rakefile
-- bin/build-matrix
-- gemfiles/Gemfile.minitest.1.3.0
-- gemfiles/Gemfile.minitest.1.4.0
-- gemfiles/Gemfile.minitest.1.4.1
-- gemfiles/Gemfile.minitest.1.4.2
-- gemfiles/Gemfile.minitest.2.0.0
-- gemfiles/Gemfile.minitest.2.0.1
-- gemfiles/Gemfile.minitest.2.11.0
-- gemfiles/Gemfile.minitest.2.11.2
-- gemfiles/Gemfile.minitest.2.3.0
-- gemfiles/Gemfile.minitest.latest
-- gemfiles/Gemfile.test-unit.2.0.0
-- gemfiles/Gemfile.test-unit.2.0.1
-- gemfiles/Gemfile.test-unit.2.0.3
-- gemfiles/Gemfile.test-unit.latest
-- init.rb
-- lib/mocha.rb
-- lib/mocha/any_instance_method.rb
-- lib/mocha/api.rb
-- lib/mocha/argument_iterator.rb
-- lib/mocha/backtrace_filter.rb
-- lib/mocha/cardinality.rb
-- lib/mocha/central.rb
-- lib/mocha/change_state_side_effect.rb
-- lib/mocha/class_method.rb
-- lib/mocha/class_methods.rb
-- lib/mocha/configuration.rb
-- lib/mocha/debug.rb
-- lib/mocha/deprecation.rb
-- lib/mocha/detection/mini_test.rb
-- lib/mocha/detection/test_unit.rb
-- lib/mocha/exception_raiser.rb
-- lib/mocha/expectation.rb
-- lib/mocha/expectation_error.rb
-- lib/mocha/expectation_error_factory.rb
-- lib/mocha/expectation_list.rb
-- lib/mocha/hooks.rb
-- lib/mocha/in_state_ordering_constraint.rb
-- lib/mocha/inspect.rb
-- lib/mocha/instance_method.rb
-- lib/mocha/integration.rb
-- lib/mocha/integration/assertion_counter.rb
-- lib/mocha/integration/mini_test.rb
-- lib/mocha/integration/mini_test/adapter.rb
-- lib/mocha/integration/mini_test/exception_translation.rb
-- lib/mocha/integration/mini_test/nothing.rb
-- lib/mocha/integration/mini_test/version_13.rb
-- lib/mocha/integration/mini_test/version_140.rb
-- lib/mocha/integration/mini_test/version_141.rb
-- lib/mocha/integration/mini_test/version_142_to_172.rb
-- lib/mocha/integration/mini_test/version_200.rb
-- lib/mocha/integration/mini_test/version_201_to_222.rb
-- lib/mocha/integration/mini_test/version_2110_to_2111.rb
-- lib/mocha/integration/mini_test/version_2112_to_320.rb
-- lib/mocha/integration/mini_test/version_230_to_2101.rb
-- lib/mocha/integration/monkey_patcher.rb
-- lib/mocha/integration/test_unit.rb
-- lib/mocha/integration/test_unit/adapter.rb
-- lib/mocha/integration/test_unit/gem_version_200.rb
-- lib/mocha/integration/test_unit/gem_version_201_to_202.rb
-- lib/mocha/integration/test_unit/gem_version_203_to_220.rb
-- lib/mocha/integration/test_unit/gem_version_230_to_250.rb
-- lib/mocha/integration/test_unit/nothing.rb
-- lib/mocha/integration/test_unit/ruby_version_185_and_below.rb
-- lib/mocha/integration/test_unit/ruby_version_186_and_above.rb
-- lib/mocha/is_a.rb
-- lib/mocha/logger.rb
-- lib/mocha/method_matcher.rb
-- lib/mocha/mini_test.rb
-- lib/mocha/mock.rb
-- lib/mocha/mockery.rb
-- lib/mocha/module_method.rb
-- lib/mocha/module_methods.rb
-- lib/mocha/multiple_yields.rb
-- lib/mocha/names.rb
-- lib/mocha/no_yields.rb
-- lib/mocha/object_methods.rb
-- lib/mocha/parameter_matchers.rb
-- lib/mocha/parameter_matchers/all_of.rb
-- lib/mocha/parameter_matchers/any_of.rb
-- lib/mocha/parameter_matchers/any_parameters.rb
-- lib/mocha/parameter_matchers/anything.rb
-- lib/mocha/parameter_matchers/base.rb
-- lib/mocha/parameter_matchers/equals.rb
-- lib/mocha/parameter_matchers/has_entries.rb
-- lib/mocha/parameter_matchers/has_entry.rb
-- lib/mocha/parameter_matchers/has_key.rb
-- lib/mocha/parameter_matchers/has_value.rb
-- lib/mocha/parameter_matchers/includes.rb
-- lib/mocha/parameter_matchers/instance_of.rb
-- lib/mocha/parameter_matchers/is_a.rb
-- lib/mocha/parameter_matchers/kind_of.rb
-- lib/mocha/parameter_matchers/not.rb
-- lib/mocha/parameter_matchers/object.rb
-- lib/mocha/parameter_matchers/optionally.rb
-- lib/mocha/parameter_matchers/query_string.rb
-- lib/mocha/parameter_matchers/regexp_matches.rb
-- lib/mocha/parameter_matchers/responds_with.rb
-- lib/mocha/parameter_matchers/yaml_equivalent.rb
-- lib/mocha/parameters_matcher.rb
-- lib/mocha/pretty_parameters.rb
-- lib/mocha/receivers.rb
-- lib/mocha/return_values.rb
-- lib/mocha/sequence.rb
-- lib/mocha/setup.rb
-- lib/mocha/single_return_value.rb
-- lib/mocha/single_yield.rb
-- lib/mocha/standalone.rb
-- lib/mocha/state_machine.rb
-- lib/mocha/stubbing_error.rb
-- lib/mocha/test_unit.rb
-- lib/mocha/thrower.rb
-- lib/mocha/unexpected_invocation.rb
-- lib/mocha/version.rb
-- lib/mocha/yield_parameters.rb
-- lib/mocha_standalone.rb
-- mocha.gemspec
-- test/acceptance/acceptance_test_helper.rb
-- test/acceptance/bug_18914_test.rb
-- test/acceptance/bug_21465_test.rb
-- test/acceptance/bug_21563_test.rb
-- test/acceptance/exception_rescue_test.rb
-- test/acceptance/expectations_on_multiple_methods_test.rb
-- test/acceptance/expected_invocation_count_test.rb
-- test/acceptance/failure_messages_test.rb
-- test/acceptance/issue_65_test.rb
-- test/acceptance/issue_70_test.rb
-- test/acceptance/mocha_example_test.rb
-- test/acceptance/mocha_test_result_test.rb
-- test/acceptance/mock_test.rb
-- test/acceptance/mock_with_initializer_block_test.rb
-- test/acceptance/mocked_methods_dispatch_test.rb
-- test/acceptance/multiple_expectations_failure_message_test.rb
-- test/acceptance/optional_parameters_test.rb
-- test/acceptance/parameter_matcher_test.rb
-- test/acceptance/partial_mocks_test.rb
-- test/acceptance/prepend_test.rb
-- test/acceptance/raise_exception_test.rb
-- test/acceptance/return_value_test.rb
-- test/acceptance/sequence_test.rb
-- test/acceptance/states_test.rb
-- test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb
-- test/acceptance/stub_any_instance_method_test.rb
-- test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb
-- test/acceptance/stub_class_method_defined_on_class_test.rb
-- test/acceptance/stub_class_method_defined_on_module_test.rb
-- test/acceptance/stub_class_method_defined_on_superclass_test.rb
-- test/acceptance/stub_everything_test.rb
-- test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb
-- test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb
-- test/acceptance/stub_instance_method_defined_on_class_test.rb
-- test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb
-- test/acceptance/stub_instance_method_defined_on_module_test.rb
-- test/acceptance/stub_instance_method_defined_on_object_class_test.rb
-- test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb
-- test/acceptance/stub_instance_method_defined_on_superclass_test.rb
-- test/acceptance/stub_module_method_test.rb
-- test/acceptance/stub_test.rb
-- test/acceptance/stubba_example_test.rb
-- test/acceptance/stubba_test_result_test.rb
-- test/acceptance/stubbing_error_backtrace_test.rb
-- test/acceptance/stubbing_frozen_object_test.rb
-- test/acceptance/stubbing_method_accepting_block_parameter_test.rb
-- test/acceptance/stubbing_method_unnecessarily_test.rb
-- test/acceptance/stubbing_nil_test.rb
-- test/acceptance/stubbing_non_existent_any_instance_method_test.rb
-- test/acceptance/stubbing_non_existent_class_method_test.rb
-- test/acceptance/stubbing_non_existent_instance_method_test.rb
-- test/acceptance/stubbing_non_public_any_instance_method_test.rb
-- test/acceptance/stubbing_non_public_class_method_test.rb
-- test/acceptance/stubbing_non_public_instance_method_test.rb
-- test/acceptance/stubbing_on_non_mock_object_test.rb
-- test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb
-- test/acceptance/throw_test.rb
-- test/acceptance/unexpected_invocation_test.rb
-- test/acceptance/unstubbing_test.rb
-- test/assertions.rb
-- test/deprecation_disabler.rb
-- test/execution_point.rb
-- test/integration/mini_test_test.rb
-- test/integration/shared_tests.rb
-- test/integration/test_unit_test.rb
-- test/method_definer.rb
-- test/mini_test_result.rb
-- test/minitest_result.rb
-- test/simple_counter.rb
-- test/test_helper.rb
-- test/test_runner.rb
-- test/test_unit_result.rb
-- test/unit/any_instance_method_test.rb
-- test/unit/array_inspect_test.rb
-- test/unit/backtrace_filter_test.rb
-- test/unit/cardinality_test.rb
-- test/unit/central_test.rb
-- test/unit/change_state_side_effect_test.rb
-- test/unit/class_method_test.rb
-- test/unit/class_methods_test.rb
-- test/unit/configuration_test.rb
-- test/unit/date_time_inspect_test.rb
-- test/unit/exception_raiser_test.rb
-- test/unit/expectation_list_test.rb
-- test/unit/expectation_test.rb
-- test/unit/hash_inspect_test.rb
-- test/unit/hooks_test.rb
-- test/unit/in_state_ordering_constraint_test.rb
-- test/unit/method_matcher_test.rb
-- test/unit/mock_test.rb
-- test/unit/mockery_test.rb
-- test/unit/module_methods_test.rb
-- test/unit/multiple_yields_test.rb
-- test/unit/no_yields_test.rb
-- test/unit/object_inspect_test.rb
-- test/unit/object_methods_test.rb
-- test/unit/parameter_matchers/all_of_test.rb
-- test/unit/parameter_matchers/any_of_test.rb
-- test/unit/parameter_matchers/anything_test.rb
-- test/unit/parameter_matchers/equals_test.rb
-- test/unit/parameter_matchers/has_entries_test.rb
-- test/unit/parameter_matchers/has_entry_test.rb
-- test/unit/parameter_matchers/has_key_test.rb
-- test/unit/parameter_matchers/has_value_test.rb
-- test/unit/parameter_matchers/includes_test.rb
-- test/unit/parameter_matchers/instance_of_test.rb
-- test/unit/parameter_matchers/is_a_test.rb
-- test/unit/parameter_matchers/kind_of_test.rb
-- test/unit/parameter_matchers/not_test.rb
-- test/unit/parameter_matchers/regexp_matches_test.rb
-- test/unit/parameter_matchers/responds_with_test.rb
-- test/unit/parameter_matchers/stub_matcher.rb
-- test/unit/parameter_matchers/yaml_equivalent_test.rb
-- test/unit/parameters_matcher_test.rb
-- test/unit/receivers_test.rb
-- test/unit/return_values_test.rb
-- test/unit/sequence_test.rb
-- test/unit/single_return_value_test.rb
-- test/unit/single_yield_test.rb
-- test/unit/state_machine_test.rb
-- test/unit/string_inspect_test.rb
-- test/unit/thrower_test.rb
-- test/unit/yield_parameters_test.rb
-- yard-templates/default/layout/html/google_analytics.erb
-- yard-templates/default/layout/html/setup.rb
-homepage: http://gofreerange.com/mocha/docs
-licenses: []
-metadata: {}
-post_install_message:
-rdoc_options: []
-require_paths:
-- lib
-required_ruby_version: !ruby/object:Gem::Requirement
- requirements:
- - - '>='
- - !ruby/object:Gem::Version
- version: '0'
-required_rubygems_version: !ruby/object:Gem::Requirement
- requirements:
- - - '>='
- - !ruby/object:Gem::Version
- version: '0'
-requirements: []
-rubyforge_project: mocha
-rubygems_version: 2.0.14
-signing_key:
-specification_version: 3
-summary: Mocking and stubbing library
-test_files: []
-has_rdoc: yard
diff --git a/mocha.gemspec b/mocha.gemspec
index 32ad409..50fc9ef 100644
--- a/mocha.gemspec
+++ b/mocha.gemspec
@@ -6,6 +6,7 @@ require "mocha/version"
Gem::Specification.new do |s|
s.name = "mocha"
s.version = Mocha::VERSION
+ s.licenses = ['MIT', 'BSD-2-Clause']
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["James Mead"]
@@ -28,26 +29,47 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_development_dependency("rake", ">= 0")
+ if RUBY_VERSION >= '1.9.3'
+ s.add_development_dependency("rake", ">= 0")
+ else
+ s.add_development_dependency("rake", "~> 10.0")
+ end
s.add_development_dependency("introspection", "~> 0.0.1")
- unless ENV["MOCHA_NO_DOCS"]
+ if RUBY_VERSION >= '2.2.0'
+ s.add_development_dependency("minitest")
+ end
+ if ENV["MOCHA_GENERATE_DOCS"]
s.add_development_dependency("yard")
- s.add_development_dependency("redcarpet", "~> 1")
+ s.add_development_dependency("redcarpet")
end
else
- s.add_dependency("rake", ">= 0")
+ if RUBY_VERSION >= '1.9.3'
+ s.add_development_dependency("rake", ">= 0")
+ else
+ s.add_development_dependency("rake", "~> 10.0")
+ end
s.add_dependency("introspection", "~> 0.0.1")
- unless ENV["MOCHA_NO_DOCS"]
+ if RUBY_VERSION >= '2.2.0'
+ s.add_dependency("minitest")
+ end
+ if ENV["MOCHA_GENERATE_DOCS"]
s.add_dependency("yard")
- s.add_dependency("redcarpet", "~> 1")
+ s.add_dependency("redcarpet")
end
end
else
- s.add_dependency("rake", ">= 0")
+ if RUBY_VERSION >= '1.9.3'
+ s.add_development_dependency("rake", ">= 0")
+ else
+ s.add_development_dependency("rake", "~> 10.0")
+ end
s.add_dependency("introspection", "~> 0.0.1")
- unless ENV["MOCHA_NO_DOCS"]
+ if RUBY_VERSION >= '2.2.0'
+ s.add_dependency("minitest")
+ end
+ if ENV["MOCHA_GENERATE_DOCS"]
s.add_dependency("yard")
- s.add_dependency("redcarpet", "~> 1")
+ s.add_dependency("redcarpet")
end
end
end
diff --git a/test/acceptance/issue_272_test.rb b/test/acceptance/issue_272_test.rb
new file mode 100644
index 0000000..ea36ad2
--- /dev/null
+++ b/test/acceptance/issue_272_test.rb
@@ -0,0 +1,52 @@
+require File.expand_path('../acceptance_test_helper', __FILE__)
+require 'mocha/setup'
+
+class Issue272Test < Mocha::TestCase
+
+ include AcceptanceTest
+
+ def setup
+ setup_acceptance_test
+ end
+
+ def teardown
+ teardown_acceptance_test
+ end
+
+ module Mod
+ private
+
+ def foo
+ 'original-foo'
+ end
+
+ def bar
+ 'original-bar'
+ end
+ end
+
+ class Klass
+ extend Mod
+
+ class << self
+ public :foo
+ public :bar
+ end
+ end
+
+ def test_private_methods_in_module_used_to_extend_class_and_made_public
+ test_result = run_as_test do
+ Klass.stubs(:foo).returns('stubbed-foo')
+ # hangs in next line executing:
+ # `@original_method = stubbee._method(method)`
+ # in Mocha::ClassMethod#hide_original_method
+ # but only in Ruby v2.3, not v2.2
+ Klass.stubs(:bar).returns('stubbed-bar')
+ assert_equal 'stubbed-foo', Klass.foo
+ assert_equal 'stubbed-bar', Klass.bar
+ end
+ assert_passed(test_result)
+ assert_equal 'original-foo', Klass.foo
+ assert_equal 'original-bar', Klass.bar
+ end
+end
diff --git a/test/acceptance/prepend_test.rb b/test/acceptance/prepend_test.rb
index 56a91e5..08272e8 100644
--- a/test/acceptance/prepend_test.rb
+++ b/test/acceptance/prepend_test.rb
@@ -1,5 +1,6 @@
require File.expand_path('../acceptance_test_helper', __FILE__)
require 'mocha/setup'
+require 'mocha/ruby_version'
class PrependTest < Mocha::TestCase
@@ -13,7 +14,7 @@ class PrependTest < Mocha::TestCase
teardown_acceptance_test
end
- if RUBY_VERSION >= '2.0'
+ if Mocha::RUBY_V2_PLUS
module Mod1
def my_method
diff --git a/test/acceptance/stub_any_instance_method_test.rb b/test/acceptance/stub_any_instance_method_test.rb
index 5e521f9..2e7926f 100644
--- a/test/acceptance/stub_any_instance_method_test.rb
+++ b/test/acceptance/stub_any_instance_method_test.rb
@@ -22,7 +22,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
instance = klass.new
test_result = run_as_test do
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_method_visiblity instance, :my_instance_method, :public
+ assert_method_visibility instance, :my_instance_method, :public
assert_equal :new_return_value, instance.my_instance_method
end
assert_passed(test_result)
@@ -77,7 +77,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
instance = klass.new
test_result = run_as_test do
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_method_visiblity instance, :my_instance_method, :protected
+ assert_method_visibility instance, :my_instance_method, :protected
end
assert_passed(test_result)
end
@@ -109,7 +109,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
instance = klass.new
test_result = run_as_test do
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_method_visiblity instance, :my_instance_method, :private
+ assert_method_visibility instance, :my_instance_method, :private
end
assert_passed(test_result)
end
@@ -127,16 +127,18 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
assert_equal 0, klass.any_instance.mocha.__expectations__.length
end
- def test_should_be_able_to_stub_a_superclass_method
+ def test_should_be_able_to_stub_a_public_superclass_method
superklass = Class.new do
def my_superclass_method
:original_return_value
end
+ public :my_superclass_method
end
klass = Class.new(superklass)
instance = klass.new
test_result = run_as_test do
klass.any_instance.stubs(:my_superclass_method).returns(:new_return_value)
+ assert_method_visibility instance, :my_superclass_method, :public
assert_equal :new_return_value, instance.my_superclass_method
end
assert_passed(test_result)
@@ -145,6 +147,46 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
assert_equal :original_return_value, instance.my_superclass_method
end
+ def test_should_be_able_to_stub_a_protected_superclass_method
+ superklass = Class.new do
+ def my_superclass_method
+ :original_return_value
+ end
+ protected :my_superclass_method
+ end
+ klass = Class.new(superklass)
+ instance = klass.new
+ test_result = run_as_test do
+ klass.any_instance.stubs(:my_superclass_method).returns(:new_return_value)
+ assert_method_visibility instance, :my_superclass_method, :protected
+ assert_equal :new_return_value, instance.send(:my_superclass_method)
+ end
+ assert_passed(test_result)
+ assert instance.protected_methods(true).any? { |m| m.to_s == 'my_superclass_method' }
+ assert !klass.protected_methods(false).any? { |m| m.to_s == 'my_superclass_method' }
+ assert_equal :original_return_value, instance.send(:my_superclass_method)
+ end
+
+ def test_should_be_able_to_stub_a_private_superclass_method
+ superklass = Class.new do
+ def my_superclass_method
+ :original_return_value
+ end
+ private :my_superclass_method
+ end
+ klass = Class.new(superklass)
+ instance = klass.new
+ test_result = run_as_test do
+ klass.any_instance.stubs(:my_superclass_method).returns(:new_return_value)
+ assert_method_visibility instance, :my_superclass_method, :private
+ assert_equal :new_return_value, instance.send(:my_superclass_method)
+ end
+ assert_passed(test_result)
+ assert instance.private_methods(true).any? { |m| m.to_s == 'my_superclass_method' }
+ assert !klass.private_methods(false).any? { |m| m.to_s == 'my_superclass_method' }
+ assert_equal :original_return_value, instance.send(:my_superclass_method)
+ end
+
def test_should_be_able_to_stub_method_if_ruby18_public_instance_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
ruby18_klass = Class.new do
class << self
@@ -185,7 +227,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
end
test_result = run_as_test do
ruby18_klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_equal :new_return_value, ruby18_klass.new.my_instance_method
+ assert_equal :new_return_value, ruby18_klass.new.send(:my_instance_method)
end
assert_passed(test_result)
end
@@ -200,7 +242,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
end
test_result = run_as_test do
ruby19_klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_equal :new_return_value, ruby19_klass.new.my_instance_method
+ assert_equal :new_return_value, ruby19_klass.new.send(:my_instance_method)
end
assert_passed(test_result)
end
@@ -215,7 +257,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
end
test_result = run_as_test do
ruby18_klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_equal :new_return_value, ruby18_klass.new.my_instance_method
+ assert_equal :new_return_value, ruby18_klass.new.send(:my_instance_method)
end
assert_passed(test_result)
end
@@ -230,7 +272,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
end
test_result = run_as_test do
ruby19_klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_equal :new_return_value, ruby19_klass.new.my_instance_method
+ assert_equal :new_return_value, ruby19_klass.new.send(:my_instance_method)
end
assert_passed(test_result)
end
diff --git a/test/acceptance/stub_class_method_defined_on_class_test.rb b/test/acceptance/stub_class_method_defined_on_class_test.rb
index c497b65..283e53a 100644
--- a/test/acceptance/stub_class_method_defined_on_class_test.rb
+++ b/test/acceptance/stub_class_method_defined_on_class_test.rb
@@ -26,7 +26,7 @@ class StubClassMethodDefinedOnClassTest < Mocha::TestCase
assert_snapshot_unchanged(klass) do
test_result = run_as_test do
klass.stubs(:my_class_method).returns(:new_return_value)
- assert_method_visiblity klass, :my_class_method, :public
+ assert_method_visibility klass, :my_class_method, :public
assert_equal :new_return_value, klass.my_class_method
end
assert_passed(test_result)
@@ -47,7 +47,7 @@ class StubClassMethodDefinedOnClassTest < Mocha::TestCase
assert_snapshot_unchanged(klass) do
test_result = run_as_test do
klass.stubs(:my_class_method).returns(:new_return_value)
- assert_method_visiblity klass, :my_class_method, :protected
+ assert_method_visibility klass, :my_class_method, :protected
assert_equal :new_return_value, klass.send(:my_class_method)
end
assert_passed(test_result)
@@ -68,7 +68,7 @@ class StubClassMethodDefinedOnClassTest < Mocha::TestCase
assert_snapshot_unchanged(klass) do
test_result = run_as_test do
klass.stubs(:my_class_method).returns(:new_return_value)
- assert_method_visiblity klass, :my_class_method, :private
+ assert_method_visibility klass, :my_class_method, :private
assert_equal :new_return_value, klass.send(:my_class_method)
end
assert_passed(test_result)
diff --git a/test/acceptance/stub_instance_method_defined_on_class_test.rb b/test/acceptance/stub_instance_method_defined_on_class_test.rb
index f8c4015..d54d576 100644
--- a/test/acceptance/stub_instance_method_defined_on_class_test.rb
+++ b/test/acceptance/stub_instance_method_defined_on_class_test.rb
@@ -23,7 +23,7 @@ class StubInstanceMethodDefinedOnClassTest < Mocha::TestCase
assert_snapshot_unchanged(instance) do
test_result = run_as_test do
instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_method_visiblity instance, :my_instance_method, :public
+ assert_method_visibility instance, :my_instance_method, :public
assert_equal :new_return_value, instance.my_instance_method
end
assert_passed(test_result)
@@ -41,7 +41,7 @@ class StubInstanceMethodDefinedOnClassTest < Mocha::TestCase
assert_snapshot_unchanged(instance) do
test_result = run_as_test do
instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_method_visiblity instance, :my_instance_method, :protected
+ assert_method_visibility instance, :my_instance_method, :protected
assert_equal :new_return_value, instance.send(:my_instance_method)
end
assert_passed(test_result)
@@ -59,7 +59,7 @@ class StubInstanceMethodDefinedOnClassTest < Mocha::TestCase
assert_snapshot_unchanged(instance) do
test_result = run_as_test do
instance.stubs(:my_instance_method).returns(:new_return_value)
- assert_method_visiblity instance, :my_instance_method, :private
+ assert_method_visibility instance, :my_instance_method, :private
assert_equal :new_return_value, instance.send(:my_instance_method)
end
assert_passed(test_result)
diff --git a/test/acceptance/stub_instance_method_defined_on_module_test.rb b/test/acceptance/stub_instance_method_defined_on_module_test.rb
index 41f9dfe..717f727 100644
--- a/test/acceptance/stub_instance_method_defined_on_module_test.rb
+++ b/test/acceptance/stub_instance_method_defined_on_module_test.rb
@@ -26,6 +26,7 @@ class StubInstanceMethodDefinedOnModuleTest < Mocha::TestCase
assert_snapshot_unchanged(instance) do
test_result = run_as_test do
instance.stubs(:my_module_method).returns(:new_return_value)
+ assert_method_visibility instance, :my_module_method, :public
assert_equal :new_return_value, instance.my_module_method
end
assert_passed(test_result)
@@ -46,6 +47,7 @@ class StubInstanceMethodDefinedOnModuleTest < Mocha::TestCase
assert_snapshot_unchanged(instance) do
test_result = run_as_test do
instance.stubs(:my_module_method).returns(:new_return_value)
+ assert_method_visibility instance, :my_module_method, :protected
assert_equal :new_return_value, instance.send(:my_module_method)
end
assert_passed(test_result)
@@ -66,6 +68,7 @@ class StubInstanceMethodDefinedOnModuleTest < Mocha::TestCase
assert_snapshot_unchanged(instance) do
test_result = run_as_test do
instance.stubs(:my_module_method).returns(:new_return_value)
+ assert_method_visibility instance, :my_module_method, :private
assert_equal :new_return_value, instance.send(:my_module_method)
end
assert_passed(test_result)
diff --git a/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb b/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb
new file mode 100644
index 0000000..d893555
--- /dev/null
+++ b/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb
@@ -0,0 +1,39 @@
+require File.expand_path('../acceptance_test_helper', __FILE__)
+require 'mocha/setup'
+
+unless Mocha::PRE_RUBY_V19
+ class StubMethodDefinedOnModuleAndAliasedTest < Mocha::TestCase
+ include AcceptanceTest
+
+ def setup
+ setup_acceptance_test
+ end
+
+ def teardown
+ teardown_acceptance_test
+ end
+
+ def test_stubbing_class_method_defined_by_aliasing_module_instance_method
+ mod = Module.new do
+ def module_instance_method
+ 'module-instance-method'
+ end
+ end
+
+ klass = Class.new do
+ extend mod
+ class << self
+ alias_method :aliased_module_instance_method, :module_instance_method
+ end
+ end
+
+ assert_snapshot_unchanged(klass) do
+ test_result = run_as_test do
+ klass.stubs(:aliased_module_instance_method).returns('stubbed-aliased-module-instance-method')
+ assert_equal 'stubbed-aliased-module-instance-method', klass.aliased_module_instance_method
+ end
+ assert_passed(test_result)
+ end
+ end
+ end
+end
diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb
index ac163e7..f8c55cd 100644
--- a/test/acceptance/stubbing_nil_test.rb
+++ b/test/acceptance/stubbing_nil_test.rb
@@ -13,47 +13,49 @@ class StubbingNilTest < Mocha::TestCase
teardown_acceptance_test
end
- def test_should_allow_stubbing_method_on_nil
- Mocha::Configuration.allow(:stubbing_method_on_nil)
- test_result = run_as_test do
- nil.stubs(:stubbed_method)
+ if RUBY_VERSION < '2.2.0'
+ def test_should_allow_stubbing_method_on_nil
+ Mocha::Configuration.allow(:stubbing_method_on_nil)
+ test_result = run_as_test do
+ nil.stubs(:stubbed_method)
+ end
+ assert_passed(test_result)
+ assert !@logger.warnings.include?("stubbing method on nil: nil.stubbed_method")
end
- assert_passed(test_result)
- assert !@logger.warnings.include?("stubbing method on nil: nil.stubbed_method")
- end
- def test_should_warn_on_stubbing_method_on_nil
- Mocha::Configuration.warn_when(:stubbing_method_on_nil)
- test_result = run_as_test do
- nil.stubs(:stubbed_method)
+ def test_should_warn_on_stubbing_method_on_nil
+ Mocha::Configuration.warn_when(:stubbing_method_on_nil)
+ test_result = run_as_test do
+ nil.stubs(:stubbed_method)
+ end
+ assert_passed(test_result)
+ assert @logger.warnings.include?("stubbing method on nil: nil.stubbed_method")
end
- assert_passed(test_result)
- assert @logger.warnings.include?("stubbing method on nil: nil.stubbed_method")
- end
- def test_should_prevent_stubbing_method_on_nil
- Mocha::Configuration.prevent(:stubbing_method_on_nil)
- test_result = run_as_test do
- nil.stubs(:stubbed_method)
+ def test_should_prevent_stubbing_method_on_nil
+ Mocha::Configuration.prevent(:stubbing_method_on_nil)
+ test_result = run_as_test do
+ nil.stubs(:stubbed_method)
+ end
+ assert_failed(test_result)
+ assert test_result.error_messages.include?("Mocha::StubbingError: stubbing method on nil: nil.stubbed_method")
end
- assert_failed(test_result)
- assert test_result.error_messages.include?("Mocha::StubbingError: stubbing method on nil: nil.stubbed_method")
- end
- def test_should_default_to_prevent_stubbing_method_on_non_mock_object
- test_result = run_as_test do
- nil.stubs(:stubbed_method)
+ def test_should_default_to_prevent_stubbing_method_on_non_mock_object
+ test_result = run_as_test do
+ nil.stubs(:stubbed_method)
+ end
+ assert_failed(test_result)
+ assert test_result.error_messages.include?("Mocha::StubbingError: stubbing method on nil: nil.stubbed_method")
end
- assert_failed(test_result)
- assert test_result.error_messages.include?("Mocha::StubbingError: stubbing method on nil: nil.stubbed_method")
- end
- def test_should_allow_stubbing_method_on_non_nil_object
- Mocha::Configuration.prevent(:stubbing_method_on_nil)
- object = Object.new
- test_result = run_as_test do
- object.stubs(:stubbed_method)
+ def test_should_allow_stubbing_method_on_non_nil_object
+ Mocha::Configuration.prevent(:stubbing_method_on_nil)
+ object = Object.new
+ test_result = run_as_test do
+ object.stubs(:stubbed_method)
+ end
+ assert_passed(test_result)
end
- assert_passed(test_result)
end
end
diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb
index bcfd0ef..58f53e6 100644
--- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb
+++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb
@@ -64,6 +64,19 @@ class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase
assert_passed(test_result)
end
+ def test_should_allow_stubbing_method_to_which_any_instance_responds
+ Mocha::Configuration.prevent(:stubbing_non_existent_method)
+ klass = Class.new do
+ def respond_to?(method, include_private = false)
+ (method == :method_to_which_instance_responds)
+ end
+ end
+ test_result = run_as_test do
+ klass.any_instance.stubs(:method_to_which_instance_responds)
+ end
+ assert_passed(test_result)
+ end
+
def test_should_allow_stubbing_existing_protected_any_instance_method
Mocha::Configuration.prevent(:stubbing_non_existent_method)
klass = Class.new do
diff --git a/test/assertions.rb b/test/assertions.rb
index 0b84199..daed7da 100644
--- a/test/assertions.rb
+++ b/test/assertions.rb
@@ -1,6 +1,8 @@
+require 'mocha/ruby_version'
+
module Assertions
- def assert_method_visiblity(object, method_name, visiblity)
- method_key = RUBY_VERSION < '1.9' ? method_name.to_s : method_name.to_sym
- assert object.send("#{visiblity}_methods", false).include?(method_key), "#{method_name} is not #{visiblity}"
+ def assert_method_visibility(object, method_name, visiblity)
+ method_key = Mocha::PRE_RUBY_V19 ? method_name.to_s : method_name.to_sym
+ assert object.send("#{visiblity}_methods").include?(method_key), "#{method_name} is not #{visiblity}"
end
-end
\ No newline at end of file
+end
diff --git a/test/minitest_result.rb b/test/minitest_result.rb
index af05cd7..0e85ac6 100644
--- a/test/minitest_result.rb
+++ b/test/minitest_result.rb
@@ -20,7 +20,7 @@ class MinitestResult
end
def failures
- @tests.map(&:failures).flatten.select { |r| Minitest::Assertion === r }.map { |f| Failure.new(f) }
+ @tests.map(&:failures).flatten.select { |r| r.instance_of?(Minitest::Assertion) }.map { |f| Failure.new(f) }
end
def failure_count
@@ -32,7 +32,7 @@ class MinitestResult
end
def errors
- @tests.map(&:failures).flatten.select { |r| Minitest::UnexpectedError === r }
+ @tests.map(&:failures).flatten.select { |r| r.instance_of?(Minitest::UnexpectedError) }
end
def error_count
diff --git a/test/test_runner.rb b/test/test_runner.rb
index dc7c51d..4c96d35 100644
--- a/test/test_runner.rb
+++ b/test/test_runner.rb
@@ -49,7 +49,7 @@ module TestRunner
def assert_passed(test_result)
flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
- flunk "Test failed unexpectedly with message: #{test_result.errors}" if test_result.error_count > 0
+ flunk "Test failed unexpectedly with message: #{test_result.errors.map(&:exception)}" if test_result.error_count > 0
end
def assert_failed(test_result)
diff --git a/test/unit/any_instance_method_test.rb b/test/unit/any_instance_method_test.rb
index 8876058..9006830 100644
--- a/test/unit/any_instance_method_test.rb
+++ b/test/unit/any_instance_method_test.rb
@@ -7,6 +7,7 @@ class AnyInstanceMethodTest < Mocha::TestCase
include Mocha
+unless RUBY_V2_PLUS
def test_should_hide_original_method
klass = Class.new { def method_x; end }
method = AnyInstanceMethod.new(klass, :method_x)
@@ -15,6 +16,7 @@ class AnyInstanceMethodTest < Mocha::TestCase
assert_equal false, klass.method_defined?(:method_x)
end
+end
def test_should_not_raise_error_hiding_method_that_isnt_defined
klass = Class.new
diff --git a/test/unit/class_method_test.rb b/test/unit/class_method_test.rb
index b349d0c..75ad96e 100644
--- a/test/unit/class_method_test.rb
+++ b/test/unit/class_method_test.rb
@@ -8,6 +8,7 @@ class ClassMethodTest < Mocha::TestCase
include Mocha
+unless RUBY_V2_PLUS
def test_should_hide_original_method
klass = Class.new { def self.method_x; end }
method = ClassMethod.new(klass, :method_x)
@@ -16,6 +17,7 @@ class ClassMethodTest < Mocha::TestCase
assert_equal false, klass.respond_to?(:method_x)
end
+end
def test_should_not_raise_error_hiding_method_that_isnt_defined
klass = Class.new
diff --git a/test/unit/mock_test.rb b/test/unit/mock_test.rb
index 868fec5..7dc02e1 100644
--- a/test/unit/mock_test.rb
+++ b/test/unit/mock_test.rb
@@ -1,4 +1,5 @@
require File.expand_path('../../test_helper', __FILE__)
+require 'mocha/ruby_version'
require 'mocha/mock'
require 'mocha/expectation_error_factory'
require 'set'
@@ -44,7 +45,7 @@ class MockTest < Mocha::TestCase
assert_equal true, mock.eql?(mock)
end
- if RUBY_VERSION < '1.9'
+ if PRE_RUBY_V19
OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || ["method_missing", "singleton_method_undefined", "initialize"].include?(m)}
else
OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || [:object_id, :method_missing, :singleton_method_undefined, :initialize, :String, :singleton_method_added].include?(m) }
diff --git a/test/unit/object_inspect_test.rb b/test/unit/object_inspect_test.rb
index e5ef1a8..d8297cc 100644
--- a/test/unit/object_inspect_test.rb
+++ b/test/unit/object_inspect_test.rb
@@ -1,4 +1,5 @@
require File.expand_path('../../test_helper', __FILE__)
+require 'mocha/ruby_version'
require 'mocha/inspect'
require 'method_definer'
@@ -25,7 +26,7 @@ class ObjectInspectTest < Mocha::TestCase
def test_should_use_underscored_id_instead_of_object_id_or_id_so_that_they_can_be_stubbed
calls = []
object = Object.new
- object.replace_instance_method(:id) { calls << :id; return 1 } if RUBY_VERSION < '1.9'
+ object.replace_instance_method(:id) { calls << :id; return 1 } if Mocha::PRE_RUBY_V19
object.replace_instance_method(:object_id) { calls << :object_id; return 1 }
object.replace_instance_method(:__id__) { calls << :__id__; return 1 }
object.replace_instance_method(:inspect) { "object-description" }
diff --git a/test/unit/parameter_matchers/includes_test.rb b/test/unit/parameter_matchers/includes_test.rb
index 0eb7893..e303941 100644
--- a/test/unit/parameter_matchers/includes_test.rb
+++ b/test/unit/parameter_matchers/includes_test.rb
@@ -1,6 +1,9 @@
require File.expand_path('../../../test_helper', __FILE__)
require 'mocha/parameter_matchers/includes'
+require 'mocha/parameter_matchers/object'
+require 'mocha/parameter_matchers/has_key'
+require 'mocha/parameter_matchers/regexp_matches'
require 'mocha/inspect'
class IncludesTest < Mocha::TestCase
@@ -56,4 +59,44 @@ class IncludesTest < Mocha::TestCase
matcher = includes(:x)
assert !matcher.matches?([:x])
end
+
+ def test_should_match_object_including_value_which_matches_nested_matcher
+ matcher = includes(has_key(:key))
+ assert matcher.matches?([[:non_matching_element, {:key => 'value'}]])
+ end
+
+ def test_should_not_match_object_which_doesnt_include_value_that_matches_nested_matcher
+ matcher = includes(has_key(:key))
+ assert !matcher.matches?([[:non_matching_element, {:other_key => 'other-value'}]])
+ end
+
+ def test_should_match_string_argument_containing_substring
+ matcher = includes('bar')
+ assert matcher.matches?(['foobarbaz'])
+ end
+
+ def test_should_not_match_string_argument_without_substring
+ matcher = includes('bar')
+ assert !matcher.matches?(['foobaz'])
+ end
+
+ def test_should_match_hash_argument_containing_given_key
+ matcher = includes(:key)
+ assert matcher.matches?([{:thing => 1, :key => 2}])
+ end
+
+ def test_should_not_match_hash_argument_missing_given_key
+ matcher = includes(:key)
+ assert !matcher.matches?([{:thing => 1, :other => :key}])
+ end
+
+ def test_should_match_hash_when_nested_matcher_matches_key
+ matcher = includes(regexp_matches(/ar/))
+ assert matcher.matches?([{'foo' => 1, 'bar' => 2}])
+ end
+
+ def test_should_not_match_hash_when_nested_matcher_doesn_not_match_key
+ matcher = includes(regexp_matches(/az/))
+ assert !matcher.matches?([{'foo' => 1, 'bar' => 2}])
+ end
end
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-mocha.git
More information about the Pkg-ruby-extras-commits
mailing list