mocha 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/Rakefile +3 -1
  2. data/examples/misc.rb +44 -36
  3. data/examples/stubba.rb +1 -1
  4. data/lib/mocha/auto_verify.rb +38 -31
  5. data/lib/mocha/central.rb +1 -1
  6. data/lib/mocha/class_method.rb +5 -1
  7. data/lib/mocha/expectation.rb +63 -61
  8. data/lib/mocha/expectation_error.rb +9 -0
  9. data/lib/mocha/expectation_list.rb +11 -10
  10. data/lib/mocha/method_matcher.rb +21 -0
  11. data/lib/mocha/missing_expectation.rb +5 -15
  12. data/lib/mocha/mock.rb +28 -26
  13. data/lib/mocha/parameter_matchers.rb +17 -1
  14. data/lib/mocha/parameter_matchers/all_of.rb +6 -3
  15. data/lib/mocha/parameter_matchers/any_of.rb +6 -3
  16. data/lib/mocha/parameter_matchers/any_parameters.rb +40 -0
  17. data/lib/mocha/parameter_matchers/anything.rb +5 -2
  18. data/lib/mocha/parameter_matchers/base.rb +15 -0
  19. data/lib/mocha/parameter_matchers/equals.rb +42 -0
  20. data/lib/mocha/parameter_matchers/has_entries.rb +42 -0
  21. data/lib/mocha/parameter_matchers/has_entry.rb +20 -4
  22. data/lib/mocha/parameter_matchers/has_key.rb +5 -2
  23. data/lib/mocha/parameter_matchers/has_value.rb +5 -2
  24. data/lib/mocha/parameter_matchers/includes.rb +5 -2
  25. data/lib/mocha/parameter_matchers/instance_of.rb +5 -2
  26. data/lib/mocha/parameter_matchers/is_a.rb +42 -0
  27. data/lib/mocha/parameter_matchers/kind_of.rb +5 -2
  28. data/lib/mocha/parameter_matchers/not.rb +42 -0
  29. data/lib/mocha/parameter_matchers/object.rb +9 -0
  30. data/lib/mocha/parameter_matchers/optionally.rb +33 -0
  31. data/lib/mocha/parameter_matchers/regexp_matches.rb +5 -2
  32. data/lib/mocha/parameters_matcher.rb +37 -0
  33. data/lib/mocha/pretty_parameters.rb +1 -1
  34. data/lib/mocha/return_values.rb +7 -4
  35. data/lib/mocha/sequence.rb +42 -0
  36. data/lib/mocha/yield_parameters.rb +3 -3
  37. data/test/acceptance/expected_invocation_count_acceptance_test.rb +8 -8
  38. data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +44 -0
  39. data/test/acceptance/optional_parameters_acceptance_test.rb +63 -0
  40. data/test/acceptance/parameter_matcher_acceptance_test.rb +38 -2
  41. data/test/acceptance/partial_mocks_acceptance_test.rb +40 -0
  42. data/test/acceptance/sequence_acceptance_test.rb +179 -0
  43. data/test/integration/mocha_test_result_integration_test.rb +3 -3
  44. data/test/integration/stubba_integration_test.rb +2 -2
  45. data/test/integration/stubba_test_result_integration_test.rb +2 -2
  46. data/test/test_runner.rb +2 -2
  47. data/test/unit/any_instance_method_test.rb +2 -0
  48. data/test/unit/auto_verify_test.rb +10 -3
  49. data/test/unit/central_test.rb +1 -1
  50. data/test/unit/class_method_test.rb +4 -0
  51. data/test/unit/expectation_error_test.rb +24 -0
  52. data/test/unit/expectation_list_test.rb +6 -0
  53. data/test/unit/expectation_test.rb +111 -27
  54. data/test/unit/method_matcher_test.rb +23 -0
  55. data/test/unit/missing_expectation_test.rb +24 -27
  56. data/test/unit/mock_test.rb +29 -22
  57. data/test/unit/object_inspect_test.rb +4 -2
  58. data/test/unit/parameter_matchers/all_of_test.rb +2 -2
  59. data/test/unit/parameter_matchers/any_of_test.rb +2 -2
  60. data/test/unit/parameter_matchers/anything_test.rb +2 -2
  61. data/test/unit/parameter_matchers/has_entries_test.rb +30 -0
  62. data/test/unit/parameter_matchers/has_entry_test.rb +20 -5
  63. data/test/unit/parameter_matchers/has_key_test.rb +2 -2
  64. data/test/unit/parameter_matchers/has_value_test.rb +2 -2
  65. data/test/unit/parameter_matchers/includes_test.rb +2 -2
  66. data/test/unit/parameter_matchers/instance_of_test.rb +2 -2
  67. data/test/unit/parameter_matchers/is_a_test.rb +25 -0
  68. data/test/unit/parameter_matchers/kind_of_test.rb +3 -3
  69. data/test/unit/parameter_matchers/not_test.rb +26 -0
  70. data/test/unit/parameter_matchers/regexp_matches_test.rb +2 -2
  71. data/test/unit/parameter_matchers/stub_matcher.rb +2 -1
  72. data/test/unit/parameters_matcher_test.rb +121 -0
  73. data/test/unit/sequence_test.rb +104 -0
  74. metadata +35 -6
  75. data/test/unit/pretty_parameters_test.rb +0 -32
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha'
3
+ require 'test_runner'
4
+
5
+ class MockWithInitializerBlockAcceptanceTest < Test::Unit::TestCase
6
+
7
+ include TestRunner
8
+
9
+ def test_should_expect_two_method_invocations_and_receive_both_of_them
10
+ test_result = run_test do
11
+ mock = mock() do
12
+ expects(:method_1)
13
+ expects(:method_2)
14
+ end
15
+ mock.method_1
16
+ mock.method_2
17
+ end
18
+ assert_passed(test_result)
19
+ end
20
+
21
+ def test_should_expect_two_method_invocations_but_receive_only_one_of_them
22
+ test_result = run_test do
23
+ mock = mock() do
24
+ expects(:method_1)
25
+ expects(:method_2)
26
+ end
27
+ mock.method_1
28
+ end
29
+ assert_failed(test_result)
30
+ end
31
+
32
+ def test_should_stub_methods
33
+ test_result = run_test do
34
+ mock = mock() do
35
+ stubs(:method_1).returns(1)
36
+ stubs(:method_2).returns(2)
37
+ end
38
+ assert_equal 1, mock.method_1
39
+ assert_equal 2, mock.method_2
40
+ end
41
+ assert_passed(test_result)
42
+ end
43
+
44
+ end
@@ -0,0 +1,63 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha'
3
+ require 'test_runner'
4
+
5
+ class OptionalParameterMatcherAcceptanceTest < Test::Unit::TestCase
6
+
7
+ include TestRunner
8
+
9
+ def test_should_pass_if_all_required_parameters_match_and_no_optional_parameters_are_supplied
10
+ test_result = run_test do
11
+ mock = mock()
12
+ mock.expects(:method).with(1, 2, optionally(3, 4))
13
+ mock.method(1, 2)
14
+ end
15
+ assert_passed(test_result)
16
+ end
17
+
18
+ def test_should_pass_if_all_required_and_optional_parameters_match_and_some_optional_parameters_are_supplied
19
+ test_result = run_test do
20
+ mock = mock()
21
+ mock.expects(:method).with(1, 2, optionally(3, 4))
22
+ mock.method(1, 2, 3)
23
+ end
24
+ assert_passed(test_result)
25
+ end
26
+
27
+ def test_should_pass_if_all_required_and_optional_parameters_match_and_all_optional_parameters_are_supplied
28
+ test_result = run_test do
29
+ mock = mock()
30
+ mock.expects(:method).with(1, 2, optionally(3, 4))
31
+ mock.method(1, 2, 3, 4)
32
+ end
33
+ assert_passed(test_result)
34
+ end
35
+
36
+ def test_should_fail_if_all_required_and_optional_parameters_match_but_too_many_optional_parameters_are_supplied
37
+ test_result = run_test do
38
+ mock = mock()
39
+ mock.expects(:method).with(1, 2, optionally(3, 4))
40
+ mock.method(1, 2, 3, 4, 5)
41
+ end
42
+ assert_failed(test_result)
43
+ end
44
+
45
+ def test_should_fail_if_all_required_parameters_match_but_some_optional_parameters_do_not_match
46
+ test_result = run_test do
47
+ mock = mock()
48
+ mock.expects(:method).with(1, 2, optionally(3, 4))
49
+ mock.method(1, 2, 4)
50
+ end
51
+ assert_failed(test_result)
52
+ end
53
+
54
+ def test_should_fail_if_all_required_parameters_match_but_no_optional_parameters_match
55
+ test_result = run_test do
56
+ mock = mock()
57
+ mock.expects(:method).with(1, 2, optionally(3, 4))
58
+ mock.method(1, 2, 4, 5)
59
+ end
60
+ assert_failed(test_result)
61
+ end
62
+
63
+ end
@@ -42,7 +42,7 @@ class ParameterMatcherAcceptanceTest < Test::Unit::TestCase
42
42
  assert_failed(test_result)
