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,47 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../../example_helper"
3
+
4
+ module RR
5
+ module Expectations
6
+ describe ArgumentEqualityExpectation, "#exact_match? with is_a argument" do
7
+ before do
8
+ @expectation = ArgumentEqualityExpectation.new(numeric)
9
+ end
10
+
11
+ it "returns true when passed in an IsA module" do
12
+ @expectation.should be_exact_match(WildcardMatchers::Numeric.new)
13
+ end
14
+
15
+ it "returns false otherwise" do
16
+ @expectation.should_not be_exact_match("hello")
17
+ @expectation.should_not be_exact_match(:hello)
18
+ @expectation.should_not be_exact_match(1)
19
+ @expectation.should_not be_exact_match(nil)
20
+ @expectation.should_not be_exact_match()
21
+ end
22
+ end
23
+
24
+ describe ArgumentEqualityExpectation, "#wildcard_match? with is_a Numeric argument" do
25
+ before do
26
+ @expectation = ArgumentEqualityExpectation.new(numeric)
27
+ end
28
+
29
+ it "returns true when passed a Numeric" do
30
+ @expectation.should be_wildcard_match(99)
31
+ end
32
+
33
+ it "returns false when not passed a Numeric" do
34
+ @expectation.should_not be_wildcard_match(:not_a_numeric)
35
+ end
36
+
37
+ it "returns true when an exact match" do
38
+ @expectation.should be_wildcard_match(numeric)
39
+ end
40
+
41
+ it "returns false when not passed correct number of arguments" do
42
+ @expectation.should_not be_wildcard_match()
43
+ @expectation.should_not be_wildcard_match(1, 2)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,60 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../../example_helper"
3
+
4
+ module RR
5
+ module Expectations
6
+ describe ArgumentEqualityExpectation, "#exact_match? with range argument" do
7
+ before do
8
+ @expectation = ArgumentEqualityExpectation.new(2..5)
9
+ end
10
+
11
+ it "returns true when passed in an Range matcher with the same argument list" do
12
+ @expectation.should be_exact_match(2..5)
13
+ end
14
+
15
+ it "returns false when passed in an Range matcher with a different argument list" do
16
+ @expectation.should_not be_exact_match(3..6)
17
+ end
18
+
19
+ it "returns false otherwise" do
20
+ @expectation.should_not be_exact_match(2)
21
+ @expectation.should_not be_exact_match(:hello)
22
+ @expectation.should_not be_exact_match(3)
23
+ @expectation.should_not be_exact_match(nil)
24
+ @expectation.should_not be_exact_match(true)
25
+ @expectation.should_not be_exact_match()
26
+ end
27
+ end
28
+
29
+ describe ArgumentEqualityExpectation, "#wildcard_match? with Range argument" do
30
+ before do
31
+ @expectation = ArgumentEqualityExpectation.new(2..6)
32
+ end
33
+
34
+ it "returns true when string matches the range" do
35
+ @expectation.should be_wildcard_match(3)
36
+ end
37
+
38
+ it "returns false when string does not match the range" do
39
+ @expectation.should_not be_wildcard_match(7)
40
+ end
41
+
42
+ it "returns true when an exact match" do
43
+ @expectation.should be_wildcard_match(2..6)
44
+ end
45
+
46
+ it "returns false when not an exact match" do
47
+ @expectation.should_not be_wildcard_match(3..9)
48
+ end
49
+
50
+ it "returns false when not a number" do
51
+ @expectation.should_not be_wildcard_match("Not a number")
52
+ end
53
+
54
+ it "returns false when not passed correct number of arguments" do
55
+ @expectation.should_not be_wildcard_match()
56
+ @expectation.should_not be_wildcard_match(2, 3)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,68 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../../example_helper"
3
+
4
+ module RR
5
+ module Expectations
6
+ describe ArgumentEqualityExpectation, "#exact_match? with regexp argument" do
7
+ before do
8
+ @expectation = ArgumentEqualityExpectation.new(/abc/)
9
+ end
10
+
11
+ it "returns true when passed in an Regexp matcher with the same argument list" do
12
+ @expectation.should be_exact_match(/abc/)
13
+ end
14
+
15
+ it "returns false when passed in an Regexp matcher with a different argument list" do
16
+ @expectation.should_not be_exact_match(/def/)
17
+ end
18
+
19
+ it "returns false otherwise" do
20
+ @expectation.should_not be_exact_match("abc")
21
+ @expectation.should_not be_exact_match(:hello)
22
+ @expectation.should_not be_exact_match(1)
23
+ @expectation.should_not be_exact_match(nil)
24
+ @expectation.should_not be_exact_match(true)
25
+ @expectation.should_not be_exact_match()
26
+ end
27
+ end
28
+
29
+ describe ArgumentEqualityExpectation, "#wildcard_match? with Regexp argument" do
30
+ before do
31
+ @matching_object = Object.new
32
+ def @matching_object.quack
33
+ end
34
+ def @matching_object.waddle
35
+ end
36
+
37
+ @partial_matching_object = Object.new
38
+ def @partial_matching_object.quack
39
+ end
40
+
41
+ @not_match_object = Object.new
42
+
43
+ @expectation = ArgumentEqualityExpectation.new(/abc/)
44
+ end
45
+
46
+ it "returns true when string matches the regexp" do
47
+ @expectation.should be_wildcard_match("Tabcola")
48
+ end
49
+
50
+ it "returns false when string does not match the regexp" do
51
+ @expectation.should_not be_wildcard_match("no match here")
52
+ end
53
+
54
+ it "returns true when an exact match" do
55
+ @expectation.should be_wildcard_match(/abc/)
56
+ end
57
+
58
+ it "returns false when not an exact match" do
59
+ @expectation.should_not be_wildcard_match(/def/)
60
+ end
61
+
62
+ it "returns false when not passed correct number of arguments" do
63
+ @expectation.should_not be_wildcard_match()
64
+ @expectation.should_not be_wildcard_match('abc', 'abc')
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,176 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../../example_helper"
3
+
4
+ module RR
5
+ module Expectations
6
+ describe TimesCalledExpectation, :shared => true do
7
+ before do
8
+ @space = Space.new
9
+ @object = Object.new
10
+ @method_name = :foobar
11
+ @double = @space.create_double(@object, @method_name)
12
+ @scenario = @space.create_scenario(@double)
13
+ @scenario.with_any_args
14
+ end
15
+
16
+ def raises_expectation_error(&block)
17
+ proc {block.call}.should raise_error(TimesCalledExpectationError)
18
+ end
19
+ end
20
+
21
+ describe TimesCalledExpectation, ".new" do
22
+ it "doesn't accept both an argument and a block" do
23
+ proc do
24
+ TimesCalledExpectation.new(2) {|value| value == 2}
25
+ end.should raise_error(ArgumentError, "Cannot pass in both an argument and a block")
26
+ end
27
+ end
28
+
29
+ describe TimesCalledExpectation, "#verify" do
30
+ it_should_behave_like "RR::Expectations::TimesCalledExpectation"
31
+
32
+ it "returns true when times called exactly matches an integer" do
33
+ @expectation = TimesCalledExpectation.new(2)
34
+ @expectation.verify.should == false
35
+ @expectation.verify_input
36
+ @expectation.verify.should == false
37
+ @expectation.verify_input
38
+ @expectation.verify.should == true
39
+ end
40
+
41
+ it "returns true when times called falls within a range" do
42
+ @expectation = TimesCalledExpectation.new(1..2)
43
+
44
+ @expectation.verify.should == false
45
+ @expectation.verify_input
46
+ @expectation.verify.should == true
47
+ @expectation.verify_input
48
+ @expectation.verify.should == true
49
+ end
50
+
51
+ it "matches a block" do
52
+ @expectation = TimesCalledExpectation.new {|value| value == 2}
53
+
54
+ @expectation.verify.should == false
55
+ @expectation.verify_input
56
+ @expectation.verify.should == false
57
+ @expectation.verify_input
58
+ @expectation.verify.should == true
59
+ @expectation.verify_input
60
+ @expectation.verify.should == false
61
+ end
62
+ end
63
+
64
+ describe TimesCalledExpectation, "#verify! when passed an Integer (2)" do
65
+ it_should_behave_like "RR::Expectations::TimesCalledExpectation"
66
+
67
+ before do
68
+ @expectation = TimesCalledExpectation.new(2)
69
+ end
70
+
71
+ it "passes after verify_input called 2 times" do
72
+ @expectation.verify_input
73
+ @expectation.verify_input
74
+ @expectation.verify!
75
+ end
76
+
77
+ it "fails after verify_input called 1 time" do
78
+ @expectation.verify_input
79
+ proc {@expectation.verify!}.should raise_error(TimesCalledExpectationError)
80
+ end
81
+
82
+ it "can't be called when verify_input is called 3 times" do
83
+ @expectation.verify_input
84
+ @expectation.verify_input
85
+ proc do
86
+ @expectation.verify_input
87
+ end.should raise_error(TimesCalledExpectationError)
88
+ end
89
+ end
90
+
91
+ describe TimesCalledExpectation, "#verify! when passed a Range (1..2)" do
92
+ it_should_behave_like "RR::Expectations::TimesCalledExpectation"
93
+
94
+ before do
95
+ @expectation = TimesCalledExpectation.new(1..2)
96
+ end
97
+
98
+ it "passes after verify_input called 1 time" do
99
+ @expectation.verify_input
100
+ @expectation.verify!
101
+ end
102
+
103
+ it "passes after verify_input called 2 times" do
104
+ @expectation.verify_input
105
+ @expectation.verify_input
106
+ @expectation.verify!
107
+ end
108
+
109
+ it "can't be called when verify_input is called 3 times" do
110
+ @expectation.verify_input
111
+ @expectation.verify_input
112
+ proc do
113
+ @expectation.verify_input
114
+ end.should raise_error(TimesCalledExpectationError)
115
+ end
116
+ end
117
+
118
+ describe TimesCalledExpectation, "#verify! when passed a block (== 2 times)" do
119
+ it_should_behave_like "RR::Expectations::TimesCalledExpectation"
120
+
121
+ before do
122
+ @expectation = TimesCalledExpectation.new {|value| value == 2}
123
+ end
124
+
125
+ it "passes after verify_input called 2 times" do
126
+ @expectation.verify_input
127
+ @expectation.verify_input
128
+ @expectation.verify!
129
+ end
130
+
131
+ it "fails after verify_input called 1 time" do
132
+ @expectation.verify_input
133
+ proc {@expectation.verify!}.should raise_error(TimesCalledExpectationError)
134
+ end
135
+
136
+ it "fails after verify_input called 3 times" do
137
+ @expectation.verify_input
138
+ @expectation.verify_input
139
+ @expectation.verify_input
140
+ proc {@expectation.verify!}.should raise_error(TimesCalledExpectationError)
141
+ end
142
+ end
143
+
144
+ describe TimesCalledExpectation, "#verify_input for an integer expectation" do
145
+ it_should_behave_like "RR::Expectations::TimesCalledExpectation"
146
+
147
+ it "raises error when verify_input called more than the expected number of times" do
148
+ @expectation = TimesCalledExpectation.new(1)
149
+ @expectation.verify_input
150
+ raises_expectation_error {@expectation.verify_input}
151
+ end
152
+ end
153
+
154
+ describe TimesCalledExpectation, "#verify_input for a range expectation" do
155
+ it_should_behave_like "RR::Expectations::TimesCalledExpectation"
156
+
157
+ it "raises error when verify_input called more than range permits" do
158
+ @expectation = TimesCalledExpectation.new(1..2)
159
+ @expectation.verify_input
160
+ @expectation.verify_input
161
+ raises_expectation_error {@expectation.verify_input}
162
+ end
163
+ end
164
+
165
+ describe TimesCalledExpectation, "#verify_input for a proc expectation" do
166
+ it_should_behave_like "RR::Expectations::TimesCalledExpectation"
167
+
168
+ it "lets everything pass" do
169
+ @expectation = TimesCalledExpectation.new {|times| times == 1}
170
+ @object.foobar
171
+ @object.foobar
172
+ @object.foobar
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,130 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../../example_helper"
3
+
4
+ module RR
5
+ module Extensions
6
+ describe DoubleMethods, "#mock" do
7
+ before do
8
+ extend RR::Extensions::DoubleMethods
9
+ @subject = Object.new
10
+ end
11
+
12
+ it "sets up the RR mock call chain" do
13
+ should_create_mock_call_chain mock(@subject)
14
+ end
15
+
16
+ it "sets up the RR mock call chain with rr_mock" do
17
+ should_create_mock_call_chain rr_mock(@subject)
18
+ end
19
+
20
+ def should_create_mock_call_chain(creator)
21
+ class << @subject
22
+ def foobar(*args)
23
+ :original_value
24
+ end
25
+ end
26
+
27
+ scenario = creator.foobar(1, 2) {:baz}
28
+ scenario.times_called_expectation.times.should == 1
29
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
30
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
31
+
32
+ @subject.foobar(1, 2).should == :baz
33
+ end
34
+ end
35
+
36
+ describe DoubleMethods, "#stub" do
37
+ before do
38
+ extend RR::Extensions::DoubleMethods
39
+ @subject = Object.new
40
+ end
41
+
42
+ it "sets up the RR stub call chain" do
43
+ should_create_stub_call_chain stub(@subject)
44
+ end
45
+
46
+ it "sets up the RR stub call chain with rr_stub" do
47
+ should_create_stub_call_chain rr_stub(@subject)
48
+ end
49
+
50
+ def should_create_stub_call_chain(creator)
51
+ class << @subject
52
+ def foobar(*args)
53
+ :original_value
54
+ end
55
+ end
56
+
57
+ scenario = creator.foobar(1, 2) {:baz}
58
+ scenario.times_called_expectation.should == nil
59
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
60
+ @subject.foobar(1, 2).should == :baz
61
+ end
62
+ end
63
+
64
+ describe DoubleMethods, "#probe" do
65
+ before do
66
+ extend RR::Extensions::DoubleMethods
67
+ @subject = Object.new
68
+ end
69
+
70
+ it "sets up the RR probe call chain" do
71
+ creator = probe(@subject)
72
+ end
73
+
74
+ it "sets up the RR probe call chain with rr_probe" do
75
+ creator = rr_probe(@subject)
76
+ end
77
+
78
+ def should_create_probe_call_chain(creator)
79
+ class << @subject
80
+ def foobar(*args)
81
+ :original_value
82
+ end
83
+ end
84
+
85
+ scenario = creator.foobar(1, 2)
86
+ scenario.times_called_expectation.times.should == 1
87
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
88
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
89
+
90
+ @subject.foobar(1, 2).should == :original_value
91
+ end
92
+ end
93
+
94
+ describe DoubleMethods, "#do_not_allow" do
95
+ before do
96
+ extend RR::Extensions::DoubleMethods
97
+ @subject = Object.new
98
+ end
99
+
100
+ it "sets up the RR do_not_allow call chain" do
101
+ should_create_do_not_allow_call_chain do_not_allow(@subject)
102
+ end
103
+
104
+ it "sets up the RR do_not_allow call chain with rr_do_not_allow" do
105
+ should_create_do_not_allow_call_chain rr_do_not_allow(@subject)
106
+ end
107
+
108
+ it "sets up the RR do_not_allow call chain" do
109
+ should_create_do_not_allow_call_chain dont_allow(@subject)
110
+ end
111
+
112
+ it "sets up the RR do_not_allow call chain with rr_do_not_allow" do
113
+ should_create_do_not_allow_call_chain rr_dont_allow(@subject)
114
+ end
115
+
116
+ def should_create_do_not_allow_call_chain(creator)
117
+ class << @subject
118
+ def foobar(*args)
119
+ :original_value
120
+ end
121
+ end
122
+
123
+ scenario = creator.foobar(1, 2)
124
+ scenario.times_called_expectation.times.should == 0
125
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
126
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
127
+ end
128
+ end
129
+ end
130
+ end