Skip to content

Commit

Permalink
feat(has_result_steps): introduce Step#original_result
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Feb 1, 2023
1 parent aa740f4 commit 8891247
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@
| Medium | 🚧 | `delegate_to` - option to specify of how to compare blocks | |
| High | 🚧 | Add specs for [Reassignment::Commands::DefineMethodInContainer](/~https://github.com/marian13/convenient_service/blob/v0.6.0/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb#L13) | |
| High | 🚧 | Example for dependency containers | |
| Low | 🚧 | `delegate_to.and_return_value` | |

Search for `TODO`s in the codebase for more tasks.
9 changes: 9 additions & 0 deletions lib/convenient_service/rspec/matchers/custom/delegate_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def and_return_its_value
self
end

##
# TODO: `and_return`.
#
# def and_return(...)
# matcher.and_return(...)
#
# self
# end

##
# @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def input_values
@input_values ||= calculate_input_values
end

def original_result
@original_result ||= calculate_original_result
end

def result
@result ||= calculate_result
end
Expand Down Expand Up @@ -114,6 +118,10 @@ def to_kwargs

attr_reader :args, :kwargs

##
# @internal
# TODO: Commands instead of private methods.
#
def calculate_input_values
assert_has_organizer!

Expand All @@ -124,7 +132,7 @@ def calculate_input_values
# @internal
# IMPORTANT: `service.result(**input_values)` is the reason, why services should have only kwargs as arguments.
#
def calculate_result
def calculate_original_result
assert_has_organizer!

result = service.result(**input_values)
Expand All @@ -134,6 +142,10 @@ def calculate_result
result
end

def calculate_result
original_result.copy(overrides: {kwargs: {step: self, service: organizer}})
end

def resolve_params
original_params = Commands::ExtractParams.call(args: args, kwargs: kwargs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,39 @@ def foo
end
end

describe "#original_result" do
context "when `organizer` is NOT set" do
let(:organizer) { nil }

let(:message) do
<<~TEXT
Step `#{step.service}` has not assigned organizer.
Did you forget to set it?
TEXT
end

it "raises `ConvenientService::Service::Plugins::HasResultSteps::Errors::StepHasNoOrganizer`" do
expect { step.original_result }
.to raise_error(ConvenientService::Service::Plugins::HasResultSteps::Errors::StepHasNoOrganizer)
.with_message(message)
end
end

context "when `organizer` is set" do
specify {
expect { step.original_result }
.to delegate_to(service, :result)
.with_arguments(**step.input_values)
.and_return_its_value
}

it "marks `step` as complete" do
expect { step.original_result }.to change(step, :completed?).from(false).to(true)
end
end
end

describe "#result" do
context "when `organizer` is NOT set" do
let(:organizer) { nil }
Expand All @@ -377,8 +410,8 @@ def foo
context "when `organizer` is set" do
specify {
expect { step.result }
.to delegate_to(service, :result)
.with_arguments(**step.input_values)
.to delegate_to(step.original_result, :copy)
.with_arguments(overrides: {kwargs: {step: step, service: organizer}})
.and_return_its_value
}

Expand Down

0 comments on commit 8891247

Please sign in to comment.