rr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/CHANGES +1 -0
  2. data/README +45 -0
  3. data/Rakefile +60 -0
  4. data/examples/environment_fixture_setup.rb +7 -0
  5. data/examples/example_helper.rb +8 -0
  6. data/examples/example_suite.rb +45 -0
  7. data/examples/high_level_example.rb +132 -0
  8. data/examples/rr/do_not_allow_creator_example.rb +90 -0
  9. data/examples/rr/double_bind_example.rb +32 -0
  10. data/examples/rr/double_dispatching_example.rb +167 -0
  11. data/examples/rr/double_example.rb +67 -0
  12. data/examples/rr/double_register_scenario_example.rb +25 -0
  13. data/examples/rr/double_reset_example.rb +60 -0
  14. data/examples/rr/double_verify_example.rb +24 -0
  15. data/examples/rr/expectations/any_argument_expectation_example.rb +43 -0
  16. data/examples/rr/expectations/anything_argument_equality_expectation_example.rb +39 -0
  17. data/examples/rr/expectations/argument_equality_expectation_example.rb +53 -0
  18. data/examples/rr/expectations/boolean_argument_equality_expectation_example.rb +49 -0
  19. data/examples/rr/expectations/duck_type_argument_equality_expectation_example.rb +68 -0
  20. data/examples/rr/expectations/is_a_argument_equality_expectation_example.rb +51 -0
  21. data/examples/rr/expectations/numeric_argument_equality_expectation_example.rb +47 -0
  22. data/examples/rr/expectations/range_argument_equality_expectation_example.rb +60 -0
  23. data/examples/rr/expectations/regexp_argument_equality_expectation_example.rb +68 -0
  24. data/examples/rr/expectations/times_called_expectation_example.rb +176 -0
  25. data/examples/rr/extensions/double_methods_example.rb +130 -0
  26. data/examples/rr/mock_creator_example.rb +65 -0
  27. data/examples/rr/probe_creator_example.rb +83 -0
  28. data/examples/rr/rspec/rspec_adapter_example.rb +64 -0
  29. data/examples/rr/rspec/rspec_usage_example.rb +38 -0
  30. data/examples/rr/scenario_example.rb +399 -0
  31. data/examples/rr/space_create_example.rb +185 -0
  32. data/examples/rr/space_example.rb +30 -0
  33. data/examples/rr/space_helper.rb +7 -0
  34. data/examples/rr/space_register_example.rb +33 -0
  35. data/examples/rr/space_reset_example.rb +73 -0
  36. data/examples/rr/space_verify_example.rb +146 -0
  37. data/examples/rr/stub_creator_example.rb +74 -0
  38. data/examples/rr/test_unit/test_helper.rb +9 -0
  39. data/examples/rr/test_unit/test_unit_integration_test.rb +39 -0
  40. data/examples/rspec_example_suite.rb +25 -0
  41. data/examples/test_unit_example_suite.rb +21 -0
  42. data/lib/rr.rb +27 -0
  43. data/lib/rr/adapters/rspec.rb +19 -0
  44. data/lib/rr/adapters/test_unit.rb +27 -0
  45. data/lib/rr/do_not_allow_creator.rb +39 -0
  46. data/lib/rr/double.rb +91 -0
  47. data/lib/rr/expectations/any_argument_expectation.rb +17 -0
  48. data/lib/rr/expectations/argument_equality_expectation.rb +35 -0
  49. data/lib/rr/expectations/times_called_expectation.rb +39 -0
  50. data/lib/rr/expectations/wildcard_matchers/anything.rb +15 -0
  51. data/lib/rr/expectations/wildcard_matchers/boolean.rb +20 -0
  52. data/lib/rr/expectations/wildcard_matchers/duck_type.rb +26 -0
  53. data/lib/rr/expectations/wildcard_matchers/is_a.rb +22 -0
  54. data/lib/rr/expectations/wildcard_matchers/numeric.rb +11 -0
  55. data/lib/rr/expectations/wildcard_matchers/range.rb +7 -0
  56. data/lib/rr/expectations/wildcard_matchers/regexp.rb +7 -0
  57. data/lib/rr/extensions/double_methods.rb +81 -0
  58. data/lib/rr/mock_creator.rb +32 -0
  59. data/lib/rr/probe_creator.rb +31 -0
  60. data/lib/rr/scenario.rb +184 -0
  61. data/lib/rr/scenario_not_found_error.rb +4 -0
  62. data/lib/rr/scenario_order_error.rb +4 -0
  63. data/lib/rr/space.rb +111 -0
  64. data/lib/rr/stub_creator.rb +36 -0
  65. metadata +113 -0
