Sha256: 385e610d5121fb22a87fe9a935954505bd60cfc32675e714e05032910f1a82c8

Contents?: true

Size: 1.96 KB

Versions: 6

Compression:

Stored size: 1.96 KB

Contents

require File.join(File.dirname(__FILE__), '..', 'test_helper')
 
class SequenceOfTerminalsTest < CompilerTestCase

  class Foo < Treetop::Runtime::SyntaxNode
  end

  testing_expression 'foo:"foo" bar:"bar" baz:"baz" <Foo> { def a_method; end }'
  
  test "successful result is an instance of the declared node class with element accessor methods and the method from the inline module" do
    parse('foobarbaz') do |result|
      result.should be_success
      result.should be_an_instance_of(Foo)      
      result.should respond_to(:a_method)
      result.foo.text_value.should == 'foo'
      result.bar.text_value.should == 'bar'
      result.baz.text_value.should == 'baz'
    end
  end
    
  test "matching at a non-zero index" do
    parse('---foobarbaz', :at_index => 3) do |result|
      result.should be_success
      result.should be_nonterminal
      (result.elements.map {|elt| elt.text_value}).join.should == 'foobarbaz'
    end
  end
  
  test "non-matching input fails with a nested failure at the first terminal that did not match" do
    parse('---foobazbaz', :at_index => 3) do |result|
      result.should be_failure
      result.index.should == 3
      result.nested_failures.size.should == 1
      nested_failure = result.nested_failures.first
      nested_failure.index.should == 6
      nested_failure.expected_string.should == 'bar'
    end
  end  
end

class SequenceOfNonterminalsTest < CompilerTestCase

  testing_grammar %{
    grammar TestGrammar
      rule sequence
        foo bar baz {
          def baz
            'override' + super.text_value
          end
        }
      end
      
      rule foo 'foo' end
      rule bar 'bar' end
      rule baz 'baz' end
    end
  }
  
  test "accessors for nonterminals are automatically defined and can be overridden in the inline block" do
    parse('foobarbaz') do |result|
      result.foo.text_value.should == 'foo'
      result.bar.text_value.should == 'bar'
      result.baz.should == 'overridebaz'
    end
    
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
treetop-1.0.1 test/compiler/sequence_test.rb
treetop-1.1.1 test/compiler/sequence_test.rb
treetop-1.1.0 test/compiler/sequence_test.rb
treetop-1.0.0 test/compiler/sequence_test.rb
treetop-1.0.2 test/compiler/sequence_test.rb
treetop-1.1.2 test/compiler/sequence_test.rb