mocha 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/RELEASE +17 -2
  2. data/Rakefile +10 -7
  3. data/examples/misc.rb +0 -1
  4. data/examples/mocha.rb +0 -1
  5. data/examples/stubba.rb +0 -1
  6. data/lib/mocha.rb +1 -47
  7. data/lib/mocha/any_instance_method.rb +5 -1
  8. data/lib/mocha/{standalone.rb → api.rb} +8 -1
  9. data/lib/mocha/class_method.rb +5 -1
  10. data/lib/mocha/expectation.rb +3 -3
  11. data/lib/mocha/integration.rb +38 -0
  12. data/lib/mocha/integration/mini_test.rb +21 -0
  13. data/lib/mocha/integration/mini_test/assertion_counter.rb +23 -0
  14. data/lib/mocha/integration/mini_test/version_131_and_above.rb +50 -0
  15. data/lib/mocha/integration/test_unit.rb +40 -0
  16. data/lib/mocha/integration/test_unit/assertion_counter.rb +23 -0
  17. data/lib/mocha/integration/test_unit/gem_version_200.rb +49 -0
  18. data/lib/mocha/integration/test_unit/gem_version_201_and_above.rb +49 -0
  19. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +48 -0
  20. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +50 -0
  21. data/lib/mocha/parameter_matchers/has_entry.rb +1 -0
  22. data/lib/mocha_standalone.rb +2 -2
  23. data/test/acceptance/{standalone_test.rb → api_test.rb} +2 -2
  24. data/test/acceptance/bug_21465_test.rb +2 -2
  25. data/test/acceptance/bug_21563_test.rb +1 -1
  26. data/test/acceptance/expected_invocation_count_test.rb +19 -19
  27. data/test/acceptance/failure_messages_test.rb +6 -6
  28. data/test/acceptance/minitest_test.rb +32 -9
  29. data/test/acceptance/mocha_test_result_test.rb +8 -8
  30. data/test/acceptance/mock_test.rb +10 -10
  31. data/test/acceptance/mock_with_initializer_block_test.rb +3 -3
  32. data/test/acceptance/mocked_methods_dispatch_test.rb +5 -5
  33. data/test/acceptance/optional_parameters_test.rb +6 -6
  34. data/test/acceptance/parameter_matcher_test.rb +20 -20
  35. data/test/acceptance/partial_mocks_test.rb +2 -2
  36. data/test/acceptance/return_value_test.rb +4 -4
  37. data/test/acceptance/sequence_test.rb +11 -11
  38. data/test/acceptance/states_test.rb +4 -4
  39. data/test/acceptance/stub_any_instance_method_test.rb +12 -12
  40. data/test/acceptance/stub_class_method_test.rb +12 -12
  41. data/test/acceptance/stub_everything_test.rb +4 -4
  42. data/test/acceptance/stub_instance_method_test.rb +13 -13
  43. data/test/acceptance/stub_module_method_test.rb +12 -12
  44. data/test/acceptance/stub_test.rb +4 -4
  45. data/test/acceptance/stubba_test.rb +1 -1
  46. data/test/acceptance/stubba_test_result_test.rb +6 -6
  47. data/test/acceptance/stubbing_error_backtrace_test.rb +4 -4
  48. data/test/acceptance/stubbing_method_unnecessarily_test.rb +5 -5
  49. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +10 -10
  50. data/test/acceptance/stubbing_non_existent_class_method_test.rb +11 -11
  51. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +11 -11
  52. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +9 -9
  53. data/test/acceptance/stubbing_non_public_class_method_test.rb +10 -10
  54. data/test/acceptance/stubbing_non_public_instance_method_test.rb +10 -10
  55. data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
  56. data/test/test_helper.rb +4 -0
  57. data/test/test_runner.rb +1 -1
  58. data/test/unit/parameter_matchers/has_entry_test.rb +20 -0
  59. metadata +14 -6
  60. data/lib/mocha/mini_test_adapter.rb +0 -50
  61. data/lib/mocha/test_case_adapter.rb +0 -103
