convenient_service 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +1 -1
  5. data/ROADMAP.md +26 -4
  6. data/Taskfile.yml +30 -0
  7. data/convenient_service.gemspec +5 -2
  8. data/lib/convenient_service/aliases.rb +2 -0
  9. data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_active_model_attribute_assignment/concern.rb +0 -2
  10. data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_dry_initializer/concern.rb +0 -2
  11. data/lib/convenient_service/common/plugins/has_around_callbacks/middleware.rb +24 -4
  12. data/lib/convenient_service/common/plugins/has_callbacks/container.rb +17 -0
  13. data/lib/convenient_service/common/plugins/has_callbacks/entities/callback.rb +98 -5
  14. data/lib/convenient_service/common/plugins/has_callbacks/middleware.rb +25 -4
  15. data/lib/convenient_service/common/plugins/has_callbacks.rb +1 -0
  16. data/lib/convenient_service/common/plugins.rb +1 -1
  17. data/lib/convenient_service/configs/standard.rb +25 -12
  18. data/lib/convenient_service/core/entities/config/entities/method_middlewares/entities/container/commands/resolve_methods_middlewares_callers.rb +1 -24
  19. data/lib/convenient_service/dependencies.rb +19 -0
  20. data/lib/convenient_service/examples/dry/gemfile/dry_service/config.rb +4 -2
  21. data/lib/convenient_service/examples/rails/gemfile/rails_service/config.rb +4 -2
  22. data/lib/convenient_service/examples/rails/gemfile/services/format.rb +35 -6
  23. data/lib/convenient_service/examples/rails/gemfile/services/format_header.rb +1 -2
  24. data/lib/convenient_service/examples/rails/gemfile/services/merge_sections.rb +25 -0
  25. data/lib/convenient_service/examples/rails/gemfile/services/replace_file_content.rb +37 -0
  26. data/lib/convenient_service/examples/rails/gemfile/services.rb +8 -4
  27. data/lib/convenient_service/examples/standard/gemfile/services/format.rb +45 -6
  28. data/lib/convenient_service/examples/standard/gemfile/services/merge_sections.rb +52 -0
  29. data/lib/convenient_service/examples/standard/gemfile/services/replace_file_content.rb +48 -0
  30. data/lib/convenient_service/examples/standard/gemfile/services.rb +8 -4
  31. data/lib/convenient_service/examples/standard/request_params/constants.rb +15 -0
  32. data/lib/convenient_service/examples/standard/request_params/entities/description.rb +40 -0
  33. data/lib/convenient_service/examples/standard/request_params/entities/format.rb +40 -0
  34. data/lib/convenient_service/examples/standard/request_params/entities/id.rb +47 -0
  35. data/lib/convenient_service/examples/standard/request_params/entities/logger.rb +21 -0
  36. data/lib/convenient_service/examples/standard/request_params/entities/request.rb +23 -0
  37. data/lib/convenient_service/examples/standard/request_params/entities/source.rb +40 -0
  38. data/lib/convenient_service/examples/standard/request_params/entities/tag.rb +40 -0
  39. data/lib/convenient_service/examples/standard/request_params/entities/title.rb +40 -0
  40. data/lib/convenient_service/examples/standard/request_params/entities.rb +11 -0
  41. data/lib/convenient_service/examples/standard/request_params/services/apply_default_param_values.rb +26 -0
  42. data/lib/convenient_service/examples/standard/request_params/services/cast_params.rb +38 -0
  43. data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb +70 -0
  44. data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb +62 -0
  45. data/lib/convenient_service/examples/standard/request_params/services/filter_out_unpermitted_params.rb +26 -0
  46. data/lib/convenient_service/examples/standard/request_params/services/log_request_params.rb +54 -0
  47. data/lib/convenient_service/examples/standard/request_params/services/merge_params.rb +26 -0
  48. data/lib/convenient_service/examples/standard/request_params/services/prepare.rb +65 -0
  49. data/lib/convenient_service/examples/standard/request_params/services/validate_casted_params.rb +94 -0
  50. data/lib/convenient_service/examples/standard/request_params/services/validate_uncasted_params.rb +72 -0
  51. data/lib/convenient_service/examples/standard/request_params/services.rb +13 -0
  52. data/lib/convenient_service/examples/standard/request_params/utils/array/wrap.rb +46 -0
  53. data/lib/convenient_service/examples/standard/request_params/utils/array.rb +21 -0
  54. data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_body.rb +42 -0
  55. data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_path.rb +40 -0
  56. data/lib/convenient_service/examples/standard/request_params/utils/http/request.rb +28 -0
  57. data/lib/convenient_service/examples/standard/request_params/utils/http.rb +3 -0
  58. data/lib/convenient_service/examples/standard/request_params/utils/integer/safe_parse.rb +31 -0
  59. data/lib/convenient_service/examples/standard/request_params/utils/integer.rb +25 -0
  60. data/lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb +40 -0
  61. data/lib/convenient_service/examples/standard/request_params/utils/json.rb +21 -0
  62. data/lib/convenient_service/examples/standard/request_params/utils/object/blank.rb +34 -0
  63. data/lib/convenient_service/examples/standard/request_params/utils/object/present.rb +31 -0
  64. data/lib/convenient_service/examples/standard/request_params/utils/object.rb +26 -0
  65. data/lib/convenient_service/examples/standard/request_params/utils.rb +7 -0
  66. data/lib/convenient_service/examples/standard/request_params.rb +48 -0
  67. data/lib/convenient_service/factories/arguments.rb +43 -0
  68. data/lib/convenient_service/factories/results.rb +214 -0
  69. data/lib/convenient_service/factories/services.rb +189 -0
  70. data/lib/convenient_service/factories/step/instance.rb +32 -0
  71. data/lib/convenient_service/factories/step.rb +3 -0
  72. data/lib/convenient_service/factories/steps.rb +126 -0
  73. data/lib/convenient_service/factories.rb +22 -0
  74. data/lib/convenient_service/factory.rb +21 -0
  75. data/lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb +2 -2
  76. data/lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb +2 -2
  77. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands/generate_printable_method.rb +50 -0
  78. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands.rb +3 -0
  79. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb +78 -0
  80. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/generate_printable_arguments.rb +100 -0
  81. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands.rb +4 -0
  82. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments.rb +95 -0
  83. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/base.rb +87 -0
  84. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/return_its_value.rb +129 -0
  85. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_any_arguments.rb +37 -0
  86. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_concrete_arguments.rb +37 -0
  87. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/without_arguments.rb +37 -0
  88. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/base.rb +41 -0
  89. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/with_calling_original.rb +30 -0
  90. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/without_calling_original.rb +30 -0
  91. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings.rb +12 -0
  92. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection/errors.rb +57 -0
  93. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection.rb +171 -0
  94. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/delegation.rb +79 -0
  95. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities.rb +5 -0
  96. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher.rb +276 -0
  97. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities.rb +3 -0
  98. data/lib/convenient_service/rspec/matchers/custom/delegate_to.rb +66 -233
  99. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_method_step.rb +74 -0
  100. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_result_method_step.rb +66 -0
  101. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/find_result_service_step.rb +48 -0
  102. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/generate_expected_step_part.rb +42 -0
  103. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/generate_got_step_part.rb +42 -0
  104. data/lib/convenient_service/rspec/matchers/custom/results/base/commands/match_result_step.rb +89 -0
  105. data/lib/convenient_service/rspec/matchers/custom/results/base/commands.rb +10 -0
  106. data/lib/convenient_service/rspec/matchers/custom/results/base/errors.rb +35 -0
  107. data/lib/convenient_service/rspec/matchers/custom/results/base.rb +73 -12
  108. data/lib/convenient_service/rspec/matchers/custom/singleton_prepend_module.rb +79 -0
  109. data/lib/convenient_service/rspec/matchers/custom.rb +1 -0
  110. data/lib/convenient_service/rspec/matchers/singleton_prepend_module.rb +13 -0
  111. data/lib/convenient_service/rspec/matchers.rb +2 -0
  112. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/concern.rb +85 -0
  113. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/initialize/middleware.rb +27 -0
  114. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/initialize.rb +3 -0
  115. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/to_kwargs/middleware.rb +25 -0
  116. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result/to_kwargs.rb +3 -0
  117. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_have_parent_result.rb +5 -0
  118. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/concern/instance_methods.rb +7 -1
  119. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_jsend_status_and_attributes/entities/data.rb +4 -0
  120. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/concern.rb +27 -0
  121. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/initialize/middleware.rb +27 -0
  122. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/initialize.rb +3 -0
  123. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/to_kwargs/middleware.rb +25 -0
  124. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step/to_kwargs.rb +3 -0
  125. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins/has_step.rb +5 -0
  126. data/lib/convenient_service/service/plugins/has_result/entities/result/plugins.rb +2 -0
  127. data/lib/convenient_service/service/plugins/has_result_method_steps.rb +0 -2
  128. data/lib/convenient_service/service/plugins/has_result_steps/concern.rb +40 -4
  129. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method.rb +3 -0
  130. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_caller.rb +8 -1
  131. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_direction.rb +3 -0
  132. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_key.rb +8 -1
  133. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_name.rb +9 -5
  134. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/define_method_in_container.rb +2 -2
  135. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/concern/instance_methods.rb +4 -0
  136. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/base.rb +4 -0
  137. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb +74 -0
  138. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands.rb +3 -0
  139. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment.rb +50 -0
  140. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers.rb +1 -0
  141. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values/reassignment.rb +43 -0
  142. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values.rb +3 -0
  143. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities.rb +1 -0
  144. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/errors.rb +22 -0
  145. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/concern/instance_methods.rb +39 -3
  146. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins/can_have_parent_result/middleware.rb +23 -0
  147. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins/can_have_parent_result.rb +3 -0
  148. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/plugins.rb +1 -0
  149. data/lib/convenient_service/service/plugins/has_result_steps/entities/step_collection.rb +13 -0
  150. data/lib/convenient_service/service/plugins/has_result_steps/middleware.rb +18 -4
  151. data/lib/convenient_service/services/run_method_in_organizer.rb +28 -0
  152. data/lib/convenient_service/services/run_own_method_in_organizer.rb +64 -0
  153. data/lib/convenient_service/{service/plugins/has_result_method_steps/services.rb → services.rb} +0 -1
  154. data/lib/convenient_service/support/arguments/null_arguments.rb +28 -0
  155. data/lib/convenient_service/support/arguments.rb +87 -0
  156. data/lib/convenient_service/support/copyable.rb +6 -2
  157. data/lib/convenient_service/support/dependency_container/commands/assert_valid_scope.rb +32 -0
  158. data/lib/convenient_service/support/dependency_container/commands/import_method.rb +13 -23
  159. data/lib/convenient_service/support/dependency_container/commands.rb +1 -0
  160. data/lib/convenient_service/support/dependency_container/constants.rb +4 -2
  161. data/lib/convenient_service/support/dependency_container/entities/method.rb +2 -9
  162. data/lib/convenient_service/support/dependency_container/entities/namespace_collection.rb +0 -9
  163. data/lib/convenient_service/support/dependency_container/errors.rb +26 -1
  164. data/lib/convenient_service/support/dependency_container/export.rb +8 -0
  165. data/lib/convenient_service/support/dependency_container/import.rb +3 -1
  166. data/lib/convenient_service/support/not_passed.rb +3 -1
  167. data/lib/convenient_service/support/version/null_version.rb +7 -0
  168. data/lib/convenient_service/support/version.rb +11 -1
  169. data/lib/convenient_service/support.rb +1 -0
  170. data/lib/convenient_service/utils/object/instance_variable_delete.rb +41 -0
  171. data/lib/convenient_service/utils/object/instance_variable_fetch.rb +4 -0
  172. data/lib/convenient_service/utils/object.rb +9 -0
  173. data/lib/convenient_service/utils/proc/display.rb +43 -0
  174. data/lib/convenient_service/utils/proc.rb +5 -0
  175. data/lib/convenient_service/version.rb +1 -1
  176. data/lib/convenient_service.rb +6 -0
  177. data/logo.png +0 -0
  178. metadata +154 -10
  179. data/lib/convenient_service/service/plugins/has_result_method_steps/errors.rb +0 -23
  180. data/lib/convenient_service/service/plugins/has_result_method_steps/services/method_step_config.rb +0 -55
  181. data/lib/convenient_service/service/plugins/has_result_method_steps/services/run_method_in_organizer.rb +0 -30
  182. data/lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb +0 -52
