rr 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/CHANGES +3 -0
  2. data/{README → README.rdoc} +14 -6
  3. data/Rakefile +2 -2
  4. data/lib/rr.rb +11 -3
  5. data/lib/rr/adapters/rr_methods.rb +12 -12
  6. data/lib/rr/adapters/rspec.rb +3 -3
  7. data/lib/rr/adapters/test_unit.rb +3 -3
  8. data/lib/rr/double.rb +12 -10
  9. data/lib/rr/double_creator.rb +48 -45
  10. data/lib/rr/double_definition.rb +17 -149
  11. data/lib/rr/double_definition_builder.rb +11 -11
  12. data/lib/rr/double_definition_creator.rb +156 -0
  13. data/lib/rr/{double_method_proxy.rb → double_definition_creator_proxy.rb} +2 -2
  14. data/lib/rr/double_injection.rb +6 -6
  15. data/lib/rr/double_matches.rb +40 -40
  16. data/lib/rr/errors/rr_error.rb +1 -1
  17. data/lib/rr/expectations/times_called_expectation.rb +1 -1
  18. data/lib/rr/space.rb +9 -30
  19. data/spec/high_level_spec.rb +140 -143
  20. data/spec/rr/adapters/rr_methods_creator_spec.rb +21 -21
  21. data/spec/rr/adapters/rr_methods_space_spec.rb +2 -2
  22. data/spec/rr/double/double_injection_dispatching_spec.rb +15 -15
  23. data/spec/rr/double/double_injection_reset_spec.rb +1 -1
  24. data/spec/rr/double/double_injection_verify_spec.rb +5 -4
  25. data/spec/rr/{double_method_proxy_spec.rb → double_definition_creator_proxy_spec.rb} +12 -10
  26. data/spec/rr/{double_creator_spec.rb → double_definition_creator_spec.rb} +166 -124
  27. data/spec/rr/double_definition_spec.rb +637 -642
  28. data/spec/rr/double_spec.rb +44 -43
  29. data/spec/rr/errors/rr_error_spec.rb +6 -6
  30. data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +3 -3
  31. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +8 -8
  32. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +11 -13
  33. data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +2 -2
  34. data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +19 -19
  35. data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +16 -16
  36. data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +16 -16
  37. data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +7 -11
  38. data/spec/rr/rspec/rspec_adapter_spec.rb +10 -10
  39. data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +1 -1
  40. data/spec/rr/space/space_spec.rb +372 -18
  41. data/spec/rr/test_unit/test_unit_backtrace_test.rb +1 -1
  42. data/spec/rr/times_called_matchers/proc_matcher_spec.rb +3 -3
  43. data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +3 -3
  44. data/spec/spec_helper.rb +14 -3
  45. data/spec/spec_suite.rb +2 -2
  46. metadata +47 -45
  47. data/spec/rr/double/double_injection_register_scenario_spec.rb +0 -24
  48. data/spec/rr/space/space_create_spec.rb +0 -268
  49. data/spec/rr/space/space_helper.rb +0 -7
  50. data/spec/rr/space/space_register_spec.rb +0 -32
  51. data/spec/rr/space/space_reset_spec.rb +0 -131
  52. data/spec/rr/space/space_verify_spec.rb +0 -181
@@ -1,5 +1,5 @@
1
1
  module RR
2
- class DoubleMethodProxy
2
+ class DoubleDefinitionCreatorProxy
3
3
  def initialize(creator, object, &block) #:nodoc:
4
4
  @creator = creator
5
5
  @object = object
@@ -9,7 +9,7 @@ module RR
9
9
  end
10
10
 
11
11
  def method_missing(method_name, *args, &block)
12
- @creator.create!(@object, method_name, *args, &block)
12
+ @creator.create(@object, method_name, *args, &block)
13
13
  end
14
14
  end
15
15
  yield(self) if block_given?
@@ -10,7 +10,7 @@ module RR
10
10
  @object = object
11
11
  @method_name = method_name.to_sym
12
12
  if object_has_method?(method_name)
