jferris-mocha 0.9.7.20090701124354 → 0.9.7.20090911190113

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -70,7 +70,11 @@ Rake::RDocTask.new('rdoc') do |task|
70
70
  task.main = 'README'
71
71
  task.title = "Mocha #{Mocha::VERSION}"
72
72
  task.rdoc_dir = 'doc'
73
- task.template = File.expand_path(File.join(File.dirname(__FILE__), "templates", "html_with_google_analytics"))
73
+ template = File.expand_path(File.join(File.dirname(__FILE__), "templates", "html_with_google_analytics.rb"))
74
+ if File.exist?(template)
75
+ puts "*** Using RDoc template incorporating Google Analytics"
76
+ task.template = template
77
+ end
74
78
  task.rdoc_files.include(
75
79
  'README',
76
80
  'RELEASE',
@@ -0,0 +1,10 @@
1
+ module Mocha
2
+ class Invocation # :nodoc:
3
+ attr_reader :mock, :method_name, :arguments
4
+ def initialize(mock, method_name, arguments)
5
+ @mock = mock
6
+ @method_name = method_name
7
+ @arguments = arguments
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,134 @@
1
+ require File.join(File.dirname(__FILE__), "acceptance_test_helper")
2
+ require 'mocha'
3
+ require 'matcher_helpers'
4
+
5
+ module SpyTestMethods
6
+
7
+ def setup
8
+ setup_acceptance_test
9
+ end
10
+
11
+ def teardown
12
+ teardown_acceptance_test
13
+ end
14
+
15
+ def test_should_accept_wildcard_stub_call_without_arguments
16
+ instance = new_instance
17
+ instance.stubs(:to_s)
18
+ instance.to_s
19
+ assert_received(instance, :to_s)
20
+ assert_matcher_accepts have_received(:to_s), instance
21
+ end
22
+
23
+ def test_should_accept_wildcard_stub_call_with_arguments
24
+ instance = new_instance
25
+ instance.stubs(:to_s)
26
+ instance.to_s(:argument)
27
+ assert_received(instance, :to_s)
28
+ assert_matcher_accepts have_received(:to_s), instance
29
+ end
30
+
31
+ def test_should_not_accept_wildcard_stub_without_call
32
+ instance = new_instance
33
+ instance.stubs(:to_s)
34
+ assert_fails { assert_received(instance, :to_s) }
35
+ assert_fails { assert_matcher_accepts have_received(:to_s), instance }
36
+ end
37
+
38
+ def test_should_not_accept_call_without_arguments
39
+ instance = new_instance
40
+ instance.stubs(:to_s)
41
+ instance.to_s
42
+ assert_fails { assert_received(instance, :to_s) {|expect| expect.with(1) } }
43
+ assert_fails { assert_matcher_accepts have_received(:to_s).with(1), instance }
44
+ end
45
+
46
+ def test_should_not_accept_call_with_different_arguments
47
+ instance = new_instance
48
+ instance.stubs(:to_s)
49
+ instance.to_s(2)
50
+ assert_fails { assert_received(instance, :to_s) {|expect| expect.with(1) } }
51
+ assert_fails { assert_matcher_accepts have_received(:to_s).with(1), instance }
52
+ end
53
+
54
+ def test_should_accept_call_with_correct_arguments
55
+ instance = new_instance
56
+ instance.stubs(:to_s)
57
+ instance.to_s(1)
58
+ assert_received(instance, :to_s) {|expect| expect.with(1) }
59
+ assert_matcher_accepts have_received(:to_s).with(1), instance
60
+ end
61
+
62
+ def test_should_accept_call_with_wildcard_arguments
63
+ instance = new_instance
64
+ instance.stubs(:to_s)
65
+ instance.to_s('hello')
66
+ assert_received(instance, :to_s) {|expect| expect.with(is_a(String)) }
67
+ assert_matcher_accepts have_received(:to_s).with(is_a(String)), instance
68
+ end
69
+
70
+ def test_should_reject_call_on_different_mock
71
+ instance = new_instance
72
+ other = new_instance
73
+ instance.stubs(:to_s)
74
+ other.stubs(:to_s)
75
+ other.to_s('hello')
76
+ assert_fails { assert_received(instance, :to_s) {|expect| expect.with(is_a(String)) } }
77
+ assert_fails { assert_matcher_accepts have_received(:to_s).with(is_a(String)), instance }
78
+ end
79
+
80
+ def test_should_accept_correct_number_of_calls
81
+ instance = new_instance
82
+ instance.stubs(:to_s)
83
+ 2.times { instance.to_s }
84
+ assert_received(instance, :to_s) {|expect| expect.twice }
85
+ assert_matcher_accepts have_received(:to_s).twice, instance
86
+ end
87
+
88
+ def test_should_reject_not_enough_calls
89
+ instance = new_instance
90
+ instance.stubs(:to_s)
91
+ instance.to_s
92
+ message = /expected exactly twice/
93
+ assert_fails(message) { assert_received(instance, :to_s) {|expect| expect.twice } }
94
+ assert_fails(message) { assert_matcher_accepts have_received(:to_s).twice, instance }
95
+ end
96
+
97
+ def test_should_reject_too_many_calls
98
+ instance = new_instance
99
+ instance.stubs(:to_s)
100
+ 2.times { instance.to_s }
101
+ message = /expected exactly once/
102
+ assert_fails(message) { assert_received(instance, :to_s) {|expect| expect.once } }
103
+ assert_fails(message) { assert_matcher_accepts have_received(:to_s).once, instance }
104
+ end
105
+
106
+ def assert_fails(message=/not yet invoked/)
107
+ begin
108
+ yield
109
+ rescue Test::Unit::AssertionFailedError => exception
110
+ assert_match message, exception.message, "Test failed, but with the wrong message"
111
+ return
112
+ end
113
+ flunk("Expected to fail")
114
+ end
115
+
116
+ end
117
+
118
+ class PartialSpyTest < Test::Unit::TestCase
119
+ include AcceptanceTest
120
+ include SpyTestMethods
121
+
122
+ def new_instance
123
+ Object.new
124
+ end
125
+ end
126
+
127
+ class PureSpyTest < Test::Unit::TestCase
128
+ include AcceptanceTest
129
+ include SpyTestMethods
130
+
131
+ def new_instance
132
+ stub
133
+ end
134
+ end
@@ -0,0 +1,5 @@
1
+ class Test::Unit::TestCase
2
+ def assert_matcher_accepts(matcher, instance)
3
+ assert matcher.matches?(instance), matcher.failure_message
4
+ end
5
+ end
@@ -0,0 +1,145 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'test_runner'
3
+ require 'mocha/api'
4
+ require 'mocha/mockery'
5
+ require 'mocha/object'
6
+
7
+ class AssertReceivedTest < Test::Unit::TestCase
8
+
9
+ include Mocha
10
+ include TestRunner
11
+ include Mocha::API
12
+
13
+ def teardown
14
+ Mockery.reset_instance
15
+ end
16
+
17
+ class FakeMock
18
+ def initialize(name)
19
+ @name = name
20
+ end
21
+
22
+ def inspect
23
+ @name
24
+ end
25
+
26
+ def mocha
27
+ self
28
+ end
29
+ end
30
+
31
+ def test_passes_if_invocation_exists
32
+ method = :a_method
33
+ mock = FakeMock.new('a mock')
34
+ Mockery.instance.invocation(mock, method, [])
35
+ assert_passes do
36
+ assert_received(mock, method)
37
+ end
38
+ end
39
+
40
+ def test_fails_if_invocation_doesnt_exist
41
+ method = :a_method
42
+ mock = FakeMock.new('a mock')
43
+ assert_fails do
44
+ assert_received(mock, method)
45
+ end
46
+ end
47
+
48
+ def test_fails_if_invocation_exists_with_different_arguments
49
+ method = :a_method
50
+ mock = FakeMock.new('a mock')
51
+ Mockery.instance.invocation(mock, method, [2, 1])
52
+ assert_fails do
53
+ assert_received(mock, method) {|expect| expect.with(1, 2) }
54
+ end
55
+ end
56
+
57
+ def test_passes_if_invocation_exists_with_wildcard_arguments
58
+ method = :a_method
59
+ mock = FakeMock.new('a mock')
60
+ Mockery.instance.invocation(mock, method, ['hello'])
61
+ assert_passes do
62
+ assert_received(mock, method) {|expect| expect.with(is_a(String)) }
63
+ end
64
+ end
65
+
66
+ def test_passes_if_invocation_exists_with_exact_arguments
67
+ method = :a_method
68
+ mock = FakeMock.new('a mock')
69
+ Mockery.instance.invocation(mock, method, ['hello'])
70
+ assert_passes do
71
+ assert_received(mock, method) {|expect| expect.with('hello') }
72
+ end
73
+ end
74
+
75
+ def test_fails_if_invocation_exists_only_on_other_mock
76
+ method = :a_method
77
+ mock = FakeMock.new('a mock')
78
+ other = 'another mock'
79
+ Mockery.instance.invocation(other, method, ['hello'])
80
+ assert_fails do
81
+ assert_received(mock, method)
82
+ end
83
+ end
84
+
85
+ def test_passes_if_invocation_exists_for_impersonating_mock
86
+ method = :a_method
87
+ object = Object.new
88
+ mock = FakeMock.new('a mock')
89
+
90
+ class << object
91
+ attr_accessor :mocha
92
+ end
93
+ object.mocha = mock
94
+
95
+ Mockery.instance.invocation(mock, method, ['hello'])
96
+ assert_passes do
97
+ assert_received(object, method) {|expect| expect.with('hello') }
98
+ end
99
+ end
100
+
101
+ def test_passes_if_invocation_count_correct
102
+ method = :a_method
103
+ mock = FakeMock.new('a mock')
104
+ 2.times { Mockery.instance.invocation(mock, method, []) }
105
+ assert_passes do
106
+ assert_received(mock, method) {|expect| expect.twice }
107
+ end
108
+ end
109
+
110
+ def test_fails_if_invocation_count_too_low
111
+ method = :a_method
112
+ mock = FakeMock.new('a mock')
113
+ Mockery.instance.invocation(mock, method, [])
114
+ assert_fails do
115
+ assert_received(mock, method) {|expect| expect.twice }
116
+ end
117
+ end
118
+
119
+ def test_fails_if_invocation_count_too_high
120
+ method = :a_method
121
+ mock = FakeMock.new('a mock')
122
+ 2.times { Mockery.instance.invocation(mock, method, []) }
123
+ assert_fails do
124
+ assert_received(mock, method) {|expect| expect.once }
125
+ end
126
+ end
127
+
128
+ def assert_passes(&block)
129
+ assert ! fails?(&block)
130
+ end
131
+
132
+ def assert_fails(&block)
133
+ assert fails?(&block)
134
+ end
135
+
136
+ def fails?
137
+ begin
138
+ yield
139
+ false
140
+ rescue Test::Unit::AssertionFailedError
141
+ true
142
+ end
143
+ end
144
+
145
+ end
@@ -0,0 +1,192 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'test_runner'
3
+ require 'mocha/api'
4
+ require 'mocha/mockery'
5
+ require 'mocha/object'
6
+ require 'matcher_helpers'
7
+
8
+ module HaveReceivedTestMethods
9
+
10
+ include Mocha
11
+
12
+ def teardown
13
+ Mockery.reset_instance
14
+ end
15
+
16
+ def test_passes_if_invocation_exists
17
+ method = :a_method
18
+ mock = new_mock('a mock')
19
+ Mockery.instance.invocation(mock, method, [])
20
+ assert_passes do
21
+ assert_matcher_accepts have_received(method), mock
22
+ end
23
+ end
24
+
25
+ def test_fails_if_invocation_doesnt_exist
26
+ method = :a_method
27
+ mock = new_mock('a mock')
28
+ assert_fails do
29
+ assert_matcher_accepts have_received(method), mock
30
+ end
31
+ end
32
+
33
+ def test_fails_if_invocation_exists_with_different_arguments
34
+ method = :a_method
35
+ mock = new_mock('a mock')
36
+ Mockery.instance.invocation(mock, method, [2, 1])
37
+ assert_fails do
38
+ assert_matcher_accepts have_received(method).with(1, 2), mock
39
+ end
40
+ end
41
+
42
+ def test_passes_if_invocation_exists_with_wildcard_arguments
43
+ method = :a_method
44
+ mock = new_mock('a mock')
45
+ Mockery.instance.invocation(mock, method, ['hello'])
46
+ assert_passes do
47
+ assert_matcher_accepts have_received(method).with(is_a(String)), mock
48
+ end
49
+ end
50
+
51
+ def test_passes_if_invocation_exists_with_exact_arguments
52
+ method = :a_method
53
+ mock = new_mock('a mock')
54
+ Mockery.instance.invocation(mock, method, ['hello'])
55
+ assert_passes do
56
+ assert_matcher_accepts have_received(method).with('hello'), mock
57
+ end
58
+ end
59
+
60
+ def test_fails_if_invocation_exists_only_on_other_mock
61
+ method = :a_method
62
+ mock = new_mock('a mock')
63
+ other = 'another mock'
64
+ Mockery.instance.invocation(other, method, ['hello'])
65
+ assert_fails do
66
+ assert_matcher_accepts have_received(method), mock
67
+ end
68
+ end
69
+
70
+ def test_passes_if_invocation_exists_for_impersonating_mock
71
+ method = :a_method
72
+ object = Object.new
73
+ mock = new_mock('a mock')
74
+
75
+ class << object
76
+ attr_accessor :mocha
77
+ end
78
+ object.mocha = mock
79
+
80
+ Mockery.instance.invocation(mock, method, ['hello'])
81
+ assert_passes do
82
+ assert_matcher_accepts have_received(method).with('hello'), object
83
+ end
84
+ end
85
+
86
+ def test_passes_if_invocation_count_correct
87
+ method = :a_method
88
+ mock = new_mock('a mock')
89
+ 2.times { Mockery.instance.invocation(mock, method, []) }
90
+ assert_passes do
91
+ assert_matcher_accepts have_received(method).twice, mock
92
+ end
93
+ end
94
+
95
+ def test_fails_if_invocation_count_incorrect
96
+ method = :a_method
97
+ mock = new_mock('a mock')
98
+ Mockery.instance.invocation(mock, method, [])
99
+ assert_fails do
100
+ assert_matcher_accepts have_received(method).twice, mock
101
+ end
102
+ end
103
+
104
+ def test_fails_if_invocation_count_too_low
105
+ method = :a_method
106
+ mock = new_mock('a mock')
107
+ Mockery.instance.invocation(mock, method, [])
108
+ assert_fails do
109
+ assert_matcher_accepts have_received(method).twice, mock
110
+ end
111
+ end
112
+
113
+ def test_fails_if_invocation_count_too_high
114
+ method = :a_method
115
+ mock = new_mock('a mock')
116
+ 2.times { Mockery.instance.invocation(mock, method, []) }
117
+ assert_fails do
118
+ assert_matcher_accepts have_received(method).once, mock
119
+ end
120
+ end
121
+
122
+ def assert_passes(&block)
123
+ assert ! fails?(&block)
124
+ end
125
+
126
+ def assert_fails(&block)
127
+ assert fails?(&block)
128
+ end
129
+
130
+ def fails?
131
+ begin
132
+ yield
133
+ false
134
+ rescue Test::Unit::AssertionFailedError
135
+ true
136
+ end
137
+ end
138
+
139
+ end
140
+
141
+ class PartialHaveReceivedTest < Test::Unit::TestCase
142
+
143
+ include TestRunner
144
+ include Mocha::API
145
+ include HaveReceivedTestMethods
146
+
147
+ class FakeMock
148
+ def initialize(name)
149
+ @name = name
150
+ end
151
+
152
+ def inspect
153
+ @name
154
+ end
155
+
156
+ def mocha
157
+ self
158
+ end
159
+ end
160
+
161
+ def new_mock(*args)
162
+ FakeMock.new(*args)
163
+ end
164
+
165
+ end
166
+
167
+
168
+ class PureHaveReceivedTest < Test::Unit::TestCase
169
+
170
+ include TestRunner
171
+ include Mocha::API
172
+ include HaveReceivedTestMethods
173
+
174
+ class FakeMock
175
+ def initialize(name)
176
+ @name = name
177
+ end
178
+
179
+ def inspect
180
+ @name
181
+ end
182
+
183
+ def mocha
184
+ self
185
+ end
186
+ end
187
+
188
+ def new_mock(*args)
189
+ Mocha::Mock.new(*args)
190
+ end
191
+
192
+ end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/invocation'
3
+
4
+ class InvocationTest < Test::Unit::TestCase
5
+
6
+ include Mocha
7
+
8
+ def test_has_mock_method_name_and_args
9
+ mock = 'a mock'
10
+ method = :call_me
11
+ args = [1, 2]
12
+ invocation = Invocation.new(mock, method, args)
13
+ assert_equal mock, invocation.mock
14
+ assert_equal method, invocation.method_name
15
+ assert_equal args, invocation.arguments
16
+ end
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jferris-mocha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7.20090701124354
4
+ version: 0.9.7.20090911190113
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mead
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-01 00:00:00 -07:00
12
+ date: 2009-09-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - lib/mocha/integration/test_unit/ruby_version_186_and_above.rb
60
60
  - lib/mocha/integration/test_unit.rb
61
61
  - lib/mocha/integration.rb
62
+ - lib/mocha/invocation.rb
62
63
  - lib/mocha/is_a.rb
63
64
  - lib/mocha/logger.rb
64
65
  - lib/mocha/metaclass.rb
@@ -123,6 +124,7 @@ files:
123
124
  - test/acceptance/partial_mocks_test.rb
124
125
  - test/acceptance/return_value_test.rb
125
126
  - test/acceptance/sequence_test.rb
127
+ - test/acceptance/spy_test.rb
126
128
  - test/acceptance/states_test.rb
127
129
  - test/acceptance/stub_any_instance_method_test.rb
128
130
  - test/acceptance/stub_class_method_test.rb
@@ -144,12 +146,14 @@ files:
144
146
  - test/acceptance/stubbing_on_non_mock_object_test.rb
145
147
  - test/deprecation_disabler.rb
146
148
  - test/execution_point.rb
149
+ - test/matcher_helpers.rb
147
150
  - test/method_definer.rb
148
151
  - test/simple_counter.rb
149
152
  - test/test_helper.rb
150
153
  - test/test_runner.rb
151
154
  - test/unit/any_instance_method_test.rb
152
155
  - test/unit/array_inspect_test.rb
156
+ - test/unit/assert_received_test.rb
153
157
  - test/unit/backtrace_filter_test.rb
154
158
  - test/unit/cardinality_test.rb
155
159
  - test/unit/central_test.rb
@@ -160,7 +164,9 @@ files:
160
164
  - test/unit/expectation_list_test.rb
161
165
  - test/unit/expectation_test.rb
162
166
  - test/unit/hash_inspect_test.rb
167
+ - test/unit/have_received_test.rb
163
168
  - test/unit/in_state_ordering_constraint_test.rb
169
+ - test/unit/invocation_test.rb
164
170
  - test/unit/metaclass_test.rb
165
171
  - test/unit/method_matcher_test.rb
166
172
  - test/unit/mock_test.rb