Sha256: 51ae8f0bf566ee95b94b4197a06eb2164890a1ed7c2444810a30a497c8514a97

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

require 'spec_helper'
describe "the README examples" do

  it 'works as announced' do

    grammar = Sexpr.load(<<-YAML)
      rules:
        # alternative rule
        bool_expr:
          - bool_and
          - bool_or
          - bool_not
          - var_ref
          - bool_lit

        # non-terminal
        bool_and:
          - [ bool_expr, bool_expr ]
        bool_or:
          - [ bool_expr, bool_expr ]
        bool_not:
          - [ bool_expr ]
        bool_lit:
          - [ truth_value ]
        var_ref:
          - [ var_name ]

        # terminals
        var_name:
          !ruby/regexp /^[a-z]+$/
        truth_value:
          - true
          - false
    YAML

    # the grammar can be used to verify the structure of s-expressions
    f = (grammar === [:bool_and, [:bool_not, [:var_ref, "x"]], [:bool_lit, true]])
    f.should be_true

    f = (grammar === [:bool_and, [:bool_lit, "true"]])
    f.should be_false

    # the grammar can also be used to automatically have support on top of
    # such s-expressions
    expr = grammar.sexpr([:bool_lit, true])
    (Sexpr===expr).should be_true

    (expr.sexpr_type).should eq(:bool_lit)
    # => :bool_lit

    (expr.sexpr_body).should eq([true])
    # => [true]

    copy = expr.sexpr_copy do |base,child|
      # copy a s-expression ala Enumerable#inject (base is [:bool_lit] initially)
      base << [:bool_lit, !child]
    end
    copy.should eq([:bool_lit, [:bool_lit, false]])
    # => [:bool_lit, [:bool_lit, false]]

    (Sexpr===copy).should be_true

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sexpr-0.6.0 spec/integration/test_readme_examples.rb
sexpr-0.5.1 spec/test_readme_examples.rb
sexpr-0.5.0 spec/test_readme_examples.rb
sexpr-0.4.0 spec/test_readme_examples.rb