13
- meta.send(:alias_method, original_method_name, method_name)
13
+ meta.__send__(:alias_method, original_method_name, method_name)
14
14
  end
15
15
  @doubles = []
16
16
  end
@@ -47,12 +47,12 @@ module RR
47
47
  # It binds the original method implementation on the object
48
48
  # if one exists.
49
49
  def reset
50
- meta.send(:remove_method, placeholder_name)
50
+ meta.__send__(:remove_method, placeholder_name)
51
51
  if object_has_original_method?
52
- meta.send(:alias_method, @method_name, original_method_name)
53
- meta.send(:remove_method, original_method_name)
52
+ meta.__send__(:alias_method, @method_name, original_method_name)
53
+ meta.__send__(:remove_method, original_method_name)
54
54
  else
55
- meta.send(:remove_method, @method_name)
55
+ meta.__send__(:remove_method, @method_name)
56
56
  end
57
57
  end
58
58
 
@@ -80,7 +80,7 @@ module RR
80
80
  end
81
81
 
82
82
  def find_double_to_attempt(args)
83
- matches = DoubleMatches.new(@doubles).find_all_matches!(args)
83
+ matches = DoubleMatches.new(@doubles).find_all_matches(args)
84
84
 
85
85
  unless matches.exact_terminal_doubles_to_attempt.empty?
86
86
  return matches.exact_terminal_doubles_to_attempt.first
@@ -1,51 +1,51 @@
1
1
  module RR
2
- class DoubleMatches
3
- attr_reader :matching_doubles,
4
- :exact_terminal_doubles_to_attempt,
5
- :exact_non_terminal_doubles_to_attempt,
6
- :wildcard_terminal_doubles_to_attempt,
7
- :wildcard_non_terminal_doubles_to_attempt
8
- def initialize(doubles) #:nodoc:
9
- @doubles = doubles
10
- @matching_doubles = []
11
- @exact_terminal_doubles_to_attempt = []
12
- @exact_non_terminal_doubles_to_attempt = []
13
- @wildcard_terminal_doubles_to_attempt = []
14
- @wildcard_non_terminal_doubles_to_attempt = []
15
- end
2
+ class DoubleMatches
3
+ attr_reader :matching_doubles,
4
+ :exact_terminal_doubles_to_attempt,
5
+ :exact_non_terminal_doubles_to_attempt,
6
+ :wildcard_terminal_doubles_to_attempt,
7
+ :wildcard_non_terminal_doubles_to_attempt
8
+ def initialize(doubles) #:nodoc:
9
+ @doubles = doubles
10
+ @matching_doubles = []
11
+ @exact_terminal_doubles_to_attempt = []
12
+ @exact_non_terminal_doubles_to_attempt = []
13
+ @wildcard_terminal_doubles_to_attempt = []
14
+ @wildcard_non_terminal_doubles_to_attempt = []
15
+ end
16
16
 
17
- def find_all_matches!(args)
18
- @doubles.each do |double|
19
- if double.exact_match?(*args)
20
- matching_doubles << double
21
- if double.attempt?
22
- exact_double_is_terminal_or_non_terminal double
23
- end
24
- elsif double.wildcard_match?(*args)
25
- matching_doubles << double
26
- if double.attempt?
27
- wildcard_double_is_terminal_or_non_terminal double
17
+ def find_all_matches(args)
18
+ @doubles.each do |double|
19
+ if double.exact_match?(*args)
20
+ matching_doubles << double
21
+ if double.attempt?
22
+ exact_double_is_terminal_or_non_terminal double
23
+ end
24
+ elsif double.wildcard_match?(*args)
25
+ matching_doubles << double
26
+ if double.attempt?
27
+ wildcard_double_is_terminal_or_non_terminal double
28
+ end
28
29
  end
29
30
  end
31
+ self
30
32
  end
31
- self
32
- end
33
33
 
34
- protected
35
- def exact_double_is_terminal_or_non_terminal(double)
36
- if double.terminal?
37
- exact_terminal_doubles_to_attempt << double
38
- else
39
- exact_non_terminal_doubles_to_attempt << double
34
+ protected
35
+ def exact_double_is_terminal_or_non_terminal(double)
36
+ if double.terminal?
37
+ exact_terminal_doubles_to_attempt << double
38
+ else
39
+ exact_non_terminal_doubles_to_attempt << double
40
+ end
40
41
  end