data/RELEASE CHANGED
@@ -1,7 +1,22 @@
1
+ = 0.9.6 ()
2
+ * Version 2.0.1 of the test-unit gem introduced a private 'run_test' method on TestCase which clashed with the public TestRunner#run_test method. So this latter method has been renamed to 'run_as_test'.
3
+ * Stop requiring rubygems - this should be an environmental choice for the user. http://gist.github.com/54177 - describes why requiring rubygems in your library code is a bad idea.
4
+ * It seems like overkill to vendorize coderay and meta_project when they're only needed to generate the examples for documentation and for publishing files on RubyForge. So I'm removing them and installing them locally as gems when I need them.
5
+ * Added support for 'test-unit' gem (version >= 2.0). Note that as with other versions of Test::Unit I'm completely replacing the TestCase#run method. Unfortunately in version 2.0.0 this method differs slightly from the same method in version 2.0.1 & 2.0.2, so we have to provide different implementations to ensure that the internal working of Test::Unit are not compromised by Mocha. Note also that unless the 'test-unit' gem is loaded, requiring 'test/unit' leads to a mixture of stdlib and gem classes being loaded causing errors. To avoid a dependency on rubygems, the gem is loaded only if MOCHA_OPTIONS is set to 'use_test_unit_gem' - this option is only intended for use in running Mocha's own tests. It might be worthwhile to create a shim gem like minitest_tu_shim to allow the test-unit gem to completely replace the stdlib, but that's a job for another day. The changes in the Rakefile are to make the default task run with the 'test-unit' gem (version >= 2.0).
6
+ * Renamed Mocha::Standalone to Mocha::API to better reflect its purpose. Added a deprecation warning for those who are referencing Mocha::Standalone.
7
+ * Fix exception raised by HasEntry#matches? if first param is not a Hash (thanks to Taylor Barstow).
8
+ * Ken Collins reported [1] that Mocha is always loading MiniTest if it is available and loading it causes some Rails/ActionPack tests to break. I've removed the loading of MiniTest, but this now means the user has to ensure that if they want to use MiniTest in conjunction with Mocha, he must load MiniTest before loading Mocha. [1] http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2060
9
+ * Implemented Bacon integration (thanks to Ubiratan Pires Alberton), but this was then removed after deciding only to maintain integration with Test::Unit and MiniTest which are both Ruby standard libraries. See mailing list for details.
10
+ * Don't monkey-patch MiniTest if it's already been monkey-patched by Mocha.
11
+ * Fixed bug: MiniTest integration was counting ExpectationErrors as errors not failures. http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/41.
12
+ * Fixed bug: Some Bacon tests were failing in Ruby 1.9.1. http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/43.
13
+ * Chad Humphries pointed out that in Ruby 1.9.1, if you are not using Test::Unit or MiniTest, Mocha will attempt to load and monkey-patch Test::Unit. Mocha will now only monkey-patch Test::Unit and/or MiniTest if they have already been loaded. MiniTest tests will now run in both Ruby 1.8.6 (with MiniTest gem) and in Ruby 1.9.1 (with MiniTest std lib). See Ligthouse ticket - http://floehopper.lighthouseapp.com/projects/22289/tickets/49.
14
+ * Made Mocha compatible with minitest 1.4.0 and above (thanks to Denis Defreyne).
15
+
1
16
  = 0.9.5 (93cad010345ce5d68f31422cfc32ed9dd6de13ec)
2
17
  * Fixed Lighthouse bug #32 - stub_everything should mean mock responds to anything.
3
- * Added Expectation#twice to improve readability. Tthanks to pull request from Celestino Gomes.
4
- * In Ruby 1.9, requiring 'test/unit' loads a thin wrapper around MiniTest and Test::Unit::TestCase ends up inheriting from MiniTest::Unit::TestCase. So we need to avoid including the Mocha modules more than once to avoid nasty consequences. Thanks to Matthias Hennemeyer for help with this.
18
+ * Added Expectation#twice to improve readability. Thanks to pull request from Celestino Gomes.
19
+ * In Ruby 1.9.1, requiring 'test/unit' loads a thin wrapper around MiniTest and Test::Unit::TestCase ends up inheriting from MiniTest::Unit::TestCase. So we need to avoid including the Mocha modules more than once to avoid nasty consequences. Thanks to Matthias Hennemeyer for help with this.
5
20
  * Ruby 1.9 includes rake, but not rake/contrib. For the moment I've moved the sshpublisher require into the only rake task that needs it, so that I can at least run the tests in Ruby 1.9. It looks like I will need to build a rake/contrib gem or similar to get this working properly - http://intertwingly.net/blog/2008/01/07/Rake-Contrib-for-1-9
6
21
 
7
22
  = 0.9.4 (8a59c6ff0f99f34b02bd99f19536a7893be2b340)
data/Rakefile CHANGED
@@ -1,10 +1,9 @@
1
- require 'rubygems'
2
1
  require 'rake/rdoctask'
3
2
  require 'rake/gempackagetask'
4
3
  require 'rake/testtask'
5
4
 
6
5
  module Mocha
7
- VERSION = "0.9.5"
6
+ VERSION = "0.9.6"
8
7
  end