@@ -1,291 +1,124 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "delegate_to/entities"
4
+
3
5
  ##
4
- # IMPORTANT: This matcher has a dedicated end-user doc. Do NOT forget to update it when needed.
5
- # https://github.com/marian13/convenient_service_docs/blob/main/docs/api/tests/rspec/matchers/delegate_to.mdx
6
- #
7
- # TODO: Refactor into composition:
8
- # - Ability to compose when `delegate_to` is used `without_arguments`.
9
- # - Ability to compose when `delegate_to` is used `with_arguments`.
10
- # - Ability to compose when `delegate_to` is used `and_return_its_value`.
11
- #
12
- # TODO: Refactor to NOT use `expect` inside this matcher.
13
- # This way the matcher will return true or false, but never raise exceptions (descendant of Exception, not StandardError).
14
- # Then it will be easier to developer a fully comprehensive spec suite for `delegate_to`.
6
+ # @internal
7
+ # IMPORTANT: This matcher has a dedicated end-user doc. Do NOT forget to update it when needed.
8
+ # https://github.com/marian13/convenient_service_docs/blob/main/docs/api/tests/rspec/matchers/delegate_to.mdx
15
9
  #
16
10
  module ConvenientService
17
11
  module RSpec
18
12
  module Matchers
19
13
  module Custom
