caricature 0.7.2 → 0.7.5

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 (52) hide show
  1. data/README.rdoc +97 -97
  2. data/Rakefile +310 -310
  3. data/caricature.gemspec +110 -106
  4. data/lib/caricature.rb +3 -1
  5. data/lib/caricature/bacon.rb +2 -2
  6. data/lib/caricature/bacon/integration.rb +75 -55
  7. data/lib/caricature/clr.rb +4 -3
  8. data/lib/caricature/clr/aspnet_mvc.rb +3 -3
  9. data/lib/caricature/clr/descriptor.rb +106 -39
  10. data/lib/caricature/clr/event_verification.rb +57 -0
  11. data/lib/caricature/clr/expectation.rb +101 -0
  12. data/lib/caricature/clr/isolation.rb +49 -13
  13. data/lib/caricature/clr/isolator.rb +141 -5
  14. data/lib/caricature/clr/messenger.rb +6 -0
  15. data/lib/caricature/clr/method_call_recorder.rb +97 -0
  16. data/lib/caricature/core_ext.rb +11 -0
  17. data/lib/{core_ext → caricature/core_ext}/array.rb +0 -0
  18. data/lib/{core_ext → caricature/core_ext}/class.rb +0 -0
  19. data/lib/{core_ext → caricature/core_ext}/hash.rb +0 -0
  20. data/lib/{core_ext → caricature/core_ext}/module.rb +0 -0
  21. data/lib/{core_ext → caricature/core_ext}/object.rb +0 -0
  22. data/lib/{core_ext → caricature/core_ext}/string.rb +0 -0
  23. data/lib/{core_ext → caricature/core_ext}/system/string.rb +0 -0
  24. data/lib/{core_ext → caricature/core_ext}/system/type.rb +6 -0
  25. data/lib/caricature/expectation.rb +108 -66
  26. data/lib/caricature/isolator.rb +3 -3
  27. data/lib/caricature/method_call_recorder.rb +32 -4
  28. data/lib/caricature/rspec/integration.rb +118 -77
  29. data/lib/caricature/version.rb +5 -5
  30. data/spec/bacon/integration/callback_spec.rb +156 -156
  31. data/spec/bacon/integration/clr_to_clr_spec.rb +1 -2
  32. data/spec/bacon/integration/event_spec.rb +98 -0
  33. data/spec/bacon/integration/indexer_spec.rb +1 -1
  34. data/spec/bacon/spec_helper.rb +4 -4
  35. data/spec/bacon/unit/descriptor_spec.rb +95 -42
  36. data/spec/bacon/unit/expectation_spec.rb +2 -2
  37. data/spec/bacon/unit/interop_spec.rb +1 -14
  38. data/spec/bacon/unit/isolation_spec.rb +1 -1
  39. data/spec/bacon/unit/isolator_spec.rb +5 -5
  40. data/spec/bin/ClrModels.dll +0 -0
  41. data/spec/models/ClrModels.cs +32 -8
  42. data/spec/models/ruby_models.rb +150 -150
  43. data/spec/rspec/integration/callback_spec.rb +156 -156
  44. data/spec/rspec/integration/indexer_spec.rb +1 -1
  45. data/spec/rspec/spec_helper.rb +12 -6
  46. data/spec/rspec/unit/descriptor_spec.rb +93 -42
  47. data/spec/rspec/unit/event_spec.rb +17 -0
  48. data/spec/rspec/unit/interop_spec.rb +0 -13
  49. data/spec/spec_helper.rb +14 -14
  50. metadata +20 -22
  51. data/lib/core_ext/core_ext.rb +0 -8
  52. data/spec/bin/ClrModels.dll.mdb +0 -0
@@ -124,13 +124,33 @@ module Caricature
124
124
  # finds an argument variation that matches the provided +args+
125
125
  def find_argument_variations(args, block_args)
126
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?
127
+ return match_hash(args, block_args) if args.size == 1 and args.last.is_a?(Hash)
128
+ return @variations.select { |ar| ar.args == args } if block_args.nil?
128
129
  return @variations.select { |ar| ar.args == args and ar.block } if block_args.last == :any
129
130
  return @variations.select do |ar|
130
131
  av = ar.args == args