43
43
  end
44
44
 
45
- def test_should_match_hash_parameter_with_specified_entry
45
+ def test_should_match_hash_parameter_with_specified_key_value_pair
46
46
  test_result = run_test do
47
47
  mock = mock()
48
48
  mock.expects(:method).with(has_entry(:key_1, 'value_1'))
@@ -51,7 +51,7 @@ class ParameterMatcherAcceptanceTest < Test::Unit::TestCase
51
51
  assert_passed(test_result)
52
52
  end
53
53
 
54
- def test_should_not_match_hash_parameter_with_specified_entry
54
+ def test_should_not_match_hash_parameter_with_specified_key_value_pair
55
55
  test_result = run_test do
56
56
  mock = mock()
57
57
  mock.expects(:method).with(has_entry(:key_1, 'value_2'))
@@ -60,6 +60,42 @@ class ParameterMatcherAcceptanceTest < Test::Unit::TestCase
60
60
  assert_failed(test_result)
61
61
  end
62
62
 
63
+ def test_should_match_hash_parameter_with_specified_hash_entry
64
+ test_result = run_test do
65
+ mock = mock()
66
+ mock.expects(:method).with(has_entry(:key_1 => 'value_1'))
67
+ mock.method(:key_1 => 'value_1', :key_2 => 'value_2')
68
+ end
69
+ assert_passed(test_result)
70
+ end
71
+
72
+ def test_should_not_match_hash_parameter_with_specified_hash_entry
73
+ test_result = run_test do
74
+ mock = mock()
75
+ mock.expects(:method).with(has_entry(:key_1 => 'value_2'))
76
+ mock.method(:key_1 => 'value_1', :key_2 => 'value_2')
77
+ end
78
+ assert_failed(test_result)
79
+ end
80
+
81
+ def test_should_match_hash_parameter_with_specified_entries
82
+ test_result = run_test do
83
+ mock = mock()
84
+ mock.expects(:method).with(has_entries(:key_1 => 'value_1', :key_2 => 'value_2'))
85
+ mock.method(:key_1 => 'value_1', :key_2 => 'value_2', :key_3 => 'value_3')
86
+ end
87
+ assert_passed(test_result)
88
+ end
89
+
90
+ def test_should_not_match_hash_parameter_with_specified_entries
91
+ test_result = run_test do
92
+ mock = mock()
93
+ mock.expects(:method).with(has_entries(:key_1 => 'value_1', :key_2 => 'value_2'))
94
+ mock.method(:key_1 => 'value_1', :key_2 => 'value_3')
95
+ end
96
+ assert_failed(test_result)
97
+ end
98
+
63
99
  def test_should_match_parameter_that_matches_regular_expression
