flexmock 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile.lock +9 -9
  3. data/Rakefile +3 -1
  4. data/lib/flexmock.rb +1 -1
  5. data/lib/flexmock/argument_matchers.rb +1 -1
  6. data/lib/flexmock/argument_types.rb +1 -2
  7. data/lib/flexmock/base.rb +2 -1
  8. data/lib/flexmock/call_record.rb +30 -0
  9. data/lib/flexmock/call_validator.rb +68 -0
  10. data/lib/flexmock/composite_expectation.rb +56 -0
  11. data/lib/flexmock/core.rb +30 -64
  12. data/lib/flexmock/core_class_methods.rb +1 -1
  13. data/lib/flexmock/default_framework_adapter.rb +1 -1
  14. data/lib/flexmock/deprecated_methods.rb +8 -3
  15. data/lib/flexmock/errors.rb +1 -1
  16. data/lib/flexmock/expectation.rb +7 -84
  17. data/lib/flexmock/expectation_builder.rb +133 -0
  18. data/lib/flexmock/expectation_director.rb +6 -4
  19. data/lib/flexmock/expectation_recorder.rb +42 -0
  20. data/lib/flexmock/extensions/active_record_model.rb +109 -0
  21. data/lib/flexmock/mock_builder.rb +139 -0
  22. data/lib/flexmock/mock_container.rb +13 -214
  23. data/lib/flexmock/noop.rb +1 -1
  24. data/lib/flexmock/ordering.rb +1 -2
  25. data/lib/flexmock/partial_mock.rb +95 -57
  26. data/lib/flexmock/rails.rb +1 -1
  27. data/lib/flexmock/recorder.rb +4 -3
  28. data/lib/flexmock/rspec.rb +1 -1
  29. data/lib/flexmock/rspec_spy_matcher.rb +4 -0
  30. data/lib/flexmock/spy_describers.rb +22 -5
  31. data/lib/flexmock/test_unit.rb +2 -40
  32. data/lib/flexmock/test_unit_integration.rb +4 -4
  33. data/lib/flexmock/test_unit_testcase_extensions.rb +49 -0
  34. data/lib/flexmock/undefined.rb +1 -1
  35. data/lib/flexmock/validators.rb +18 -12
  36. data/lib/flexmock/version.rb +1 -1
  37. data/test/based_partials_test.rb +1 -1
  38. data/test/container_methods_test.rb +1 -1
  39. data/test/default_framework_adapter_test.rb +1 -1
  40. data/test/demeter_mocking_test.rb +52 -2
  41. data/test/deprecated_methods_test.rb +1 -1
  42. data/test/examples_from_readme_test.rb +1 -1
  43. data/test/expectation_description_test.rb +1 -1
  44. data/test/extended_should_receive_test.rb +1 -1
  45. data/test/mock_builder_test.rb +68 -0
  46. data/test/naming_test.rb +1 -1
  47. data/test/new_instances_test.rb +1 -1
  48. data/test/partial_mock_test.rb +23 -6
  49. data/test/record_mode_test.rb +1 -1
  50. data/test/rspec_integration/integration_spec.rb +1 -1
  51. data/test/samples_test.rb +1 -2
  52. data/test/should_ignore_missing_test.rb +1 -1
  53. data/test/should_receive_test.rb +1 -1
  54. data/test/test_setup.rb +17 -0
  55. data/test/test_unit_integration/auto_test_unit_test.rb +1 -1
  56. data/test/tu_integration_test.rb +1 -1
  57. data/test/undefined_test.rb +1 -1
  58. metadata +52 -52
  59. data/TAGS +0 -1056
  60. data/lib/flexmock/composite.rb +0 -10
  61. data/lib/flexmock/rails/view_mocking.rb +0 -144
  62. data/test/rails_view_stub_test.rb +0 -145
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c8fc2db94cd104d7f780ee838c07b73a09649317
4
+ data.tar.gz: 18c344caf1b37f52f0226b563e8c18fe2832d46f
5
+ SHA512:
6
+ metadata.gz: aaa6e22a1f390003d539e753d1ebbed9f20e37ce2fb825f39cd1032811f3084236ee4bb591e52644a1eb0bf09d173bda64f9169e619f75a4bf033c70279c51e3
7
+ data.tar.gz: f106c0edb36b898a2907a7e5930b078a6d36ee7389081ce3fc9a192aad29ebe785ff8d848953ba84955449c37405bd75396cd662db375134fe364f8c0e2a5370
@@ -1,16 +1,16 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- diff-lcs (1.2.4)
5
- rake (10.0.4)
6
- rspec (2.13.0)
7
- rspec-core (~> 2.13.0)
8
- rspec-expectations (~> 2.13.0)
9
- rspec-mocks (~> 2.13.0)
10
- rspec-core (2.13.1)
11
- rspec-expectations (2.13.0)
4
+ diff-lcs (1.2.5)
5
+ rake (10.1.0)
6
+ rspec (2.14.1)
7
+ rspec-core (~> 2.14.0)
8
+ rspec-expectations (~> 2.14.0)
9
+ rspec-mocks (~> 2.14.0)
10
+ rspec-core (2.14.7)
11
+ rspec-expectations (2.14.4)
12
12
  diff-lcs (>= 1.1.3, < 2.0)
