par / spec / lib / par / source_spec.rb

# 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.