Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match multiple step names with ResultMatcher on failure #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/dry/transaction/result_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module Transaction
resolve: -> result { result.value! }
),
failure: Dry::Matcher::Case.new(
match: -> result, step_name = nil {
if step_name
result.failure? && result.failure.step.name == step_name
match: -> result, *step_names {
if step_names.any?
result.failure? && step_names.include?(result.failure.step.name)
else
result.failure?
end
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/result_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

RSpec.describe Dry::Transaction::ResultMatcher do
include Dry::Monads[:result]

describe "when failure" do
it "can match using multiple step names" do
expected = Object.new
actual = nil

failure = Failure(Dry::Transaction::StepFailure.new(Struct.new(:name).new(:step), expected))
Dry::Transaction::ResultMatcher.(failure) do |on|
on.success { raise }
on.failure(:step, :other_step) { |value| actual = value }
end

expect(actual).to be expected
end
end
end