mocha 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/README +4 -4
  2. data/RELEASE +32 -0
  3. data/Rakefile +3 -3
  4. data/examples/misc.rb +36 -0
  5. data/examples/mocha.rb +26 -0
  6. data/examples/stubba.rb +65 -0
  7. data/lib/mocha.rb +17 -5
  8. data/lib/{stubba → mocha}/any_instance_method.rb +2 -2
  9. data/lib/mocha/auto_verify.rb +7 -7
  10. data/lib/{stubba → mocha}/central.rb +2 -2
  11. data/lib/{stubba → mocha}/class_method.rb +7 -10
  12. data/lib/mocha/expectation.rb +88 -33
  13. data/lib/mocha/expectation_error.rb +6 -0
  14. data/lib/mocha/inspect.rb +1 -1
  15. data/lib/mocha/instance_method.rb +8 -0
  16. data/lib/mocha/metaclass.rb +1 -1
  17. data/lib/mocha/mock.rb +8 -8
  18. data/lib/mocha/mock_methods.rb +60 -16
  19. data/lib/{stubba → mocha}/object.rb +8 -10
  20. data/lib/mocha/setup_and_teardown.rb +23 -0
  21. data/lib/mocha/standalone.rb +30 -0
  22. data/lib/mocha/test_case_adapter.rb +49 -0
  23. data/lib/mocha_standalone.rb +2 -0
  24. data/lib/stubba.rb +2 -8
  25. data/test/all_tests.rb +9 -10
  26. data/test/method_definer.rb +2 -2
  27. data/test/{stubba → mocha}/any_instance_method_test.rb +1 -3
  28. data/test/mocha/auto_verify_test.rb +9 -10
  29. data/test/{stubba → mocha}/central_test.rb +5 -4
  30. data/test/{stubba → mocha}/class_method_test.rb +40 -10
  31. data/test/mocha/expectation_test.rb +144 -67
  32. data/test/mocha/inspect_test.rb +12 -1
  33. data/test/mocha/metaclass_test.rb +22 -0
  34. data/test/mocha/mock_methods_test.rb +65 -7
  35. data/test/mocha/mock_test.rb +41 -4
  36. data/test/{stubba → mocha}/object_test.rb +9 -9
  37. data/test/mocha/setup_and_teardown_test.rb +76 -0
  38. data/test/mocha_acceptance_test.rb +8 -8
  39. data/test/mocha_test_result_integration_test.rb +5 -6
  40. data/test/standalone_acceptance_test.rb +110 -0
  41. data/test/stubba_acceptance_test.rb +2 -2
  42. data/test/stubba_integration_test.rb +17 -24
  43. data/test/stubba_test_result_integration_test.rb +5 -6
  44. metadata +22 -18
  45. data/lib/shared/backtracefilter.rb +0 -46
  46. data/lib/smart_test_case.rb +0 -5
  47. data/lib/smart_test_case/multiple_setup_and_teardown.rb +0 -123
  48. data/lib/stubba/instance_method.rb +0 -18
  49. data/lib/stubba/setup_and_teardown.rb +0 -25
  50. data/test/smart_test_case/multiple_setup_and_teardown_test.rb +0 -192
  51. data/test/stubba/instance_method_test.rb +0 -46
  52. data/test/stubba/setup_and_teardown_test.rb +0 -134
@@ -0,0 +1,6 @@
1
+ module Mocha
2
+
3
+ class ExpectationError < StandardError
4
+ end
5
+
6
+ end
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  class Object
4
4
  def mocha_inspect
5
- inspect =~ /#</ ? "#<#{self.class}: #{self.object_id}>" : inspect
5
+ inspect =~ /#</ ? "#<#{self.class}:0x#{self.__id__.to_s(16)}>" : inspect
6
6
  end
7
7
  end
8
8
 
@@ -0,0 +1,8 @@
1
+ require 'mocha/class_method'
2
+
3
+ module Mocha
4
+
5
+ class InstanceMethod < ClassMethod
6
+ end
7
+
8
+ end
@@ -1,6 +1,6 @@
1
1
  class Object