41
- end
42
42
 
43
- def wildcard_double_is_terminal_or_non_terminal(double)
44
- if double.terminal?
45
- wildcard_terminal_doubles_to_attempt << double
46
- else
47
- wildcard_non_terminal_doubles_to_attempt << double
43
+ def wildcard_double_is_terminal_or_non_terminal(double)
44
+ if double.terminal?
45
+ wildcard_terminal_doubles_to_attempt << double
46
+ else
47
+ wildcard_non_terminal_doubles_to_attempt << double
48
+ end
48
49
  end
49
50
  end
50
- end
51
51
  end
@@ -6,7 +6,7 @@ module RR
6
6
  attr_writer :backtrace
7
7
  def backtrace
8
8
  original_backtrace = (@backtrace) ? @backtrace : super
9
- return original_backtrace unless RR::Space.trim_backtrace
9
+ return original_backtrace unless RR.trim_backtrace
10
10
 
11
11
  return original_backtrace unless original_backtrace.respond_to?(:each)
12
12
  new_backtrace = []
@@ -15,7 +15,7 @@ module RR
15
15
  @matcher.attempt?(@times_called)
16
16
  end
17
17
 
18
- def attempt!
18
+ def attempt
19
19
  @times_called += 1
20
20
  verify_input_error unless @matcher.possible_match?(@times_called)
21
21
  return
@@ -1,14 +1,18 @@
1
1
  module RR
2
- # RR::Space is a Dependency Injection http://en.wikipedia.org/wiki/Dependency_injection
3
- # and global state object for the RR framework. The RR::Space.instance
4
- # is a singleton that holds the state.
2
+ # RR::Space.instance is the global state object for the RR framework.
5
3
  class Space
4
+ module Reader
5
+ def space
6
+ RR::Space.instance
7
+ end
8
+ end
9
+
6
10
  class << self
7
11
  def instance
8
12
  @instance ||= new
9
13
  end
10
14
  attr_writer :instance
11
-
15
+
12
16
  protected
13
17
  def method_missing(method_name, *args, &block)
14
18
  instance.__send__(method_name, *args, &block)
@@ -23,32 +27,6 @@ module RR
23
27
  @trim_backtrace = false
24
28
  end
25
29
 
26
- def double_method_proxy(creator, object, method_name=nil, &definition)
27
- if method_name && definition
28
- raise ArgumentError, "Cannot pass in a method name and a block"
29
- end
30
- proxy = DoubleMethodProxy.new(creator, object, &definition)
31
- return proxy unless method_name
32
- proxy.__send__(method_name)
33
- end
34
-
35
- # Creates a DoubleCreator.
36
- def double_creator
37
- DoubleCreator.new(self)
38
- end
39
-
40
- # Creates and registers a Double to be verified.
41
- def double(double_injection, definition = double_definition)
42
- double = Double.new(self, double_injection, definition)
43
- double.definition.double = double
44
- double_injection.register_double double
45
- double
46
- end
47
-
48
- def double_definition
49
- DoubleDefinition.new(self)
50
- end
51
-
52
30
  # Reuses or creates, if none exists, a DoubleInjection for the passed
53
31
  # in object and method_name.
54
32
  # When a DoubleInjection is created, it binds the dispatcher to the
@@ -94,6 +72,7 @@ module RR
94
72
  end
95
73
  end
96
74
  end
75
+ alias_method :verify, :verify_doubles
97
76
 
98
77
  # Resets the registered Doubles and ordered Doubles
99
78
  def reset
@@ -1,187 +1,184 @@
1
1
  dir = File.dirname(__FILE__)
2
2
  require "#{dir}/spec_helper"
3
3
 
4
- describe "RR", :shared => true do
4
+ class HighLevelSpec
5
+ end
6
+
7
+ describe "RR" do
5
8
  before(:each) do
6
9
  @obj = Object.new
