caricature 0.2.0 → 0.3.0
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.
- data/README +8 -8
- data/README.markdown +9 -4
- data/spec/integration_spec.rb +142 -0
- data/spec/interop_spec.rb +1 -1
- data/spec/isolation_spec.rb +30 -40
- data/spec/{proxy_spec.rb → isolator_spec.rb} +11 -17
- metadata +4 -3
data/README
CHANGED
@@ -2,28 +2,28 @@ Caricature - Bringing simple mocking to the DLR
|
|
2
2
|
===============================================
|
3
3
|
|
4
4
|
This project aims to make interop between IronRuby objects and .NET objects easier.
|
5
|
-
The idea is that it integrates nicely with bacon and later rspec and that it transparently lets you
|
5
|
+
The idea is that it integrates nicely with bacon and later rspec and that it transparently lets you isolate ironruby ojbects
|
6
6
|
as well as CLR objects/interfaces.
|
7
7
|
Caricature handles interfaces, interface inheritance, CLR objects, CLR object instances, Ruby classes and instances of Ruby classes.
|
8
8
|
|
9
9
|
From the start I wanted to do away with names like mock, stub, record, replay, verify etc.
|
10
10
|
Instead I took the advice from Roy Osherhove and went with a name of Isolation for creating a mock.
|
11
11
|
|
12
|
-
An Isolation will create what in Rhino.Mocks would be called a DynamicMock (but can be a partial too
|
12
|
+
An Isolation will create what in Rhino.Mocks would be called a DynamicMock (but can be a partial too :) )
|
13
13
|
In Moq it would be the Loose mocking strategy.
|
14
14
|
|
15
|
-
The naming of the methods for creating
|
16
|
-
To specify a stub/expectation on
|
15
|
+
The naming of the methods for creating isolations is the one that JP Boodhoo proposed WhenToldTo and WasToldTo.
|
16
|
+
To specify a stub/expectation on an isolation you have one and only one way of doing that with the method called when_told_to.
|
17
17
|
Then only if you're interested in asserting if a method has been called you can use the was_told_to? method for this.
|
18
18
|
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
isolation = Isolation.for(Ninja)
|
21
|
+
isolation.when_told_to(:attack) do |exp|
|
22
22
|
exp.with(:shuriken)
|
23
23
|
exp.return(3)
|
24
24
|
end
|
25
25
|
|
26
|
-
Battle.new(
|
26
|
+
Battle.new(isolation)
|
27
27
|
battle.combat
|
28
28
|
|
29
|
-
|
29
|
+
isolation.was_told_to?(:attack).should.be.true?
|
data/README.markdown
CHANGED
@@ -13,12 +13,12 @@ An Isolation will create what in Rhino.Mocks would be called a DynamicMock (but
|
|
13
13
|
In Moq it would be the Loose mocking strategy.
|
14
14
|
|
15
15
|
The naming of the methods for creating mocks is the one that JP Boodhoo proposed WhenToldTo and WasToldTo.
|
16
|
-
To specify a stub/expectation on
|
16
|
+
To specify a stub/expectation on an isolation you have one and only one way of doing that with the method called when_told_to.
|
17
17
|
Then only if you're interested in asserting if a method has been called you can use the was_told_to? method for this.
|
18
18
|
|
19
19
|
<pre>
|
20
|
-
|
21
|
-
|
20
|
+
isolation = Isolation.for(Ninja)
|
21
|
+
isolation.when_told_to(:attack) do |exp|
|
22
22
|
exp.with(:shuriken)
|
23
23
|
exp.return(3)
|
24
24
|
end
|
@@ -26,9 +26,14 @@ end
|
|
26
26
|
Battle.new(mock)
|
27
27
|
battle.combat
|
28
28
|
|
29
|
-
|
29
|
+
isolation.was_told_to?(:attack).should.be.true?
|
30
30
|
</pre>
|
31
31
|
|
32
|
+
It may be very important to note that when you're going to be isolating CLR classes to be used in other CLR classes
|
33
|
+
you still need to obide by the CLR rules. That means if you want to redefine a method you'll need to make sure it's
|
34
|
+
marked as virtual. Static types are still governed by static type rules. I'm working on a solution around those
|
35
|
+
problems but for the time being this is how it has to be.
|
36
|
+
|
32
37
|
License:
|
33
38
|
--------
|
34
39
|
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/bacon_helper"
|
2
|
+
|
3
|
+
describe "Full scenarios" do
|
4
|
+
|
5
|
+
describe "when isolating CLR interfaces" do
|
6
|
+
before do
|
7
|
+
@ninja = ClrModels::Ninja.new
|
8
|
+
@weapon = Caricature::Isolation.for(ClrModels::IWeapon)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should work without expectations" do
|
12
|
+
@ninja.attack ClrModels::Ninja.new, @weapon
|
13
|
+
|
14
|
+
@weapon.was_told_to?(:attack).should.be.successful
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should work with an expectation with any arguments" do
|
18
|
+
@weapon.when_told_to(:damage).return(5)
|
19
|
+
|
20
|
+
@ninja.is_killed_by(@weapon).should.be.true?
|
21
|
+
@weapon.was_told_to?(:damage).should.be.successful
|
22
|
+
end
|
23
|
+
|
24
|
+
# it "should work with an expectation with any arguments" do
|
25
|
+
# @weapon.when_told_to(:damage).return(5)
|
26
|
+
#
|
27
|
+
# @ninja.is_killed_by(@weapon).should.be.true?
|
28
|
+
# @weapon.was_told_to?(:damage).should.be.successful
|
29
|
+
# end
|
30
|
+
|
31
|
+
it "should work with an expectation getting different method call result" do
|
32
|
+
@weapon.when_told_to(:damage).return(2)
|
33
|
+
|
34
|
+
@ninja.is_killed_by(@weapon).should.be.false?
|
35
|
+
@weapon.was_told_to?(:damage).should.be.successful
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should work for an assertion on a specific argument" do
|
39
|
+
@weapon.when_told_to(:damage).return(2)
|
40
|
+
|
41
|
+
@ninja.is_killed_by(@weapon).should.be.false?
|
42
|
+
@weapon.was_told_to?(:damage).should.be.successful
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "when isolating CLR classes" do
|
48
|
+
|
49
|
+
before do
|
50
|
+
@weapon = ClrModels::Sword.new
|
51
|
+
@ninja = Caricature::Isolation.for(ClrModels::Ninja)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should work without expectations" do
|
55
|
+
result = @weapon.attack @ninja
|
56
|
+
result.should.equal 0
|
57
|
+
|
58
|
+
@ninja.was_told_to?(:survive_attack_with).with(@weapon).should.be.successful
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should work with an expectation for any arguments" do
|
62
|
+
@ninja.when_told_to(:survive_attack_with).return(5)
|
63
|
+
|
64
|
+
result = @weapon.attack @ninja
|
65
|
+
result.should.equal 5
|
66
|
+
|
67
|
+
@ninja.was_told_to?(:survive_attack_with).with(:any).should.be.successful
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should work with an assertion for specific arguments" do
|
71
|
+
@ninja.when_told_to(:survive_attack_with) do |method_should|
|
72
|
+
method_should.return(5)
|
73
|
+
end
|
74
|
+
|
75
|
+
result = @weapon.attack @ninja
|
76
|
+
result.should.equal 5
|
77
|
+
|
78
|
+
@ninja.was_told_to?(:survive_attack_with).with(@weapon).should.be.successful
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should fail for an assertion with wrong arguments" do
|
82
|
+
@ninja.when_told_to(:survive_attack_with) do |method_should|
|
83
|
+
method_should.return(5)
|
84
|
+
end
|
85
|
+
|
86
|
+
result = @weapon.attack @ninja
|
87
|
+
result.should.equal 5
|
88
|
+
|
89
|
+
@ninja.
|
90
|
+
was_told_to?(:survive_attack_with).
|
91
|
+
with(Caricature::Isolation.for(ClrModels::IWeapon)).
|
92
|
+
should.not.be.successful
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "when isolating CLR instances" do
|
98
|
+
|
99
|
+
before do
|
100
|
+
@weapon = ClrModels::Sword.new
|
101
|
+
@ninja = Caricature::Isolation.for(ClrModels::Ninja.new)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should work without expectations" do
|
105
|
+
result = @weapon.attack @ninja
|
106
|
+
result.should.equal 0
|
107
|
+
|
108
|
+
@ninja.was_told_to?(:survive_attack_with).with(@weapon).should.be.successful
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should work with an expectation for any arguments" do
|
112
|
+
@ninja.when_told_to(:survive_attack_with).return(5)
|
113
|
+
|
114
|
+
result = @weapon.attack @ninja
|
115
|
+
result.should.equal 5
|
116
|
+
|
117
|
+
@ninja.was_told_to?(:survive_attack_with).with(:any).should.be.successful
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should fail for an assertion for specific arguments" do
|
121
|
+
@ninja.when_told_to(:survive_attack_with) do |method_should|
|
122
|
+
method_should.return(5)
|
123
|
+
end
|
124
|
+
|
125
|
+
result = @weapon.attack @ninja
|
126
|
+
result.should.equal 5
|
127
|
+
var = @ninja.was_told_to?(:survive_attack_with).with(:any)
|
128
|
+
@ninja.was_told_to?(:survive_attack_with).with(@weapon).should.be.successful
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should allow to delegate the method call to the real instance (partial mock)" do
|
132
|
+
@ninja.when_told_to(:survive_attack_with).super_after
|
133
|
+
|
134
|
+
result = @weapon.attack @ninja
|
135
|
+
result.should.equal 6
|
136
|
+
|
137
|
+
@ninja.was_told_to?(:survive_attack_with).should.be.successful
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
data/spec/interop_spec.rb
CHANGED
@@ -30,7 +30,7 @@ describe "Event handling" do
|
|
30
30
|
describe "for an IR generated interface proxy" do
|
31
31
|
|
32
32
|
before do
|
33
|
-
@proxy = Caricature::
|
33
|
+
@proxy = Caricature::ClrIsolator.new ClrModels::IExposingWarrior
|
34
34
|
end
|
35
35
|
|
36
36
|
# apparently events don't work yet in IronRuby.. keeping this spec here to find out when it does
|
data/spec/isolation_spec.rb
CHANGED
@@ -59,16 +59,12 @@ describe "Caricature::Isolation" do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should create a proxy" do
|
62
|
-
@isolator.
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should create the Ruby objects proxy" do
|
66
|
-
@isolator.proxy.is_clr_proxy?.should.be.false?
|
62
|
+
@isolator.should.not.be == nil
|
67
63
|
end
|
68
64
|
|
69
65
|
describe "when asked to stub a method" do
|
70
66
|
|
71
|
-
it "should create an expectation" do
|
67
|
+
it "should create an expectation with a block" do
|
72
68
|
nm = "What's in a name"
|
73
69
|
expectation = @isolator.when_told_to(:name) do |cl|
|
74
70
|
cl.return(nm)
|
@@ -77,41 +73,18 @@ describe "Caricature::Isolation" do
|
|
77
73
|
expectation.has_return_value?.should.be.true?
|
78
74
|
expectation.return_value.should.equal nm
|
79
75
|
end
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
83
76
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should be successful with any arguments allowed" do
|
94
|
-
iso = Caricature::Isolation.new(@proxy, @rec)
|
95
|
-
iso.was_told_to?(:my_method).should.be.true?
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should be successful with a correct set of arguments provided for my_method" do
|
99
|
-
iso = Caricature::Isolation.new(@proxy, @rec)
|
100
|
-
iso.was_told_to?(:my_method){ |ver| ver.with(1, 2, 3) }.should.be.true?
|
101
|
-
end
|
102
|
-
|
103
|
-
it "should be unsuccessful when a wrong set of arguments is provided" do
|
104
|
-
iso = Caricature::Isolation.new(@proxy, @rec)
|
105
|
-
iso.was_told_to?(:my_method){|ver| ver.with(1, 3, 6) }.should.be.false?
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should be unsuccessful when the wrong method name is provided" do
|
109
|
-
iso = Caricature::Isolation.new(@proxy, @rec)
|
110
|
-
iso.was_told_to?(:some_method).should.be.false?
|
77
|
+
it "should create an expectation without a block" do
|
78
|
+
nm = "What's in a name"
|
79
|
+
expectation = @isolator.when_told_to(:name).return(nm)
|
80
|
+
expectation.method_name.should.equal :name
|
81
|
+
expectation.has_return_value?.should.be.true?
|
82
|
+
expectation.return_value.should.equal nm
|
83
|
+
end
|
111
84
|
end
|
112
85
|
|
113
86
|
end
|
114
|
-
|
87
|
+
|
115
88
|
describe "when creating an isolation for CLR objects" do
|
116
89
|
|
117
90
|
it "should not raise" do
|
@@ -127,11 +100,28 @@ describe "Caricature::Isolation" do
|
|
127
100
|
end
|
128
101
|
|
129
102
|
it "should create a proxy" do
|
130
|
-
@isolator.
|
103
|
+
@isolator.should.not.be == nil
|
131
104
|
end
|
132
105
|
|
133
|
-
|
134
|
-
|
106
|
+
describe "when asked to stub a method" do
|
107
|
+
|
108
|
+
it "should create an expectation with a block" do
|
109
|
+
nm = "What's in a name"
|
110
|
+
expectation = @isolator.when_told_to(:name) do |cl|
|
111
|
+
cl.return(nm)
|
112
|
+
end
|
113
|
+
expectation.method_name.should.equal :name
|
114
|
+
expectation.has_return_value?.should.be.true?
|
115
|
+
expectation.return_value.should.equal nm
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should create an expectation without a block" do
|
119
|
+
nm = "What's in a name"
|
120
|
+
expectation = @isolator.when_told_to(:name).return(nm)
|
121
|
+
expectation.method_name.should.equal :name
|
122
|
+
expectation.has_return_value?.should.be.true?
|
123
|
+
expectation.return_value.should.equal nm
|
124
|
+
end
|
135
125
|
end
|
136
126
|
|
137
127
|
end
|
@@ -1,21 +1,14 @@
|
|
1
1
|
require File.dirname(__FILE__) + "/bacon_helper"
|
2
2
|
|
3
|
-
describe "Caricature::
|
3
|
+
describe "Caricature::RubyIsolator" do
|
4
4
|
|
5
5
|
before do
|
6
6
|
@subj = Soldier.new
|
7
7
|
@recorder = Caricature::MethodCallRecorder.new
|
8
|
-
@proxy = Caricature::
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should forward existing methods" do
|
12
|
-
@proxy.___super___.name.should.equal @subj.name
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should call to_s on the proxied object" do
|
16
|
-
@proxy.___super___.to_s.should.equal @subj.to_s
|
8
|
+
@proxy = Caricature::RubyIsolator.for(@subj, @recorder)
|
17
9
|
end
|
18
10
|
|
11
|
+
|
19
12
|
describe "when invoking a method" do
|
20
13
|
|
21
14
|
before do
|
@@ -48,7 +41,7 @@ describe "Caricature::RecordingClrProxy" do
|
|
48
41
|
@samurai = ClrModels::Samurai.new
|
49
42
|
@samurai.name = "Nakiro"
|
50
43
|
@recorder = Caricature::MethodCallRecorder.new
|
51
|
-
@proxy = Caricature::
|
44
|
+
@proxy = Caricature::ClrIsolator.for(@samurai, @recorder)
|
52
45
|
end
|
53
46
|
|
54
47
|
it "should create a proxy" do
|
@@ -86,12 +79,13 @@ describe "Caricature::RecordingClrProxy" do
|
|
86
79
|
|
87
80
|
before do
|
88
81
|
@recorder = Caricature::MethodCallRecorder.new
|
89
|
-
@proxy = Caricature::
|
82
|
+
@proxy = Caricature::ClrIsolator.for(ClrModels::Ninja, @recorder)
|
90
83
|
end
|
91
84
|
|
92
85
|
it "should create a proxy" do
|
93
|
-
|
94
|
-
@proxy.___super___.
|
86
|
+
|
87
|
+
@proxy.___super___.class.superclass.should.equal ClrModels::Ninja
|
88
|
+
|
95
89
|
end
|
96
90
|
|
97
91
|
|
@@ -119,7 +113,7 @@ describe "Caricature::RecordingClrProxy" do
|
|
119
113
|
|
120
114
|
before do
|
121
115
|
@recorder = Caricature::MethodCallRecorder.new
|
122
|
-
@proxy = Caricature::
|
116
|
+
@proxy = Caricature::ClrInterfaceIsolator.for(ClrModels::IWarrior, @recorder)
|
123
117
|
end
|
124
118
|
|
125
119
|
it "should create a proxy" do
|
@@ -163,7 +157,7 @@ describe "Caricature::RecordingClrProxy" do
|
|
163
157
|
|
164
158
|
before do
|
165
159
|
@recorder = Caricature::MethodCallRecorder.new
|
166
|
-
@proxy = Caricature::
|
160
|
+
@proxy = Caricature::ClrInterfaceIsolator.for(ClrModels::IExposing, @recorder)
|
167
161
|
end
|
168
162
|
|
169
163
|
it "should create an add method for the event" do
|
@@ -179,7 +173,7 @@ describe "Caricature::RecordingClrProxy" do
|
|
179
173
|
describe "for CLR interface recursion" do
|
180
174
|
|
181
175
|
before do
|
182
|
-
@proxy = Caricature::
|
176
|
+
@proxy = Caricature::ClrInterfaceIsolator.for(ClrModels::IExposingWarrior, Caricature::MethodCallRecorder.new)
|
183
177
|
end
|
184
178
|
|
185
179
|
it "should create a method defined on the CLR interface" do
|
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.
|
4
|
+
version: 0.3.0
|
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-05-
|
12
|
+
date: 2009-05-15 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -57,8 +57,9 @@ test_files:
|
|
57
57
|
- spec/bacon_helper.rb
|
58
58
|
- spec/core_ext_spec.rb
|
59
59
|
- spec/expectation_spec.rb
|
60
|
+
- spec/integration_spec.rb
|
60
61
|
- spec/interop_spec.rb
|
61
62
|
- spec/isolation_spec.rb
|
63
|
+
- spec/isolator_spec.rb
|
62
64
|
- spec/method_call_spec.rb
|
63
|
-
- spec/proxy_spec.rb
|
64
65
|
- spec/verification_spec.rb
|