132
+ # idx = 0
133
+ # ags = ar.args
134
+ # av = args.all? do |ag|
135
+ # rag = ags[idx]
136
+ # res = if ag.is_a?(Hash) and rag.is_a?(Hash)
137
+ # ag.all? { |k, v| ar.args[idx][k] == v }
138
+ # else
139
+ # ag == rag
140
+ # end
141
+ # idx += 1
142
+ # res
143
+ # end
131
144
  av and not ar.find_block_variation(*block_args).empty?
132
145
  end
133
146
  end
147
+
148
+ def match_hash(args, block_args)
149
+ @variations.select do |ar|
150
+ ags = ar.args.last
151
+ args.last.all? { |k, v| ags[k] == v }
152
+ end
153
+ end
134
154
  end
135
155
 
136
156
  # The recorder that will collect method calls and provides an interface for finding those recordings
@@ -168,6 +188,7 @@ module Caricature
168
188
  res
169
189
  })
170
190
  end
191
+
171
192
 
172
193
  def error
173
194
  @error
@@ -177,9 +198,16 @@ module Caricature
177
198
  def was_called?(method_name, block_args, mode=:instance, *args)
178
199
  mc = method_calls[mode][method_name.to_s.to_sym]
179
200
  if mc
180
- result = mc.find_argument_variations(args, block_args).first == args
181
- @error = "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
182
- result
201
+ vari = mc.find_argument_variations(args, block_args)
202
+ result = vari.any? { |agv| agv == args }
203
+ return result if result
204
+ if args.size == 1 and args.last.is_a?(Hash)
205
+ result = vari.any? do |agv|
206
+ agv.args.last.is_a?(Hash) and args.last.all? { |k, v| agv.args.last[k] == v }
207
+ end
208
+ end
209
+ @error = "Arguments don't match.\nYou expected:\n#{args.join(", ")}.\nI did find the following variations:\n#{mc.args.collect {|ar| ar.args.join(', ') }.join("\nand\n")}" unless result
210
+ result
183
211
  else
184
212
  @error = "Couldn't find a method with name #{method_name}"
185
213
  return !!mc