9
8
 
10
9
  desc "Run all tests"
@@ -55,9 +54,15 @@ namespace 'test' do
55
54
  end
56
55
 
57
56
  def benchmark_test_case(klass, iterations)
58
- require 'test/unit/ui/console/testrunner'
59
57
  require 'benchmark'
60
- time = Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, Test::Unit::UI::SILENT) } }
58
+ require 'test/unit/ui/console/testrunner'
59
+ begin
60
+ require 'test/unit/ui/console/outputlevel'
61
+ silent_option = { :output_level => Test::Unit::UI::Console::OutputLevel::SILENT }
62
+ rescue LoadError
63
+ silent_option = Test::Unit::UI::SILENT
64
+ end
65
+ time = Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, silent_option) } }
61
66
  end
62
67
 
63
68
  desc 'Generate RDoc'
@@ -72,7 +77,7 @@ Rake::RDocTask.new('rdoc') do |task|
72
77
  'COPYING',
73
78
  'MIT-LICENSE',
74
79
  'agiledox.txt',
75
- 'lib/mocha/standalone.rb',
80
+ 'lib/mocha/api.rb',
76
81
  'lib/mocha/mock.rb',
77
82
  'lib/mocha/expectation.rb',
78
83
  'lib/mocha/object.rb',
@@ -111,7 +116,6 @@ end
111
116
 
112
117
  desc "Convert example ruby files to syntax-highlighted html"
113
118
  task 'examples' do
114
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "vendor", "coderay-0.7.4.215", "lib"))
115
119
  require 'coderay'
116
120
  mkdir_p 'doc/examples'
117
121
  File.open('doc/examples/coderay.css', 'w') do |output|
@@ -180,7 +184,6 @@ end
180
184
 
181
185
  desc "Publish package files on RubyForge."
182
186
  task 'publish_packages' => ['verify_user', 'verify_password', 'clobber_package', 'package'] do
183
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "vendor", "meta_project-0.4.15", "lib"))
184
187
  require 'meta_project'
185
188
  require 'rake/contrib/xforge'
