From 8cf78b07d2d91febc815a4c3005cd95fe2b047ec Mon Sep 17 00:00:00 2001 From: Alex Kalderimis Date: Mon, 22 Jul 2024 19:24:00 -0400 Subject: [PATCH 1/3] Alias @value to @exception in Try::Error This allows deconstruct to work correctly --- lib/dry/monads/try.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dry/monads/try.rb b/lib/dry/monads/try.rb index 63ee16c..9f47468 100644 --- a/lib/dry/monads/try.rb +++ b/lib/dry/monads/try.rb @@ -186,7 +186,7 @@ class Error < Try def initialize(exception) super() - @exception = exception + @exception = @value = exception end # @return [String] From 8bf975342fd899b8677515eceea0bb9b580e82fb Mon Sep 17 00:00:00 2001 From: Alex Kalderimis Date: Mon, 22 Jul 2024 19:33:19 -0400 Subject: [PATCH 2/3] Update try_spec.rb to test deconstruction --- spec/unit/try_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/unit/try_spec.rb b/spec/unit/try_spec.rb index 2ac671d..2c37c58 100644 --- a/spec/unit/try_spec.rb +++ b/spec/unit/try_spec.rb @@ -57,6 +57,10 @@ it { is_expected.to eql(described_class.new([ZeroDivisionError], "foo")) } it { is_expected.not_to eql(error[division_error]) } + it "deconstructs to the value" do + expect(subject.deconstruct).to eq ["foo"] + end + it "dumps to string" do expect(subject.to_s).to eql('Try::Value("foo")') expect(value[[], unit].to_s).to eql("Try::Value()") @@ -210,6 +214,10 @@ let(:upcase_value) { described_class.new([ZeroDivisionError], "FOO") } let(:upcase_error) { try::Error.new(division_error) } + it "deconstructs to the exception" do + expect(subject.deconstruct).to eq [division_error] + end + describe ".call" do it "is an alias for new" do expect(try::Error.(division_error)).to eql(subject) From 2f35d4c3fa5a944c6b7bd7a4d478a5e09330a8af Mon Sep 17 00:00:00 2001 From: Alex Kalderimis Date: Tue, 23 Jul 2024 13:53:31 -0400 Subject: [PATCH 3/3] Move pattern match tests from unit to integration specs --- spec/integration/pattern_matching_spec.rb | 51 +++++++++++++++++++++++ spec/unit/try_spec.rb | 8 ---- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/spec/integration/pattern_matching_spec.rb b/spec/integration/pattern_matching_spec.rb index 2c8bc94..81ff174 100644 --- a/spec/integration/pattern_matching_spec.rb +++ b/spec/integration/pattern_matching_spec.rb @@ -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] diff --git a/spec/unit/try_spec.rb b/spec/unit/try_spec.rb index 2c37c58..2ac671d 100644 --- a/spec/unit/try_spec.rb +++ b/spec/unit/try_spec.rb @@ -57,10 +57,6 @@ it { is_expected.to eql(described_class.new([ZeroDivisionError], "foo")) } it { is_expected.not_to eql(error[division_error]) } - it "deconstructs to the value" do - expect(subject.deconstruct).to eq ["foo"] - end - it "dumps to string" do expect(subject.to_s).to eql('Try::Value("foo")') expect(value[[], unit].to_s).to eql("Try::Value()") @@ -214,10 +210,6 @@ let(:upcase_value) { described_class.new([ZeroDivisionError], "FOO") } let(:upcase_error) { try::Error.new(division_error) } - it "deconstructs to the exception" do - expect(subject.deconstruct).to eq [division_error] - end - describe ".call" do it "is an alias for new" do expect(try::Error.(division_error)).to eql(subject)