@@ -1,77 +1,118 @@
1
- module Spec
2
- module Adapters
3
- module MockFramework
4
-
5
- def setup_mocks_for_rspec
6
- # No setup required
7
- end
8
-
9
- def verify_mocks_for_rspec
10
- end
11
-
12
- def teardown_mocks_for_rspec
13
- end
14
- end
15
- end
16
- end
17
-
18
- module Caricature
19
-
20
- module RSpecMatchers
21
-
22
- class HaveReceived
23
- def initialize(expected)
24
- @expected = expected
25
- @block_args, @error, @args = nil, "", [:any]
26
- end
27
-
28
- def matches?(target)
29
- @target = target
30
-
31
- veri = @target.did_receive?(@expected).with(*@args)
32
- veri.with_block_args(*@block_args) if @block_args
33
- result = veri.successful?
34
-
35
- @error = "\n#{veri.error}" unless result
36
-
37
- result
38
- end
39
-
40
- def failure_message_for_should
41
- "expected #{@target.inspect} to have received #{@expected}" + @error
42
- end
43
-
44
- def failure_message_for_should_not
45
- "expected #{@target.inspect} not to have received #{@expected}" + @error
46
- end
47
-
48
- # constrain this verification to the provided arguments
49
- def with(*args)
50
- ags = *args
51
- @args = args
52
- @args = [:any] if (args.first.is_a?(Symbol) and args.first == :any) || ags.nil?
53
- # @callback = b if b
54
- self
55
- end
56
-
57
- def with_block_args(*args)
58
- @block_args = args
59
- self
60
- end
61
-
62
- # allow any arguments ignore the argument constraint
63
- def allow_any_arguments
64
- @args = :any
65
- self
66
- end
67
- end
68
-
69
- def have_received(method_name, *args)
70
- HaveReceived.new(method_name).with(*args)
71
- end
72
- end
73
- end
74
-
75
- Spec::Runner.configure do |config|
76
- config.include(Caricature::RSpecMatchers)
77
- end
1
+ module Caricature
2
+ module RSpecAdapter
3
+
4
+ def setup_mocks_for_rspec
5
+ # No setup required
6
+ end
7
+
8
+ def verify_mocks_for_rspec
9
+ end
10
+
11
+ def teardown_mocks_for_rspec
12
+ end
13
+ end
14
+
15
+ module RSpecMatchers
16
+
17
+ class HaveReceived
18
+ def initialize(expected)
19
+ @expected = expected
20
+ @block_args, @error, @args = nil, "", [:any]
21
+ end
22
+
23
+ def matches?(target)
24
+ @target = target
25
+
26
+ veri = @target.did_receive?(@expected).with(*@args)
27
+ veri.with_block_args(*@block_args) if @block_args
28
+ result = veri.successful?
29
+
30
+ @error = "\n#{veri.error}" unless result
31
+
32
+ result
33
+ end
34
+
35
+ def failure_message_for_should
36
+ "expected #{@target.inspect} to have received #{@expected}" + @error
37
+ end
38
+
39
+ def failure_message_for_should_not
40
+ "expected #{@target.inspect} not to have received #{@expected}" + @error
41
+ end
42
+
43
+ # constrain this verification to the provided arguments
44
+ def with(*args)
45
+ ags = *args
46
+ @args = args
47
+ @args = [:any] if (args.first.is_a?(Symbol) and args.first == :any) || ags.nil?
48
+ # @callback = b if b
49
+ self
50
+ end
51
+
52
+ def with_block_args(*args)
53
+ @block_args = args
54
+ self
55
+ end
56
+
57
+ # allow any arguments ignore the argument constraint
58
+ def allow_any_arguments
59
+ @args = :any
60
+ self
61
+ end
62
+ end
63
+
64
+ class HaveRaised
65
+ def initialize(expected)
66
+ @expected = expected
67
+ @block_args, @error, @args = nil, "", [:any]
68
+ end
69
+
70
+ def matches?(target)
71
+ @target = target
72
+
73
+ veri = @target.did_raise_event?(@expected).with(*@args)
74
+ result = veri.successful?
75
+
76
+ @error = "\n#{veri.error}" unless result
77
+
78
+ result
79
+ end
80
+
81
+ def failure_message_for_should
82
+ "expected #{@target.inspect} to have received #{@expected}" + @error
83
+ end
84
+
85
+ def failure_message_for_should_not
86
+ "expected #{@target.inspect} not to have received #{@expected}" + @error
87
+ end
88
+
89
+ # constrain this verification to the provided arguments
90
+ def with(*args)
91
+ ags = *args
92
+ @args = args
93
+ @args = [:any] if (args.first.is_a?(Symbol) and args.first == :any) || ags.nil?
94
+ # @callback = b if b
95
+ self
96
+ end
97
+
98
+ # allow any arguments ignore the argument constraint
99
+ def allow_any_arguments
100
+ @args = :any
101
+ self
102
+ end
103
+ end
104
+
105
+ def have_received(method_name, *args)
106
+ HaveReceived.new(method_name).with(*args)
107
+ end
108
+
109
+ def have_raised(event_name, *args)
110
+ HaveRaised.new(event_name).with(*args)
111
+ end
112
+ end
113
+ end
114
+
115
+ #Spec::Runner.configure do |config|
116
+ # config.mock_with Caricature::RSpecAdapter
117
+ # config.include Caricature::RSpecMatchers
118
+ #end
@@ -1,6 +1,6 @@
1
- module Caricature
2
-
3
- # The version number of the +Caricature+ library
4
- VERSION = '0.7.2'
5
-
1
+ module Caricature
2
+
3
+ # The version number of the +Caricature+ library
4
+ VERSION = '0.7.5'
5
+
6
6
  end
