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

Alias @value to @exception in Try::Error #177

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion lib/dry/monads/try.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class Error < Try
def initialize(exception)
super()

@exception = exception
@exception = @value = exception
end

# @return [String]
Expand Down
51 changes: 51 additions & 0 deletions spec/integration/pattern_matching_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
# frozen_string_literal: true

RSpec.describe "pattern matching" do
describe "Dry::Monads::Try" do
include Dry::Monads[:try]

context "Value" do
it "matches on the singleton array of the value when value is not an array" do
expect(
(Value(1) in [1])
).to be(true)
end

it "matches on the value when value is an array" do
expect(
(Value([1]) in [1])
).to be(true)
end

it "matches on the empty array when value is Unit" do
expect(
(Value(Dry::Monads::Unit) in [])
).to be(true)
end

it "matches on the value's keys when value acts like a hash" do
expect(
(Value({code: 101, foo: :bar}) in { code: 101 })
).to be(true)
end

it "matches on the empty hash when value doesn't act like a hash" do
expect(
(Value(:foo) in {})
).to be(true)
end
end

context "Error" do
it "matches on the singleton array of the exception" do
exception = StandardError.new("boom")
failure = Try { raise exception }

expect(
(failure in [exception])
).to be(true)

expect(
(failure in Dry::Monads::Try::Error(exception))
).to be(true)
end
end
end

describe "Dry::Monads::Result" do
include Dry::Monads[:result]

Expand Down
Loading