@@ -0,0 +1,185 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../example_helper"
3
+
4
+ module RR
5
+ describe Space, "#create_mock_creator" do
6
+ it_should_behave_like "RR::Space"
7
+
8
+ before do
9
+ @space = Space.new
10
+ @object = Object.new
11
+ end
12
+
13
+ it "creates a MockCreator" do
14
+ creator = @space.create_mock_creator(@object)
15
+ creator.foobar(1) {:baz}
16
+ @object.foobar(1).should == :baz
17
+ proc {@object.foobar(1)}.should raise_error(Expectations::TimesCalledExpectationError)
18
+ end
19
+
20
+ it "uses block definition when passed a block" do
21
+ creator = @space.create_mock_creator(@object) do |c|
22
+ c.foobar(1) {:baz}
23
+ end
24
+ @object.foobar(1).should == :baz
25
+ proc {@object.foobar(1)}.should raise_error(Expectations::TimesCalledExpectationError)
26
+ end
27
+ end
28
+
29
+ describe Space, "#create_stub_creator" do
30
+ it_should_behave_like "RR::Space"
31
+
32
+ before do
33
+ @space = Space.new
34
+ @object = Object.new
35
+ @method_name = :foobar
36
+ end
37
+
38
+ it "creates a StubCreator" do
39
+ creator = @space.create_stub_creator(@object)
40
+ creator.foobar {:baz}
41
+ @object.foobar.should == :baz
42
+ @object.foobar.should == :baz
43
+ end
44
+
45
+ it "uses block definition when passed a block" do
46
+ creator = @space.create_stub_creator(@object) do |c|
47
+ c.foobar(1) {:return_value}
48
+ c.foobar.with_any_args {:default}
49
+ c.baz(1) {:baz_value}
50
+ end
51
+ @object.foobar(1).should == :return_value
52
+ @object.foobar.should == :default
53
+ proc {@object.baz.should == :return_value}.should raise_error
54
+ end
55
+ end
56
+
57
+ describe Space, "#create_probe_creator" do
58
+ it_should_behave_like "RR::Space"
59
+
60
+ before do
61
+ @space = Space.new
62
+ @object = Object.new
63
+ @method_name = :foobar
64
+ def @object.foobar(*args)
65
+ :original_foobar
66
+ end
67
+ end
68
+
69
+ it "creates a ProbeCreator" do
70
+ creator = @space.create_probe_creator(@object)
71
+ creator.foobar(1)
72
+ @object.foobar(1).should == :original_foobar
73
+ proc {@object.foobar(1)}.should raise_error(Expectations::TimesCalledExpectationError)
74
+ end
75
+
76
+ it "uses block definition when passed a block" do
77
+ creator = @space.create_probe_creator(@object) do |c|
78
+ c.foobar(1)
79
+ end
80
+ @object.foobar(1).should == :original_foobar
81
+ proc {@object.foobar(1)}.should raise_error(Expectations::TimesCalledExpectationError)
82
+ end
83
+ end
84
+
85
+ describe Space, "#create_do_not_allow_creator" do
86
+ it_should_behave_like "RR::Space"
87
+
88
+ before do
89
+ @space = Space.new
90
+ @object = Object.new
91
+ end
92
+
93
+ it "creates a MockCreator" do
94
+ creator = @space.create_do_not_allow_creator(@object)
95
+ creator.foobar(1)
96
+ proc {@object.foobar(1)}.should raise_error(Expectations::TimesCalledExpectationError)
97
+ end
98
+
99
+ it "uses block definition when passed a block" do
100
+ creator = @space.create_do_not_allow_creator(@object) do |c|
101
+ c.foobar(1)
102
+ end
103
+ proc {@object.foobar(1)}.should raise_error(Expectations::TimesCalledExpectationError)
104
+ end
105
+ end
106
+
107
+ describe Space, "#create_scenario" do
108
+ it_should_behave_like "RR::Space"
109
+
110
+ before do
111
+ @space = Space.new
112
+ @object = Object.new
113
+ @method_name = :foobar
114
+ end
115
+
116
+ it "creates a Scenario and registers it to the double" do
117
+ double = @space.create_double(@object, @method_name)
118
+ def double.scenarios
119
+ @scenarios
120
+ end
121
+
122
+ scenario = @space.create_scenario(double)
123
+ double.scenarios.should include(scenario)
124
+ end
125
+ end
126
+
127
+ describe Space, "#create_double when double does not exist" do
128
+ it_should_behave_like "RR::Space"
129
+
130
+ before do
131
+ @space = Space.new
132
+ @object = Object.new
133
+ def @object.foobar(*args)
134
+ :original_foobar
135
+ end
136
+ @method_name = :foobar
137
+ end
138
+
139
+ it "returns double and adds double to double list when method_name is a symbol" do
140
+ double = @space.create_double(@object, @method_name)
141
+ @space.doubles[@object][@method_name].should === double
142
+ double.space.should === @space
143
+ double.object.should === @object
144
+ double.method_name.should === @method_name
145
+ end
146
+
147
+ it "returns double and adds double to double list when method_name is a string" do
148
+ double = @space.create_double(@object, 'foobar')
149
+ @space.doubles[@object][@method_name].should === double
150
+ double.space.should === @space
151
+ double.object.should === @object
152
+ double.method_name.should === @method_name
153
+ end
154
+
155
+ it "overrides the method when passing a block" do
156
+ double = @space.create_double(@object, @method_name)
157
+ @object.methods.should include("__rr__#{@method_name}__rr__")
158
+ end
159
+ end
160
+
161
+ describe Space, "#create_double when double exists" do
162
+ it_should_behave_like "RR::Space"
163
+
164
+ before do
165
+ @space = Space.new
166
+ @object = Object.new
167
+ def @object.foobar(*args)
168
+ :original_foobar
169
+ end
170
+ @method_name = :foobar
171
+ end
172
+
173
+ it "returns the existing double" do
174
+ original_foobar_method = @object.method(:foobar)
175
+ double = @space.create_double(@object, 'foobar')
176
+
177
+ double.original_method.should == original_foobar_method
178
+
179
+ @space.create_double(@object, 'foobar').should === double
180
+
181
+ double.reset
182
+ @object.foobar.should == :original_foobar
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,30 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../example_helper"
3
+
4
+ module RR
5
+ describe Space, " class" do
6
+ it_should_behave_like "RR::Space"
7
+
8
+ before(:each) do
9
+ @original_space = Space.instance
10
+ @space = Space.new
11
+ Space.instance = @space
12
+ end
13
+
14
+ after(:each) do
15
+ Space.instance = @original_space
16
+ end
17
+
18
+ it "proxies to a singleton instance of Space" do
19
+ create_double_args = nil
20
+ (class << @space; self; end).class_eval do
21
+ define_method :create_double do |*args|
22
+ create_double_args = args
23
+ end
24
+ end
25
+
26
+ Space.create_double(:foo, :bar)
27
+ create_double_args.should == [:foo, :bar]
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ module RR
2
+ describe Space, :shared => true do
3
+ after(:each) do
4
+ Space.instance.verify_doubles
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../example_helper"
3
+
4
+ module RR
5
+ describe Space, "#register_ordered_scenario" do
6
+ it_should_behave_like "RR::Space"
7
+
8
+ before(:each) do
9
+ @space = Space.new
10
+ @original_space = Space.instance
11
+ Space.instance = @space
12
+ @object = Object.new
13
+ @method_name = :foobar
14
+ @double = @space.create_double(@object, @method_name)
15
+ end
16
+
17
+ after(:each) do
18
+ Space.instance = @original_space
19
+ end
20
+
21
+ it "adds the ordered scenario to the ordered_scenarios collection" do
22
+ scenario1 = @space.create_scenario(@double)
23
+
24
+ @space.ordered_scenarios.should == []
25
+ @space.register_ordered_scenario scenario1
26
+ @space.ordered_scenarios.should == [scenario1]
27
+
28
+ scenario2 = @space.create_scenario(@double)
29
+ @space.register_ordered_scenario scenario2
30
+ @space.ordered_scenarios.should == [scenario1, scenario2]
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,73 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../example_helper"
3
+
4
+ module RR
5
+ describe Space, "#reset_double" do
6
+ it_should_behave_like "RR::Space"
7
+
8
+ before do
9
+ @space = Space.new
10
+ @object = Object.new
11
+ @method_name = :foobar
12
+ end
13
+
14
+ it "resets the doubles" do
15
+ double = @space.create_double(@object, @method_name)
16
+ @space.doubles[@object][@method_name].should === double
17
+ @object.methods.should include("__rr__#{@method_name}__rr__")
18
+
19
+ @space.reset_double(@object, @method_name)
20
+ @space.doubles[@object][@method_name].should be_nil
21
+ @object.methods.should_not include("__rr__#{@method_name}__rr__")
22
+ end
23
+
24
+ it "removes the object from the doubles map when it has no doubles" do
25
+ double1 = @space.create_double(@object, :foobar1)
26
+ double2 = @space.create_double(@object, :foobar2)
27
+
28
+ @space.doubles.include?(@object).should == true
29
+ @space.doubles[@object][:foobar1].should_not be_nil
30
+ @space.doubles[@object][:foobar2].should_not be_nil
31
+
32
+ @space.reset_double(@object, :foobar1)
33
+ @space.doubles.include?(@object).should == true
34
+ @space.doubles[@object][:foobar1].should be_nil
35
+ @space.doubles[@object][:foobar2].should_not be_nil
36
+
37
+ @space.reset_double(@object, :foobar2)
38
+ @space.doubles.include?(@object).should == false
39
+ end
40
+ end
41
+
42
+ describe Space, "#reset_doubles" do
43
+ it_should_behave_like "RR::Space"
44
+
45
+ before do
46
+ @space = Space.new
47
+ @object1 = Object.new
48
+ @object2 = Object.new
49
+ @method_name = :foobar
50
+ end
51
+
52
+ it "resets the double and removes it from the doubles list" do
53
+ double1 = @space.create_double(@object1, @method_name)
54
+ double1_reset_calls = 0
55
+ (class << double1; self; end).class_eval do
56
+ define_method(:reset) do ||
57
+ double1_reset_calls += 1
58
+ end
59
+ end
60
+ double2 = @space.create_double(@object2, @method_name)
61
+ double2_reset_calls = 0
62
+ (class << double2; self; end).class_eval do
63
+ define_method(:reset) do ||
64
+ double2_reset_calls += 1
65
+ end
66
+ end
67
+
68
+ @space.reset_doubles
69
+ double1_reset_calls.should == 1
70
+ double1_reset_calls.should == 1
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,146 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../example_helper"
3
+
4
+ module RR
5
+ describe Space, "#verify_doubles" do
6
+ it_should_behave_like "RR::Space"
7
+
8
+ before do
9
+ @space = Space.new
10
+ @object1 = Object.new
11
+ @object2 = Object.new
12
+ @method_name = :foobar
13
+ end
14
+
15
+ it "verifies and deletes the doubles" do
16
+ double1 = @space.create_double(@object1, @method_name)
17
+ double1_verify_calls = 0
18
+ double1_reset_calls = 0
19
+ (class << double1; self; end).class_eval do
20
+ define_method(:verify) do ||
21
+ double1_verify_calls += 1
22
+ end
23
+ define_method(:reset) do ||
24
+ double1_reset_calls += 1
25
+ end
26
+ end
27
+ double2 = @space.create_double(@object2, @method_name)
28
+ double2_verify_calls = 0
29
+ double2_reset_calls = 0
30
+ (class << double2; self; end).class_eval do
31
+ define_method(:verify) do ||
32
+ double2_verify_calls += 1
33
+ end
34
+ define_method(:reset) do ||
35
+ double2_reset_calls += 1
36
+ end
37
+ end
38
+
39
+ @space.verify_doubles
40
+ double1_verify_calls.should == 1
41
+ double2_verify_calls.should == 1
42
+ double1_reset_calls.should == 1
43
+ double1_reset_calls.should == 1
44
+ end
45
+ end
46
+
47
+ describe Space, "#verify_double" do
48
+ it_should_behave_like "RR::Space"
49
+
50
+ before do
51
+ @space = Space.new
52
+ @object = Object.new
53
+ @method_name = :foobar
54
+ end
55
+
56
+ it "verifies and deletes the double" do
57
+ double = @space.create_double(@object, @method_name)
58
+ @space.doubles[@object][@method_name].should === double
59
+ @object.methods.should include("__rr__#{@method_name}__rr__")
60
+
61
+ verify_calls = 0
62
+ (class << double; self; end).class_eval do
63
+ define_method(:verify) do ||
64
+ verify_calls += 1
65
+ end
66
+ end
67
+ @space.verify_double(@object, @method_name)
68
+ verify_calls.should == 1
69
+
70
+ @space.doubles[@object][@method_name].should be_nil
71
+ @object.methods.should_not include("__rr__#{@method_name}__rr__")
72
+ end
73
+
74
+ it "deletes the double when verifying the double raises an error" do
75
+ double = @space.create_double(@object, @method_name)
76
+ @space.doubles[@object][@method_name].should === double
77
+ @object.methods.should include("__rr__#{@method_name}__rr__")
78
+
79
+ verify_called = true
80
+ (class << double; self; end).class_eval do
81
+ define_method(:verify) do ||
82
+ verify_called = true
83
+ raise "An Error"
84
+ end
85
+ end
86
+ proc {@space.verify_double(@object, @method_name)}.should raise_error
87
+ verify_called.should be_true
88
+
89
+ @space.doubles[@object][@method_name].should be_nil
90
+ @object.methods.should_not include("__rr__#{@method_name}__rr__")
91
+ end
92
+ end
93
+
94
+ describe Space, "#verify_ordered_scenario", :shared => true do
95
+ it_should_behave_like "RR::Space"
96
+
97
+ before do
98
+ @space = Space.new
99
+ @object = Object.new
100
+ @method_name = :foobar
101
+ @double = @space.create_double(@object, @method_name)
102
+ end
103
+ end
104
+
105
+ describe Space, "#verify_ordered_scenario where the passed in scenario is at the front of the queue" do
106
+ it_should_behave_like "RR::Space#verify_ordered_scenario"
107
+
108
+ it "keeps the scenario when times called is not verified" do
109
+ scenario = @space.create_scenario(@double)
110
+ @space.register_ordered_scenario(scenario)
111
+
112
+ scenario.twice
113
+ scenario.should_not be_times_called_verified
114
+
115
+ @space.verify_ordered_scenario(scenario)
116
+ @space.ordered_scenarios.should include(scenario)
117
+ end
118
+
119
+ it "removes the scenario when times called in verified" do
120
+ scenario = @space.create_scenario(@double)
121
+ @space.register_ordered_scenario(scenario)
122
+
123
+ scenario.with(1).once
124
+ @object.foobar(1)
125
+ scenario.should be_times_called_verified
126
+
127
+ @space.verify_ordered_scenario(scenario)
128
+ @space.ordered_scenarios.should_not include(scenario)
129
+ end
130
+ end
131
+
132
+ describe Space, "#verify_ordered_scenario where the passed in scenario is not at the front of the queue" do
133
+ it_should_behave_like "RR::Space#verify_ordered_scenario"
134
+
135
+ it "raises error" do
136
+ first_scenario = @space.create_scenario(@double)
137
+ @space.register_ordered_scenario(first_scenario)
138
+ second_scenario = @space.create_scenario(@double)
139
+ @space.register_ordered_scenario(second_scenario)
140
+
141
+ proc do
142
+ @space.verify_ordered_scenario(second_scenario)
143
+ end.should raise_error(RR::ScenarioOrderError)
144
+ end
145
+ end
146
+ end