186
189
  release_files = FileList[
@@ -1,5 +1,4 @@
1
1
  require 'test/unit'
2
- require 'rubygems'
3
2
  require 'mocha'
4
3
 
5
4
  class MiscExampleTest < Test::Unit::TestCase
@@ -11,7 +11,6 @@ class Enterprise
11
11
  end
12
12
 
13
13
  require 'test/unit'
14
- require 'rubygems'
15
14
  require 'mocha'
16
15
 
17
16
  class EnterpriseTest < Test::Unit::TestCase
@@ -33,7 +33,6 @@ class Order
33
33
  end
34
34
 
35
35
  require 'test/unit'
36
- require 'rubygems'
37
36
  require 'mocha'
38
37
 
39
38
  class OrderTest < Test::Unit::TestCase
@@ -1,49 +1,3 @@
1
1
  require 'mocha_standalone'
2
2
  require 'mocha/configuration'
3
-
4
- if RUBY_VERSION < '1.9'
5
- begin
6
- require 'rubygems'
7
- begin
8
- gem 'minitest', '>=1.3'
9
- require 'minitest/unit'
10
- rescue Gem::LoadError
11
- # Compatible version of MiniTest gem not available
12
- end
13
- rescue LoadError
14
- # RubyGems not available
15
- end
16
- else
17
- begin
18
- require 'minitest/unit'
19
- rescue LoadError
20
- # MiniTest not available
21
- end
22
- end
23
-
24
- if defined?(MiniTest)
25
- require 'mocha/mini_test_adapter'
26
-
27
- module MiniTest
28
- class Unit
29
- class TestCase
30
- include Mocha::Standalone
31
- include Mocha::MiniTestCaseAdapter
32
- end
33
- end
34
- end
35
- end
36
-
37
- require 'mocha/test_case_adapter'
38
- require 'test/unit/testcase'
39
-
40
- unless Test::Unit::TestCase.ancestors.include?(Mocha::Standalone)
41
- module Test
42
- module Unit
43
- class TestCase
44
- include Mocha::Standalone
45
- include Mocha::TestCaseAdapter
46
- end
47
- end
48
- end
49
- end
3
+ require 'mocha/integration'
@@ -25,7 +25,11 @@ module Mocha
25
25
  end
26
26
 
27
27
  def define_new_method
28
- stubbee.class_eval("def #{method}(*args, &block); self.class.any_instance.mocha.method_missing(:#{method}, *args, &block); end", __FILE__, __LINE__)
28
+ stubbee.class_eval(%{
29
+ def #{method}(*args, &block)
30
+ self.class.any_instance.mocha.method_missing(:#{method}, *args, &block)
31
+ end
32
+ }, __FILE__, __LINE__)
29
33
  end
30
34
 
31
35
  def remove_new_method
@@ -5,7 +5,7 @@ require 'mocha/sequence'
5
5
  module Mocha # :nodoc:
6
6
 
7
7
  # Methods added to Test::Unit::TestCase or equivalent.
8
- module Standalone
8
+ module API
9
9
 
10
10
  include ParameterMatchers
11
11
 
@@ -163,4 +163,11 @@ module Mocha # :nodoc:
163
163
 
164
164
  end
165
165
 
166
+ def self.const_missing(name)
167
+ return super unless name == :Standalone
168
+ require 'mocha/deprecation'
169
+ Deprecation.warning "Mocha::Standalone has been renamed to Mocha::API"
170
+ return API
171
+ end
172
+
166
173
  end
@@ -37,7 +37,11 @@ module Mocha
37
37
  end
38
38
 
39
39
  def define_new_method
40
- stubbee.__metaclass__.class_eval("def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end", __FILE__, __LINE__)
40
+ stubbee.__metaclass__.class_eval(%{
41
+ def #{method}(*args, &block)
42
+ mocha.method_missing(:#{method}, *args, &block)
43
+ end
44
+ }, __FILE__, __LINE__)
41
45
  end
42
46
 
43
47
  def remove_new_method
@@ -320,7 +320,7 @@ module Mocha # :nodoc:
320
320
  #
321
321
  # <tt>then(state_machine.is(state))</tt> is used to change the +state_machine+ to the specified +state+ when the invocation occurs.
322
322
  #
323
- # See also Standalone#states, StateMachine and Expectation#when.
323
+ # See also API#states, StateMachine and Expectation#when.
324
324
  # power = states('power').starts_as('off')
325
325
  #
326
326
  # radio = mock('radio')
@@ -342,7 +342,7 @@ module Mocha # :nodoc:
342
342
  #
343
343
  # Constrains the expectation to occur only when the +state_machine+ is in the named +state+.
344
344
  #
345
- # See also Standalone#states, StateMachine#starts_as and Expectation#then.
345
+ # See also API#states, StateMachine#starts_as and Expectation#then.
346
346
  # power = states('power').starts_as('off')
347
347
  #
348
348
  # radio = mock('radio')
@@ -367,7 +367,7 @@ module Mocha # :nodoc:
367
367
  #
368
368
  # If an expectation in a sequence is stubbed, rather than expected, it can be skipped in the sequence.
369
369
  #
370
- # See also Standalone#sequence.
370
+ # See also API#sequence.
371
371
  # breakfast = sequence('breakfast')
372
372
  #
373
373
  # egg = mock('egg')
@@ -0,0 +1,38 @@
1
+ module Mocha
2
+
3
+ module Integration
4
+
5
+ class << self
6
+
7
+ def monkey_patches
8
+ patches = []
9
+ if test_unit_testcase_defined? && !test_unit_testcase_inherits_from_miniunit_testcase?
10
+ patches << 'mocha/integration/test_unit'
11
+ end
12
+ if mini_unit_testcase_defined?
13
+ patches << 'mocha/integration/mini_test'
14
+ end
15
+ patches
16
+ end
17
+
18
+ def test_unit_testcase_defined?
19
+ defined?(Test) && defined?(Test::Unit) && defined?(Test::Unit::TestCase)
20
+ end
21
+
22
+ def mini_unit_testcase_defined?
23
+ defined?(MiniTest) && defined?(MiniTest::Unit) && defined?(MiniTest::Unit::TestCase)
24
+ end
25
+
26
+ def test_unit_testcase_inherits_from_miniunit_testcase?
27
+ test_unit_testcase_defined? && mini_unit_testcase_defined? && Test::Unit::TestCase.ancestors.include?(MiniTest::Unit::TestCase)
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ Mocha::Integration.monkey_patches.each do |patch|
37
+ require patch
38
+ end
@@ -0,0 +1,21 @@
1
+ require 'mocha/api'
2
+
3
+ if !MiniTest::Unit::TestCase.ancestors.include?(Mocha::API)
4
+
5
+ require 'mocha/integration/mini_test/version_131_and_above'
6
+
7
+ module MiniTest
8
+ class Unit
9
+ class TestCase
10
+
11
+ include Mocha::API
12
+
13
+ alias_method :run_before_mocha, :run
14
+ remove_method :run
15
+
16
+ include Mocha::Integration::MiniTest::Version131AndAbove
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module Mocha
2
+
3
+ module Integration
4
+
5
+ module MiniTest
6
+
7
+ class AssertionCounter
8
+
9
+ def initialize(test_case)
10
+ @test_case = test_case
11
+ end
12
+
13
+ def increment
14
+ @test_case._assertions += 1
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,50 @@
1
+ require 'mocha/integration/mini_test/assertion_counter'
2
+ require 'mocha/expectation_error'
3
+
4
+ module Mocha
5
+
6
+ module Integration
7
+
8
+ module MiniTest
9
+
10
+ def self.translate(exception)
11
+ return exception unless exception.kind_of?(::Mocha::ExpectationError)
12
+ translated_exception = ::MiniTest::Assertion.new(exception.message)
13
+ translated_exception.set_backtrace(exception.backtrace)
14
+ translated_exception
15
+ end
16
+
17
+ module Version131AndAbove
18
+ def run runner
19
+ assertion_counter = AssertionCounter.new(self)
20
+ result = '.'
21
+ name = (self.respond_to?(:name) ? self.name : self.__name__)
22
+ begin
23
+ begin
24
+ @passed = nil
25
+ self.setup
26
+ self.__send__ name
27
+ mocha_verify(assertion_counter)
28
+ @passed = true
29
+ rescue Exception => e
30
+ @passed = false
31
+ result = runner.puke(self.class, name, Mocha::Integration::MiniTest.translate(e))
32
+ ensure
33
+ begin
34
+ self.teardown
35
+ rescue Exception => e
36
+ result = runner.puke(self.class, name, Mocha::Integration::MiniTest.translate(e))
37
+ end
38
+ end
39
+ ensure
40
+ mocha_teardown
41
+ end
42
+ result
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,40 @@
1
+ require 'mocha/api'
2
+
3
+ if !Test::Unit::TestCase.ancestors.include?(Mocha::API)
4
+
5
+ require 'mocha/integration/test_unit/gem_version_200'
6
+ require 'mocha/integration/test_unit/gem_version_201_and_above'
7
+ require 'mocha/integration/test_unit/ruby_version_185_and_below'
8
+ require 'mocha/integration/test_unit/ruby_version_186_and_above'
9
+
10
+ module Test
11
+ module Unit
12
+ class TestCase
13
+
14
+ include Mocha::API
15
+
16
+ alias_method :run_before_mocha, :run
17
+ remove_method :run
18
+
19
+ test_unit_version = begin
20
+ require 'test/unit/version'
21
+ Test::Unit::VERSION
22
+ rescue LoadError
23
+ '1.x'
24
+ end
25
+
26
+ if test_unit_version == '2.0.0'
27
+ include Mocha::Integration::TestUnit::GemVersion200
28
+ elsif test_unit_version >= '2.0.1'
29
+ include Mocha::Integration::TestUnit::GemVersion201AndAbove
30
+ elsif RUBY_VERSION < '1.8.6'
31
+ include Mocha::Integration::TestUnit::RubyVersion185AndBelow
32
+ else
33
+ include Mocha::Integration::TestUnit::RubyVersion186AndAbove
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,23 @@
1
+ module Mocha
2
+
3
+ module Integration
4
+
5
+ module TestUnit
6
+
7
+ class AssertionCounter
8
+
9
+ def initialize(test_result)
10
+ @test_result = test_result
11
+ end
12
+
13
+ def increment
14
+ @test_result.add_assertion
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,49 @@
1
+ require 'test/unit/testcase'
2
+ require 'mocha/integration/test_unit/assertion_counter'
3
+ require 'mocha/expectation_error'
4
+
5
+ module Mocha
6
+
7
+ module Integration
8
+
9
+ module TestUnit
10
+
11
+ module GemVersion200
12
+ def run(result)
13
+ assertion_counter = AssertionCounter.new(result)
14
+ begin
15
+ @_result = result
16
+ yield(Test::Unit::TestCase::STARTED, name)
17
+ begin
18
+ begin
19
+ run_setup
20
+ __send__(@method_name)
21
+ mocha_verify(assertion_counter)
22
+ rescue Mocha::ExpectationError => e
23
+ add_failure(e.message, e.backtrace)
24
+ rescue Exception
25
+ @interrupted = true
26
+ raise unless handle_exception($!)
27
+ ensure
28
+ begin
29
+ run_teardown
30
+ rescue Exception
31
+ raise unless handle_exception($!)
32
+ end
33
+ end
34
+ ensure
35
+ mocha_teardown
36
+ end
37
+ result.add_run
38
+ yield(Test::Unit::TestCase::FINISHED, name)
39
+ ensure
40
+ @_result = nil
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end