Sha256: 1a832c8eb1b09ff7de275a1eec80fa445d16d7f6f3379240f31fe7e80f3d0087

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'spec_helper'

describe Option do

  let (:cat) { Cat.new("MOGGIE!") }
  let (:dog) { Dog.new("DOGGIE!") }

  context "== pattern matching ==" do

    subject do
      option.match do |m|
        m.some { |cat| cat.name }
        m.none { "no cat :-(" }
      end
    end

    context "a match can have one None branch, which matches only None" do
      let (:option) { None }
      it { should eq "no cat :-(" }
    end

    context "a match can have a catchall Some branch which matches any Some" do
      let (:option) { Some[cat] }
      it { should eq "MOGGIE!" }
    end

    it "can have multiple Some branches, each one matching a value" do
      Some[cat].match do |m|
        m.some(dog) { :canteloupe }
        m.some(cat) { :fishsticks }
      end.should eq :fishsticks
    end

    it "can take a lambda as a guard to match against" do
      Some[cat].match do |m|
        m.some ->(pet) { pet.name == "DOGGIE!" } { :fishsticks }
        m.some ->(pet) { pet.name == "MOGGIE!" } { :canteloupe }
      end.should eq :canteloupe
    end
  end

  it 'can create a some from a passed value' do
    Option[4].should eq Some[4]
  end

  it 'can create a none from a passed value' do
    Option[nil].should be_none
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
optional-0.0.6 spec/lib/optional/option_spec.rb
optional-0.0.5 spec/lib/optional/option_spec.rb