jferris-rr 0.7.1.0.1239654108

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. data/CHANGES +196 -0
  2. data/README.rdoc +329 -0
  3. data/Rakefile +77 -0
  4. data/lib/rr.rb +84 -0
  5. data/lib/rr/adapters/rr_methods.rb +122 -0
  6. data/lib/rr/adapters/rspec.rb +58 -0
  7. data/lib/rr/adapters/test_unit.rb +29 -0
  8. data/lib/rr/double.rb +212 -0
  9. data/lib/rr/double_definitions/child_double_definition_creator.rb +27 -0
  10. data/lib/rr/double_definitions/double_definition.rb +346 -0
  11. data/lib/rr/double_definitions/double_definition_creator.rb +167 -0
  12. data/lib/rr/double_definitions/double_definition_creator_proxy.rb +37 -0
  13. data/lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb +15 -0
  14. data/lib/rr/double_definitions/strategies/implementation/proxy.rb +62 -0
  15. data/lib/rr/double_definitions/strategies/implementation/reimplementation.rb +14 -0
  16. data/lib/rr/double_definitions/strategies/implementation/strongly_typed_reimplementation.rb +17 -0
  17. data/lib/rr/double_definitions/strategies/scope/instance.rb +15 -0
  18. data/lib/rr/double_definitions/strategies/scope/instance_of_class.rb +46 -0
  19. data/lib/rr/double_definitions/strategies/scope/scope_strategy.rb +15 -0
  20. data/lib/rr/double_definitions/strategies/strategy.rb +70 -0
  21. data/lib/rr/double_definitions/strategies/verification/dont_allow.rb +34 -0
  22. data/lib/rr/double_definitions/strategies/verification/mock.rb +44 -0
  23. data/lib/rr/double_definitions/strategies/verification/stub.rb +45 -0
  24. data/lib/rr/double_definitions/strategies/verification/verification_strategy.rb +15 -0
  25. data/lib/rr/double_injection.rb +143 -0
  26. data/lib/rr/double_matches.rb +51 -0
  27. data/lib/rr/errors/argument_equality_error.rb +6 -0
  28. data/lib/rr/errors/double_definition_error.rb +6 -0
  29. data/lib/rr/errors/double_not_found_error.rb +6 -0
  30. data/lib/rr/errors/double_order_error.rb +6 -0
  31. data/lib/rr/errors/rr_error.rb +20 -0
  32. data/lib/rr/errors/spy_verification_errors/double_injection_not_found_error.rb +8 -0
  33. data/lib/rr/errors/spy_verification_errors/invocation_count_error.rb +8 -0
  34. data/lib/rr/errors/spy_verification_errors/spy_verification_error.rb +8 -0
  35. data/lib/rr/errors/subject_does_not_implement_method_error.rb +6 -0
  36. data/lib/rr/errors/subject_has_different_arity_error.rb +6 -0
  37. data/lib/rr/errors/times_called_error.rb +6 -0
  38. data/lib/rr/expectations/any_argument_expectation.rb +21 -0
  39. data/lib/rr/expectations/argument_equality_expectation.rb +41 -0
  40. data/lib/rr/expectations/times_called_expectation.rb +57 -0
  41. data/lib/rr/hash_with_object_id_key.rb +41 -0
  42. data/lib/rr/recorded_calls.rb +103 -0
  43. data/lib/rr/space.rb +123 -0
  44. data/lib/rr/spy_verification.rb +48 -0
  45. data/lib/rr/spy_verification_proxy.rb +18 -0
  46. data/lib/rr/times_called_matchers/any_times_matcher.rb +18 -0
  47. data/lib/rr/times_called_matchers/at_least_matcher.rb +15 -0
  48. data/lib/rr/times_called_matchers/at_most_matcher.rb +23 -0
  49. data/lib/rr/times_called_matchers/integer_matcher.rb +19 -0
  50. data/lib/rr/times_called_matchers/non_terminal.rb +27 -0
  51. data/lib/rr/times_called_matchers/proc_matcher.rb +11 -0
  52. data/lib/rr/times_called_matchers/range_matcher.rb +21 -0
  53. data/lib/rr/times_called_matchers/terminal.rb +20 -0
  54. data/lib/rr/times_called_matchers/times_called_matcher.rb +44 -0
  55. data/lib/rr/wildcard_matchers/anything.rb +18 -0
  56. data/lib/rr/wildcard_matchers/boolean.rb +23 -0
  57. data/lib/rr/wildcard_matchers/duck_type.rb +32 -0
  58. data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
  59. data/lib/rr/wildcard_matchers/is_a.rb +25 -0
  60. data/lib/rr/wildcard_matchers/numeric.rb +13 -0
  61. data/lib/rr/wildcard_matchers/range.rb +7 -0
  62. data/lib/rr/wildcard_matchers/regexp.rb +7 -0
  63. data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
  64. data/spec/core_spec_suite.rb +19 -0
  65. data/spec/environment_fixture_setup.rb +6 -0
  66. data/spec/high_level_spec.rb +368 -0
  67. data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
  68. data/spec/rr/adapters/rr_methods_creator_spec.rb +149 -0
  69. data/spec/rr/adapters/rr_methods_space_spec.rb +115 -0
  70. data/spec/rr/adapters/rr_methods_spec_helper.rb +11 -0
  71. data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +17 -0
  72. data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +112 -0
  73. data/spec/rr/double_definitions/double_definition_creator_proxy_spec.rb +155 -0
  74. data/spec/rr/double_definitions/double_definition_creator_spec.rb +502 -0
  75. data/spec/rr/double_definitions/double_definition_spec.rb +1159 -0
  76. data/spec/rr/double_injection/double_injection_bind_spec.rb +111 -0
  77. data/spec/rr/double_injection/double_injection_dispatching_spec.rb +244 -0
  78. data/spec/rr/double_injection/double_injection_has_original_method_spec.rb +55 -0
  79. data/spec/rr/double_injection/double_injection_reset_spec.rb +90 -0
  80. data/spec/rr/double_injection/double_injection_spec.rb +77 -0
  81. data/spec/rr/double_injection/double_injection_verify_spec.rb +29 -0
  82. data/spec/rr/double_spec.rb +352 -0
  83. data/spec/rr/errors/rr_error_spec.rb +67 -0
  84. data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
  85. data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +14 -0
  86. data/spec/rr/expectations/argument_equality_expectation_spec.rb +135 -0
  87. data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +34 -0
  88. data/spec/rr/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
  89. data/spec/rr/expectations/hash_including_spec.rb +17 -0
  90. data/spec/rr/expectations/satisfy_argument_equality_expectation_spec.rb +59 -0
  91. data/spec/rr/expectations/satisfy_spec.rb +14 -0
  92. data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +46 -0
  93. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +69 -0
  94. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +71 -0
  95. data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +23 -0
  96. data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +104 -0
  97. data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +81 -0
  98. data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +83 -0
  99. data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +38 -0
  100. data/spec/rr/rspec/invocation_matcher_spec.rb +279 -0
  101. data/spec/rr/rspec/rspec_adapter_spec.rb +66 -0
  102. data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +31 -0
  103. data/spec/rr/rspec/rspec_backtrace_tweaking_spec_fixture.rb +11 -0
  104. data/spec/rr/rspec/rspec_usage_spec.rb +86 -0
  105. data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
  106. data/spec/rr/space/space_spec.rb +542 -0
  107. data/spec/rr/test_unit/test_helper.rb +7 -0
  108. data/spec/rr/test_unit/test_unit_backtrace_test.rb +35 -0
  109. data/spec/rr/test_unit/test_unit_integration_test.rb +57 -0
  110. data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +47 -0
  111. data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +55 -0
  112. data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +70 -0
  113. data/spec/rr/times_called_matchers/integer_matcher_spec.rb +70 -0
  114. data/spec/rr/times_called_matchers/proc_matcher_spec.rb +55 -0
  115. data/spec/rr/times_called_matchers/range_matcher_spec.rb +76 -0
  116. data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +118 -0
  117. data/spec/rr/wildcard_matchers/anything_spec.rb +24 -0
  118. data/spec/rr/wildcard_matchers/boolean_spec.rb +36 -0
  119. data/spec/rr/wildcard_matchers/duck_type_spec.rb +52 -0
  120. data/spec/rr/wildcard_matchers/is_a_spec.rb +32 -0
  121. data/spec/rr/wildcard_matchers/numeric_spec.rb +32 -0
  122. data/spec/rr/wildcard_matchers/range_spec.rb +35 -0
  123. data/spec/rr/wildcard_matchers/regexp_spec.rb +43 -0
  124. data/spec/rr_spec.rb +28 -0
  125. data/spec/rspec_spec_suite.rb +16 -0
  126. data/spec/spec_helper.rb +107 -0
  127. data/spec/spec_suite.rb +27 -0
  128. data/spec/spy_verification_spec.rb +129 -0
  129. data/spec/test_unit_spec_suite.rb +21 -0
  130. metadata +187 -0
