Sha256: 23f2623ac9d40424a47c465a480dad0300e9602f7827b3095d068a799a86c577

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 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 lambda { |pet| pet.name == "DOGGIE!" } do :fishsticks end
        m.some lambda { |pet| pet.name == "MOGGIE!" } do :canteloupe end
      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

1 entries across 1 versions & 1 rubygems

Version Path
optional-0.0.7 spec/lib/optional/option_spec.rb