7
10
  extend RR::Adapters::RRMethods
8
11
  end
9
12
 
10
13
  after(:each) do
11
- RR::Space.instance.reset
12
- end
13
- end
14
-
15
- describe "RR mock:" do
16
- it_should_behave_like "RR"
17
-
18
- it "mocks via inline call" do
19
- mock(@obj).to_s {"a value"}
20
- @obj.to_s.should == "a value"
21
- proc {@obj.to_s}.should raise_error(RR::Errors::TimesCalledError)
22
- end
23
-
24
- it "allows ordering" do
25
- mock(@obj).to_s {"value 1"}.ordered
26
- mock(@obj).to_s {"value 2"}.twice.ordered
27
- @obj.to_s.should == "value 1"
28
- @obj.to_s.should == "value 2"
29
- @obj.to_s.should == "value 2"
30
- proc {@obj.to_s}.should raise_error(RR::Errors::TimesCalledError)
14
+ RR.reset
31
15
  end
32
16
 
33
- it "mocks via block" do
34
- mock @obj do |c|
35
- c.to_s {"a value"}
36
- c.to_sym {:crazy}
17
+ describe "RR mock:" do
18
+ it "mocks via inline call" do
19
+ mock(@obj).to_s {"a value"}
20
+ @obj.to_s.should == "a value"
21
+ lambda {@obj.to_s}.should raise_error(RR::Errors::TimesCalledError)
37
22
  end
38
- @obj.to_s.should == "a value"
39
- @obj.to_sym.should == :crazy
40
- end
41
-
42
- it "has wildcard matchers" do
43
- mock(@obj).foobar(
44
- is_a(String),
45
- anything,
46
- numeric,
47
- boolean,
48
- duck_type(:to_s),
49
- /abc/
50
- ) {"value 1"}.twice
51
- @obj.foobar(
52
- 'hello',
53
- Object.new,
54
- 99,
55
- false,
56
- "My String",
57
- "Tabcola"
58
- ).should == "value 1"
59
- proc do
60
- @obj.foobar(:failure)
61
- end.should raise_error( RR::Errors::DoubleNotFoundError )
62
- end
63
-
64
- it "mocks methods without letters" do
65
- mock(@obj) == 55
66
-
67
- @obj == 55
68
- proc do
69
- @obj == 99
70
- end.should raise_error(RR::Errors::DoubleNotFoundError)
71
- end
72
- end
73
23
 
74
- describe "RR proxy:" do
75
- it_should_behave_like "RR"
24
+ it "allows ordering" do
25
+ mock(@obj).to_s {"value 1"}.ordered
26
+ mock(@obj).to_s {"value 2"}.twice.ordered
27
+ @obj.to_s.should == "value 1"
28
+ @obj.to_s.should == "value 2"
29
+ @obj.to_s.should == "value 2"
30
+ lambda {@obj.to_s}.should raise_error(RR::Errors::TimesCalledError)
31
+ end
76
32
 
77
- it "proxies via inline call" do
78
- expected_to_s_value = @obj.to_s
79
- mock.proxy(@obj).to_s
80
- @obj.to_s.should == expected_to_s_value
81
- proc {@obj.to_s}.should raise_error
82
- end
33
+ it "mocks via block" do
34
+ mock @obj do |c|
35
+ c.to_s {"a value"}
36
+ c.to_sym {:crazy}
37
+ end
38
+ @obj.to_s.should == "a value"
39
+ @obj.to_sym.should == :crazy
40
+ end
83
41
 
84
- it "proxy allows ordering" do
85
- def @obj.to_s(arg)
86
- "Original to_s with arg #{arg}"
42
+ it "has wildcard matchers" do
43
+ mock(@obj).foobar(
44
+ is_a(String),
45
+ anything,
46
+ numeric,
47
+ boolean,
48
+ duck_type(:to_s),
49
+ /abc/
50
+ ) {"value 1"}.twice
51
+ @obj.foobar(
52
+ 'hello',
53
+ Object.new,
54
+ 99,
55
+ false,
56
+ "My String",
57
+ "Tabcola"
58
+ ).should == "value 1"
59
+ lambda do
60
+ @obj.foobar(:failure)
61
+ end.should raise_error( RR::Errors::DoubleNotFoundError )
87
62
  end
