caricature 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  = Caricature - Bringing simple mocking to the DLR
2
2
 
3
- * http://github.com/#{github_username}/#{project_name}
3
+ * http://github.com/casualjim/caricature
4
4
 
5
5
  == DESCRIPTION:
6
6
 
@@ -33,25 +33,25 @@ problems but for the time being this is how it has to be.
33
33
 
34
34
  isolation = Isolation.for(Ninja)
35
35
  isolation.when_receiving(:attack) do |exp|
36
- exp.with(:shuriken)
37
- exp.return(3)
36
+ exp.with(:shuriken)
37
+ exp.return(3)
38
38
  end
39
39
 
40
- Battle.new(mock)
41
- battle.combat
40
+ Battle.new(mock)
41
+ battle.combat
42
42
 
43
- isolation.did_receive?(:attack).should.be.successful
43
+ isolation.should.have_received?(:attack)
44
44
 
45
45
 
46
46
  == REQUIREMENTS:
47
47
 
48
48
  * To use the caricature library you need to have uuidtools installed
49
- sudo gem install uuidtools
50
- sudo igem install uuidtools
49
+ (sudo) gem install uuidtools
50
+ (sudo) igem install uuidtools
51
51
 
52
52
  == INSTALL:
53
53
 
54
- sudo gem install caricature
54
+ (sudo) gem install caricature
55
55
 
56
56
  == LICENSE:
57
57
 
data/Rakefile CHANGED
@@ -59,9 +59,8 @@ SUMMARY = "Caricature brings simple mocking to Ruby, DLR and CLR."
59
59
  RDOC_OPTIONS = [
60
60
  "--quiet",
61
61
  "--title", SUMMARY,
62
- "--main", "lib/caricature/isolation.rb",
62
+ "--main", "README.rdoc",
63
63
  "--line-numbers",
64
- "--inline-source",
65
64
  "--format","darkfish"
66
65
  ]
67
66
 
@@ -132,7 +131,7 @@ begin
132
131
  s.extra_rdoc_files = RDOC_FILES
133
132
  s.rdoc_options = RDOC_OPTIONS
134
133
  s.required_ruby_version = ">= 1.8.4"
135
- s.add_runtime_dependency(%q<uuidtools>, [">= 2.0.0"])
134
+ s.add_dependency 'uuidtools', ">= 2.0.0"
136
135
  end
137
136
 