20
14
  ##
21
- # specify {
22
- # expect { method_class.cast(other, **options) }
23
- # .to delegate_to(ConvenientService::Service::Plugins::HasResultSteps::Entities::Method::Commands::CastMethod, :call)
24
- # .with_arguments(other: other, options: options)
25
- # .and_return_its_value
26
- # }
27
- #
28
- # { method_class.cast(other, **options) }
29
- # # => block_expectation
30
- #
31
- # ConvenientService::Service::Plugins::HasResultSteps::Entities::Method::Commands::CastMethod
32
- # # => object
33
- #
34
- # :call
35
- # #=> method
36
- #
37
- # (other: other, options: options)
38
- # # => chain[:with]
39
- #
40
- # and_return_its_value
41
- # # => chain[:and_return_its_value]
42
- #
43
- # NOTE: A similar (with different behaviour) matcher exists in `saharspec`.
44
- # https://github.com/zverok/saharspec#send_messageobject-method-matcher
15
+ # @internal
16
+ # NOTE: This is a Facade class.
17
+ # https://refactoring.guru/design-patterns/facade
45
18
  #
46
19
  class DelegateTo
20
+ include Support::Delegate
21
+
47
22
  ##
48
- # NOTE: `include ::RSpec::Expectations`.
49
- # - https://github.com/rspec/rspec-expectations/blob/v3.11.0/lib/rspec/expectations.rb
50
- # - https://github.com/rspec/rspec-expectations/blob/main/lib/rspec/expectations.rb#L60
23
+ # @return [Boolean]
51
24
  #