88
- mock.proxy(@obj).to_s(:foo).ordered
89
- mock.proxy(@obj).to_s(:bar).twice.ordered
90
63
 
91
- @obj.to_s(:foo).should == "Original to_s with arg foo"
92
- @obj.to_s(:bar).should == "Original to_s with arg bar"
93
- @obj.to_s(:bar).should == "Original to_s with arg bar"
94
- proc {@obj.to_s(:bar)}.should raise_error(RR::Errors::TimesCalledError)
95
- end
64
+ it "mocks methods without letters" do
65
+ mock(@obj) == 55
96
66
 
97
- it "proxy allows ordering" do
98
- def @obj.to_s(arg)
99
- "Original to_s with arg #{arg}"
67
+ @obj == 55
68
+ lambda do
69
+ @obj == 99
70
+ end.should raise_error(RR::Errors::DoubleNotFoundError)
100
71
  end
101
- mock.proxy(@obj).to_s(:foo).ordered
102
- mock.proxy(@obj).to_s(:bar).twice.ordered
103
-
104
- @obj.to_s(:foo).should == "Original to_s with arg foo"
105
- @obj.to_s(:bar).should == "Original to_s with arg bar"
106
- @obj.to_s(:bar).should == "Original to_s with arg bar"
107
- proc {@obj.to_s(:bar)}.should raise_error(RR::Errors::TimesCalledError)
108
72
  end
109
73
 
110
- it "proxies via block" do
111
- def @obj.foobar_1(*args)
112
- :original_value_1
74
+ describe "RR proxy:" do
75
+ it "proxies via inline call" do
76
+ expected_to_s_value = @obj.to_s
77
+ mock.proxy(@obj).to_s
78
+ @obj.to_s.should == expected_to_s_value
79
+ lambda {@obj.to_s}.should raise_error
113
80
  end
114
81
 
115
- def @obj.foobar_2
116
- :original_value_2
82
+ it "proxy allows ordering" do
83
+ def @obj.to_s(arg)
84
+ "Original to_s with arg #{arg}"
85
+ end
86
+ mock.proxy(@obj).to_s(:foo).ordered
87
+ mock.proxy(@obj).to_s(:bar).twice.ordered
88
+
89
+ @obj.to_s(:foo).should == "Original to_s with arg foo"
90
+ @obj.to_s(:bar).should == "Original to_s with arg bar"
91
+ @obj.to_s(:bar).should == "Original to_s with arg bar"
92
+ lambda {@obj.to_s(:bar)}.should raise_error(RR::Errors::TimesCalledError)
117
93
  end
118
94
 
119
- mock.proxy @obj do |c|
120
- c.foobar_1(1)
121
- c.foobar_2
95
+ it "proxy allows ordering" do
96
+ def @obj.to_s(arg)
97
+ "Original to_s with arg #{arg}"
98
+ end
99
+ mock.proxy(@obj).to_s(:foo).ordered
100
+ mock.proxy(@obj).to_s(:bar).twice.ordered
101
+
102
+ @obj.to_s(:foo).should == "Original to_s with arg foo"
103
+ @obj.to_s(:bar).should == "Original to_s with arg bar"
104
+ @obj.to_s(:bar).should == "Original to_s with arg bar"
105
+ lambda {@obj.to_s(:bar)}.should raise_error(RR::Errors::TimesCalledError)
122
106
  end
123
- @obj.foobar_1(1).should == :original_value_1
124
- proc {@obj.foobar_1(:blah)}.should raise_error
125
107
 
126
- @obj.foobar_2.should == :original_value_2
127
- proc {@obj.foobar_2(:blah)}.should raise_error
128
- end
108
+ it "proxies via block" do
109
+ def @obj.foobar_1(*args)
110
+ :original_value_1
111
+ end
129
112
 