2
2
 
3
- def metaclass
3
+ def __metaclass__
4
4
  class << self; self; end
5
5
  end
6
6
 
@@ -1,20 +1,20 @@
1
1
  require 'mocha/mock_methods'
2
2
 
3
3
  module Mocha
4
+
4
5
  class Mock
5
-
6
- include MockMethods
7
6
 
8
- attr_reader :__mock_name
7
+ include MockMethods
8
+
9
9
  def initialize(stub_everything = false, name = nil)
10
10
  @stub_everything = stub_everything
11
- @__mock_name = name
11
+ @mock_name = name
12
12
  end
13
-
14
- alias :mocha_inspect_before_hijacked_by_named_mocks :mocha_inspect
13
+
15
14
  def mocha_inspect
16
- @__mock_name ? "#<Mock: '#{@__mock_name}'>" : mocha_inspect_before_hijacked_by_named_mocks
15
+ @mock_name ? "#<Mock:#{@mock_name}>" : "#<Mock:0x#{__id__.to_s(16)}>"
17
16
  end
18
-
17
+
19
18
  end
19
+
20
20
  end
@@ -1,4 +1,5 @@
1
1
  require 'mocha/expectation'
2
+ require 'mocha/metaclass'
2
3
 
3
4
  module Mocha
4
5
  # Methods added to mock objects.
@@ -15,22 +16,69 @@ module Mocha
15
16
 
16
17
  # :startdoc:
17
18
 
18
- # :call-seq: expects(symbol) -> expectation
19
+ # :call-seq: expects(method_name) -> expectation
20
+ # expects(method_names) -> last expectation
19
21
  #
20
- # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
21
- def expects(symbol, backtrace = nil)
22
- expectations << Expectation.new(symbol, backtrace)
22
+ # Adds an expectation that a method identified by +method_name+ symbol must be called exactly once with any parameters.
23
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
24
+ # object = mock()
25
+ # object.expects(:method1)
26
+ # object.method1
27
+ # # no error raised
28
+ #
29
+ # object = mock()
30
+ # object.expects(:method1)
31
+ # # error raised, because method1 not called exactly once
32
+ # If +method_names+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
33
+ # object = mock()
34
+ # object.expects(:method1 => :result1, :method2 => :result2)
35
+ #
36
+ # # exactly equivalent to
37
+ #
38
+ # object = mock()
39
+ # object.expects(:method1).returns(:result1)
40
+ # object.expects(:method2).returns(:result2)
41
+ def expects(method_names, backtrace = nil)
42
+ method_names = method_names.is_a?(Hash) ? method_names : { method_names => nil }
43
+ method_names.each do |method_name, return_value|
44
+ expectations << Expectation.new(self, method_name, backtrace).returns(return_value)
45
+ self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
46
+ end
23
47
  expectations.last
24
48
  end
25
49
 
26
- # :call-seq: stubs(symbol) -> expectation
50
+ alias_method :__expects__, :expects
51
+
52
+ # :call-seq: stubs(method_name) -> expectation
53
+ # stubs(method_names) -> last expectation
54
+ #
55
+ # Adds an expectation that a method identified by +method_name+ symbol may be called any number of times with any parameters.
56
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
57
+ # object = mock()
58
+ # object.stubs(:method1)
59
+ # object.method1
60
+ # object.method1
61
+ # # no error raised
62
+ # If +method_names+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
63
+ # object = mock()
64
+ # object.stubs(:method1 => :result1, :method2 => :result2)
65
+ #
66
+ # # exactly equivalent to
27
67
  #
28
- # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
29
- def stubs(symbol, backtrace = nil)
30
- expectations << Stub.new(symbol, backtrace)
68
+ # object = mock()
69
+ # object.stubs(:method1).returns(:result1)
70
+ # object.stubs(:method2).returns(:result2)
71
+ def stubs(method_names, backtrace = nil)
72
+ method_names = method_names.is_a?(Hash) ? method_names : { method_names => nil }
73
+ method_names.each do |method_name, return_value|
74
+ expectations << Stub.new(self, method_name, backtrace).returns(return_value)
75
+ self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
76
+ end
31
77
  expectations.last