@@ -1,157 +1,157 @@
1
- require File.dirname(__FILE__) + "/../spec_helper"
2
-
3
- describe "Callbacks on expectations" do
4
-
5
- describe "CLR to CLR interactions" do
6
-
7
- describe "when isolating CLR interfaces" do
8
-
9
- before do
10
- @ninja = ClrModels::Ninja.new
11
- @weapon = Caricature::Isolation.for(ClrModels::IWeapon)
12
- end
13
-
14
- it "should execute the callback when the expectation is invoked" do
15
- ninja = ClrModels::Ninja.new
16
- cnt = 0
17
- @weapon.when_receiving(:attack).with(:any) do |*args|
18
- cnt += 1
19
- end
20
- @ninja.attack ninja, @weapon
21
-
22
- cnt.should == 1
23
- end
24
-
25
- end
26
-
27
- describe "when isolating CLR classes" do
28
-
29
- before do
30
- @ninja = ClrModels::Ninja.new
31
- @weapon = Caricature::Isolation.for(ClrModels::Sword)
32
- end
33
-
34
- it "should execute the callback when the expectation is invoked" do
35
- ninja = ClrModels::Ninja.new
36
- cnt = 0
37
- @weapon.when_receiving(:attack).with(:any) do |*args|
38
- cnt += 1
39
- end
40
- @ninja.attack ninja, @weapon
41
-
42
- cnt.should == 1
43
- end
44
-
45
- end
46
-
47
- describe "when isolating CLR instances" do
48
- before do
49
- @ninja = ClrModels::Ninja.new
50
- @weapon = Caricature::Isolation.for(ClrModels::Sword.new)
51
- end
52
-
53
- it "should execute the callback when the expectation is invoked" do
54
- ninja = ClrModels::Ninja.new
55
- cnt = 0
56
- @weapon.when_receiving(:attack).with(ninja) do |*args|
57
- cnt += 1
58
- end
59
- @ninja.attack ninja, @weapon
60
-
61
- cnt.should == 1
62
- end
63
- end
64
-
65
- end
66
-
67
- describe "CLR to ruby interactions" do
68
-
69
- describe "when isolating CLR interfaces" do
70
-
71
- before do
72
- @ninja = Soldier.new
73
- @weapon = Caricature::Isolation.for(ClrModels::IWeapon)
74
- end
75
-
76
- it "should execute the callback when the expectation is invoked" do
77
- ninja = Soldier.new
78
- cnt = 0
79
- @weapon.when_receiving(:attack).with(:any) do |*args|
80
- cnt += 1
81
- end
82
- @ninja.attack ninja, @weapon
83
-
84
- cnt.should == 1
85
- end
86
-
87
- end
88
-
89
- describe "when isolating CLR classes" do
90
-
91
- before do
92
- @ninja = Soldier.new
93
- @weapon = Caricature::Isolation.for(ClrModels::Sword)
94
- end
95
-
96
- it "should execute the callback when the expectation is invoked" do
97
- ninja = Soldier.new
98
- cnt = 0
99
- @weapon.when_receiving(:attack).with(:any) do |*args|
100
- cnt += 1
101
- end
102
- @ninja.attack ninja, @weapon
103
-
104
- cnt.should == 1
105
- end
106
-
107
- end
108
-
109
- describe "when isolating CLR instances" do
110
- before do
111
- @ninja = Soldier.new
112
- @weapon = Caricature::Isolation.for(ClrModels::Sword.new)
113
- end
114
-
115
- it "should execute the callback when the expectation is invoked" do
116
- ninja = Soldier.new
117
- cnt = 0
118
- @weapon.when_receiving(:attack).with(ninja) do |*args|
119
- cnt += 1
120
- end
121
- @ninja.attack ninja, @weapon
122
-
123
- cnt.should == 1
124
- end
125
- end
126
-
127
-
128
- end
129
-
130
- describe "Ruby to Ruby interactions" do
131
-
132
- it "should execute a callback when an expectation is being invoked and with is not defined in a block" do
133
- iso = Caricature::Isolation.for(Dagger)
134
- cnt = 0
135
- iso.when_receiving(:damage).with(:any) do |*args|
136
- cnt += 1
137
- end
138
- iso.damage
139
- cnt.should == 1
140
- end
141
-
142
- it "should execute a callback when an expectation is being invoked and with is defined in a block" do
143
- cnt = 0
144
- iso = Caricature::Isolation.for(Dagger)
145
- iso.when_receiving(:damage) do |exp|
146
- exp.with(:any) do |*args|
147
- cnt += 1
148
- end
149
- end
150
- iso.damage
151
- cnt.should == 1
152
- end
153
- end
154
-
155
-
156
-
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe "Callbacks on expectations" do
4
+
5
+ describe "CLR to CLR interactions" do
6
+
7
+ describe "when isolating CLR interfaces" do
8
+
9
+ before do
10
+ @ninja = ClrModels::Ninja.new
11
+ @weapon = Caricature::Isolation.for(ClrModels::IWeapon)
12
+ end
13
+
14
+ it "should execute the callback when the expectation is invoked" do
15
+ ninja = ClrModels::Ninja.new
16
+ cnt = 0
17
+ @weapon.when_receiving(:attack).with(:any) do |*args|
18
+ cnt += 1
19
+ end
20
+ @ninja.attack ninja, @weapon
21
+
22
+ cnt.should == 1
23
+ end
24
+
25
+ end
26
+
27
+ describe "when isolating CLR classes" do
28
+
29
+ before do
30
+ @ninja = ClrModels::Ninja.new
31
+ @weapon = Caricature::Isolation.for(ClrModels::Sword)
32
+ end
33
+
34
+ it "should execute the callback when the expectation is invoked" do
35
+ ninja = ClrModels::Ninja.new
36
+ cnt = 0
37
+ @weapon.when_receiving(:attack).with(:any) do |*args|
38
+ cnt += 1
39
+ end
40
+ @ninja.attack ninja, @weapon
41
+
42
+ cnt.should == 1
43
+ end
44
+
45
+ end
46
+
47
+ describe "when isolating CLR instances" do
48
+ before do
49
+ @ninja = ClrModels::Ninja.new
50
+ @weapon = Caricature::Isolation.for(ClrModels::Sword.new)
51
+ end
52
+
53
+ it "should execute the callback when the expectation is invoked" do
54
+ ninja = ClrModels::Ninja.new
55
+ cnt = 0
56
+ @weapon.when_receiving(:attack).with(ninja) do |*args|
57
+ cnt += 1
58
+ end
59
+ @ninja.attack ninja, @weapon
60
+
61
+ cnt.should == 1
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ describe "CLR to ruby interactions" do
68
+
69
+ describe "when isolating CLR interfaces" do
70
+
71
+ before do
72
+ @ninja = Soldier.new
73
+ @weapon = Caricature::Isolation.for(ClrModels::IWeapon)
74
+ end
75
+
76
+ it "should execute the callback when the expectation is invoked" do
77
+ ninja = Soldier.new
78
+ cnt = 0
79
+ @weapon.when_receiving(:attack).with(:any) do |*args|
80
+ cnt += 1
81
+ end
82
+ @ninja.attack ninja, @weapon
83
+
84
+ cnt.should == 1
85
+ end
86
+
87
+ end
88
+
89
+ describe "when isolating CLR classes" do
90
+
91
+ before do
92
+ @ninja = Soldier.new
93
+ @weapon = Caricature::Isolation.for(ClrModels::Sword)
94
+ end
95
+
96
+ it "should execute the callback when the expectation is invoked" do
97
+ ninja = Soldier.new
98
+ cnt = 0
99
+ @weapon.when_receiving(:attack).with(:any) do |*args|
100
+ cnt += 1
101
+ end
102
+ @ninja.attack ninja, @weapon
103
+
104
+ cnt.should == 1
105
+ end
106
+
107
+ end
108
+
109
+ describe "when isolating CLR instances" do
110
+ before do
111
+ @ninja = Soldier.new
112
+ @weapon = Caricature::Isolation.for(ClrModels::Sword.new)
113
+ end
114
+
115
+ it "should execute the callback when the expectation is invoked" do
116
+ ninja = Soldier.new
117
+ cnt = 0
118
+ @weapon.when_receiving(:attack).with(ninja) do |*args|
119
+ cnt += 1
120
+ end
121
+ @ninja.attack ninja, @weapon
122
+
123
+ cnt.should == 1
124
+ end
125
+ end
126
+
127
+
128
+ end
129
+
130
+ describe "Ruby to Ruby interactions" do
131
+
132
+ it "should execute a callback when an expectation is being invoked and with is not defined in a block" do
133
+ iso = Caricature::Isolation.for(Dagger)
134
+ cnt = 0
135
+ iso.when_receiving(:damage).with(:any) do |*args|
136
+ cnt += 1
137
+ end
138
+ iso.damage
139
+ cnt.should == 1
140
+ end
141
+
142
+ it "should execute a callback when an expectation is being invoked and with is defined in a block" do
143
+ cnt = 0
144
+ iso = Caricature::Isolation.for(Dagger)
145
+ iso.when_receiving(:damage) do |exp|
146
+ exp.with(:any) do |*args|
147
+ cnt += 1
148
+ end
149
+ end
150
+ iso.damage
151
+ cnt.should == 1
152
+ end
153
+ end
154
+
155
+
156
+
157
157
  end