mocha 0.5.6 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. data/README +4 -4
  2. data/RELEASE +45 -0
  3. data/Rakefile +55 -33
  4. data/lib/mocha.rb +1 -0
  5. data/lib/mocha/any_instance_method.rb +24 -4
  6. data/lib/mocha/backtrace_filter.rb +17 -0
  7. data/lib/mocha/cardinality.rb +92 -0
  8. data/lib/mocha/central.rb +1 -9
  9. data/lib/mocha/change_state_side_effect.rb +19 -0
  10. data/lib/mocha/class_method.rb +25 -5
  11. data/lib/mocha/configuration.rb +60 -0
  12. data/lib/mocha/exception_raiser.rb +1 -1
  13. data/lib/mocha/expectation.rb +109 -48
  14. data/lib/mocha/expectation_error.rb +6 -6
  15. data/lib/mocha/expectation_list.rb +10 -14
  16. data/lib/mocha/in_state_ordering_constraint.rb +19 -0
  17. data/lib/mocha/instance_method.rb +9 -0
  18. data/lib/mocha/logger.rb +15 -0
  19. data/lib/mocha/mock.rb +19 -14
  20. data/lib/mocha/mockery.rb +166 -0
  21. data/lib/mocha/module_method.rb +17 -0
  22. data/lib/mocha/names.rb +53 -0
  23. data/lib/mocha/object.rb +26 -9
  24. data/lib/mocha/parameter_matchers.rb +2 -1
  25. data/lib/mocha/parameter_matchers/all_of.rb +3 -3
  26. data/lib/mocha/parameter_matchers/any_of.rb +3 -3
  27. data/lib/mocha/parameter_matchers/anything.rb +1 -1
  28. data/lib/mocha/parameter_matchers/has_entries.rb +4 -1
  29. data/lib/mocha/parameter_matchers/has_entry.rb +3 -2
  30. data/lib/mocha/parameter_matchers/has_key.rb +1 -1
  31. data/lib/mocha/parameter_matchers/has_value.rb +1 -1
  32. data/lib/mocha/parameter_matchers/not.rb +2 -2
  33. data/lib/mocha/parameter_matchers/object.rb +1 -1
  34. data/lib/mocha/parameter_matchers/optionally.rb +22 -0
  35. data/lib/mocha/parameter_matchers/regexp_matches.rb +2 -2
  36. data/lib/mocha/parameter_matchers/responds_with.rb +43 -0
  37. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +43 -0
  38. data/lib/mocha/single_return_value.rb +2 -9
  39. data/lib/mocha/standalone.rb +151 -17
  40. data/lib/mocha/state_machine.rb +91 -0
  41. data/lib/mocha/stubbing_error.rb +16 -0
  42. data/lib/mocha/test_case_adapter.rb +76 -22
  43. data/lib/stubba.rb +2 -1
  44. data/test/acceptance/acceptance_test_helper.rb +38 -0
  45. data/test/acceptance/bug_18914_test.rb +43 -0
  46. data/test/acceptance/{expected_invocation_count_acceptance_test.rb → expected_invocation_count_test.rb} +29 -20
  47. data/test/acceptance/failure_messages_test.rb +64 -0
  48. data/test/acceptance/{mocha_acceptance_test.rb → mocha_example_test.rb} +5 -5
  49. data/test/{integration/mocha_test_result_integration_test.rb → acceptance/mocha_test_result_test.rb} +19 -40
  50. data/test/acceptance/mock_test.rb +100 -0
  51. data/test/acceptance/{mock_with_initializer_block_acceptance_test.rb → mock_with_initializer_block_test.rb} +12 -5
  52. data/test/acceptance/{mocked_methods_dispatch_acceptance_test.rb → mocked_methods_dispatch_test.rb} +12 -5
  53. data/test/acceptance/{optional_parameters_acceptance_test.rb → optional_parameters_test.rb} +11 -4
  54. data/test/acceptance/{parameter_matcher_acceptance_test.rb → parameter_matcher_test.rb} +67 -5
  55. data/test/acceptance/{partial_mocks_acceptance_test.rb → partial_mocks_test.rb} +12 -5
  56. data/test/acceptance/return_value_test.rb +52 -0
  57. data/test/acceptance/{sequence_acceptance_test.rb → sequence_test.rb} +13 -6
  58. data/test/acceptance/{standalone_acceptance_test.rb → standalone_test.rb} +19 -11
  59. data/test/acceptance/states_test.rb +70 -0
  60. data/test/acceptance/stub_any_instance_method_test.rb +195 -0
  61. data/test/acceptance/stub_class_method_test.rb +203 -0
  62. data/test/acceptance/stub_everything_test.rb +56 -0
  63. data/test/acceptance/stub_instance_method_test.rb +165 -0
  64. data/test/acceptance/stub_module_method_test.rb +163 -0
  65. data/test/acceptance/stub_test.rb +52 -0
  66. data/test/acceptance/{stubba_acceptance_test.rb → stubba_example_test.rb} +1 -1
  67. data/test/{integration/stubba_test_result_integration_test.rb → acceptance/stubba_test_result_test.rb} +17 -36
  68. data/test/acceptance/stubbing_error_backtrace_test.rb +64 -0
  69. data/test/acceptance/stubbing_method_unnecessarily_test.rb +65 -0
  70. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +130 -0
  71. data/test/acceptance/stubbing_non_existent_class_method_test.rb +155 -0
  72. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +145 -0
  73. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +130 -0
  74. data/test/acceptance/stubbing_non_public_class_method_test.rb +161 -0
  75. data/test/acceptance/stubbing_non_public_instance_method_test.rb +141 -0
  76. data/test/acceptance/stubbing_on_non_mock_object_test.rb +64 -0
  77. data/test/execution_point.rb +3 -1
  78. data/test/simple_counter.rb +13 -0
  79. data/test/test_helper.rb +0 -1
  80. data/test/test_runner.rb +6 -4
  81. data/test/unit/any_instance_method_test.rb +1 -1
  82. data/test/unit/array_inspect_test.rb +1 -1
  83. data/test/unit/backtrace_filter_test.rb +19 -0
  84. data/test/unit/cardinality_test.rb +56 -0
  85. data/test/unit/central_test.rb +4 -63
  86. data/test/unit/change_state_side_effect_test.rb +41 -0
  87. data/test/unit/class_method_test.rb +38 -1
  88. data/test/unit/date_time_inspect_test.rb +1 -1
  89. data/test/unit/{expectation_raiser_test.rb → exception_raiser_test.rb} +14 -0
  90. data/test/unit/expectation_list_test.rb +4 -22
  91. data/test/unit/expectation_test.rb +70 -94
  92. data/test/unit/in_state_ordering_constraint_test.rb +43 -0
  93. data/test/unit/mock_test.rb +16 -37
  94. data/test/unit/mockery_test.rb +149 -0
  95. data/test/unit/{no_yield_test.rb → no_yields_test.rb} +0 -0
  96. data/test/unit/object_test.rb +6 -89
  97. data/test/unit/parameter_matchers/equals_test.rb +25 -0
  98. data/test/unit/parameter_matchers/has_entries_test.rb +22 -1
  99. data/test/unit/parameter_matchers/has_entry_test.rb +24 -2
  100. data/test/unit/parameter_matchers/has_key_test.rb +11 -0
  101. data/test/unit/parameter_matchers/has_value_test.rb +12 -0
  102. data/test/unit/parameter_matchers/regexp_matches_test.rb +1 -1
  103. data/test/unit/parameter_matchers/responds_with_test.rb +25 -0
  104. data/test/unit/parameter_matchers/stub_matcher.rb +4 -0
  105. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +25 -0
  106. data/test/unit/single_return_value_test.rb +0 -19
  107. data/test/unit/state_machine_test.rb +98 -0
  108. metadata +108 -69
  109. data/lib/mocha/auto_verify.rb +0 -118
  110. data/lib/mocha/infinite_range.rb +0 -25
  111. data/lib/mocha/missing_expectation.rb +0 -17
  112. data/lib/mocha/setup_and_teardown.rb +0 -23
  113. data/lib/mocha/stub.rb +0 -18
  114. data/test/integration/stubba_integration_test.rb +0 -89
  115. data/test/unit/auto_verify_test.rb +0 -129
  116. data/test/unit/expectation_error_test.rb +0 -24
  117. data/test/unit/infinite_range_test.rb +0 -53
  118. data/test/unit/missing_expectation_test.rb +0 -42
  119. data/test/unit/setup_and_teardown_test.rb +0 -76
  120. data/test/unit/stub_test.rb +0 -24