32
78
  end
33
79
 
80
+ alias_method :__stubs__, :stubs
81
+
34
82
  # :stopdoc:
35
83
 
36
84
  def method_missing(symbol, *arguments, &block)
@@ -57,22 +105,18 @@ module Mocha
57
105
  end
58
106
 
59
107
  def unexpected_method_called(symbol, *arguments)
60
- MissingExpectation.new(symbol, self, expectations).with(*arguments).verify
108
+ MissingExpectation.new(self, symbol).with(*arguments).verify
61
109
  end
62
110
 
63
111
  def matching_expectation(symbol, *arguments)
64
- expectations.detect { |expectation| expectation.match?(symbol, *arguments) }
112
+ expectations.reverse.detect { |expectation| expectation.match?(symbol, *arguments) }
65
113
  end
66
114
 
67
- # :startdoc:
68
-
69
- # :call-seq: verify
70
- #
71
- # Asserts that all expectations have been fulfilled.
72
- # Called automatically at the end of a test for mock objects created by methods in Mocha::AutoVerify.
73
115
  def verify(&block)
74
116
  expectations.each { |expectation| expectation.verify(&block) }
75
117
  end
76
118
 
119
+ # :startdoc:
120
+
77
121
  end
78
122
  end
@@ -1,7 +1,7 @@
1
1
  require 'mocha/mock'
2
- require 'stubba/instance_method'
3
- require 'stubba/class_method'
4
- require 'stubba/any_instance_method'
2
+ require 'mocha/instance_method'
3
+ require 'mocha/class_method'
4
+ require 'mocha/any_instance_method'
5
5
 
6
6
  # Methods added all Objects.
7
7
  class Object
@@ -15,7 +15,7 @@ class Object
15
15
  end
16
16
 
17
17
  def stubba_method # :nodoc:
18
- Stubba::InstanceMethod
18
+ Mocha::InstanceMethod
19
19
  end
20
20
 
21
21
  def stubba_object # :nodoc:
@@ -48,9 +48,7 @@ class Object
48
48
  mocha.stubs(symbol, caller)
49
49
  end
50
50
 
51
- # Asserts that all expectations have been fulfilled.
52
- # Called automatically at the end of a test for all objects with expectations.
53
- def verify
51
+ def verify # :nodoc:
54
52
  mocha.verify
55
53
  end
56
54
 
@@ -59,7 +57,7 @@ end
59
57
  class Module # :nodoc:
60
58
 
61
59
  def stubba_method
62
- Stubba::ClassMethod
60
+ Mocha::ClassMethod
63
61
  end
64
62
 
65
63
  end
@@ -67,7 +65,7 @@ end
67
65
  class Class
68
66
 
69
67
  def stubba_method # :nodoc:
70
- Stubba::ClassMethod
68
+ Mocha::ClassMethod
71
69
  end
72
70
 
73
71
  class AnyInstance # :nodoc:
@@ -77,7 +75,7 @@ class Class
77
75
  end
78
76
 
79
77
  def stubba_method
80
- Stubba::AnyInstanceMethod
78
+ Mocha::AnyInstanceMethod
81
79
  end
82
80
 
83
81
  def stubba_object