52
- include ::RSpec::Expectations
25
+ delegate :matches?, to: :matcher
53
26
 
54
27
  ##
55
- # NOTE: `include ::RSpec::Matchers`.
56
- # - https://github.com/rspec/rspec-expectations/blob/v3.11.0/lib/rspec/matchers.rb
57
- # - https://github.com/rspec/rspec-expectations/blob/main/lib/rspec/matchers.rb
28
+ # @internal
29
+ # NOTE: Required by RSpec.
30
+ # https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/custom-matchers/define-a-matcher-supporting-block-expectations
58
31
  #
59
- include ::RSpec::Matchers
32
+ delegate :supports_block_expectations?, to: :matcher
60
33
 
61
34
  ##
62
- # NOTE: `include ::RSpec::Mocks::ExampleMethods`.
63
- # - https://github.com/rspec/rspec-mocks/blob/v3.11.1/lib/rspec/mocks/example_methods.rb
64
- # - https://github.com/rspec/rspec-mocks/blob/main/lib/rspec/mocks/example_methods.rb
35
+ # @return [String]
65
36
  #
66
- include ::RSpec::Mocks::ExampleMethods
37
+ delegate :description, to: :matcher
67
38
 
68
39
  ##
40
+ # @return [String]
69
41
  #
70
- #
71
- def initialize(object, method)
72
- @object = object
73
- @method = method
74
- end
42
+ delegate :failure_message, to: :matcher
75
43
 
76
44
  ##
45
+ # @return [String]
77
46
  #
