caricature 0.1.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 ADDED
@@ -0,0 +1,29 @@
1
+ Caricature - Bringing simple mocking to the DLR
2
+ ===============================================
3
+
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 mock ironruby ojbects
6
+ as well as CLR objects/interfaces.
7
+ Caricature handles interfaces, interface inheritance, CLR objects, CLR object instances, Ruby classes and instances of Ruby classes.
8
+
9
+ From the start I wanted to do away with names like mock, stub, record, replay, verify etc.
10
+ Instead I took the advice from Roy Osherhove and went with a name of Isolation for creating a mock.
11
+
12
+ An Isolation will create what in Rhino.Mocks would be called a DynamicMock (but can be a partial too) :)
13
+ In Moq it would be the Loose mocking strategy.
14
+
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 a mock you have one and only one way of doing that with the method called when_told_to.
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
+
19
+
20
+ mock = Isolation.for(Ninja)
21
+ mock.when_told_to(:attack) do |exp|
22
+ exp.with(:shuriken)
23
+ exp.return(3)
24
+ end
25
+
26
+ Battle.new(mock)
27
+ battle.combat
28
+
29
+ mock.was_told_to?(:attack).should.be.true?
data/README.markdown ADDED
@@ -0,0 +1,30 @@
1
+ Caricature - Bringing simple mocking to the DLR
2
+ ===============================================
3
+
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 mock ironruby ojbects
6
+ as well as CLR objects/interfaces.
7
+ Caricature handles interfaces, interface inheritance, CLR objects, CLR object instances, Ruby classes and instances of Ruby classes.
8
+
9
+ From the start I wanted to do away with names like mock, stub, record, replay, verify etc.
10
+ Instead I took the advice from Roy Osherhove and went with a name of Isolation for creating a mock.
11
+
12
+ An Isolation will create what in Rhino.Mocks would be called a DynamicMock (but can be a partial too) :)
13
+ In Moq it would be the Loose mocking strategy.
14
+
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 a mock you have one and only one way of doing that with the method called when_told_to.
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
+
19
+ <pre>
20
+ mock = Isolation.for(Ninja)
21
+ mock.when_told_to(:attack) do |exp|
22
+ exp.with(:shuriken)
23
+ exp.return(3)
24
+ end
25
+
26
+ Battle.new(mock)
27
+ battle.combat
28
+
29
+ mock.was_told_to?(:attack).should.be.true?
30
+ </pre>
@@ -0,0 +1,63 @@
1
+ $: << File.dirname(__FILE__) + "/bin"
2
+ $: << File.dirname(__FILE__) + "/../lib"
3
+
4
+ require "caricature"
5
+ require 'bacon'
6
+ require 'mscorlib'
7
+
8
+
9
+
10
+
11
+ load_assembly 'ClrModels'
12
+
13
+ class Soldier
14
+
15
+ def name
16
+ "Tommy Boy"
17
+ end
18
+
19
+ def to_s
20
+ "I'm a soldier"
21
+ end
22
+
23
+ end
24
+
25
+ module Caricature
26
+
27
+ module InterfaceIncludingModule
28
+ include ClrModels::IWarrior
29
+ end
30
+
31
+ module PureRubyModule
32
+
33
+ end
34
+
35
+ module RubyModuleIncludingModule
36
+ include PureRubyModule
37
+ end
38
+
39
+ module InterfaceUpTheWazoo
40
+ include InterfaceIncludingModule
41
+ end
42
+
43
+ class InterfaceIncludingClass
44
+ include ClrModels::IWarrior
45
+ end
46
+
47
+ class SubClassingClrClass < ClrModels::Ninja
48
+
49
+ end
50
+
51
+ class InterfaceUpTheWazooClass
52
+ include InterfaceUpTheWazoo
53
+ end
54
+
55
+ class SubclassingRubyClass < Soldier
56
+
57
+ end
58
+
59
+ class ModuleIncludingClass
60
+ include RubyModuleIncludingModule
61
+ end
62
+ end
63
+
@@ -0,0 +1,78 @@
1
+ require File.dirname(__FILE__) + "/bacon_helper"
2
+
3
+ describe "String" do
4
+
5
+ it "should underscore a camel cased name" do
6
+ "MockingAndStubbingForIronRuby1".underscore.should.equal "mocking_and_stubbing_for_iron_ruby1"
7
+ end
8
+
9
+ it "should get the class if it exists" do
10
+ "String".classify.should.equal String
11
+ end
12
+
13
+ end
14
+
15
+ describe "Module" do
16
+
17
+ it "should strip the module names" do
18
+ ClrModels::IExposingWarrior.demodulize.should.equal "IExposingWarrior"
19
+ end
20
+
21
+ it "should identify it's not a CLR type for a Ruby defined module" do
22
+ Caricature.is_clr_type?.should.be.false?
23
+ end
24
+
25
+ it "should identify it's a CLR type for a CLR defined interface" do
26
+ ClrModels::IExposingWarrior.is_clr_type?.should.be.true?
27
+ end
28
+
29
+ it "should identify it's a CLR type for a Ruby defined module that includes a CLR interface" do
30
+ Caricature::InterfaceIncludingModule.is_clr_type?.should.be.true?
31
+ end
32
+
33
+ it "should identify it's not a CLR type for a Ruby defined module that includes a Ruby module" do
34
+ Caricature::RubyModuleIncludingModule.is_clr_type?.should.be.false?
35
+ end
36
+
37
+ it "should identify it's a CLR type when an ancestor includes a CLR interface" do
38
+ Caricature::InterfaceUpTheWazoo.is_clr_type?.should.be.true?
39
+ end
40
+
41
+ end
42
+
43
+ describe "Class" do
44
+
45
+ it "should strip the module names" do
46
+ ClrModels::Ninja.demodulize.should.equal "Ninja"
47
+ end
48
+
49
+ it "should identify it's not a CLR type for a ruby defined type" do
50
+ Soldier.is_clr_type?.should.be.false?
51
+ end
52
+
53
+ it "should identify it's not a CLR type for a ruby defined type that subclasses a Ruby class" do
54
+ Caricature::SubclassingRubyClass.is_clr_type?.should.be.false?
55
+ end
56
+
57
+ it "should identify it's not a CLR type for a ruby defined type that includes only ruby modueles in its hierarchy" do
58
+ Caricature::ModuleIncludingClass.is_clr_type?.should.be.false?
59
+ end
60
+
61
+ it "should identify it's a CLR type for a type defined in C#" do
62
+ ClrModels::Ninja.is_clr_type?.should.be.true?
63
+ end
64
+
65
+ it "should identify it's a CLR type for a type defined in Ruby that includes a CLR interface" do
66
+ Caricature::InterfaceIncludingClass.is_clr_type?.should.be.true?
67
+ end
68
+
69
+ it "should identify it's a CLR type for a type defined in Ruby that subclasses a CLR class" do
70
+ Caricature::SubClassingClrClass.is_clr_type?.should.be.true?
71
+ end
72
+
73
+ it "should identify it's a CLR type for a type defined in Ruby that includes a CLR interface in its hierarchy" do
74
+ Caricature::InterfaceUpTheWazooClass.is_clr_type?.should.be.true?
75
+ end
76
+
77
+ end
78
+
@@ -0,0 +1,190 @@
1
+ require File.dirname(__FILE__) + "/bacon_helper"
2
+
3
+ describe "Caricature::ExpectationBuilder" do
4
+
5
+ it "should create an expectation builder" do
6
+ builder = Caricature::ExpectationBuilder.new :some_method, nil
7
+ builder.should.not.equal nil
8
+ end
9
+
10
+ describe "when using all defaults" do
11
+
12
+ before do
13
+ builder = Caricature::ExpectationBuilder.new :some_method, nil
14
+ @expectation = builder.build
15
+ end
16
+
17
+ it "should have the correct method_name" do
18
+ @expectation.method_name.should.equal :some_method
19
+ end
20
+
21
+ it "should have empty args" do
22
+ @expectation.args.should.be.empty?
23
+ end
24
+
25
+ it "should have no super call" do
26
+ @expectation.super.should.equal nil
27
+ end
28
+
29
+ it "should have no error args" do
30
+ @expectation.error_args.should.equal nil
31
+ end
32
+
33
+ it "should have no return_value" do
34
+ @expectation.return_value.should.equal nil
35
+ end
36
+
37
+ end
38
+
39
+ describe "when specifying only arguments" do
40
+
41
+ before do
42
+ builder = Caricature::ExpectationBuilder.new :some_method,nil
43
+ builder.with(1, 2, 3)
44
+ @expectation = builder.build
45
+ end
46
+
47
+ it "should have the correct method_name" do
48
+ @expectation.method_name.should.equal :some_method
49
+ end
50
+
51
+ it "should have empty args" do
52
+ @expectation.args.should.equal [1,2,3]
53
+ end
54
+
55
+ it "should have no super call" do
56
+ @expectation.super.should.equal nil
57
+ end
58
+
59
+ it "should have no error args" do
60
+ @expectation.error_args.should.equal nil
61
+ end
62
+
63
+ it "should have no return_value" do
64
+ @expectation.return_value.should.equal nil
65
+ end
66
+
67
+ end
68
+
69
+ describe "when specifying only a block for the return value" do
70
+
71
+ before do
72
+ builder = Caricature::ExpectationBuilder.new :some_method, nil
73
+ builder.return { 5 }
74
+ @expectation = builder.build
75
+ end
76
+
77
+ it "should have the correct method_name" do
78
+ @expectation.method_name.should.equal :some_method
79
+ end
80
+
81
+ it "should have empty args" do
82
+ @expectation.args.should.be.empty?
83
+ end
84
+
85
+ it "should have no super call" do
86
+ @expectation.super.should.equal nil
87
+ end
88
+
89
+ it "should have no error args" do
90
+ @expectation.error_args.should.equal nil
91
+ end
92
+
93
+ it "should have the correct return_value" do
94
+ @expectation.return_value.should.equal 5
95
+ end
96
+
97
+ end
98
+
99
+ describe "when specifying a return value" do
100
+
101
+ before do
102
+ builder = Caricature::ExpectationBuilder.new :some_method, nil
103
+ builder.return 5
104
+ @expectation = builder.build
105
+ end
106
+
107
+ it "should have the correct method_name" do
108
+ @expectation.method_name.should.equal :some_method
109
+ end
110
+
111
+ it "should have empty args" do
112
+ @expectation.args.should.be.empty?
113
+ end
114
+
115
+ it "should have no super call" do
116
+ @expectation.super.should.equal nil
117
+ end
118
+
119
+ it "should have no error args" do
120
+ @expectation.error_args.should.equal nil
121
+ end
122
+
123
+ it "should have the correct return_value" do
124
+ @expectation.return_value.should.equal 5
125
+ end
126
+
127
+ end
128
+
129
+ describe "when specifying a raise arguments" do
130
+
131
+ before do
132
+ @msg = "Hold on, that wasn't supposed to happen"
133
+ builder = Caricature::ExpectationBuilder.new :some_method, nil
134
+ builder.raise @msg
135
+ @expectation = builder.build
136
+ end
137
+
138
+ it "should have the correct method_name" do
139
+ @expectation.method_name.should.equal :some_method
140
+ end
141
+
142
+ it "should have empty args" do
143
+ @expectation.args.should.be.empty?
144
+ end
145
+
146
+ it "should have no super call" do
147
+ @expectation.super.should.equal nil
148
+ end
149
+
150
+ it "should have no error args" do
151
+ @expectation.error_args.should.equal [@msg]
152
+ end
153
+
154
+ it "should have the correct return_value" do
155
+ @expectation.return_value.should.equal nil
156
+ end
157
+
158
+ end
159
+
160
+ describe "when specifying a return value and telling you want a call to super before" do
161
+
162
+ before do
163
+ builder = Caricature::ExpectationBuilder.new :some_method, nil
164
+ builder.return(5).super_before
165
+ @expectation = builder.build
166
+ end
167
+
168
+ it "should have the correct method_name" do
169
+ @expectation.method_name.should.equal :some_method
170
+ end
171
+
172
+ it "should have empty args" do
173
+ @expectation.args.should.be.empty?
174
+ end
175
+
176
+ it "should have no super call" do
177
+ @expectation.super.should.equal :before
178
+ end
179
+
180
+ it "should have no error args" do
181
+ @expectation.error_args.should.equal nil
182
+ end
183
+
184
+ it "should have the correct return_value" do
185
+ @expectation.return_value.should.equal 5
186
+ end
187
+
188
+ end
189
+
190
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + "/bacon_helper"
2
+
3
+ describe "Event handling" do
4
+
5
+
6
+
7
+ describe "for precompiled CLR classes" do
8
+
9
+ before do
10
+ @warrior = ClrModels::ExposingWarrior.new
11
+ end
12
+
13
+ it "should subscribe to an event" do
14
+ ClrModels::ExposedChangedSubscriber.new(@warrior)
15
+ @warrior.has_event_subscriptions.should.be.true?
16
+ end
17
+
18
+ it "should not raise an error when subcribing to an event" do
19
+ lambda { ClrModels::ExposedChangedSubscriber.new(@warrior) }.should.not.raise
20
+ end
21
+
22
+ it "should handle an event when raised" do
23
+ subscriber = ClrModels::ExposedChangedSubscriber.new(@warrior)
24
+ @warrior.change_is_exposed
25
+ subscriber.counter.should.equal 1
26
+ end
27
+
28
+ end
29
+
30
+ describe "for an IR generated interface proxy" do
31
+
32
+ before do
33
+ @proxy = Caricature::RecordingClrProxy.new ClrModels::IExposingWarrior
34
+ end
35
+
36
+ # apparently events don't work yet in IronRuby.. keeping this spec here to find out when it does
37
+ # it "should not raise an error when subcribing to an event" do
38
+ # lambda { ClrModels::ExposedChangedSubscriber.new(@proxy.subject) }.should.not.raise
39
+ # end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,139 @@
1
+ # Caricature -- A simple mocking framework for IronRuby
2
+ #
3
+ # Copyright (c) 2009, Caricature Team
4
+ # http://github.com/casualjim/caricature
5
+ # All rights reserved.
6
+ #
7
+ # Redistribution and use in source and binary forms,
8
+ # with or without modification, are permitted provided
9
+ # that the following conditions are met:
10
+ #
11
+ # * Redistributions of source code must retain the
12
+ # above copyright notice, this list of conditions and
13
+ # the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce
16
+ # the above copyright notice, this list of conditions
17
+ # and the following disclaimer in the documentation
18
+ # and/or other materials provided with the distribution.
19
+ #
20
+ # * Neither the name of the Caricature Team nor the
21
+ # names of its contributors may be used to endorse
22
+ # or promote products derived from this software
23
+ # without specific prior written permission.
24
+ #
25
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26
+ # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27
+ # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28
+ # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35
+ # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
+ # SUCH DAMAGE.
39
+ #
40
+ # [This is the BSD license, see
41
+ # http://www.opensource.org/licenses/bsd-license.php]
42
+
43
+ require File.dirname(__FILE__) + "/bacon_helper"
44
+
45
+ describe "Caricature::Isolation" do
46
+
47
+ describe "when creating an isolation for ruby objects" do
48
+
49
+ it "should not raise" do
50
+ lambda { Caricature::Isolation.for(Soldier) }.should.not.raise
51
+ end
52
+
53
+ end
54
+
55
+ describe "after creation of the isolation for a ruby object" do
56
+
57
+ before do
58
+ @isolator = Caricature::Isolation.for(Soldier)
59
+ end
60
+
61
+ it "should create a proxy" do
62
+ @isolator.proxy.should.not.be == nil
63
+ end
64
+
65
+ it "should create the Ruby objects proxy" do
66
+ @isolator.proxy.is_clr_proxy?.should.be.false?
67
+ end
68
+
69
+ describe "when asked to stub a method" do
70
+
71
+ it "should create an expectation" do
72
+ nm = "What's in a name"
73
+ expectation = @isolator.when_told_to(:name) do |cl|
74
+ cl.return(nm)
75
+ end
76
+ expectation.method_name.should.equal :name
77
+ expectation.has_return_value?.should.be.true?
78
+ expectation.return_value.should.equal nm
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ describe 'when verifying if a call was made' do
85
+
86
+ before do
87
+ @rec = Caricature::MethodCallRecorder.new
88
+ @rec.record_call :my_method
89
+ @rec.record_call :my_method, 1, 2, 3
90
+ @proxy = Caricature::RecordingProxy.new(Soldier, @rec)
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?
111
+ end
112
+
113
+ end
114
+
115
+ describe "when creating an isolation for CLR objects" do
116
+
117
+ it "should not raise" do
118
+ lambda { Caricature::Isolation.for(ClrModels::Ninja) }.should.not.raise
119
+ end
120
+
121
+ end
122
+
123
+ describe "after creation of the isolation for a CLR object" do
124
+
125
+ before do
126
+ @isolator = Caricature::Isolation.for(ClrModels::Ninja)
127
+ end
128
+
129
+ it "should create a proxy" do
130
+ @isolator.proxy.should.not.be == nil
131
+ end
132
+
133
+ it "should create the CLR objects proxy" do
134
+ @isolator.proxy.is_clr_proxy?.should.be.true?
135
+ end
136
+
137
+ end
138
+
139
+ end
@@ -0,0 +1,295 @@
1
+ require File.dirname(__FILE__) + "/bacon_helper"
2
+
3
+ describe "MethodCallRecorder" do
4
+
5
+ before do
6
+ @recorder = Caricature::MethodCallRecorder.new
7
+ end
8
+
9
+ describe "when recording a call without arguments" do
10
+
11
+ before do
12
+ @recorder.record_call :my_method
13
+ end
14
+
15
+ it "should have 1 method call" do
16
+ @recorder.method_calls.size.should.equal 1
17
+ end
18
+
19
+ describe "recorded call" do
20
+
21
+ before do
22
+ @mc = @recorder.method_calls[:my_method]
23
+ end
24
+
25
+ it "should have a method call collected" do
26
+ @mc.should.not.equal nil
27
+ end
28
+
29
+ it "should have the correct name" do
30
+ @mc.method_name.should.equal :my_method
31
+ end
32
+
33
+ it "should have no arguments" do
34
+ @mc.args.should.equal [Caricature::ArgumentRecording.new]
35
+ end
36
+
37
+ it "should have no block" do
38
+ @mc.block.should.equal nil
39
+ end
40
+
41
+ it "should have a count a 1" do
42
+ @mc.count.should.equal 1
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ describe "when recording a call without arguments but with a block" do
50
+
51
+ before do
52
+ @block_content = "I'm in the block"
53
+ @recorder.record_call :my_method do
54
+ @block_content
55
+ end
56
+ end
57
+
58
+ it "should have 1 method call" do
59
+ @recorder.method_calls.size.should.equal 1
60
+ end
61
+
62
+ describe "recorded call" do
63
+
64
+ before do
65
+ @mc = @recorder.method_calls[:my_method]
66
+ end
67
+
68
+ it "should have a method call collected" do
69
+ @mc.should.not.equal nil
70
+ end
71
+
72
+ it "should have the correct name" do
73
+ @mc.method_name.should.equal :my_method
74
+ end
75
+
76
+ it "should have no arguments" do
77
+ @mc.args.should.equal [Caricature::ArgumentRecording.new]
78
+ end
79
+
80
+ it "should have a block" do
81
+ @mc.args.first.block.should.not.equal nil
82
+ end
83
+
84
+ it "should have the correct block" do
85
+ @mc.args.first.block.call.should.equal @block_content
86
+ end
87
+
88
+ it "should have a count a 1" do
89
+ @mc.count.should.equal 1
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
96
+ describe "when recording a call 1 argument" do
97
+
98
+ before do
99
+ @recorder.record_call :my_method, 1
100
+ end
101
+
102
+ it "should have 1 method call" do
103
+ @recorder.method_calls.size.should.equal 1
104
+ end
105
+
106
+ describe "recorded call" do
107
+
108
+ before do
109
+ @mc = @recorder.method_calls[:my_method]
110
+ end
111
+
112
+ it "should have a method call collected" do
113
+ @mc.should.not.equal nil
114
+ end
115
+
116
+ it "should have the correct name" do
117
+ @mc.method_name.should.equal :my_method
118
+ end
119
+
120
+ it "should have 1 argument" do
121
+ @mc.args.size.should.equal 1
122
+ end
123
+
124
+ it "should have the correct argument" do
125
+ @mc.args.should.equal [Caricature::ArgumentRecording.new([1])]
126
+ end
127
+
128
+ it "should have no block" do
129
+ @mc.block.should.equal nil
130
+ end
131
+
132
+ it "should have a count a 1" do
133
+ @mc.count.should.equal 1
134
+ end
135
+
136
+ end
137
+
138
+ end
139
+
140
+ describe "when recording a call 2 arguments" do
141
+
142
+ before do
143
+ @recorder.record_call :my_method, 1, 2
144
+ end
145
+
146
+ it "should have 1 method call" do
147
+ @recorder.method_calls.size.should.equal 1
148
+ end
149
+
150
+ describe "recorded call" do
151
+
152
+ before do
153
+ @mc = @recorder.method_calls[:my_method]
154
+ end
155
+
156
+ it "should have a method call collected" do
157
+ @mc.should.not.equal nil
158
+ end
159
+
160
+ it "should have the correct name" do
161
+ @mc.method_name.should.equal :my_method
162
+ end
163
+
164
+ it "should have 1 argument recording" do
165
+ @mc.args.size.should.equal 1
166
+ end
167
+
168
+ it "should have the correct arguments" do
169
+ @mc.args.should.equal [Caricature::ArgumentRecording.new([1, 2])]
170
+ end
171
+
172
+ it "should have no block" do
173
+ @mc.block.should.equal nil
174
+ end
175
+
176
+ it "should have a count a 1" do
177
+ @mc.count.should.equal 1
178
+ end
179
+
180
+ end
181
+
182
+ end
183
+
184
+ describe "when recording 2 calls with no arguments" do
185
+
186
+ before do
187
+ @recorder.record_call :my_method
188
+ @recorder.record_call :my_method
189
+ end
190
+
191
+ it "should have 1 method call" do
192
+ @recorder.method_calls.size.should.equal 1
193
+ end
194
+
195
+ describe "recorded call" do
196
+
197
+ before do
198
+ @mc = @recorder.method_calls[:my_method]
199
+ end
200
+
201
+ it "should have a method call collected" do
202
+ @mc.should.not.equal nil
203
+ end
204
+
205
+ it "should have the correct name" do
206
+ @mc.method_name.should.equal :my_method
207
+ end
208
+
209
+ it "should have no arguments" do
210
+ @mc.args.should.equal [Caricature::ArgumentRecording.new]
211
+ end
212
+
213
+ it "should have no block" do
214
+ @mc.block.should.equal nil
215
+ end
216
+
217
+ it "should have a count a 1" do
218
+ @mc.count.should.equal 2
219
+ end
220
+
221
+ end
222
+
223
+ end
224
+
225
+ describe "when recording 2 calls with different arguments" do
226
+
227
+ before do
228
+ @recorder.record_call :my_method
229
+ @recorder.record_call :my_method, 1, 3, 4
230
+ end
231
+
232
+ it "should have 1 method call" do
233
+ @recorder.method_calls.size.should.equal 1
234
+ end
235
+
236
+ describe "recorded call" do
237
+
238
+ before do
239
+ @mc = @recorder.method_calls[:my_method]
240
+ end
241
+
242
+ it "should have a method call collected" do
243
+ @mc.should.not.equal nil
244
+ end
245
+
246
+ it "should have the correct name" do
247
+ @mc.method_name.should.equal :my_method
248
+ end
249
+
250
+ it "should have argument variations" do
251
+ @mc.has_argument_variations?.should.be.true?
252
+ end
253
+
254
+ it "should have no block" do
255
+ @mc.block.should.equal nil
256
+ end
257
+
258
+ it "should have a count a 1" do
259
+ @mc.count.should.equal 2
260
+ end
261
+
262
+ end
263
+
264
+ end
265
+
266
+ describe "when asked if a certain method was called" do
267
+
268
+ before do
269
+ @recorder.record_call :my_method
270
+ @recorder.record_call :my_method, 1, 3, 4
271
+ end
272
+
273
+ it "should confirm when we don't care about the arguments" do
274
+ @recorder.was_called?(:my_method, :any).should.be.true?
275
+ end
276
+
277
+ it "should confirm when there are no argument variations" do
278
+ @recorder.record_call :another_method
279
+ @recorder.was_called?(:another_method, :any).should.be.true?
280
+ end
281
+
282
+ it "should be negative when we provide the wrong arguments" do
283
+ @recorder.was_called?(:my_method, 1, 2, 5).should.be.false?
284
+ end
285
+
286
+ it "should be positive when we provide the correct arguments" do
287
+ @recorder.was_called?(:my_method, 1, 3, 4).should.be.true?
288
+ end
289
+
290
+ it "should be positive when we provide no arguments and a call had been recorded without arguments" do
291
+ @recorder.was_called?(:my_method).should.be.true?
292
+ end
293
+ end
294
+
295
+ end
@@ -0,0 +1,206 @@
1
+ require File.dirname(__FILE__) + "/bacon_helper"
2
+
3
+ describe "Caricature::RecordingProxy" do
4
+
5
+ before do
6
+ @subj = Soldier.new
7
+ @recorder = Caricature::MethodCallRecorder.new
8
+ @proxy = Caricature::RecordingProxy.new(@subj, @recorder)
9
+ end
10
+
11
+ it "should forward existing methods" do
12
+ @proxy.name.should.equal @subj.name
13
+ end
14
+
15
+ it "should call to_s on the proxied object" do
16
+ @proxy.to_s.should.equal @subj.to_s
17
+ end
18
+
19
+ describe "when invoking a method" do
20
+
21
+ before do
22
+ @proxy.name
23
+ end
24
+
25
+ it "should record a call" do
26
+ @recorder.size.should.equal 1
27
+ end
28
+
29
+ it "should record the correct call" do
30
+ mc = @recorder[:name]
31
+ mc.method_name.should.equal :name
32
+ mc.args.should.equal [Caricature::ArgumentRecording.new]
33
+ mc.block.should.equal nil
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+ describe "Caricature::RecordingClrProxy" do
40
+
41
+ describe "for an instance of a CLR class" do
42
+
43
+ before do
44
+ @samurai = ClrModels::Samurai.new
45
+ @samurai.name = "Nakiro"
46
+ @recorder = Caricature::MethodCallRecorder.new
47
+ @proxy = Caricature::RecordingClrProxy.new(@samurai, @recorder)
48
+ end
49
+
50
+ it "should create a proxy" do
51
+
52
+ @proxy.name.should.equal @samurai.name
53
+ @proxy.id.should.equal 0
54
+ end
55
+
56
+ describe "when invoking a method" do
57
+
58
+ before do
59
+ @proxy.name
60
+ end
61
+
62
+ it "should record a call" do
63
+ @recorder.size.should.equal 1
64
+ end
65
+
66
+ it "should record the correct call" do
67
+ mc = @recorder[:name]
68
+ mc.method_name.should.equal :name
69
+ mc.args.should.equal [Caricature::ArgumentRecording.new]
70
+ mc.block.should.equal nil
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ describe "for a CLR class" do
78
+
79
+ before do
80
+ @recorder = Caricature::MethodCallRecorder.new
81
+ @proxy = Caricature::RecordingClrProxy.new(ClrModels::Ninja, @recorder)
82
+ end
83
+
84
+ it "should create a proxy" do
85
+ @proxy.___subject___.class.should.equal ClrModels::Ninja
86
+ @proxy.id.should.equal 0
87
+ end
88
+
89
+
90
+ describe "when invoking a method" do
91
+
92
+ before do
93
+ @proxy.name
94
+ end
95
+
96
+ it "should record a call" do
97
+ @recorder.size.should.equal 1
98
+ end
99
+
100
+ it "should record the correct call" do
101
+ mc = @recorder[:name]
102
+ mc.method_name.should.equal :name
103
+ mc.args.should.equal [Caricature::ArgumentRecording.new]
104
+ mc.block.should.equal nil
105
+ end
106
+
107
+ end
108
+ end
109
+
110
+ describe "for a CLR interface" do
111
+
112
+ before do
113
+ @recorder = Caricature::MethodCallRecorder.new
114
+ @proxy = Caricature::RecordingClrProxy.new(ClrModels::IWarrior, @recorder)
115
+ end
116
+
117
+ it "should create a proxy" do
118
+ @proxy.class.to_s.should.match /^IWarrior/
119
+ end
120
+
121
+ it "should create a method on the proxy" do
122
+ @proxy.should.respond_to?(:is_killed_by)
123
+ end
124
+
125
+ it "should create a getter for a property on the proxy" do
126
+ @proxy.should.respond_to?(:id)
127
+ end
128
+
129
+ it "should create a setter for a writable property on the proxy" do
130
+ @proxy.should.respond_to?(:name=)
131
+ end
132
+
133
+ describe "when invoking a method" do
134
+
135
+ before do
136
+ @proxy.name
137
+ end
138
+
139
+ it "should record a call" do
140
+ @recorder.size.should.equal 1
141
+ end
142
+
143
+ it "should record the correct call" do
144
+ mc = @recorder[:name]
145
+ mc.method_name.should.equal :name
146
+ mc.args.should.equal [Caricature::ArgumentRecording.new]
147
+ mc.block.should.equal nil
148
+ end
149
+
150
+ end
151
+
152
+ end
153
+
154
+ describe "for a CLR Interface with an event" do
155
+
156
+ before do
157
+ @recorder = Caricature::MethodCallRecorder.new
158
+ @proxy = Caricature::RecordingClrProxy.new(ClrModels::IExposing, @recorder)
159
+ end
160
+
161
+ it "should create an add method for the event" do
162
+ @proxy.should.respond_to?(:add_is_exposed_changed)
163
+ end
164
+
165
+ it "should create a remove method for the event" do
166
+ @proxy.should.respond_to?(:remove_is_exposed_changed)
167
+ end
168
+
169
+ end
170
+
171
+ describe "for CLR interface recursion" do
172
+
173
+ before do
174
+ @proxy = Caricature::RecordingClrProxy.new(ClrModels::IExposingWarrior, Caricature::MethodCallRecorder.new)
175
+ end
176
+
177
+ it "should create a method defined on the CLR interface" do
178
+ @proxy.should.respond_to?(:own_method)
179
+ end
180
+
181
+ it "should create a method defined on one of the composing interfaces" do
182
+ @proxy.should.respond_to?(:some_method)
183
+ end
184
+
185
+ it "should create a method defined on one of the topmost composing interfaces" do
186
+ @proxy.should.respond_to?(:is_killed_by)
187
+ end
188
+
189
+ it "should create an add method for an event defined on a composing interface" do
190
+ @proxy.should.respond_to?(:add_is_exposed_changed)
191
+ end
192
+
193
+ it "should create a remove method for an event defined on a composing interface" do
194
+ @proxy.should.respond_to?(:remove_is_exposed_changed)
195
+ end
196
+
197
+ it "should create a getter for a property on the proxy" do
198
+ @proxy.should.respond_to?(:id)
199
+ end
200
+
201
+ it "should create a setter for a writable property on the proxy" do
202
+ @proxy.should.respond_to?(:name=)
203
+ end
204
+ end
205
+
206
+ end
@@ -0,0 +1,104 @@
1
+ require File.dirname(__FILE__) + "/bacon_helper"
2
+
3
+ describe "Caricature::Verification" do
4
+
5
+ describe "Matching" do
6
+
7
+ before do
8
+ @rec = Caricature::MethodCallRecorder.new
9
+ @ver = Caricature::Verification.new(:my_method, @rec)
10
+ end
11
+
12
+ describe "Default initialisation" do
13
+
14
+ it "should allow any arguments" do
15
+ @ver.any_args?.should.be.true?
16
+ end
17
+
18
+ it "should match the provided method name when no arguments have been given" do
19
+ @ver.matches?(:my_method).should.be.true?
20
+ end
21
+
22
+ it "should match the method name when arguments have been given" do
23
+ @ver.matches?(:my_method, 1, 3, 4).should.be.true?
24
+ end
25
+
26
+ end
27
+
28
+ describe "when initialized with and constrained by arguments" do
29
+
30
+ before do
31
+ @ver.with(1, 3, 6)
32
+ end
33
+
34
+ it "should match the provided method name when the correct arguments are given" do
35
+ @ver.matches?(:my_method, 1, 3, 6).should.be.true?
36
+ end
37
+
38
+ it "should not match the method name when the arguments are not correct" do
39
+ @ver.matches?(:my_method, 1, 3, 3).should.be.false?
40
+ end
41
+
42
+ it "should not match the method name when no arguments have been given" do
43
+ @ver.matches?(:my_method).should.be.false?
44
+ end
45
+
46
+ end
47
+
48
+ describe "when initialized with and not constrained by arguments" do
49
+
50
+ before do
51
+ @ver.with(1, 3, 6).allow_any_arguments
52
+ end
53
+
54
+ it "should match the provided method name when the correct arguments are given" do
55
+ @ver.matches?(:my_method, 1, 3, 6).should.be.true?
56
+ end
57
+
58
+ it "should match the method name when the arguments are not correct" do
59
+ @ver.matches?(:my_method, 1, 3, 3).should.be.true?
60
+ end
61
+
62
+ it "should match the method name when no arguments have been given" do
63
+ @ver.matches?(:my_method).should.be.true?
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ describe "Verifying" do
71
+
72
+ before do
73
+ @rec = Caricature::MethodCallRecorder.new
74
+ @rec.record_call :my_method
75
+ @rec.record_call :my_method, 1, 2, 3
76
+ @rec.record_call :another_method
77
+
78
+ end
79
+
80
+ it "should be successful with any arguments allowed" do
81
+ ver = Caricature::Verification.new(:my_method, @rec)
82
+ ver.should.be.successful
83
+ end
84
+
85
+ it "should be successful with a correct set of arguments provided for my_method" do
86
+ ver = Caricature::Verification.new(:my_method, @rec)
87
+ ver.with 1, 2, 3
88
+ ver.should.be.successful
89
+ end
90
+
91
+ it "should be unsuccessful when a wrong set of arguments is provided" do
92
+ ver = Caricature::Verification.new(:my_method, @rec)
93
+ ver.with 1, 5, 7
94
+ ver.should.not.be.successful
95
+ end
96
+
97
+ it "should be unsuccessful when the wrong method name is provided" do
98
+ ver = Caricature::Verification.new(:some_method, @rec)
99
+ ver.should.not.be.successful
100
+ end
101
+
102
+ end
103
+
104
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caricature
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Porto Carrero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-10 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Caricature - Bringing simple mocking to the DLR
17
+ email: ivan@flanders.co.nz
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - README.markdown
25
+ files:
26
+ - README
27
+ - README.markdown
28
+ has_rdoc: true
29
+ homepage: http://github.com/casualjim/caricature
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: caricature
52
+ rubygems_version: 1.3.2
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Caricature - Bringing simple mocking to the DLR
56
+ test_files:
57
+ - spec/bacon_helper.rb
58
+ - spec/core_ext_spec.rb
59
+ - spec/expectation_spec.rb
60
+ - spec/interop_spec.rb
61
+ - spec/isolator_spec.rb
62
+ - spec/method_call_spec.rb
63
+ - spec/proxy_spec.rb
64
+ - spec/verification_spec.rb