64
100
  test_result = run_test do
65
101
  mock = mock()
@@ -0,0 +1,40 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha'
3
+ require 'test_runner'
4
+
5
+ class PartialMockAcceptanceTest < Test::Unit::TestCase
6
+
7
+ include TestRunner
8
+
9
+ def test_should_pass_if_all_expectations_are_satisfied
10
+ test_result = run_test do
11
+ partial_mock_one = "partial_mock_one"
12
+ partial_mock_two = "partial_mock_two"
13
+
14
+ partial_mock_one.expects(:first)
15
+ partial_mock_one.expects(:second)
16
+ partial_mock_two.expects(:third)
17
+
18
+ partial_mock_one.first
19
+ partial_mock_one.second
20
+ partial_mock_two.third
21
+ end
22
+ assert_passed(test_result)
23
+ end
24
+
25
+ def test_should_fail_if_all_expectations_are_not_satisfied
26
+ test_result = run_test do
27
+ partial_mock_one = "partial_mock_one"
28
+ partial_mock_two = "partial_mock_two"
29
+
30
+ partial_mock_one.expects(:first)
31
+ partial_mock_one.expects(:second)
32
+ partial_mock_two.expects(:third)
33
+
34
+ partial_mock_one.first
35
+ partial_mock_two.third
36
+ end
37
+ assert_failed(test_result)
38
+ end
39
+
40
+ end
@@ -0,0 +1,179 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha'
3
+ require 'test_runner'
4
+
5
+ class SequenceAcceptanceTest < Test::Unit::TestCase
6
+
7
+ include TestRunner
8
+
9
+ def test_should_constrain_invocations_to_occur_in_expected_order
10
+ test_result = run_test do
11
+ mock = mock()
12
+ sequence = sequence('one')
13
+
14
+ mock.expects(:first).in_sequence(sequence)
15
+ mock.expects(:second).in_sequence(sequence)
16
+
17
+ mock.second
18
+ end
19
+ assert_failed(test_result)
20
+ end
21
+
22
+ def test_should_allow_invocations_in_sequence
23
+ test_result = run_test do
24
+ mock = mock()
25
+ sequence = sequence('one')
26
+
27
+ mock.expects(:first).in_sequence(sequence)
28
+ mock.expects(:second).in_sequence(sequence)
29
+
30
+ mock.first
31
+ mock.second
32
+ end
33
+ assert_passed(test_result)
34
+ end
35
+
36
+ def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expected_on_different_mocks
37
+ test_result = run_test do
38
+ mock_one = mock('1')
39
+ mock_two = mock('2')
40
+ sequence = sequence('one')
41
+
42
+ mock_one.expects(:first).in_sequence(sequence)
43
+ mock_two.expects(:second).in_sequence(sequence)
44
+
45
+ mock_two.second
46
+ end
47
+ assert_failed(test_result)
48
+ end
49
+
50
+ def test_should_allow_invocations_in_sequence_even_if_expected_on_different_mocks
51
+ test_result = run_test do
52
+ mock_one = mock('1')
53
+ mock_two = mock('2')
54
+ sequence = sequence('one')
55
+
56
+ mock_one.expects(:first).in_sequence(sequence)
57
+ mock_two.expects(:second).in_sequence(sequence)
58
+
59
+ mock_one.first
60
+ mock_two.second
61
+ end
62
+ assert_passed(test_result)
63
+ end
64
+
65
+ def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expected_on_partial_mocks
66
+ test_result = run_test do
67
+ partial_mock_one = "1"
68
+ partial_mock_two = "2"
69
+ sequence = sequence('one')
70
+
71
+ partial_mock_one.expects(:first).in_sequence(sequence)
72
+ partial_mock_two.expects(:second).in_sequence(sequence)
73
+
74
+ partial_mock_two.second
75
+ end
76
+ assert_failed(test_result)
77
+ end
78
+
79
+ def test_should_allow_invocations_in_sequence_even_if_expected_on_partial_mocks
80
+ test_result = run_test do
81
+ partial_mock_one = "1"
82
+ partial_mock_two = "2"
83
+ sequence = sequence('one')
84
+
85
+ partial_mock_one.expects(:first).in_sequence(sequence)
86
+ partial_mock_two.expects(:second).in_sequence(sequence)
87
+
88
+ partial_mock_one.first
89
+ partial_mock_two.second
90
+ end
91
+ assert_passed(test_result)
92
+ end
93
+
94
+ def test_should_allow_stub_expectations_to_be_skipped_in_sequence
95
+ test_result = run_test do
96
+ mock = mock()
97
+ sequence = sequence('one')
98
+
99
+ mock.expects(:first).in_sequence(sequence)
100
+ s = mock.stubs(:second).in_sequence(sequence)
101
+ mock.expects(:third).in_sequence(sequence)
102
+
103
+ mock.first
104
+ mock.third
105
+ end
106
+ assert_passed(test_result)
107
+ end
108
+
109
+ def test_should_regard_sequences_as_independent_of_each_other
110
+ test_result = run_test do
111
+ mock = mock()
112
+ sequence_one = sequence('one')
113
+ sequence_two = sequence('two')
114
+
115
+ mock.expects(:first).in_sequence(sequence_one)
116
+ mock.expects(:second).in_sequence(sequence_one)
117
+
118
+ mock.expects(:third).in_sequence(sequence_two)
119
+ mock.expects(:fourth).in_sequence(sequence_two)
120
+
121
+ mock.first
122
+ mock.third
123
+ mock.second
124
+ mock.fourth
125
+ end
126
+ assert_passed(test_result)
127
+ end
128
+
129
+ def test_should_include_sequence_in_failure_message
130
+ test_result = run_test do
131
+ mock = mock()
132
+ sequence = sequence('one')
133
+
134
+ mock.expects(:first).in_sequence(sequence)
135
+ mock.expects(:second).in_sequence(sequence)
136
+
137
+ mock.second
138
+ end
139
+ assert_failed(test_result)
140
+ assert_match Regexp.new("in sequence 'one'"), test_result.failures.first.message
141
+ end
142
+
143
+ def test_should_allow_expectations_to_be_in_more_than_one_sequence
144
+ test_result = run_test do
145
+ mock = mock()
146
+ sequence_one = sequence('one')
147
+ sequence_two = sequence('two')
148
+
149
+ mock.expects(:first).in_sequence(sequence_one)
150
+ mock.expects(:second).in_sequence(sequence_two)
151
+ mock.expects(:three).in_sequence(sequence_one).in_sequence(sequence_two)
152
+
153
+ mock.first
154
+ mock.three
155
+ end
156
+ assert_failed(test_result)
157
+ assert_match Regexp.new("in sequence 'one'"), test_result.failures.first.message
158
+ assert_match Regexp.new("in sequence 'two'"), test_result.failures.first.message
159
+ end
160
+
161
+ def test_should_have_shortcut_for_expectations_to_be_in_more_than_one_sequence
162
+ test_result = run_test do
163
+ mock = mock()
164
+ sequence_one = sequence('one')
165
+ sequence_two = sequence('two')
166
+
167
+ mock.expects(:first).in_sequence(sequence_one)
168
+ mock.expects(:second).in_sequence(sequence_two)
169
+ mock.expects(:three).in_sequence(sequence_one, sequence_two)
170
+
171
+ mock.first
172
+ mock.three
173
+ end
174
+ assert_failed(test_result)
175
+ assert_match Regexp.new("in sequence 'one'"), test_result.failures.first.message
176
+ assert_match Regexp.new("in sequence 'two'"), test_result.failures.first.message
177
+ end
178
+
179
+ end
@@ -62,7 +62,7 @@ class MochaTestResultIntegrationTest < Test::Unit::TestCase
62
62
  object = mock()