@@ -0,0 +1,37 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ class DoubleDefinitionCreatorProxy
4
+ class << self
5
+ def blank_slate_methods
6
+ instance_methods.each do |m|
7
+ unless m =~ /^_/ || m.to_s == 'object_id' || m.to_s == 'respond_to?' || m.to_s == 'method_missing'
8
+ alias_method "__blank_slated_#{m}", m
9
+ undef_method m
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ def initialize(creator, &block) #:nodoc:
16
+ @creator = creator
17
+ respond_to?(:class) ? self.class.blank_slate_methods : __blank_slated_class.blank_slate_methods
18
+
19
+ if block_given?
20
+ if block.arity == 1
21
+ yield(self)
22
+ else
23
+ respond_to?(:instance_eval) ? instance_eval(&block) : __blank_slated_instance_eval(&block)
24
+ end
25
+ end
26
+ end
27
+
28
+ def method_missing(method_name, *args, &block)
29
+ @creator.create(method_name, *args, &block)
30
+ end
31
+
32
+ def __creator__
33
+ @creator
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Implementation
5
+ class ImplementationStrategy < Strategy
6
+ class << self
7
+ def register_self_at_double_definition_creator(domain_name)
8
+ DoubleDefinitionCreator.register_implementation_strategy_class(self, domain_name)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,62 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Implementation
5
+ # This method add proxy capabilities to the Double. proxy can be called
6
+ # with mock or stub.
7
+ #
8
+ # mock.proxy(controller.template).render(:partial => "my/socks")
9
+ #
10
+ # stub.proxy(controller.template).render(:partial => "my/socks") do |html|
11
+ # html.should include("My socks are wet")
12
+ # html
13
+ # end
14
+ #
15
+ # mock.proxy(controller.template).render(:partial => "my/socks") do |html|
16
+ # html.should include("My socks are wet")
17
+ # "My new return value"
18
+ # end
19
+ #
20
+ # mock.proxy also takes a block for definitions.
21
+ # mock.proxy(subject) do
22
+ # render(:partial => "my/socks")
23
+ #
24
+ # render(:partial => "my/socks") do |html|
25
+ # html.should include("My socks are wet")
26
+ # html
27
+ # end
28
+ #
29
+ # render(:partial => "my/socks") do |html|
30
+ # html.should include("My socks are wet")
31
+ # html
32
+ # end
33
+ #
34
+ # render(:partial => "my/socks") do |html|
35
+ # html.should include("My socks are wet")
36
+ # "My new return value"
37
+ # end
38
+ # end
39
+ #
40
+ # Passing a block to the Double (after the method name and arguments)
41
+ # allows you to intercept the return value.
42
+ # The return value can be modified, validated, and/or overridden by
43
+ # passing in a block. The return value of the block will replace
44
+ # the actual return value.
45
+ #
46
+ # mock.proxy(controller.template).render(:partial => "my/socks") do |html|
47
+ # html.should include("My socks are wet")
48
+ # "My new return value"
49
+ # end
50
+ class Proxy < ImplementationStrategy
51
+ register("proxy", :probe)
52
+
53
+ protected
54
+ def do_call
55
+ definition.implemented_by_original_method
56
+ definition.after_call(&handler) if handler
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,14 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Implementation
5
+ class Reimplementation < ImplementationStrategy
6
+ protected
7
+ def do_call
8
+ reimplementation
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Implementation
5
+ class StronglyTypedReimplementation < Reimplementation
6
+ register("strong")
7
+
8
+ protected
9
+ def do_call
10
+ super
11
+ definition.verify_method_signature
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Scope
5
+ class Instance < ScopeStrategy
6
+ protected
7
+ def do_call
8
+ double_injection = space.double_injection(subject, method_name)
9
+ Double.new(double_injection, definition)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Scope
5
+ # Calling instance_of will cause all instances of the passed in Class
6
+ # to have the Double defined.
7
+ #
8
+ # The following example mocks all User's valid? method and return false.
9
+ # mock.instance_of(User).valid? {false}
10
+ #
11
+ # The following example mocks and proxies User#projects and returns the
12
+ # first 3 projects.
13
+ # mock.instance_of(User).projects do |projects|
14
+ # projects[0..2]
15
+ # end
16
+ class InstanceOfClass < ScopeStrategy
17
+ register "instance_of"
18
+
19
+ def initialize(*args)
20
+ super
21
+
22
+ if !double_definition_creator.no_subject? && !double_definition_creator.subject.is_a?(Class)
23
+ raise ArgumentError, "instance_of only accepts class objects"
24
+ end
25
+ end
26
+
27
+ protected
28
+ def do_call
29
+ class_handler = lambda do |return_value|
30
+ #####
31
+ double_injection = space.double_injection(return_value, method_name)
32
+ Double.new(double_injection, definition)
33
+ #####
34
+ return_value
35
+ end
36
+
37
+ instance_of_subject_creator = DoubleDefinitionCreator.new
38
+ instance_of_subject_creator.strong if definition.verify_method_signature?
39
+ instance_of_subject_creator.stub.proxy(subject)
40
+ instance_of_subject_creator.create(:new, &class_handler)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,15 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Scope
5
+ class ScopeStrategy < Strategy
6
+ class << self
7
+ def register_self_at_double_definition_creator(domain_name)
8
+ DoubleDefinitionCreator.register_scope_strategy_class(self, domain_name)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,70 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ class Strategy
5
+ class << self
6
+ attr_reader :domain_name
7
+ def register(domain_name, *alias_method_names)
8
+ @domain_name = domain_name
9
+ register_self_at_double_definition_creator(domain_name)
10
+ DoubleDefinitionCreator.class_eval do
11
+ alias_method_names.each do |alias_method_name|
12
+ alias_method alias_method_name, domain_name
13
+ end
14
+ end
15
+ RR::Adapters::RRMethods.register_strategy_class(self, domain_name)
16
+ DoubleDefinition.register_strategy_class(self, domain_name)
17
+ RR::Adapters::RRMethods.class_eval do
18
+ alias_method_names.each do |alias_method_name|
19
+ alias_method alias_method_name, domain_name
20
+ end
21
+ end
22
+ end
23
+
24
+ def register_self_at_double_definition_creator
25
+ end
26
+ end
27
+
28
+ attr_reader :double_definition_creator, :definition, :method_name, :args, :handler
29
+ include Space::Reader
30
+
31
+ def initialize(double_definition_creator)
32
+ @double_definition_creator = double_definition_creator
33
+ end
34
+
35
+ def call(definition, method_name, args, handler)
36
+ @definition, @method_name, @args, @handler = definition, method_name, args, handler
37
+ do_call
38
+ end
39
+
40
+ def name
41
+ self.class.domain_name
42
+ end
43
+
44
+ def verify_subject(subject)
45
+ end
46
+
47
+ protected
48
+ def do_call
49
+ raise NotImplementedError
50
+ end
51
+
52
+ def permissive_argument
53
+ if args.empty?
54
+ definition.with_any_args
55
+ else
56
+ definition.with(*args)
57
+ end
58
+ end
59
+
60
+ def reimplementation
61
+ definition.returns(&handler)
62
+ end
63
+
64
+ def subject
65
+ definition.subject
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,34 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Verification
5
+ # This method sets the Double to have a dont_allow strategy.
6
+ # A dont_allow strategy sets the default state of the Double
7
+ # to expect never to be called. The Double's expectations can be
8
+ # changed.
9
+ #
10
+ # The following example sets the expectation that subject.method_name
11
+ # will never be called with arg1 and arg2.
12
+ #
13
+ # do_not_allow(subject).method_name(arg1, arg2)
14
+ #
15
+ # dont_allow also supports a block sytnax.
16
+ # dont_allow(subject) do |m|
17
+ # m.method1 # Do not allow method1 with any arguments
18
+ # m.method2(arg1, arg2) # Do not allow method2 with arguments arg1 and arg2
19
+ # m.method3.with_no_args # Do not allow method3 with no arguments
20
+ # end
21
+ class DontAllow < VerificationStrategy
22
+ register("dont_allow", :do_not_allow, :dont_call, :do_not_call)
23
+
24
+ protected
25
+ def do_call
26
+ definition.never
27
+ permissive_argument
28
+ reimplementation
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Verification
5
+ # This method sets the Double to have a mock strategy. A mock strategy
6
+ # sets the default state of the Double to expect the method call
7
+ # with arguments exactly one time. The Double's expectations can be
8
+ # changed.
9
+ #
10
+ # This method can be chained with proxy.
11
+ # mock.proxy(subject).method_name_1
12
+ # or
13
+ # proxy.mock(subject).method_name_1
14
+ #
15
+ # When passed the subject, a DoubleDefinitionCreatorProxy is returned. Passing
16
+ # a method with arguments to the proxy will set up expectations that
17
+ # the a call to the subject's method with the arguments will happen,
18
+ # and return the prescribed value.
19
+ # mock(subject).method_name_1 {return_value_1}
20
+ # mock(subject).method_name_2(arg1, arg2) {return_value_2}
21
+ #
22
+ # When passed the subject and the method_name, this method returns
23
+ # a mock Double with the method already set.
24
+ #
25
+ # mock(subject, :method_name_1) {return_value_1}
26
+ # mock(subject, :method_name_2).with(arg1, arg2) {return_value_2}
27
+ #
28
+ # mock also takes a block for definitions.
29
+ # mock(subject) do
30
+ # method_name_1 {return_value_1}
31
+ # method_name_2(arg_1, arg_2) {return_value_2}
32
+ # end
33
+ class Mock < VerificationStrategy
34
+ register "mock"
35
+
36
+ protected
37
+ def do_call
38
+ definition.with(*args).once
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,45 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Verification
5
+ # This method sets the Double to have a stub strategy. A stub strategy
6
+ # sets the default state of the Double to expect the method call
7
+ # with any arguments any number of times. The Double's
8
+ # expectations can be changed.
9
+ #
10
+ # This method can be chained with proxy.
11
+ # stub.proxy(subject).method_name_1
12
+ # or
13
+ # proxy.stub(subject).method_name_1
14
+ #
15
+ # When passed the subject, a DoubleDefinitionCreatorProxy is returned. Passing
16
+ # a method with arguments to the proxy will set up expectations that
17
+ # the a call to the subject's method with the arguments will happen,
18
+ # and return the prescribed value.
19
+ # stub(subject).method_name_1 {return_value_1}
20
+ # stub(subject).method_name_2(arg_1, arg_2) {return_value_2}
21
+ #
22
+ # When passed the subject and the method_name, this method returns
23
+ # a stub Double with the method already set.
24
+ #
25
+ # mock(subject, :method_name_1) {return_value_1}
26
+ # mock(subject, :method_name_2).with(arg1, arg2) {return_value_2}
27
+ #
28
+ # stub also takes a block for definitions.
29
+ # stub(subject) do
30
+ # method_name_1 {return_value_1}
31
+ # method_name_2(arg_1, arg_2) {return_value_2}
32
+ # end
33
+ class Stub < VerificationStrategy
34
+ register "stub"
35
+
36
+ protected
37
+ def do_call
38
+ definition.any_number_of_times
39
+ permissive_argument
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ module RR
2
+ module DoubleDefinitions
3
+ module Strategies
4
+ module Verification
5
+ class VerificationStrategy < Strategy
6
+ class << self
7
+ def register_self_at_double_definition_creator(domain_name)
8
+ DoubleDefinitionCreator.register_verification_strategy_class(self, domain_name)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end