130
- it "proxies via block" do
131
- def @obj.foobar_1(*args)
132
- :original_value_1
133
- end
113
+ def @obj.foobar_2
114
+ :original_value_2
115
+ end
134
116
 
135
- def @obj.foobar_2
136
- :original_value_2
137
- end
117
+ mock.proxy @obj do |c|
118
+ c.foobar_1(1)
119
+ c.foobar_2
120
+ end
121
+ @obj.foobar_1(1).should == :original_value_1
122
+ lambda {@obj.foobar_1(:blah)}.should raise_error
138
123
 
139
- mock.proxy @obj do |c|
140
- c.foobar_1(1)
141
- c.foobar_2
124
+ @obj.foobar_2.should == :original_value_2
125
+ lambda {@obj.foobar_2(:blah)}.should raise_error
142
126
  end
143
- @obj.foobar_1(1).should == :original_value_1
144
- proc {@obj.foobar_1(:blah)}.should raise_error
145
127
 
146
- @obj.foobar_2.should == :original_value_2
147
- proc {@obj.foobar_2(:blah)}.should raise_error
148
- end
149
- end
128
+ it "proxies via block" do
129
+ def @obj.foobar_1(*args)
130
+ :original_value_1
131
+ end
132
+
133
+ def @obj.foobar_2
134
+ :original_value_2
135
+ end
150
136
 
151
- describe "RR stub:" do
152
- it_should_behave_like "RR"
137
+ mock.proxy @obj do |c|
138
+ c.foobar_1(1)
139
+ c.foobar_2
140
+ end
141
+ @obj.foobar_1(1).should == :original_value_1
142
+ lambda {@obj.foobar_1(:blah)}.should raise_error
153
143
 
154
- it "stubs via inline call" do
155
- stub(@obj).to_s {"a value"}
156
- @obj.to_s.should == "a value"
144
+ @obj.foobar_2.should == :original_value_2
145
+ lambda {@obj.foobar_2(:blah)}.should raise_error
146
+ end
157
147
  end
158
148
 
159
- it "allows ordering" do
160
- stub(@obj).to_s {"value 1"}.once.ordered
161
- stub(@obj).to_s {"value 2"}.once.ordered
149
+ describe "RR stub:" do
150
+ it "stubs via inline call" do
151
+ stub(@obj).to_s {"a value"}
152
+ @obj.to_s.should == "a value"
153
+ end
162
154
 
163
- @obj.to_s.should == "value 1"
164
- @obj.to_s.should == "value 2"
165
- end
155
+ it "allows ordering" do
156
+ stub(@obj).to_s {"value 1"}.once.ordered
157
+ stub(@obj).to_s {"value 2"}.once.ordered
166
158
 
167
- it "stubs via block" do
168
- stub @obj do |d|
169
- d.to_s {"a value"}
170
- d.to_sym {:crazy}
159
+ @obj.to_s.should == "value 1"
160
+ @obj.to_s.should == "value 2"
171
161
  end
172
- @obj.to_s.should == "a value"
173
- @obj.to_sym.should == :crazy
174
- end
175
162
 
176
- it "stubs instance_of" do
177
- stub.instance_of(Date) do |o|
178
- o.to_s {"The Date"}
163
+ it "stubs via block" do
164
+ stub @obj do |d|
165
+ d.to_s {"a value"}
166
+ d.to_sym {:crazy}
167
+ end
168
+ @obj.to_s.should == "a value"
169
+ @obj.to_sym.should == :crazy
170
+ end
171
+
172
+ it "stubs instance_of" do
173
+ stub.instance_of(HighLevelSpec) do |o|
174
+ o.to_s {"High Level Spec"}
175
+ end
176
+ HighLevelSpec.new.to_s.should == "High Level Spec"
179
177
  end
180
- Date.new.to_s.should == "The Date"
181
- end
182
178
 
183
- it "stubs methods without letters" do
184
- stub(@obj).__send__(:==) {:equality}
185
- (@obj == 55).should == :equality
179
+ it "stubs methods without letters" do
180
+ stub(@obj).__send__(:==) {:equality}
181
+ (@obj == 55).should == :equality
182
+ end
186
183
  end
187
184
  end