63
63
  execution_point = ExecutionPoint.current; object.expects(:message)
64
64
  end
65
- assert_equal 1, faults.size
65
+ assert_equal 1, faults.length
66
66
  assert_equal execution_point, ExecutionPoint.new(faults.first.location)
67
67
  end
68
68
 
@@ -75,7 +75,7 @@ class MochaTestResultIntegrationTest < Test::Unit::TestCase
75
75
  object = mock()
76
76
  execution_point = ExecutionPoint.current; object.message
77
77
  end
78
- assert_equal 1, faults.size
78
+ assert_equal 1, faults.length
79
79
  assert_equal execution_point, ExecutionPoint.new(faults.first.location)
80
80
  end
81
81
 
@@ -87,7 +87,7 @@ class MochaTestResultIntegrationTest < Test::Unit::TestCase
87
87
  run_test(test_result) do
88
88
  execution_point = ExecutionPoint.current; flunk
89
89
  end
90
- assert_equal 1, faults.size
90
+ assert_equal 1, faults.length
91
91
  assert_equal execution_point, ExecutionPoint.new(faults.first.location)
92
92
  end
93
93
 
@@ -41,7 +41,7 @@ class StubbaIntegrationTest < Test::Unit::TestCase
41
41
  end
42
42
 
43
43
  test.run(Test::Unit::TestResult.new) {}