13
- rspec-mocks (2.13.1)
13
+ rspec-mocks (2.14.4)
14
14
 
15
15
  PLATFORMS
16
16
  ruby
data/Rakefile CHANGED
@@ -100,7 +100,9 @@ end
100
100
  task :rdoc => ["html/index.html", :fixcss]
101
101
 
102
102
  file "html/index.html" => ["Rakefile"] + RDOC_FILES do
103
- sh "rdoc -o html --title FlexMock --line-numbers -m doc/index.rdoc #{RDOC_FILES}"
103
+ Bundler.with_clean_env do
104
+ sh "rdoc -o html --title FlexMock --line-numbers -m doc/index.rdoc #{RDOC_FILES}"
105
+ end
104
106
  end
105
107
 
106
108
  EXAMPLE_RB.zip(EXAMPLE_DOC).each do |source, target|
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
  #
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -13,7 +13,6 @@ require 'flexmock/argument_matchers'
13
13
 
14
14
  class FlexMock
15
15
 
16
- ####################################################################
17
16
  # Include this module in your test class if you wish to use the +eq+
18
17
  # and +any+ argument matching methods without a prefix. (Otherwise
19
18
  # use <tt>FlexMock.any</tt> and <tt>FlexMock.eq(obj)</tt>.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -26,3 +26,4 @@ require 'flexmock/mock_container'
26
26
  require 'flexmock/partial_mock'
27
27
  require 'flexmock/undefined'
28
28
  require 'flexmock/deprecated_methods'
29
+ require 'flexmock/extensions/active_record_model'
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #---
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
+ # All rights reserved.
6
+ #
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #+++
11
+
12
+ class FlexMock
13
+
14
+ CallRecord = Struct.new(:method_name, :args, :block_given, :expectation) do
15
+ def matches?(sym, actual_args, options)
16
+ method_name == sym &&
17
+ ArgumentMatching.all_match?(actual_args, args) &&
18
+ matches_block?(options[:with_block])
19
+ end
20
+
21
+ private
22
+
23
+ def matches_block?(block_option)
24
+ block_option.nil? ||
25
+ (block_option && block_given) ||
26
+ (!block_option && !block_given)
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #---
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
+ # All rights reserved.
6
+ #
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #+++
11
+
12
+ class FlexMock
13
+
14
+ # A CallValidator checks the list of call records for a particular
15
+ # method name and arguments.
16
+ class CallValidator
17
+
18
+ # Does the +calls+ list record a method named +method_name+ with
19
+ # +args+. Options include:
20
+ #
21
+ # * :times => n -- If given, the call should match exactly +n+ times.
22
+ # * :and => [] -- A list of argument validations to be run on each
23
+ # matching invocation.
24
+ # * :on_count => n -- If given, the :and validations on only run on the
25
+ # nth invocation.
26
+ #
27
+ def received?(calls, method_name, args, options)
28
+ count = 0
29
+ calls.each { |call_record|
30
+ if call_record.matches?(method_name, args, options)
31
+ count += 1
32
+ run_additional_validations(call_record, count, options)
33
+ end
34
+ }
35
+ count_matches?(count, options[:times])
36
+ end
37
+
38
+ private
39
+
40
+ def additionals(options)
41
+ ands = options[:and]
42
+ if ands.nil?
43
+ []
44
+ elsif ands.is_a?(Proc)
45
+ [ands]
46
+ else
47
+ ands
48
+ end
49
+ end
50
+
51
+ def run_additional_validations(call_record, count, options)
52
+ if options[:on_count].nil? || count == options[:on_count]
53
+ additionals(options).each do |add|
54
+ add.call(*call_record.args)
55
+ end
56
+ end
57
+ end
58
+
59
+ def count_matches?(count, times)
60
+ if times
61
+ count == times
62
+ else
63
+ count > 0
64
+ end
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,56 @@
1
+ class FlexMock
2
+
3
+ # A composite expectation allows several expectations to be grouped into a
4
+ # single composite and then apply the same constraints to all expectations
5
+ # in the group.
6
+ class CompositeExpectation
7
+
8
+ # Initialize the composite expectation.
9
+ def initialize
10
+ @expectations = []
11
+ end
12
+
13
+ # Add an expectation to the composite.
14
+ def add(expectation)
15
+ @expectations << expectation
16
+ end
17
+
18
+ # Apply the constraint method to all expectations in the composite.
19
+ def method_missing(sym, *args, &block)
20
+ @expectations.each do |expectation|
21
+ expectation.send(sym, *args, &block)
22
+ end
23
+ self
24
+ end
25
+
26
+ # The following methods return a value, so we make an arbitrary choice
27
+ # and return the value for the first expectation in the composite.
28
+
29
+ # Return the order number of the first expectation in the list.
30
+ def order_number
31
+ @expectations.first.order_number
32
+ end
33
+
34
+ # Return the associated mock object.
35
+ def mock
36
+ @expectations.first.mock
37
+ end
38
+
39
+ # Start a new method expectation. The following constraints will be
40
+ # applied to the new expectation.
41
+ def should_receive(*args, &block)
42
+ @expectations.first.mock.
43
+ flexmock_define_expectation(caller.first, *args, &block)
44
+ end
45
+
46
+ # Return a string representations
47
+ def to_s
48
+ if @expectations.size > 1
49
+ "[" + @expectations.collect { |e| e.to_s }.join(', ') + "]"
50
+ else
51
+ @expectations.first.to_s
52
+ end
53
+ end
54
+ end
55
+
56
+ end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
  #
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -10,17 +10,16 @@
10
10
  #+++