78
- #
79
- def matches?(block_expectation)
80
- @block_expectation = block_expectation
81
-
82
- ##
83
- # TODO: Support multiple `with_arguments` calls.
84
- #
85
- if used_with_arguments?
86
- ##
87
- # NOTE: RSpec `allow(object).to receive(method).with(*args, **kwargs)` does NOT support block.
88
- # https://github.com/rspec/rspec-mocks/issues/1182#issuecomment-679820352
89
- #
90
- # NOTE: RSpec `allow(object).to receive(method) do` does NOT support `and_call_original`.
91
- # https://github.com/rspec/rspec-mocks/issues/774#issuecomment-54245277
92
- #
93
- # NOTE: That is why `and_wrap_original` is used.
94
- # https://relishapp.com/rspec/rspec-mocks/docs/configuring-responses/wrapping-the-original-implementation
95
- #
96
- allow(object).to receive(method).and_wrap_original do |original, *actual_args, **actual_kwargs, &actual_block|
97
- actual_arguments_collection << [actual_args, actual_kwargs, actual_block]
98
-
99
- ##
100
- # NOTE: Imitates `and_call_original`.
101
- #
102
- original.call(*actual_args, **actual_kwargs, &actual_block)
103
- end
104
- else
105
- allow(object).to receive(method).and_call_original
106
- end
107
-
108
- ##
109
- # NOTE: Calls `block_expectation` before checking expections.
110
- #
111
- value = block_expectation.call
112
-
113
- ##
114
- # NOTE: If this expectation fails, it means `delegate_to` is NOT met.
115
- #
116
- expect(object).to have_received(method).at_least(1) unless used_with_arguments?
117
-
118
- ##
119
- # IMPORTANT: `and_return_its_value` works only when `delegate_to` checks a pure function.
120
- #
121
- # For example (1):
122
- # def require_dependencies_pure
123
- # RequireDependenciesPure.call
124
- # end
125
- #
126
- # class RequireDependenciesPure
127
- # def self.call
128
- # dependencies.require!
129
- #
130
- # true
131
- # end
132
- # end
133
- #
134
- # # Works since `RequireDependenciesPure.call` always returns `true`.
135
- # specify do
136
- # expect { require_dependencies_pure }
137
- # .to delegate_to(RequireDependenciesPure, :call)
138
- # .and_return_its_value
139
- # end
140
- #
141
- # Example (2):
142
- # def require_dependencies_not_pure
143
- # RequireDependenciesNotPure.call
144
- # end
145
- #
146
- # class RequireDependenciesNotPure
147
- # def self.call
148
- # return false if dependencies.required?
149
- #
150
- # dependencies.require!
151
- #
152
- # true
153
- # end
154
- # end
155
- #
156
- # # Does NOT work since `RequireDependenciesNotPure.call` returns `true` for the first time and `false` for the subsequent call.
157
- # specify do
158
- # expect { require_dependencies_not_pure }
159
- # .to delegate_to(RequireDependenciesNotPure, :call)
160
- # .and_return_its_value
161
- # end
162
- #
163
- # NOTE: If this expectation fails, it means `and_return_its_value` is NOT met.
164
- #
165
- expect(value).to eq(object.__send__(method, *args, **kwargs, &block)) if used_and_return_its_value?
166
-
167
- ##
168
- # IMPORTANT: A matcher should always return a boolean.
169
- # https://github.com/zverok/saharspec/blob/master/lib/saharspec/matchers/send_message.rb#L59
170
- #
171
- # NOTE: RSpec raises exception when any `expect` is NOT satisfied.
172
- # So, this `true` is returned only when all `expect` are successful.
173
- #
174
- if used_with_arguments?
175
- actual_arguments_collection.any? do |(actual_args, actual_kwargs, actual_block)|
176
- actual_args == expected_args && actual_kwargs == expected_kwargs && actual_block == expected_block
177
- end
178
- else
179
- true
180
- end
181
- end
47
+ delegate :failure_message_when_negated, to: :matcher
182
48
 
183
49
  ##
184
- # NOTE: Required by RSpec.
185
- # https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/custom-matchers/define-a-matcher-supporting-block-expectations
50
+ # @overload initialize(object, method)
51
+ # @param object [Object]
52
+ # @param method [String, Symbol]
53
+ # @return [void]
54
+ #
55
+ # @overload initialize(matcher)
56
+ # @param matcher [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher]
57
+ # @return [void]
58
+ # @api private
186
59
  #
187
- def supports_block_expectations?
188
- true
60
+ # @internal
61
+ # TODO: `overload do`?
62
+ #
63
+ def initialize(object = nil, method = nil, matcher: nil)
64
+ @matcher = matcher || Entities::Matcher.new(object, method)
189
65
  end
190
66
 
