Source

par / spec / lib / par / result_spec.rb

require 'spec_helper'

describe Par::Result do
  include Par

  context 'a simple result' do
    let(:r) { result 'r' }

    it 'should not be bottom' do 
      r.should_not be_bottom
    end
    it 'should allow concatenation with any object, using #+ to form a composite' do
      o = 's'
      c = r >> o

      c.obj.should == 'rs'
    end
    it 'converts to a string (by delegating to obj)' do 
      ('a' + r).should == 'ar'
    end
  end
  context 'given 2 results' do
    let(:a) { result 'foo' }
    let(:b) { result 'bar' }
    
    it "should allow concatenation" do
      (a >> b).to_s.should == 'foobar'
    end
    it "should allow alternation" do
      (a | b).should == a
    end
    it "allow calling maybe" do
      a.maybe.should == a
    end
  end
  context '.bottom' do
    let(:any) { result 'any' }

    it "cannot be concatenated left" do
      (bottom >> any).should == bottom
    end
    it "cannot be concatenated right" do
      (any >> bottom).should == bottom 
    end
    it "can be alternated left" do
      (bottom | any).should == any
    end
    it "can be alternated right" do
      (any | bottom).should == any
    end
    it "can be tested for bottom?" do 
      bottom.should be_bottom
    end
  end
  context '.top' do
    let(:any) { result('any') }
    it 'allows concatenation left' do
      (top >> any).should == any
    end
    it 'allows concatenation right' do
      (any >> top).should == any
    end
    it 'allows alternation' do
      (any | top).should == any
      (top | any).should == top
    end
    it 'can be maybeed' do
      top.maybe.should == top
    end
    it 'should not be bottom' do 
      top.should_not be_bottom
    end
  end
end