@@ -0,0 +1,23 @@
1
+ require 'mocha/central'
2
+
3
+ module Mocha
4
+
5
+ module SetupAndTeardown
6
+
7
+ def setup_stubs
8
+ $stubba = Mocha::Central.new
9
+ end
10
+
11
+ def verify_stubs
12
+ $stubba.verify_all { yield if block_given? } if $stubba
13
+ end
14
+
15
+ def teardown_stubs
16
+ if $stubba then
17
+ $stubba.unstub_all
18
+ $stubba = nil
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'mocha/auto_verify'
2
+ require 'mocha/setup_and_teardown'
3
+
4
+ module Mocha
5
+
6
+ module Standalone
7
+
8
+ include AutoVerify
9
+ include SetupAndTeardown
10
+
11
+ def mocha_setup
12
+ setup_stubs
13
+ end
14
+
15
+ def mocha_verify(&block)
16
+ verify_mocks(&block)
17
+ verify_stubs(&block)
18
+ end
19
+
20
+ def mocha_teardown
21
+ begin
22
+ teardown_mocks
23
+ ensure
24
+ teardown_stubs
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,49 @@
1
+ require 'mocha/expectation_error'
2
+
3
+ module Mocha
4
+
5
+ module TestCaseAdapter
6
+
7
+ def self.included(base)
8
+ base.class_eval do
9
+
10
+ alias_method :run_before_mocha_test_case_adapter, :run
11
+
12
+ def run(result)
13
+ yield(Test::Unit::TestCase::STARTED, name)
14
+ @_result = result
15
+ begin
16
+ mocha_setup
17
+ begin
18
+ setup
19
+ __send__(@method_name)
20
+ mocha_verify { add_assertion }
21
+ rescue Mocha::ExpectationError => e
22
+ add_failure(e.message, e.backtrace)
23
+ rescue Test::Unit::AssertionFailedError => e
24
+ add_failure(e.message, e.backtrace)
25
+ rescue StandardError, ScriptError
26
+ add_error($!)
27
+ ensure
28
+ begin
29
+ teardown
30
+ rescue Test::Unit::AssertionFailedError => e
31
+ add_failure(e.message, e.backtrace)
32
+ rescue StandardError, ScriptError
33
+ add_error($!)
34
+ end
35
+ end
36
+ ensure
37
+ mocha_teardown
38
+ end
39
+ result.add_run
40
+ yield(Test::Unit::TestCase::FINISHED, name)
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,2 @@
1
+ require 'mocha/standalone'
2
+ require 'mocha/object'
@@ -1,8 +1,2 @@
1
- require 'stubba/object'
2
- require 'smart_test_case'
3
- require 'stubba/setup_and_teardown'
4
- require 'shared/backtracefilter'
5
-
6
- class Test::Unit::TestCase
7
- include SetupAndTeardown unless ancestors.include?(SetupAndTeardown)
8
- end
1
+ # for backwards compatibility
2
+ require 'mocha'
@@ -8,14 +8,12 @@ require 'mocha/mock_methods_test'
8
8
  require 'mocha/mock_test'
9
9
  require 'mocha/auto_verify_test'
10
10
 
11
- require 'stubba/central_test'
12
- require 'stubba/class_method_test'
13
- require 'stubba/instance_method_test'
14
- require 'stubba/any_instance_method_test'
15
- require 'stubba/setup_and_teardown_test'
16
- require 'stubba/object_test'
17
-
18
- require 'smart_test_case/multiple_setup_and_teardown_test'
11
+ require 'mocha/central_test'
12
+ require 'mocha/class_method_test'
13
+ require 'mocha/any_instance_method_test'
14
+ require 'mocha/setup_and_teardown_test'
15
+ require 'mocha/object_test'
16
+ require 'mocha/metaclass_test'
19
17
 
20
18
  class UnitTests
21
19
 
@@ -30,11 +28,10 @@ class UnitTests
30
28
  suite << AutoVerifyTest.suite
31
29
  suite << CentralTest.suite
32
30
  suite << ClassMethodTest.suite
33
- suite << InstanceMethodTest.suite
34
31
  suite << AnyInstanceMethodTest.suite
35
32
  suite << SetupAndTeardownTest.suite
36
33
  suite << ObjectTest.suite
37
- suite << MultipleSetupAndTeardownTest.suite
34
+ suite << MetaclassTest.suite
38
35
  suite
39
36
  end
40
37
 
@@ -61,6 +58,7 @@ Test::Unit::UI::Console::TestRunner.run(IntegrationTests)
61
58
 
62
59
  require 'mocha_acceptance_test'
63
60
  require 'stubba_acceptance_test'
61
+ require 'standalone_acceptance_test'
64
62
 
65
63
  class AcceptanceTests
66
64
 
@@ -68,6 +66,7 @@ class AcceptanceTests
68
66
  suite = Test::Unit::TestSuite.new('AcceptanceTests')