11
11
 
12
12
  require 'flexmock/errors'
13
- require 'flexmock/composite'
14
13
  require 'flexmock/ordering'
15
14
  require 'flexmock/argument_matching'
16
15
  require 'flexmock/explicit_needed'
17
16
  require 'flexmock/class_extensions'
17
+ require 'flexmock/expectation_builder'
18
+ require 'flexmock/call_validator'
19
+ require 'flexmock/call_record'
18
20
 
19
- ######################################################################
20
- # FlexMock is a flexible mock object framework for supporting testing.
21
- #
22
- # FlexMock has a simple interface that's easy to remember, and leaves
23
- # the hard stuff to all those other mock object implementations.
21
+ # FlexMock is a flexible mock object framework for creating and using
22
+ # test doubles (mocks, stubs and spies).
24
23
  #
25
24
  # Basic Usage:
26
25
  #
@@ -33,8 +32,6 @@ require 'flexmock/class_extensions'
33
32
  # With Test::Unit Integration:
34
33
  #
35
34
  # class TestSomething < Test::Unit::TestCase
36
- # include FlexMock::TestCase
37
- #
38
35
  # def test_something
39
36
  # m = flexmock("name")
40
37
  # m.should_receive(:hi).and_return("Hello")
@@ -42,9 +39,7 @@ require 'flexmock/class_extensions'
42
39
  # end
43
40
  # end
44
41
  #
45
- # Note: When using Test::Unit integeration, don't forget to include
46
- # FlexMock::TestCase. Also, if you override +teardown+, make sure you
47
- # call +super+.
42
+ # Note: Also, if you override +teardown+, make sure you call +super+.
48
43
  #
49
44
  class FlexMock
50
45
  include Ordering
@@ -104,22 +99,6 @@ class FlexMock
104
99
  self
105
100
  end
106
101
 
107
- CallRecord = Struct.new(:method_name, :args, :block_given, :expectation) do
108
- def matches?(sym, actual_args, options)
109
- method_name == sym &&
110
- ArgumentMatching.all_match?(actual_args, args) &&
111
- matches_block?(options[:with_block])
112
- end
113
-
114
- private
115
-
116
- def matches_block?(block_option)
117
- block_option.nil? ||
118
- (block_option && block_given) ||
119
- (!block_option && !block_given)
120
- end
121
- end
122
-
123
102
  # Handle missing methods by attempting to look up a handler.
124
103
  def method_missing(sym, *args, &block)
125
104
  enhanced_args = block_given? ? args + [block] : args
@@ -162,25 +141,11 @@ class FlexMock
162
141
  should_receive(:class => base_class)
163
142
  end
164
143
 
144
+ CALL_VALIDATOR = CallValidator.new
145
+
165
146
  # True if the mock received the given method and arguments.