191
67
  ##
68
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
69
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ArgumentsChainingIsAlreadySet]
192
70
  #
193
- #
194
- def description
195
- "delegate to `#{printable_method}`"
196
- end
71
+ def with_arguments(...)
72
+ matcher.with_arguments(...)
197
73
 
198
- def failure_message
199
- if used_with_arguments?
200
- "expected `#{printable_block_expectation}` to delegate to `#{printable_method}` with expected arguments at least once, but it didn't."
201
- else
202
- "expected `#{printable_block_expectation}` to delegate to `#{printable_method}` at least once, but it didn't."
203
- end
74
+ self
204
75
  end
205
76
 
206
77
  ##
207
- # IMPORTANT: `failure_message_when_negated` is NOT supported yet.
78
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
79
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ArgumentsChainingIsAlreadySet]
208
80
  #
209
-
210
- def with_arguments(*args, **kwargs, &block)
211
- chain[:with_arguments] = {args: args, kwargs: kwargs, block: block}
81
+ def without_arguments
82
+ matcher.without_arguments
212
83
 
213
84
  self
214
85
  end
215
86
 
87
+ ##
88
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
89
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ReturnItsValueChainingIsAlreadySet]
90
+ #
216
91
  def and_return_its_value
217
- chain[:and_return_its_value] = true
92
+ matcher.and_return_its_value
218
93
 
219
94
  self
220
95
  end
221
96
 
222
- def printable_method
223
- @printable_method ||=
224
- case Utils::Object.resolve_type(object)
225
- when "class", "module"
226
- "#{object}.#{method}"
227
- when "instance"
228
- "#{object.class}##{method}"
229
- end
230
- end
231
-
232
- private
233
-
234
- attr_reader :object, :method, :block_expectation
235
-
236
- def used_with_arguments?
237
- chain.key?(:with_arguments)
238
- end
239
-
240
- def used_and_return_its_value?
241
- chain.key?(:and_return_its_value)
242
- end
243
-
244
- def chain
245
- @chain ||= {}
246
- end
247
-
248
- def args
249
- @args ||= chain.dig(:with_arguments, :args) || []
250
- end
251
-
252
- alias_method :expected_args, :args
253
-
254
- def kwargs
255
- @kwargs ||= chain.dig(:with_arguments, :kwargs) || {}
256
- end
257
-
258
- alias_method :expected_kwargs, :kwargs
97
+ ##
98
+ # TODO: `and_return`.
99
+ #
100
+ # def and_return(...)
101
+ # matcher.and_return(...)
102
+ #
103
+ # self
104
+ # end
259
105
 
260
106
  ##
261
- # NOTE: `if defined?` is used in order to cache `nil` if needed.
107
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
262
108
  #
263
- def block
264
- return @block if defined? @block
109
+ def without_calling_original
110
+ matcher.without_calling_original
265
111
 
266
- @block = chain.dig(:with_arguments, :block)
112
+ self
267
113
  end
268
114
 
269
- alias_method :expected_block, :block
270
-
271
- def actual_arguments_collection
272
- @actual_arguments_collection ||= []
273
- end
115
+ private
274
116
 
275
117
  ##
276
- # NOTE: An example of how RSpec extracts block source, but they marked it as private.
277
- # https://github.com/rspec/rspec-expectations/blob/311aaf245f2c5493572bf683b8c441cb5f7e44c8/lib/rspec/matchers/built_in/change.rb#L437
278
- #
279
- # TODO: `printable_block_expectation` when `method_source` is available.
280
- # https://github.com/banister/method_source
118
+ # @!attribute [r] matcher
119
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher]
281
120
  #
282
- # def printable_block_expectation
283
- # @printable_block_expectation ||= block_expectation.source
284
- # end
285
- #
286
- def printable_block_expectation
287
- @printable_block_expectation ||= "{ ... }"
288
- end
121
+ attr_reader :matcher
289
122
  end
290
123
  end