69
67
  suite << MochaAcceptanceTest.suite
70
68
  suite << StubbaAcceptanceTest.suite
69
+ suite << StandaloneAcceptanceTest.suite
71
70
  suite
72
71
  end
73
72
 
@@ -3,7 +3,7 @@ require 'mocha/metaclass'
3
3
  class Object
4
4
 
5
5
  def define_instance_method(method_symbol, &block)
6
- metaclass.send(:define_method, method_symbol, block)
6
+ __metaclass__.send(:define_method, method_symbol, block)
7
7
  end
8
8
 
9
9
  def replace_instance_method(method_symbol, &block)
@@ -12,7 +12,7 @@ class Object
12
12
  end
13
13
 
14
14
  def define_instance_accessor(*symbols)
15
- symbols.each { |symbol| metaclass.send(:attr_accessor, symbol) }
15
+ symbols.each { |symbol| __metaclass__.send(:attr_accessor, symbol) }
16
16
  end
17
17
 
18
18
  end
@@ -1,12 +1,10 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "test_helper")
2
2
  require 'method_definer'
3
3
  require 'mocha/mock'
4
-
5
- require 'stubba/any_instance_method'
4
+ require 'mocha/any_instance_method'
6
5
 
7
6
  class AnyInstanceMethodTest < Test::Unit::TestCase
8
7
 
9
- include Stubba
10
8
  include Mocha
11
9
 
12
10
  def test_should_hide_original_method
@@ -95,7 +95,7 @@ class AutoVerifyTest < Test::Unit::TestCase
95
95
  assert_equal expected, test_case.mocks
96
96
  end
97
97
 
98
- def test_should_verify_each_mock_on_teardown
98
+ def test_should_verify_each_mock
99
99
  mocks = Array.new(3) do
100
100
  mock = Object.new
101
101
  mock.define_instance_accessor(:verify_called)
@@ -103,20 +103,19 @@ class AutoVerifyTest < Test::Unit::TestCase
103
103
  mock
104
104
  end
105
105
  test_case.replace_instance_method(:mocks) { mocks }
106
- test_case.teardown_mocks
106
+ test_case.verify_mocks
107
107
  assert mocks.all? { |mock| mock.verify_called }
108
108
  end
109
109
 
110
- def test_should_provide_block_for_adding_assertion
110
+ def test_should_yield_to_block_for_each_assertion
111
111
  mock_class = Class.new do
112
112
  def verify(&block); yield; end
113
113
  end
114
114
  mock = mock_class.new
115
115
  test_case.replace_instance_method(:mocks) { [mock] }
116
- test_case.define_instance_accessor(:add_assertion_called)
117
- test_case.define_instance_method(:add_assertion) { self.add_assertion_called = true }
118
- test_case.teardown_mocks
119
- assert test_case.add_assertion_called
116
+ yielded = false
117
+ test_case.verify_mocks { yielded = true }
118
+ assert yielded
120
119
  end
121
120
 
122
121
  def test_should_reset_mocks_on_teardown
@@ -148,17 +147,17 @@ class AutoVerifyTest < Test::Unit::TestCase
148
147
 
149
148
  def test_should_create_mock_with_name
150
149
  mock = test_case.mock('named_mock')
151
- assert_equal 'named_mock', mock.__mock_name
150
+ assert_equal '#<Mock:named_mock>', mock.mocha_inspect
152
151
  end
153
152
 
154
153
  def test_should_create_stub_with_name
155
154
  stub = test_case.stub('named_stub')
156
- assert_equal 'named_stub', stub.__mock_name
155
+ assert_equal '#<Mock:named_stub>', stub.mocha_inspect
157
156
  end
158
157
 
159
158
  def test_should_create_greedy_stub_with_name
160
159
  greedy_stub = test_case.stub_everything('named_greedy_stub')
161
- assert_equal 'named_greedy_stub', greedy_stub.__mock_name
160
+ assert_equal '#<Mock:named_greedy_stub>', greedy_stub.mocha_inspect
162
161
  end
163
162
 
164
163
  end