44
- assert_equal 0, DontMessWithMe.mocha.expectations.size
44
+ assert_equal 0, DontMessWithMe.mocha.expectations.length
45
45
  end
46
46
 
47
47
  def test_should_stub_instance_method_within_test
@@ -72,7 +72,7 @@ class StubbaIntegrationTest < Test::Unit::TestCase
72
72
  end
73
73
 
74
74
  test.run(Test::Unit::TestResult.new) {}
75
- assert_equal 0, instance.mocha.expectations.size
75
+ assert_equal 0, instance.mocha.expectations.length
76
76
  end
77
77
 
78
78
  private
@@ -55,7 +55,7 @@ class StubbaTestResultIntegrationTest < Test::Unit::TestCase
55
55
  object = Class.new { def message; end }.new
56
56
  execution_point = ExecutionPoint.current; object.expects(:message)
57
57
  end
58
- assert_equal 1, faults.size
58
+ assert_equal 1, faults.length
59
59
  assert_equal execution_point, ExecutionPoint.new(faults.first.location)
60
60
  end
61
61
 
@@ -67,7 +67,7 @@ class StubbaTestResultIntegrationTest < Test::Unit::TestCase
67
67
  run_test(test_result) do
68
68
  execution_point = ExecutionPoint.current; flunk
69
69
  end
70
- assert_equal 1, faults.size
70
+ assert_equal 1, faults.length
71
71
  assert_equal execution_point, ExecutionPoint.new(faults.first.location)
72
72
  end
73
73