Sha256: 4ea53029ce1514c637b83747627aea26f3ec1af3a3eea18ab59e3b03ca2b0866

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

RSpec.describe Fear::PatternMatch do
  context "else at the end" do
    let(:matcher) do
      described_class.new do |m|
        m.case(Integer) { |x| "#{x} is int" }
        m.case(String) { |x| "#{x} is str" }
        m.else { |x| "#{x} is something else" }
      end
    end

    context "Integer" do
      subject { matcher.(4) }

      it { is_expected.to eq("4 is int") }
    end

    context "String" do
      subject { matcher.("4") }

      it { is_expected.to eq("4 is str") }
    end

    context "Symbol" do
      subject { matcher.(:a) }

      it { is_expected.to eq("a is something else") }
    end
  end

  context "else before other branches" do
    subject { matcher.(4) }

    let(:matcher) do
      described_class.new do |m|
        m.else { |x| "#{x} is something else" }
        m.case(Integer) { |x| "#{x} is int" }
      end
    end

    it { is_expected.to eq("4 is something else") }
  end

  context "several else branches" do
    subject { matcher.(4) }

    let(:matcher) do
      described_class.new do |m|
        m.else { |x| "#{x} else 1" }
        m.else { |x| "#{x} else 2" }
      end
    end

    it "first one wins" do
      is_expected.to eq("4 else 1")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fear-3.0.0 spec/fear/pattern_match_spec.rb
fear-2.0.1 spec/fear/pattern_match_spec.rb
fear-2.0.0 spec/fear/pattern_match_spec.rb