@@ -1,18 +0,0 @@
1
- require 'mocha/expectation'
2
-
3
- module Mocha # :nodoc:
4
-
5
- class Stub < Expectation # :nodoc:
6
-
7
- def initialize(mock, method_name, backtrace = nil)
8
- super
9
- @expected_count = Range.at_least(0)
10
- end
11
-
12
- def verify
13
- true
14
- end
15
-
16
- end
17
-
18
- end
@@ -1,89 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
-
3
- require 'mocha/object'
4
- require 'mocha/test_case_adapter'
5
- require 'mocha/standalone'
6
-
7
- class StubbaIntegrationTest < Test::Unit::TestCase
8
-
9
- class DontMessWithMe
10
- def self.my_class_method
11
- :original_return_value
12
- end
13
- def my_instance_method
14
- :original_return_value
15
- end
16
- end
17
-
18
- def test_should_stub_class_method_within_test
19
- test = build_test do
20
- DontMessWithMe.expects(:my_class_method).returns(:new_return_value)
21
- assert_equal :new_return_value, DontMessWithMe.my_class_method
22
- end
23
-
24
- test_result = Test::Unit::TestResult.new
25
- test.run(test_result) {}
26
- assert test_result.passed?
27
- end
28
-
29
- def test_should_leave_stubbed_class_method_unchanged_after_test
30
- test = build_test do
31
- DontMessWithMe.expects(:my_class_method).returns(:new_return_value)
32
- end
33
-
34
- test.run(Test::Unit::TestResult.new) {}
35
- assert_equal :original_return_value, DontMessWithMe.my_class_method
36
- end
37
-
38
- def test_should_reset_class_expectations_after_test
39
- test = build_test do
40
- DontMessWithMe.expects(:my_class_method)
41
- end
42
-
43
- test.run(Test::Unit::TestResult.new) {}
44
- assert_equal 0, DontMessWithMe.mocha.expectations.length
45
- end
46
-
47
- def test_should_stub_instance_method_within_test
48
- instance = DontMessWithMe.new
49
- test = build_test do
50
- instance.expects(:my_instance_method).returns(:new_return_value)
51
- assert_equal :new_return_value, instance.my_instance_method
52
- end
53
- test_result = Test::Unit::TestResult.new
54
- test.run(test_result) {}
55
- assert test_result.passed?
56
- end
57
-
58
- def test_should_leave_stubbed_instance_method_unchanged_after_test
59
- instance = DontMessWithMe.new
60
- test = build_test do
61
- instance.expects(:my_instance_method).returns(:new_return_value)
62
- end
63
-
64
- test.run(Test::Unit::TestResult.new) {}
65
- assert_equal :original_return_value, instance.my_instance_method
66
- end
67
-
68
- def test_should_reset_instance_expectations_after_test
69
- instance = DontMessWithMe.new
70
- test = build_test do
71
- instance.expects(:my_instance_method).returns(:new_return_value)
72
- end
73
-
74
- test.run(Test::Unit::TestResult.new) {}
75
- assert_equal 0, instance.mocha.expectations.length
76
- end
77
-
78
- private
79
-
80
- def build_test(&block)
81
- test_class = Class.new(Test::Unit::TestCase) do
82
- include Mocha::Standalone
83
- include Mocha::TestCaseAdapter
84
- define_method(:test_me, &block)
85
- end
86
- test_class.new(:test_me)
87
- end
88
-
89
- end
@@ -1,129 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/auto_verify'
3
- require 'method_definer'
4
-
5
- class AutoVerifyTest < Test::Unit::TestCase
6
-
7
- attr_reader :test_case
8
-
9
- def setup
10
- @test_case = Object.new
11
- class << test_case
12
- include Mocha::AutoVerify
13
- end
14
- end
15
-
16
- def test_should_build_mock
17
- mock = test_case.mock
18
- assert mock.is_a?(Mocha::Mock)
19
- end
20
-
21
- def test_should_add_expectations_to_mock
22
- mock = test_case.mock(:method_1 => 'result_1', :method_2 => 'result_2')
23
- assert_equal 'result_1', mock.method_1
24
- assert_equal 'result_2', mock.method_2
25
- end
26
-
27
- def test_should_build_stub
28
- stub = test_case.stub
29
- assert stub.is_a?(Mocha::Mock)
30
- end
31
-
32
- def test_should_add_expectation_to_stub
33
- stub = test_case.stub(:method_1 => 'result_1', :method_2 => 'result_2')
34
- assert_equal 'result_1', stub.method_1
35
- assert_equal 'result_2', stub.method_2
36
- end
37
-
38
- def test_should_build_stub_that_stubs_all_methods
39
- stub = test_case.stub_everything
40
- assert stub.everything_stubbed
41
- end
42
-
43
- def test_should_add_expectations_to_stub_that_stubs_all_methods
44
- stub = test_case.stub_everything(:method_1 => 'result_1', :method_2 => 'result_2')
45
- assert_equal 'result_1', stub.method_1
46
- assert_equal 'result_2', stub.method_2
47
- end
48
-
49
- def test_should_always_new_mock
50
- assert_not_equal test_case.mock, test_case.mock
51
- end
52
-
53
- def test_should_always_build_new_stub
54
- assert_not_equal test_case.stub, test_case.stub
55
- end
56
-
57
- def test_should_always_build_new_stub_that_stubs_all_methods
58
- assert_not_equal test_case.stub, test_case.stub
59
- end
60
-
61
- def test_should_store_each_new_mock
62
- expected = Array.new(3) { test_case.mock }
63
- assert_equal expected, test_case.mocks
64
- end
65
-
66
- def test_should_store_each_new_stub
67
- expected = Array.new(3) { test_case.stub }
68
- assert_equal expected, test_case.mocks
69
- end
70
-
71
- def test_should_store_each_new_stub_that_stubs_all_methods
72
- expected = Array.new(3) { test_case.stub_everything }
73
- assert_equal expected, test_case.mocks
74
- end
75
-
76
- def test_should_verify_each_mock
77
- mocks = Array.new(3) do
78
- mock = Object.new
79
- mock.define_instance_accessor(:verify_called)
80
- class << mock
81
- def verify(&block)
82
- self.verify_called = true
83
- end
84
- end
85
- mock
86
- end
87
- test_case.replace_instance_method(:mocks) { mocks }
88
- test_case.verify_mocks
89
- assert mocks.all? { |mock| mock.verify_called }
90
- end
91
-
92
- def test_should_yield_to_block_for_each_assertion
93
- mock_class = Class.new do
94
- def verify(&block); yield; end
95
- end
96
- mock = mock_class.new
97
- test_case.replace_instance_method(:mocks) { [mock] }
98
- yielded = false
99
- test_case.verify_mocks { yielded = true }
100
- assert yielded
101
- end
102
-
103
- def test_should_reset_mocks_on_teardown
104
- mock = Class.new { define_method(:verify) {} }.new
105
- test_case.mocks << mock
106
- test_case.teardown_mocks
107
- assert test_case.mocks.empty?
108
- end
109
-
110
- def test_should_create_named_mock
111
- mock = test_case.mock('named_mock')
112
- assert_equal '#<Mock:named_mock>', mock.mocha_inspect
113
- end
114
-
115
- def test_should_create_named_stub
116
- stub = test_case.stub('named_stub')
117
- assert_equal '#<Mock:named_stub>', stub.mocha_inspect
118
- end
119
-
120
- def test_should_create_named_stub_that_stubs_all_methods
121
- stub = test_case.stub_everything('named_stub')
122
- assert_equal '#<Mock:named_stub>', stub.mocha_inspect
123
- end
124
-
125
- def test_should_build_sequence
126
- assert_not_nil test_case.sequence('name')
127
- end
128
-
129
- end
@@ -1,24 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/expectation_error'
3
-
4
- class ExpectationErrorTest < Test::Unit::TestCase
5
-
6
- include Mocha
7
-
8
- def test_should_exclude_mocha_locations_from_backtrace
9
- mocha_lib = "/username/workspace/mocha_wibble/lib/"
10
- backtrace = [ mocha_lib + 'exclude/me/1', mocha_lib + 'exclude/me/2', '/keep/me', mocha_lib + 'exclude/me/3']
11
- expectation_error = ExpectationError.new(nil, backtrace, mocha_lib)
12
- assert_equal ['/keep/me'], expectation_error.backtrace
13
- end
14
-
15
- def test_should_determine_path_for_mocha_lib_directory
16
- assert_match Regexp.new("/lib/$"), ExpectationError::LIB_DIRECTORY
17
- end
18
-
19
- def test_should_set_error_message
20
- expectation_error = ExpectationError.new('message')
21
- assert_equal 'message', expectation_error.message
22
- end
23
-
24
- end
@@ -1,53 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/infinite_range'
3
- require 'date'
4
-
5
- class InfiniteRangeTest < Test::Unit::TestCase
6
-
7
- def test_should_include_values_at_or_above_minimum
8
- range = Range.at_least(10)
9
- assert(range === 10)
10
- assert(range === 11)
11
- assert(range === 1000000)
12
- end
13
-
14
- def test_should_not_include_values_below_minimum
15
- range = Range.at_least(10)
16
- assert_false(range === 0)
17
- assert_false(range === 9)
18
- assert_false(range === -11)
19
- end
20
-
21
- def test_should_be_human_readable_description_for_at_least
22
- assert_equal "at least 10", Range.at_least(10).mocha_inspect
23
- end
24
-
25
- def test_should_include_values_at_or_below_maximum
26
- range = Range.at_most(10)
27
- assert(range === 10)
28
- assert(range === 0)
29
- assert(range === -1000000)
30
- end
31
-
32
- def test_should_not_include_values_above_maximum
33
- range = Range.at_most(10)
34
- assert_false(range === 11)
35
- assert_false(range === 1000000)
36
- end
37
-
38
- def test_should_be_human_readable_description_for_at_most
39
- assert_equal "at most 10", Range.at_most(10).mocha_inspect
40
- end
41
-
42
- def test_should_be_same_as_standard_to_string
43
- assert_equal((1..10).to_s, (1..10).mocha_inspect)
44
- assert_equal((1...10).to_s, (1...10).mocha_inspect)
45
- date_range = Range.new(Date.parse('2006-01-01'), Date.parse('2007-01-01'))
46
- assert_equal date_range.to_s, date_range.mocha_inspect
47
- end
48
-
49
- def assert_false(condition)
50
- assert(!condition)
51
- end
52
-
53
- end
@@ -1,42 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
-
3
- require 'mocha/missing_expectation'
4
- require 'mocha/mock'
5
-
6
- class MissingExpectationTest < Test::Unit::TestCase
7
-
8
- include Mocha
9
-
10
- def test_should_report_similar_expectations
11
- mock = Mock.new
12
- expectation_1 = mock.expects(:method_one).with(1)
13
- expectation_2 = mock.expects(:method_one).with(1, 1)
14
- expectation_3 = mock.expects(:method_two).with(2)
15
-
16
- missing_expectation = MissingExpectation.new(mock, :method_one)
17
- exception = assert_raise(ExpectationError) { missing_expectation.verify }
18
-
19
- expected_message = [
20
- "#{missing_expectation.error_message(0, 1)}",
21
- "Similar expectations:",
22
- "#{expectation_1.method_signature}",
23
- "#{expectation_2.method_signature}"
24
- ].join("\n")
25
-
26
- assert_equal expected_message, exception.message
27
- end
28
-
29
- def test_should_not_report_similar_expectations_if_there_are_none
30
- mock = Mock.new
31
- mock.expects(:method_two).with(2)
32
- mock.expects(:method_two).with(2, 2)
33
-
34
- missing_expectation = MissingExpectation.new(mock, :method_one)
35
- exception = assert_raise(ExpectationError) { missing_expectation.verify }
36
-
37
- expected_message = "#{missing_expectation.error_message(0, 1)}"
38
-
39
- assert_equal expected_message, exception.message
40
- end
41
-
42
- end
@@ -1,76 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/mock'
3
-
4
- require 'mocha/setup_and_teardown'
5
-
6
- class SetupAndTeardownTest < Test::Unit::TestCase
7
-
8
- include Mocha
9
-
10
- def test_should_instantiate_new_stubba
11
- test_case = stubbed_test_case_class.new
12
- test_case.setup_stubs
13
-
14
- assert $stubba
15
- assert $stubba.is_a?(Mocha::Central)
16
- end
17
-
18
- def test_should_verify_all_expectations
19
- test_case = stubbed_test_case_class.new
20
- stubba = Mock.new
21
- stubba.expects(:verify_all)
22
- $stubba = stubba
23
-
24
- test_case.verify_stubs
25
-
26
- stubba.verify
27
- end
28
-
29
- def test_should_yield_to_block_for_each_assertion
30
- test_case = stubbed_test_case_class.new
31
- $stubba = Mock.new
32
- $stubba.stubs(:verify_all).yields
33
- yielded = false
34
-
35
- test_case.verify_stubs { yielded = true }
36
-
37
- assert_equal true, yielded
38
- end
39
-
40
- def test_should_unstub_all_stubbed_methods
41
- test_case = stubbed_test_case_class.new
42
- stubba = Mock.new
43
- stubba.expects(:unstub_all)
44
- $stubba = stubba
45
-
46
- test_case.teardown_stubs
47
-
48
- stubba.verify
49
- end
50
-
51
- def test_should_set_stubba_to_nil
52
- test_case = stubbed_test_case_class.new
53
- $stubba = Mock.new
54
- $stubba.stubs(:unstub_all)
55
-
56
- test_case.teardown_stubs
57
-
58
- assert_nil $stubba
59
- end
60
-
61
- def test_should_not_raise_exception_if_no_stubba_central_available
62
- test_case = stubbed_test_case_class.new
63
- $stubba = nil
64
- assert_nothing_raised { test_case.teardown_stubs }
65
- end
66
-
67
- private
68
-
69
- def stubbed_test_case_class
70
- Class.new do
71
- include Mocha::SetupAndTeardown
72
- end
73
- end
74
-
75
- end
76
-
@@ -1,24 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/stub'
3
-
4
- class StubTest < Test::Unit::TestCase
5
-
6
- include Mocha
7
-
8
- def test_should_always_verify_successfully
9
- stub = Stub.new(nil, :expected_method)
10
- assert stub.verify
11
- stub.invoke
12
- assert stub.verify
13
- end
14
-
15
- def test_should_match_successfully_for_any_number_of_invocations
16
- stub = Stub.new(nil, :expected_method)
17
- assert stub.match?(:expected_method)
18
- stub.invoke
19
- assert stub.match?(:expected_method)
20
- stub.invoke
21
- assert stub.match?(:expected_method)
22
- end
23
-
24
- end