Sha256: 546ccb7fb02fc594aba362397a992a8f213c8c9977a26372f898935b3d46d460

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

require 'spec_helper'

describe "Tree output" do
  extend Parslet

  def self.hash_examples(h)
    h.each do |atom, expected|
      it "should convert #{atom} to #{expected.inspect}" do
        atom.parse(input).should == expected
      end
    end
  end

  context "when parsing the empty string" do
    let(:input) { '' }
    hash_examples( 
      # No naming 
      str('a').maybe              => '', 
      str('a').repeat             => '',
      
      # Named contents: maybe yields nil
      str('a').maybe.as(:f)       => {:f => nil},
      str('a').repeat.as(:f)      => {:f => []}, 
      
      # Contents that aren't simple strings
      (str('a') >> str('b')).maybe.as(:f)     => {:f => nil},
      (str('a') >> str('b')).repeat.as(:f)    => {:f => []}, 
      
      # The other way around: Contents would be tagged, but nil result isn't
      (str('a') >> str('b')).as(:f).maybe     => '',
      (str('a') >> str('b')).as(:f).repeat    => ''
    )
  end
  
  context "when parsing 'aa'" do
    let(:input) { 'aa' }
    hash_examples(
      # since they're not named, repetitions get merged together.
      str('a').as(:a).repeat >> str('a').as(:a).repeat => [{:a=>'a'},{:a=>'a'}]
    )
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
parslet-2.0.0 spec/acceptance/repetition_and_maybe_spec.rb
parslet-1.8.2 spec/acceptance/repetition_and_maybe_spec.rb
parslet-1.8.1 spec/acceptance/repetition_and_maybe_spec.rb
parslet-1.8.0 spec/acceptance/repetition_and_maybe_spec.rb
swift-pyrite-0.1.1 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/spec/acceptance/repetition_and_maybe_spec.rb
swift-pyrite-0.1.0 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/spec/acceptance/repetition_and_maybe_spec.rb
parslet-1.7.1 spec/acceptance/repetition_and_maybe_spec.rb