Skip to content

Commit

Permalink
Add new test for Operation::DSL#map
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed Aug 8, 2024
1 parent 3b2917c commit edcee9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/pathway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ def set(callable, *args, to: @operation.result_key, **kwargs, &bl)
end

# Execute step and replace the current state completely
def map(callable)
def map(callable,...)
bl = _callable(callable)
@result = @result.then(bl)
@result = @result.then { |state| bl.call(state,...) }
end

def around(execution_strategy, &dsl_block)
Expand Down
25 changes: 25 additions & 0 deletions spec/plugins/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class OperationWithSteps < Operation

process do
step :custom_validate
map :add_misc
set :get_value
set :get_aux_value, to: :aux_value
around(-> run, st { run.call if cond.call(st) }) do
Expand All @@ -34,6 +35,10 @@ def custom_validate(state)
state[:params] = @validator.call(state)
end

def add_misc(state)
State.new(self, state.to_h.merge(misc: -1))
end

def get_value(state)
@back_end.call(state[:params])
end
Expand Down Expand Up @@ -112,6 +117,26 @@ def notify(state)
end
end

describe "#map" do
it "defines a step that replaces the current state" do
old_state = nil

expect(validator).to receive(:call) do |state|
expect(state.to_h).to_not include(:misc)

old_state = state
state[:input]
end

expect(notifier).to receive(:call) do |state|
expect(state.to_h).to include(misc: -1)
expect(state).to_not be_equal(old_state)
end

operation.call(input)
end
end

describe "#set" do
it "defines an updating step which sets the result key if no key is specified" do
expect(back_end).to receive(:call).and_return(:SOME_VALUE)
Expand Down

0 comments on commit edcee9c

Please sign in to comment.