Source

par / spec / lib / par / source_spec.rb

Full commit
# Encoding: UTF-8
require 'spec_helper'

describe Par::Source do
  include Par

  describe ".source" do
    it "constructs a source" do
      source('foo').should be_kind_of(described_class)
    end
  end
  describe "#n" do
    let(:s) { source 'äöü' }
    it "consumes n characters" do
      s.n(1).should == 'ä'
      s.n(2).should == 'öü'
    end
  end
  describe "#pattern" do
    let(:s) { source 'foobar' }
    it "matches a pattern and consumes it" do
      r = s.pattern /foo/
      r == 'foo'
    end
  end
  describe "#cr" do
    let(:s) { source 'foobar' }
    it 'rewinds the source if the block exits with Par.bot' do
      s.cr { 
        s.string 'foo'
        Par.bottom
      }

      s.head.should == 'foobar'
    end
    it "doesn't rewind if the block has other return values" do
      s.cr {
        s.string 'foo'
      }
      s.head.should == 'bar'
    end
  end
end