Sha256: 1dd38868a4308cf5eba346f8eeb16c741b227c37baf63ce01ccb80c46a34e9a4

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'spec_helper'
module Sexpr
  describe Grammar, "sexpr" do

    def sexpr(expr, opts = {})
      @sexpr = Sexpr.load(:parser => parser).sexpr(expr, opts)
    end

    after{
      @sexpr.should be_a(Sexpr) if @sexpr
    }

    context 'when no parser is set' do
      let(:parser){ nil }

      it 'silently returns a sexpr array' do
        sexpr([:sexpr, "world"]).should eq([:sexpr, "world"])
      end

      it 'raises an error when parser is needed' do
        lambda{
          sexpr("Hello world")
        }.should raise_error(NoParserError)
      end

    end

    context 'when a parser is set' do
      let(:parser){
        Object.new.extend Module.new{
          include Parser
          def sexpr(s, options = {})
            [options[:root] || :parsed, s]
          end
        }
      }

      it 'silently returns a sexpr array' do
        sexpr([:sexpr, "world"]).should eq([:sexpr, "world"])
      end

      it 'delegates the call to the parser' do
        sexpr("Hello world").should eq([:parsed, "Hello world"])
      end

      it 'passes options' do
        sexpr("world", :root => :hello).should eq([:hello, "world"])
      end

    end # when a parser is set

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sexpr-0.3.0 spec/grammar/test_sexpr.rb