Sha256: 1192ac57dd9cdeddfee0fe34b94f249ee6b3c33e4d1cf772ad0b4b6a709831c1

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require "dry-monads"
require "dry/matcher/either_matcher"

RSpec.describe "Class enhancement with Dry::Matcher.for" do
  let(:operation) {
    Class.new do
      include Dry::Matcher.for(:call, with: Dry::Matcher::EitherMatcher)

      def call(bool)
        bool ? Dry::Monads::Right("a success") : Dry::Monads::Left("a failure")
      end
    end.new
  }

  describe "match blocks" do
    subject(:match) {
      operation.call(input) do |m|
        m.success do |v|
          "Matched success: #{v}"
        end

        m.failure do |v|
          "Matched failure: #{v}"
        end
      end
    }

    context "successful result" do
      let(:input) { true }

      it "matches on success" do
        expect(match).to eq "Matched success: a success"
      end
    end

    context "failed result" do
      let(:input) { false }

      it "matches on failure" do
        expect(match).to eq "Matched failure: a failure"
      end
    end
  end

  describe "without match blocks" do
    subject(:result) { operation.call(input) }

    context "successful result" do
      let(:input) { true }

      it "returns the result" do
        expect(result).to eq Dry::Monads::Right("a success")
      end
    end

    context "failed result" do
      let(:input) { false }

      it "returns the result" do
        expect(result).to eq Dry::Monads::Left("a failure")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-matcher-0.6.0 spec/integration/class_enhancement_spec.rb
dry-matcher-0.5.0 spec/integration/class_enhancement_spec.rb