138
137
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -11,9 +11,9 @@ Gem::Specification.new do |s|
11
11
  s.files = ["README.rdoc",
12
12
  "Rakefile",
13
13
  "caricature.gemspec",
14
- "lib/caricature",
15
14
  "lib/caricature.rb",
16
- "lib/caricature/clr",
15
+ "lib/caricature/bacon.rb",
16
+ "lib/caricature/bacon/integration.rb",
17
17
  "lib/caricature/clr.rb",
18
18
  "lib/caricature/clr/aspnet_mvc.rb",
19
19
  "lib/caricature/clr/descriptor.rb",
@@ -26,9 +26,10 @@ Gem::Specification.new do |s|
26
26
  "lib/caricature/isolator.rb",
27
27
  "lib/caricature/messenger.rb",
28
28
  "lib/caricature/method_call_recorder.rb",
29
+ "lib/caricature/rspec.rb",
30
+ "lib/caricature/rspec/integration.rb",
29
31
  "lib/caricature/verification.rb",
30
32
  "lib/caricature/version.rb",
31
- "lib/core_ext",
32
33
  "lib/core_ext/array.rb",
33
34
  "lib/core_ext/class.rb",
34
35
  "lib/core_ext/core_ext.rb",
@@ -36,22 +37,16 @@ Gem::Specification.new do |s|
36
37
  "lib/core_ext/module.rb",
37
38
  "lib/core_ext/object.rb",
38
39
  "lib/core_ext/string.rb",
39
- "lib/core_ext/system",
40
40
  "lib/core_ext/system/string.rb",
41
41
  "lib/core_ext/system/type.rb",
42
42
  "spec/bacon_helper.rb",
43
- "spec/bin",
44
43
  "spec/bin/ClrModels.dll",
45
- "spec/bin/ClrModels.dll.mdb",
46
- "spec/integration",
47
44
  "spec/integration/callback_spec.rb",
48
45
  "spec/integration/clr_to_clr_spec.rb",
49
46
  "spec/integration/clr_to_ruby_spec.rb",
50
47
  "spec/integration/indexer_spec.rb",
51
48
  "spec/integration/ruby_to_ruby_spec.rb",
52
- "spec/models",
53
49
  "spec/models/ClrModels.cs",
54
- "spec/unit",
55
50
  "spec/unit/core_ext_spec.rb",
56
51
  "spec/unit/descriptor_spec.rb",
57
52
  "spec/unit/expectation_spec.rb",
@@ -67,7 +62,7 @@ Gem::Specification.new do |s|
67
62
  s.loaded = false
68
63
  s.name = "caricature"
69
64
  s.platform = "ruby"
70
- s.rdoc_options = ["--quiet", "--title", "Caricature brings simple mocking to Ruby, DLR and CLR.", "--main", "lib/caricature/isolation.rb", "--line-numbers", "--inline-source", "--format", "darkfish"]
65
+ s.rdoc_options = ["--quiet", "--title", "Caricature brings simple mocking to Ruby, DLR and CLR.", "--main", "README.rdoc", "--line-numbers", "--format", "darkfish"]
71
66
  s.require_paths = ["lib"]
72
67
  s.required_ruby_version = ">= 1.8.4"
73
68
  s.required_rubygems_version = ">= 0"
@@ -75,5 +70,5 @@ Gem::Specification.new do |s|
75
70
  s.rubygems_version = "1.3.5"
76
71
  s.specification_version = 3
77
72
  s.summary = "Caricature brings simple mocking to Ruby, DLR and CLR."
78
- s.version = "0.7.0"
73
+ s.version = "0.7.1"
79
74
  end
@@ -11,4 +11,5 @@ end
11
11
  require 'core_ext/core_ext'
12
12
  require 'caricature/version'
13
13
  require 'caricature/isolation'
14
- require 'caricature/clr' if defined? IRONRUBY_VERSION
14
+ require 'caricature/clr' if defined? IRONRUBY_VERSION
15
+ require 'caricature/bacon' if defined? Bacon
@@ -0,0 +1,2 @@
1
+ require 'caricature/bacon/integration' if defined? Bacon
2
+
@@ -0,0 +1,57 @@
1
+ class Should
2
+
3
+ def have_received?(name, &b)
4
+ lambda { |obj, *args| obj.did_receive?(name, &b).successful? }
5
+ end
6
+
7
+ def satisfy(*args, &block)
8
+ if args.size == 1 && String === args.first
9
+ description = args.shift
10
+ else
11
+ description = ""
12
+ end
13
+
14
+ r=nil
15
+ err = nil
16
+ begin
17
+ r = yield(@object, *args)
18
+ rescue Caricature::ArgumentMatchError => e
19
+ err =e
20
+ end
21
+ if Bacon::Counter[:depth] > 0
22
+ Bacon::Counter[:requirements] += 1
23
+ raise (err.is_a?(Caricature::ArgumentMatchError) ? err : Bacon::Error.new(:failed, description)) unless @negated ^ r
24
+ r
25
+ else
26
+ @negated ? !r : !!r
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ module Caricature
33
+
34
+ # Describes a verification of a method call.
35
+ # This corresponds kind of to an assertion
36
+ class Verification
37
+
38
+
39
+ # indicate that this method verification is successful
40
+ def successful?
41
+ a = any_args? ? [:any] : @args
42
+ begin
43
+ return @recorder.was_called?(@method_name, @block_args, @mode, *a)
44
+ rescue ArgumentError => e
45
+ raise Caricature::ArgumentMatchError.new :failed, e
46
+ end
47
+ end
48
+
49
+
50
+ end
51
+
52
+ class ArgumentMatchError < Bacon::Error;
53
+
54
+ end
55
+
56
+ end
57
+
@@ -2,15 +2,6 @@ module Caricature
2
2
 
3
3
  class Isolation
4
4
 
5
- def internal_create_override(method_name, mode=:instance, &block)
6
- builder = ExpectationBuilder.new method_name
7
- block.call builder unless block.nil?
8
- exp = builder.build
9
-
10
- expectations.add_expectation exp, mode
11
- exp
12
- end
13
-
14
5
  class << self
15
6
 
16
7
  # Creates an isolation object complete with proxy and method call recorder
@@ -34,8 +25,18 @@ module Caricature
34
25
  return ClrInterfaceIsolator if subject.respond_to? :class_eval and !subject.respond_to? :new
35
26
  ClrIsolator
36
27
  end
37
- end
28
+ end
29
+
30
+ protected
38
31
 
32
+ def internal_create_override(method_name, mode=:instance, &block)
33
+ builder = ExpectationBuilder.new method_name
34
+ block.call builder unless block.nil?
35
+ exp = builder.build
36
+
37
+ expectations.add_expectation exp, mode
38
+ exp
39
+ end
39
40
 
40
41
  end
41
42
 
@@ -7,13 +7,15 @@ module Caricature
7
7
 
8
8
  # deliver the message to the receiving isolation
9
9
  def internal_deliver(mode, method_name, return_type, *args, &b)
10
- exp = expectations.find(method_name, mode, *args)
10
+ exp = expectations.find(method_name, mode, *args)
11
+ bl = record_call(method_name, mode, exp, *args, &b)
11
12
  is_value_type = return_type && return_type != System::Void.to_clr_type && return_type.is_value_type
12
13
  res = nil
13
14
  if exp
14
- res = instance.__send__(method_name, *args, &b) if exp.super_before?
15
- res = exp.execute *args
16
- res = instance.__send__(method_name, *args, &b) if !exp.super_before? and exp.call_super?
15
+ block = exp.block || b
16
+ res = instance.__send__(method_name, *args, &block) if exp.super_before?
17
+ res = exp.execute *args, &bl
18
+ res = instance.__send__(method_name, *args, &block) if !exp.super_before? and exp.call_super?
17
19
  end
18
20
  res ||= System::Activator.create_instance(return_type) if is_value_type
19
21
  res
@@ -31,7 +33,8 @@ module Caricature
31
33
  res = nil
32
34
  is_value_type = return_type && return_type != System::Void.to_clr_type && return_type.is_value_type
33
35
  exp = expectations.find(method_name, mode, *args)
34
- res = exp.execute *args if exp
36
+ bl = record_call(method_name, mode, exp, *args, &b)
37
+ res = exp.execute *args, &bl if exp
35
38
  res ||= System::Activator.create_instance(return_type) if is_value_type
36
39
  res
37
40
  end
@@ -47,9 +47,17 @@ module Caricature
47
47
 
48
48
  # tell the expectation it nees to return this value or the value returned by the block
49
49
  # you provide to this method.
50
- def return(value=nil)
50
+ def return(value=nil, &b)
51
51
  @return_value = value
52
- @return_value ||= yield if block_given?
52
+ @return_callback = b if b
53
+ self
54
+ end
55
+
56
+ # Sets up arguments for the block that is being passed into the isolated method call
57
+ def pass_block(*args, &b)
58
+ @any_block_args = args.first.is_a?(Symbol) and args.first == :any
59
+ @block_args = args
60
+ @block_callback = b unless b.nil?
53
61
  self
54
62
  end
55
63
 
@@ -61,13 +69,17 @@ module Caricature
61
69
  end
62
70
 
63
71
  # tell the expecation it needs to call the super before the expectation exectution
64
- def super_before
72
+ def super_before(&b)
65
73
  @super = :before
74
+ @block = b if b
75
+ self
66
76
  end
67
77
 
68
78
  # tell the expectation it needs to call the super after the expecation execution
69
- def super_after
79
+ def super_after(&b)
70
80
  @super = :after
81
+ @block = b if b
82
+ self
71
83
  end
72
84
 
73
85
  # indicates whether this expectation should match with any arguments
@@ -101,10 +113,24 @@ module Caricature
101
113
  # contains the callback if one is given
102
114
  attr_reader :callback
103
115
 
116
+ # contains the callback that is used to return the value when this expectation
117
+ # is executed
118
+ attr_reader :return_callback
119
+
120
+ # contains the arguments that will be passed on to the block
121
+ attr_reader :block_args
122
+
123
+ # The block that will be used as value provider for the block in the method
124
+ attr_reader :block_callback
125
+
126
+ # the block that will be used
127
+ attr_accessor :block
128
+
129
+
104
130
  # Initializes a new instance of an expectation
105
- def initialize(method_name, args, error_args, return_value, super_mode, callback)
106
- @method_name, @args, @error_args, @return_value, @super, @callback =
107
- method_name, args, error_args, return_value, super_mode, callback
131
+ def initialize(*args)
132
+ @method_name, @args, @error_args, @return_value, @return_callback,
133
+ @super, @callback, @block_args, @block_callback, @block = *args
108
134
  @any_args = true
109
135
  end
110
136
 
@@ -132,12 +158,32 @@ module Caricature
132
158
  def has_callback?
133
159
  !@callback.nil?
134
160
  end
161
+
162
+ # indicates whether this expectation has a block as value provider for the method call block
163
+ def has_block_callback?
164
+ !@block_callback.nil?
165
+ end
166
+
167
+ # a flag to indicate it has a return value callback
168
+ def has_return_callback?
169
+ !@return_callback.nil?
170
+ end
135
171
 
136
172
  # executes this expectation with its configuration
137
- def execute(*margs)
173
+ def execute(*margs,&b)
138
174
  ags = any_args? ? (margs.empty? ? :any : margs) : args
139
175
  actual_raise *@error_args if has_error_args?
140
176
  callback.call(*ags) if has_callback?
177
+
178
+ if b
179
+ if has_block_callback?
180
+ b.call(*@block_callback.call)
181
+ else
182
+ b.call(*@block_args)
183
+ end
184
+ end
185
+
186
+ return @return_callback.call(*margs) if has_return_callback?
141
187
  return return_value if has_return_value?
142
188
  nil
143
189
  end
@@ -161,15 +207,14 @@ module Caricature
161
207
  # this builder is passed into the block to allow only certain
162
208
  # operations in the block.
163
209
  def initialize(method_name)
164
- @method_name, @return_value, @super, @block, @error_args, @args, @any_args, @callback =
165
- method_name, nil, nil, nil, nil, [], true, nil
210
+ @method_name, @args, @any_args = method_name, [], true
166
211
  end
167
212
 
168
213
 
169
214
 
170
215
  # build up the expectation with the provided arguments
171
216
  def build
172
- Expectation.new @method_name, @args, @error_args, @return_value, @super, @callback
217
+ Expectation.new @method_name, @args, @error_args, @return_value, @return_callback, @super, @callback, @block_args, @block_callback, @block
173
218
  end
174
219
 
175
220
  end
@@ -64,9 +64,9 @@ module Caricature
64
64
  # Initializes a new instance of this isolation.
65
65
  def initialize(isolator, context)
66
66
  @instance = isolator.subject
67
- @recorder = context.recorder
68
67
  @messenger = context.messenger
69
- @expectations = context.expectations
68
+ @messenger.recorder = @recorder = context.recorder
69
+ @expectations = context.expectations
70
70
  @proxy = isolator.isolation
71
71
  isolator.isolation.class.instance_variable_set("@___context___", self)
72
72
  end
@@ -74,14 +74,12 @@ module Caricature
74
74
  # record and send the message to the isolation.
75
75
  # takes care of following expectations rules when sending messages.
76
76
  def send_message(method_name, return_type, *args, &b)
77
- recorder.record_call method_name, :instance, *args, &b
78
77
  @messenger.deliver(method_name, return_type, *args, &b)
79
78
  end
80
79
 
81
80
  # record and send the message to the isolation.
82
81
  # takes care of following expectations rules when sending messages.
83
82
  def send_class_message(method_name, return_type, *args, &b)
84
- recorder.record_call method_name, :class, *args, &b
85
83
  @messenger.deliver_to_class(method_name, return_type, *args, &b)
86
84
  end
87
85
 
@@ -126,11 +124,6 @@ module Caricature
126
124
  builder = ExpectationBuilder.new method_name
127
125
  block.call builder unless block.nil?
128
126
  exp = builder.build
129
- @proxy.class.send((mode == :instance ? :define_method : :define_cmethod), method_name.to_sym, lambda do |*args|
130
- b = nil
131
- b = Proc.new { yield } if block_given?
132
- isolation_context.send_message(method_name, nil, *args, &b)
133
- end)
134
127
  expectations.add_expectation exp, mode
135
128
  exp
136
129
  end
@@ -56,7 +56,7 @@ module Caricature
56
56
  # You will probably be using this method only when you're interested in whether a method has been called
57
57
  # during the course of the test you're running.
58
58
  def did_receive?(method_name, &block)
59
- isolation_context.class_verify method_name
59
+ isolation_context.class_verify method_name, &block
60
60
  end
61
61
 
62
62
  end
@@ -129,7 +129,7 @@ module Caricature
129
129
  # You will probably be using this method only when you're interested in whether a method has been called
130
130
  # during the course of the test you're running.
131
131
  def did_receive?(method_name, &block)
132
- isolation_context.verify method_name
132
+ isolation_context.verify method_name, &block
133
133
  end
134
134
 
135
135
  # Verifies whether the specified class method has been called
@@ -158,7 +158,7 @@ module Caricature
158
158
  # It expects the constructor parameters if they are needed.
159
159
  def with_subject(*args, &b)
160
160
  isolation_context.instance = self.class.superclass.new *args
161
- yield self if block_given?
161
+ b.call self if b
162
162
  self
163
163
  end
164
164
 
@@ -3,6 +3,9 @@ module Caricature
3
3
  # A base class to encapsulate method invocation
4
4
  class Messenger
5
5
 
6
+ # contains the recorder for recording method calls
7
+ attr_accessor :recorder
8
+
6
9
  # the real instance of the isolated subject
7
10
  # used to forward calls in partial mocks
8
11
  attr_reader :instance
@@ -32,6 +35,11 @@ module Caricature
32
35
  def internal_deliver(mode, method_name, return_type, *args, &b)
33
36
  raise NotImplementedError.new("Override in an implementing class")
34
37
  end
38
+
39
+ private
40
+ def record_call(method_name, mode, expectation, *args, &b)
41
+ recorder.record_call method_name, mode, expectation, *args, &b if recorder
42
+ end
35
43
 
36
44
  end
37
45
 
@@ -43,10 +51,12 @@ module Caricature
43
51
  # implementation of the template method for looking up the expectation and/or returning a value
44
52
  def internal_deliver(mode, method_name, return_type, *args, &b)
45
53
  exp = expectations.find(method_name, mode, *args)
46
- if exp
47
- res = instance.__send__(method_name, *args, &b) if exp.super_before?
48
- res = exp.execute *args
49
- res = instance.__send__(method_name, *args, &b) if !exp.super_before? and exp.call_super?
54
+ bl = record_call(method_name, mode, exp, *args, &b)
55
+ if exp
56
+ block = exp.block || b
57
+ res = instance.__send__(method_name, *args, &block) if exp.super_before?
58
+ res = exp.execute *args, &bl
59
+ res = instance.__send__(method_name, *args, &block) if !exp.super_before? and exp.call_super?
50
60
  res
51
61
  else
52
62
  nil
@@ -1,5 +1,24 @@
1
1
  module Caricature
2
2
 
3
+ class BlockCallRecording
4
+ attr_accessor :call_number
5
+ attr_reader :args
6
+
7
+ def initialize(*args)
8
+ @call_number = 1
9
+ @args = args
10
+ end
11
+
12
+ # compares one block variation to another.
13
+ # Also takes an array as an argument
14
+ def ==(other)
15
+ other = self.class.new(other) if other.respond_to?(:each)
16
+ return true if other.args.first.is_a?(Symbol) and other.args.first == :any
17
+ other.args == args
18
+ end
19
+
20
+ end
21
+
3
22
  # A recording of an argument variation.
4
23
  # Every time a method is called with different arguments
5
24
  # the method call recorder will create an ArgumentVariation
@@ -16,21 +35,44 @@ module Caricature
16
35
  # the number of the call that has the following parameters
17
36
  attr_accessor :call_number
18
37
 
38
+ # the collection of block calls variations on this argument variation
39
+ attr_reader :blocks
40
+
19
41
  # initializes a new instance of an argument recording.
20
42
  # configures it with 1 call count and the args as an +Array+
21
43
  def initialize(args=[], call_number=1, block=nil)
22
44
  @args = args
23
45
  @block = block
46
+ @blocks = []
24
47
  @call_number = call_number
25
48
  end
26
49
 
50
+ def add_block_variation(*args)
51
+ bv = find_block_variation(*args)
52
+ if bv.empty?
53
+ blocks << BlockCallRecording.new(*args)
54
+ else
55
+ bv.first.call_number += 1
56
+ end
57
+ end
58
+
59
+ def find_block_variation(*args)
60
+ return @blocks if args.last.is_a?(Symbol) and args.last == :any
61
+ @blocks.select { |bv| bv.args == args }
62
+ end
63
+
27
64
  # compares one argument variation to another.
28
65
  # Also takes an array as an argument
29
66
  def ==(other)
30
67
  other = self.class.new(other) if other.respond_to?(:each)
31
68
  return true if other.args.first.is_a?(Symbol) and other.args.first == :any
32
69
  other.args == args
70
+ end
71
+
72
+ def to_s
73
+ "<ArgumentRecording @args=#{args}, @block= #{block}, @call_number=#{call_number}"
33
74
  end
75
+ alias_method :inspect, :to_s
34
76
  end
35
77
 
36
78
  # A recording that represents a method call
@@ -71,14 +113,23 @@ module Caricature
71
113
 
72
114
  # add an argument variation
73
115
  def add_argument_variation(args, block)
74
- variation = find_argument_variations args
75
- @variations << ArgumentRecording.new(args, @variations.size+1, block) if variation == []
116
+ variation = find_argument_variations args, nil
117
+ if variation.empty?
118
+ @variations << ArgumentRecording.new(args, @variations.size+1, block) if variation == []
119
+ else
120
+ variation.first.call_number += 1
121
+ end
76
122
  end
77
123
 
78
124
  # finds an argument variation that matches the provided +args+
79
- def find_argument_variations(args)
80
- return @variations if args.first.is_a?(Symbol) and args.first == :any
81
- @variations.select { |ar| ar.args == args }
125
+ def find_argument_variations(args, block_args)
126
+ return @variations if args.first.is_a?(Symbol) and args.last == :any
127
+ return @variations.select { |ar| ar.args == args } if block_args.nil?
128
+ return @variations.select { |ar| ar.args == args and ar.block } if block_args.last == :any
129
+ return @variations.select do |ar|
130
+ av = ar.args == args
131
+ av and not ar.find_block_variation(*block_args).empty?
132
+ end
82
133
  end
83
134
  end
84
135
 
@@ -97,19 +148,34 @@ module Caricature
97
148
  end
98
149
 
99
150
  # records a method call or increments the count of how many times this method was called.
100
- def record_call(method_name, mode=:instance, *args, &block)
151
+ def record_call(method_name, mode=:instance, expectation=nil, *args, &block)
101
152
  mn_sym = method_name.to_s.to_sym
102
153
  method_calls[mode][mn_sym] ||= MethodCallRecording.new method_name
103
154
  mc = method_calls[mode][mn_sym]
104
155
  mc.count += 1
105
- mc.add_argument_variation args, block
106
- end
156
+ agc = mc.add_argument_variation args, block
157
+ b = (expectation && expectation.block) ? (expectation.block||block) : block
158
+ expectation.block = lambda do |*ags|
159
+ res = nil
160
+ res = b.call *ags if b
161
+ agc.first.add_block_variation *ags
162
+ res
163
+ end if expectation
164
+ block.nil? ? nil : (lambda { |*ags|
165
+ res = nil
166
+ res = block.call *ags if block
167
+ agc.first.add_block_variation *ags
168
+ res
169
+ })
170
+ end
107
171
 
108
172
  # returns whether the method was actually called with the specified constraints
109
- def was_called?(method_name, mode=:instance, *args)
110
- mc = method_calls[mode][method_name.to_s.to_sym]
111
- if mc
112
- return mc.find_argument_variations(args).first == args
173
+ def was_called?(method_name, block_args, mode=:instance, *args)
174
+ mc = method_calls[mode][method_name.to_s.to_sym]
175
+ if mc
176
+ result = mc.find_argument_variations(args, block_args).first == args
177
+ raise ArgumentError, "Arguments don't match.\nYou expected: #{args.join(", ")}.\nI did find the following variations: #{mc.args.collect {|ar| ar.args.join(', ') }.join(' and ')}" unless result
178
+ result
113
179
  else
114
180
  return !!mc
115
181
  end
File without changes
File without changes
@@ -6,7 +6,12 @@ module Caricature
6
6
 
7
7
  # Initializes a new instance of a +Verification+
8
8
  def initialize(method_name, recorder, mode=:instance)
9
- @method_name, @args, @any_args, @recorder, @mode = method_name, [], true, recorder, mode
9
+ @method_name, @args, @any_args, @recorder, @mode, @block_args = method_name, [], true, recorder, mode, nil
10
+ init_plugin
11
+ end
12
+
13
+ def init_plugin
14
+
10
15
  end
11
16
 
12
17
  # indicates whether this verification can be for any arguments
@@ -22,6 +27,10 @@ module Caricature
22
27
  self
23
28
  end
24
29
 
30
+ def with_block_args(*args)
31
+ @block_args = args
32
+ end
33
+
25
34
  # allow any arguments ignore the argument constraint
26
35
  def allow_any_arguments
27
36
  @any_args = true
@@ -36,7 +45,7 @@ module Caricature
36
45
  # indicate that this method verification is successful
37
46
  def successful?
38
47
  a = any_args? ? [:any] : @args
39
- @recorder.was_called?(@method_name, @mode, *a)
48
+ @recorder.was_called?(@method_name, @block_args, @mode, *a)
40
49
  end
41
50
 
42
51
  end
@@ -1,6 +1,6 @@
1
1
  module Caricature
2
2
 
3
3
  # The version number of the +Caricature+ library
4
- VERSION = '0.7.0'
4
+ VERSION = '0.7.1'
5
5
 
6
6
  end
@@ -139,7 +139,7 @@ describe "Callbacks on expectations" do
139
139
  cnt.should == 1
140
140
  end
141
141
 
142
- it "should execute a callback when an expectation is being invoked and with is not defined in a block" do
142
+ it "should execute a callback when an expectation is being invoked and with is defined in a block" do
143
143
  cnt = 0
144
144
  iso = Caricature::Isolation.for(Dagger)
145
145
  iso.when_receiving(:damage) do |exp|
@@ -27,6 +27,7 @@ describe "CLR to CLR interactions" do
27
27
  @weapon.when_receiving(:attack).with(ClrModels::Ninja.new).return(5)
28
28
 
29
29
  @ninja.attack(ClrModels::Ninja.new, @weapon).should.equal 0
30
+ #@ninja.attack(ClrModels::Ninja.new, ClrModels::Sword.new).should.equal 0
30
31
  end
31
32
 
32
33
  it "should work for expectations with an argument constraint and an assertion argument constraint" do
@@ -102,8 +102,12 @@ describe "Caricature::ExpectationBuilder" do
102
102
  @expectation.error_args.should.equal nil
103
103
  end
104
104
 
105
+ it "should have a return callback" do
106
+ @expectation.return_callback.should.not.be.nil
107
+ end
108
+
105
109
  it "should have the correct return_value" do
106
- @expectation.return_value.should.equal 5
110
+ @expectation.return_callback.call.should.equal 5
107
111
  end
108
112
 
109
113
  end
@@ -226,4 +230,72 @@ describe "Caricature::ExpectationBuilder" do
226
230
  end
227
231
  end
228
232
 
233
+ describe "when giving a block to a super method it should register it" do
234
+
235
+ before do
236
+ builder = Caricature::ExpectationBuilder.new :some_method
237
+ @counter = 0
238
+ builder.super_after do
239
+ @counter += 1
240
+ end
241
+ @expectation = builder.build
242
+ end
243
+
244
+ it "should should have a callback" do
245
+ @expectation.block.should.not.be.nil
246
+ end
247
+
248
+ it "should pass on the correct callback" do
249
+ @expectation.block.call
250
+ @counter.should == 1
251
+ end
252
+
253
+ end
254
+
255
+ describe "when given arguments to the block pass method" do
256
+
257
+ before do
258
+ builder = Caricature::ExpectationBuilder.new :some_method
259
+ builder.pass_block(1,2,3)
260
+ @expectation = builder.build
261
+ end
262
+
263
+ it "should not have a block callback" do
264
+ @expectation.should.not.has_block_callback
265
+ end
266
+
267
+ it "should should execute the block with the provided arguments when executed" do
268
+ ags = []
269
+ @expectation.execute do |*args|
270
+ ags = args
271
+ end
272
+ ags.should == [1,2,3]
273
+ end
274
+
275
+ end
276
+
277
+ describe "when given arguments to the block pass method" do
278
+
279
+ before do
280
+ builder = Caricature::ExpectationBuilder.new :some_method
281
+ builder.pass_block do
282
+ [1,3,4]
283
+ end
284
+ @expectation = builder.build
285
+ end
286
+
287
+ it "should have a block callback" do
288
+ @expectation.should.has_block_callback
289
+ end
290
+
291
+ it "should execute the block with the result of the provided callback when executed" do
292
+ ags = []
293
+ @expectation.execute do |*args|
294
+ ags = args
295
+ end
296
+ ags.should == [1,3,4]
297
+ end
298
+
299
+ end
300
+
229
301
  end
@@ -1,6 +1,7 @@
1
1
  require File.dirname(__FILE__) + "/../bacon_helper"
2
2
 
3
3
  class EmptyExpectations
4
+
4
5
  def find(method_name, *args, &b)
5
6
  nil
6
7
  end
@@ -14,6 +15,10 @@ class ReturnValueExpecation
14
15
  @called_super_after = false
15
16
  end
16
17
 
18
+ def block
19
+ nil
20
+ end
21
+
17
22
  def super_before?
18
23
  @super_before_called = true
19
24
  false
@@ -39,6 +44,7 @@ class ReturnValueExpecation
39
44
  end
40
45
 
41
46
  class ReturnValueExpectations
47
+
42
48
  def expectation
43
49
  @expectation ||= ReturnValueExpecation.new
44
50
  @expectation
@@ -49,6 +55,76 @@ class ReturnValueExpectations
49
55
  end
50
56
  end
51
57
 
58
+ class PassThruBlockExpectation
59
+
60
+ def block
61
+ @block
62
+ end
63
+
64
+ def block=(val)
65
+ @block=val
66
+ end
67
+
68
+ def block_args
69
+ [5,6,7]
70
+ end
71
+
72
+ def execute(*args, &b)
73
+ b.call *block_args
74
+ end
75
+
76
+ def super_before?
77
+ false
78
+ end
79
+
80
+ def call_super?
81
+ false
82
+ end
83
+
84
+ end
85
+
86
+ class CallingBlock
87
+ def a_message(*args, &b)
88
+ b.call(7,8,9)
89
+ end
90
+ end
91
+
92
+ class BlockExpectation
93
+ def block
94
+ lambda { |*args| return args }
95
+ end
96
+
97
+ def super_before?
98
+ false
99
+ end
100
+
101
+ def call_super?
102
+ true
103
+ end
104
+
105
+ def execute(*args, &b)
106
+
107
+ end
108
+
109
+ end
110
+
111
+ class BlockExpectations
112
+
113
+ def initialize(pass_thru=true)
114
+ @pass_thru = pass_thru
115
+ end
116
+
117
+ def expectation
118
+ @expectation ||= (@pass_thru ? PassThruBlockExpectation.new : BlockExpectation.new)
119
+ @expectation
120
+ end
121
+ def find(method_name, *args, &b)
122
+ return expectation if method_name == :a_message
123
+ nil
124
+ end
125
+ end
126
+
127
+
52
128
  describe "Caricature::Messenger strategies" do
53
129
 
54
130
  describe "Caricature::RubyMessenger" do
@@ -84,7 +160,29 @@ describe "Caricature::Messenger strategies" do
84
160
 
85
161
  end
86
162
 
163
+ describe "when an expectation with a block has been defined" do
164
+
165
+ it "should invoke the block that is passed with the args from the expectation only once" do
166
+ messenger = Caricature::RubyMessenger.new BlockExpectations.new
167
+ messenger.recorder = Caricature::MethodCallRecorder.new
168
+ counter = 0
169
+ arguments = []
170
+ messenger.deliver(:a_message, nil) do |*args|
171
+ counter += 1
172
+ arguments = args
173
+ end
174
+ counter.should == 1
175
+ [5,6,7].should == arguments
176
+ end
87
177
 
178
+ it "should call the block that is defined on the expectation by super when call super is enabled" do
179
+ exp = BlockExpectations.new(false)
180
+ messenger = Caricature::RubyMessenger.new exp, CallingBlock.new
181
+ result = messenger.deliver(:a_message, nil, exp.expectation)
182
+ [7,8,9].should == result
183
+ end
184
+
185
+ end
88
186
 
89
187
 
90
188
  end
@@ -127,6 +225,29 @@ describe "Caricature::Messenger strategies" do
127
225
 
128
226
  end
129
227
 
228
+ describe "when an expectation with a block has been defined" do
229
+
230
+ it "should invoke the block that is passed with the args from the expectation only once" do
231
+ messenger = Caricature::ClrClassMessenger.new BlockExpectations.new
232
+ messenger.recorder = Caricature::MethodCallRecorder.new
233
+ counter = 0
234
+ arguments = []
235
+ messenger.deliver(:a_message, nil) do |*args|
236
+ counter += 1
237
+ arguments = args
238
+ end
239
+ counter.should == 1
240
+ [5,6,7].should == arguments
241
+ end
242
+
243
+ it "should call the block that is defined on the expectation by super when call super is enabled" do
244
+ messenger = Caricature::ClrClassMessenger.new BlockExpectations.new(false), CallingBlock.new
245
+ result = messenger.deliver(:a_message, nil)
246
+ [7,8,9].should == result
247
+ end
248
+
249
+ end
250
+
130
251
  end
131
252
 
132
253
  describe "Caricature::ClrInterfaceMessenger" do
@@ -167,6 +288,24 @@ describe "Caricature::Messenger strategies" do
167
288
 
168
289
  end
169
290
 
291
+ describe "when an expectation with a block has been defined" do
292
+
293
+ it "should invoke the block that is passed with the args from the expectation only once" do
294
+ exp = BlockExpectations.new
295
+ messenger = Caricature::ClrInterfaceMessenger.new exp
296
+ messenger.recorder = Caricature::MethodCallRecorder.new
297
+ counter = 0
298
+ arguments = []
299
+ messenger.deliver(:a_message, nil, exp.expectation) do |*args|
300
+ counter += 1
301
+ arguments = args
302
+ end
303
+ counter.should == 1
304
+ [5,6,7].should == arguments
305
+ end
306
+
307
+ end
308
+
170
309
  end
171
310
 
172
311
  end
@@ -35,7 +35,7 @@ describe "MethodCallRecorder" do
35
35
  end
36
36
 
37
37
  it "should have no block" do
38
- @mc.block.should.equal nil
38
+ @mc.block.should.equal nil
39
39
  end
40
40
 
41
41
  it "should have a count a 1" do
@@ -44,7 +44,7 @@ describe "MethodCallRecorder" do
44
44
 
45
45
  end
46
46
 
47
- end
47
+ end
48
48
 
49
49
  describe "when recording a call without arguments but with a block" do
50
50
 
@@ -78,7 +78,7 @@ describe "MethodCallRecorder" do
78
78
  end
79
79
 
80
80
  it "should have a block" do
81
- @mc.args.first.block.should.not.equal nil
81
+ @mc.args.first.block.should.not.equal nil
82
82
  end
83
83
 
84
84
  it "should have the correct block" do
@@ -96,7 +96,7 @@ describe "MethodCallRecorder" do
96
96
  describe "when recording a call 1 argument" do
97
97
 
98
98
  before do
99
- @recorder.record_call :my_method, :instance, 1
99
+ @recorder.record_call :my_method, :instance, nil, 1
100
100
  end
101
101
 
102
102
  it "should have 1 method call" do
@@ -126,7 +126,7 @@ describe "MethodCallRecorder" do
126
126
  end
127
127
 
128
128
  it "should have no block" do
129
- @mc.block.should.equal nil
129
+ @mc.block.should.equal nil
130
130
  end
131
131
 
132
132
  it "should have a count a 1" do
@@ -140,7 +140,7 @@ describe "MethodCallRecorder" do
140
140
  describe "when recording a call 2 arguments" do
141
141
 
142
142
  before do
143
- @recorder.record_call :my_method, :instance, 1, 2
143
+ @recorder.record_call :my_method, :instance, nil, 1, 2
144
144
  end
145
145
 
146
146
  it "should have 1 method call" do
@@ -170,7 +170,7 @@ describe "MethodCallRecorder" do
170
170
  end
171
171
 
172
172
  it "should have no block" do
173
- @mc.block.should.equal nil
173
+ @mc.block.should.equal nil
174
174
  end
175
175
 
176
176
  it "should have a count a 1" do
@@ -211,10 +211,10 @@ describe "MethodCallRecorder" do
211
211
  end
212
212
 
213
213
  it "should have no block" do
214
- @mc.block.should.equal nil
214
+ @mc.block.should.equal nil
215
215
  end
216
216
 
217
- it "should have a count a 1" do
217
+ it "should have a count of 2" do
218
218
  @mc.count.should.equal 2
219
219
  end
220
220
 
@@ -222,11 +222,35 @@ describe "MethodCallRecorder" do
222
222
 
223
223
  end
224
224
 
225
+ describe "when recording a call with a block" do
226
+ before do
227
+ @args = []
228
+ @b = @recorder.record_call :some_method, :instance, nil, 5, 6, 7 do |*args|
229
+ @args = args
230
+ end
231
+
232
+ end
233
+
234
+ it "should return a block" do
235
+ @b.should.not.be.nil
236
+ end
237
+
238
+ it "should return a block that wraps a recording" do
239
+ @b.call 8, 9, 0
240
+ @recorder.method_calls[:instance][:some_method].args.first.blocks.first.args.should.equal [8, 9, 0]
241
+ end
242
+
243
+ it "should call the original block" do
244
+ @b.call 8, 9, 0
245
+ @args.should.equal [8, 9, 0]
246
+ end
247
+ end
248
+
225
249
  describe "when recording 2 calls with different arguments" do
226
250
 
227
251
  before do
228
252
  @recorder.record_call :my_method
229
- @recorder.record_call :my_method, :instance, 1, 3, 4
253
+ @recorder.record_call :my_method, :instance, nil, 1, 3, 4
230
254
  end
231
255
 
232
256
  it "should have 1 method call" do
@@ -252,10 +276,10 @@ describe "MethodCallRecorder" do
252
276
  end
253
277
 
254
278
  it "should have no block" do
255
- @mc.block.should.equal nil
279
+ @mc.block.should.equal nil
256
280
  end
257
281
 
258
- it "should have a count a 1" do
282
+ it "should have a count of 2" do
259
283
  @mc.count.should.equal 2
260
284
  end
261
285
 
@@ -267,29 +291,53 @@ describe "MethodCallRecorder" do
267
291
 
268
292
  before do
269
293
  @recorder.record_call :my_method
270
- @recorder.record_call :my_method, :instance, 1, 3, 4
294
+ @recorder.record_call :my_method, :instance, nil, 1, 3, 4
295
+
271
296
  end
272
297
 
273
298
  it "should confirm when we don't care about the arguments" do
274
- @recorder.was_called?(:my_method, :instance, :any).should.be.true?
299
+ @recorder.was_called?(:my_method, nil, :instance, :any).should.be.true?
275
300
  end
276
301
 
277
302
  it "should confirm when there are no argument variations" do
278
303
  @recorder.record_call :another_method
279
- @recorder.was_called?(:another_method, :instance, :any).should.be.true?
304
+ @recorder.was_called?(:another_method, nil, :instance, :any).should.be.true?
280
305
  end
281
306
 
282
307
  it "should be negative when we provide the wrong arguments" do
283
- @recorder.was_called?(:my_method, :instance, 1, 2, 5).should.be.false?
308
+ lambda { @recorder.was_called?(:my_method, nil, :instance, 1, 2, 5) }.should.raise(ArgumentError)
284
309
  end
285
310
 
286
311
  it "should be positive when we provide the correct arguments" do
287
- @recorder.was_called?(:my_method, :instance, 1, 3, 4).should.be.true?
312
+ @recorder.was_called?(:my_method, nil, :instance, 1, 3, 4).should.be.true?
288
313
  end
289
314
 
290
315
  it "should be positive when we provide no arguments and a call had been recorded without arguments" do
291
- @recorder.was_called?(:my_method, :instance).should.be.true?
316
+ @recorder.was_called?(:my_method, nil, :instance).should.be.true?
317
+ end
318
+
319
+ describe "with block" do
320
+
321
+ before do
322
+ @args = []
323
+ b = @recorder.record_call :some_method, :instance, nil, 5, 6, 7 do |*args|
324
+ @args << args
325
+ end
326
+ b.call 1, 3, 5
327
+ b.call 3, 4, 6
328
+ b.call
329
+ end
330
+
331
+ it "should be positive when we provide any arguments" do
332
+ @recorder.was_called?(:some_method, [:any], :instance, :any).should.be.true?
333
+ end
334
+
335
+ it "should be positive when we provide specific arguments" do
336
+ @recorder.was_called?(:some_method, [1, 3, 5], :instance, :any).should.be.true?
337
+ end
338
+
292
339
  end
340
+
293
341
  end
294
342
 
295
343
  end
@@ -72,7 +72,7 @@ describe "Caricature::Verification" do
72
72
  before do
73
73
  @rec = Caricature::MethodCallRecorder.new
74
74
  @rec.record_call :my_method
75
- @rec.record_call :my_method, :instance, 1, 2, 3
75
+ @rec.record_call :my_method, :instance, nil, 1, 2, 3
76
76
  @rec.record_call :another_method
77
77
 
78
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caricature
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Porto Carrero
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-09 00:00:00 +02:00
12
+ date: 2009-09-23 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -35,6 +35,8 @@ files:
35
35
  - Rakefile
36
36
  - caricature.gemspec
37
37
  - lib/caricature.rb
38
+ - lib/caricature/bacon.rb
39
+ - lib/caricature/bacon/integration.rb
38
40
  - lib/caricature/clr.rb
39
41
  - lib/caricature/clr/aspnet_mvc.rb
40
42
  - lib/caricature/clr/descriptor.rb
@@ -47,6 +49,8 @@ files:
47
49
  - lib/caricature/isolator.rb
48
50
  - lib/caricature/messenger.rb
49
51
  - lib/caricature/method_call_recorder.rb
52
+ - lib/caricature/rspec.rb
53
+ - lib/caricature/rspec/integration.rb
50
54
  - lib/caricature/verification.rb
51
55
  - lib/caricature/version.rb
52
56
  - lib/core_ext/array.rb
@@ -60,7 +64,6 @@ files:
60
64
  - lib/core_ext/system/type.rb
61
65
  - spec/bacon_helper.rb
62
66
  - spec/bin/ClrModels.dll
63
- - spec/bin/ClrModels.dll.mdb
64
67
  - spec/integration/callback_spec.rb
65
68
  - spec/integration/clr_to_clr_spec.rb
66
69
  - spec/integration/clr_to_ruby_spec.rb
@@ -87,9 +90,8 @@ rdoc_options:
87
90
  - --title
88
91
  - Caricature brings simple mocking to Ruby, DLR and CLR.
89
92
  - --main
90
- - lib/caricature/isolation.rb
93
+ - README.rdoc
91
94
  - --line-numbers
92
- - --inline-source
93
95
  - --format
94
96
  - darkfish
95
97
  require_paths:
Binary file