291
124
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ module Results
8
+ class Base
9
+ module Commands
10
+ class FindResultMethodStep < Support::Command
11
+ ##
12
+ # @!attribute result [r]
13
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
14
+ #
15
+ attr_reader :result
16
+
17
+ ##
18
+ # @!attribute method_name [r]
19
+ # @return [Symbol]
20
+ #
21
+ attr_reader :method_name
22
+
23
+ ##
24
+ # @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result]
25
+ # @param method_name [Symbol]
26
+ # @return [void]
27
+ #
28
+ def initialize(result:, method_name:)
29
+ @result = result
30
+ @method_name = method_name
31
+ end
32
+
33
+ ##
34
+ # @return [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
35
+ #
36
+ def call
37
+ return nil unless result.step
38
+
39
+ result.step.inputs.find { |input| match_service_class?(input) && match_key?(input) && match_value?(input) }
40
+ end
41
+
42
+ private
43
+
44
+ ##
45
+ # @param input [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
46
+ # @return [Boolean]
47
+ #
48
+ def match_service_class?(input)
49
+ result.step.service_class == Services::RunMethodInOrganizer
50
+ end
51
+
52
+ ##
53
+ # @param input [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
54
+ # @return [Boolean]
55
+ #
56
+ def match_key?(input)
57
+ input.key.to_sym == :method_name
58
+ end
59
+
60
+ ##
61
+ # @param input [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
62
+ # @return [Boolean]
63
+ #
64
+ def match_value?(input)
65
+ input.value.to_sym == method_name
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ module Results
8
+ class Base
9
+ module Commands
10
+ class FindResultResultMethodStep < Support::Command
11
+ ##
12
+ # @!attribute result [r]
13
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
14
+ #
15
+ attr_reader :result
16
+
17
+ ##
18
+ # @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result]
19
+ # @return [void]
20
+ #
21
+ def initialize(result:)
22
+ @result = result
23
+ end
24
+
25
+ ##
26
+ # @return [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
27
+ #
28
+ def call
29
+ return nil unless result.step
30
+
31
+ result.step.inputs.find { |input| match_service_class?(input) && match_key?(input) && match_value?(input) }
32
+ end
33
+
34
+ private
35
+
36
+ ##
37
+ # @param input [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
38
+ # @return [Boolean]
39
+ #
40
+ def match_service_class?(input)
41
+ result.step.service_class == Services::RunOwnMethodInOrganizer
42
+ end
43
+
44
+ ##
45
+ # @param input [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
46
+ # @return [Boolean]
47
+ #
48
+ def match_key?(input)
49
+ input.key.to_sym == :method_name
50
+ end
51
+
52
+ ##
53
+ # @param input [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
54
+ # @return [Boolean]
55
+ #
56
+ def match_value?(input)
57
+ input.value.to_sym == :result
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ module Results
8
+ class Base
9
+ module Commands
10
+ class FindResultServiceStep < Support::Command
11
+ ##
12
+ # @!attribute result [r]
13
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
14
+ #
15
+ attr_reader :result
16
+
17
+ ##
18
+ # @!attribute service_class [r]
19
+ # @return [Class]
20
+ #
21
+ attr_reader :service_class
22
+
23
+ ##
24
+ # @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result]
25
+ # @param service_class [Class]
26
+ # @return [void]
27
+ #
28
+ def initialize(result:, service_class:)
29
+ @result = result
30
+ @service_class = service_class
31
+ end
32
+
33
+ ##
34
+ # @return [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method]
35
+ #
36
+ def call
37
+ return nil unless result.step
38
+
39
+ result.step.service_class == service_class
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ module Results
8
+ class Base
9
+ module Commands
10
+ class GenerateExpectedStepPart < Support::Command
11
+ ##
12
+ # @!attribute step [r]
13
+ # @return [Class, Symbol]
14
+ #
15
+ attr_reader :step
16
+
17
+ ##
18
+ # @param step [Class, Symbol]
19
+ # @return [void]
20
+ #
21
+ def initialize(step:)
22
+ @step = step
23
+ end
24
+
25
+ ##
26
+ # @return [String]
27
+ #
28
+ def call
29
+ if step.nil?
30
+ "without step"
31
+ else
32
+ "of step `#{step}`"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ module Results
8
+ class Base
9
+ module Commands
10
+ class GenerateGotStepPart < Support::Command
11
+ ##
12
+ # @!attribute result [r]
13
+ # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
14
+ #
15
+ attr_reader :result
16
+
17
+ ##
18
+ # @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result]
19
+ # @return [void]
20
+ #
21
+ def initialize(result:)
22
+ @result = result
23
+ end
24
+
25
+ ##
26
+ # @return [String]
27
+ #
28
+ def call
29
+ if result.step.nil?
30
+ "without step"
31
+ else
32
+ "of step `#{result.step.service.klass}`"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end