166
- def flexmock_received?(sym, args, options={})
167
- count = 0
168
- additional = options[:and] || []
169
- additional = [additional] if additional.is_a?(Proc)
170
- @calls.each { |call_record|
171
- if call_record.matches?(sym, args, options)
172
- count += 1
173
- if options[:on_count].nil? || count == options[:on_count]
174
- additional.each do |add| add.call(*call_record.args) end
175
- end
176
- end
177
- }
178
- if options[:times]
179
- result = count == options[:times]
180
- else
181
- result = count > 0
182
- end
183
- result
147
+ def flexmock_received?(method_name, args, options={})
148
+ CALL_VALIDATOR.received?(@calls, method_name, args, options)
184
149
  end
185
150
 
186
151
  # Return the list of calls made on this mock. Used in formatting
@@ -191,13 +156,13 @@ class FlexMock
191
156
 
192
157
  # Invocke the original non-mocked functionality for the given
193
158
  # symbol.
194
- def flexmock_invoke_original(sym, args)
159
+ def flexmock_invoke_original(method_name, args)
195
160
  return FlexMock.undefined
196
161
  end
197
162
 
198
163
  # Override the built-in +method+ to include the mocked methods.
199
- def method(sym)
200
- @expectations[sym] || super
164
+ def method(method_name)
165
+ @expectations[method_name] || super
201
166
  rescue NameError => ex
202
167
  if @ignore_missing
203
168
  proc { FlexMock.undefined }
@@ -230,14 +195,15 @@ class FlexMock
230
195
  flexmock_define_expectation(location, *args)
231
196
  end
232
197
 
198
+ # Using +location+, define the expectations specified by +args+.
233
199
  def flexmock_define_expectation(location, *args)
234
- @last_expectation = ContainerHelper.parse_should_args(self, args) do |sym|
235
- @expectations[sym] ||= ExpectationDirector.new(sym)
236
- result = Expectation.new(self, sym, location)
237
- @expectations[sym] << result
238
- override_existing_method(sym) if flexmock_respond_to?(sym)
239
- result = ExplicitNeeded.new(result, sym, @base_class) if
240
- @base_class && ! @base_class.flexmock_defined?(sym)
200
+ @last_expectation = EXP_BUILDER.parse_should_args(self, args) do |method_name|
201
+ @expectations[method_name] ||= ExpectationDirector.new(method_name)
202
+ result = Expectation.new(self, method_name, location)
203
+ @expectations[method_name] << result
204
+ override_existing_method(method_name) if flexmock_respond_to?(method_name)
205
+ result = ExplicitNeeded.new(result, method_name, @base_class) if
206
+ @base_class && ! @base_class.flexmock_defined?(method_name)
241
207
  result
242
208
  end
243
209
  end
@@ -271,15 +237,15 @@ class FlexMock
271
237
  end
272
238
 
273
239
 
274
- # Override the existing definition of method +sym+ in the mock.
275
- # Most methods depend on the method_missing trick to be invoked.
276
- # However, if the method already exists, it will not call
277
- # method_missing. This method defines a singleton method on the
278
- # mock to explicitly invoke the method_missing logic.
279
- def override_existing_method(sym)
240
+ # Override the existing definition of method +method_name+ in the
241
+ # mock. Most methods depend on the method_missing trick to be
242
+ # invoked. However, if the method already exists, it will not call
243
+ # method_missing. This method defines a singleton method on the mock
244
+ # to explicitly invoke the method_missing logic.
245
+ def override_existing_method(method_name)
280
246
  sclass.class_eval <<-EOS
281
- def #{sym}(*args, &block)
282
- method_missing(:#{sym}, *args, &block)
247
+ def #{method_name}(*args, &block)
248
+ method_missing(:#{method_name}, *args, &block)
283
249
  end
284
250
  EOS
285
251
  end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003-2012 by Jim Weirich (jim.weirich@gmail.com).
4
+ # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
5
  # All rights reserved.
6
6
  #
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -38,7 +38,8 @@ class FlexMock
38
38
  # that by passing in the number of expected calls as a second
39
39
  # paramter.
40
40
  def mock_handle(sym, expected_count=nil, &block) # :nodoc:
41
- $stderr.puts "mock_handle is deprecated, use the new should_receive interface instead."
41
+ $stderr.puts "mock_handle is deprecated, " +
42
+ "use the new should_receive interface instead."
42
43
  self.should_receive(sym).times(expected_count).returns(&block)
43
44
  end
44
45
 
@@ -57,6 +58,10 @@ class FlexMock
57
58
  end
58
59
 
59
60
  module Ordering
60
- flexmock_deprecate :mock_allocate_order, :mock_groups, :mock_current_order, :mock_validate_order
61
+ flexmock_deprecate(
62
+ :mock_allocate_order,
63
+ :mock_groups,
64
+ :mock_current_order,
65
+ :mock_validate_order)
61
66
  end
62
67
  end