Permalink
Browse files

restructure parser spec for speed and profit

  • Loading branch information...
1 parent eea3e68 commit abcd6c596ffdbe97e27903dbf95e75fae49dd444 @manveru manveru committed May 6, 2011
Showing with 40 additions and 24 deletions.
  1. +40 −24 tests/parsing/literals.rb
View
@@ -6,26 +6,30 @@ module AST
end
end
-describe 'Literals' do
- # so much fuss just to get debugging working.
- require 'kpeg'
- path = File.expand_path("../../../boot/fancy.kpeg", __FILE__)
+# so much fuss just to get debugging working.
+require 'kpeg'
+path = File.expand_path("../../../boot/fancy.kpeg", __FILE__)
- format_parser = KPeg::FormatParser.new(File.read(path))
- format_parser.parse
- @cg = KPeg::CodeGenerator.new("TestFancy", format_parser.grammar, debug = ENV['D'])
- @cg.make('')
- require File.expand_path('../../../boot/ast', __FILE__)
+format_parser = KPeg::FormatParser.new(File.read(path))
+format_parser.parse
+TestFancyParser = KPeg::CodeGenerator.new("TestFancyParser", format_parser.grammar, debug = ENV['D'])
+TestFancyParser.make('')
+require File.expand_path('../../../boot/ast', __FILE__)
+shared :parser do
def parse(str)
- parser = @cg.make(str)
+ parser = TestFancyParser.make(str)
parser.parse.should == true
parser.result
end
def ast(str)
parse(str).to_ast
end
+end
+
+describe 'Literals' do
+ behaves_like :parser
it 'parses Integer' do
ast('1').should == [[:int, 1]]
@@ -67,10 +71,6 @@ def ast(str)
ast("'foo[]bar*=some:thing?").should == [[:sym, "foo[]bar*=some:thing?"]]
end
- it 'parses a = 1' do
- ast('a = 1').should == [[:assign, [:id, "a"], [:int, 1]]]
- end
-
it 'parses String' do
ast('"foo"').should == [[:str, "foo"]]
end
@@ -102,16 +102,6 @@ def ast(str)
ast('<[1 => 2, 3 => 4]>').should == [[:hash, {[:int, 1] => [:int, 2], [:int, 3] => [:int, 4]}]]
end
- it 'parses return statement' do
- ast('return').should == [[:ret, nil]]
- ast('return 1').should == [[:ret, [:int, 1]]]
- end
-
- it 'parses local_return statement' do
- ast('return_local').should == [[:retl, nil]]
- ast('return_local 1').should == [[:retl, [:int, 1]]]
- end
-
it 'parses empty regexp' do
ast('//').should == [[:regex, //, ""]]
end
@@ -149,3 +139,29 @@ def ast(str)
end
=end
end
+
+describe 'Method definition' do
+ behaves_like :parser
+end
+
+describe 'Assignment' do
+ behaves_like :parser
+
+ it 'parses a = 1' do
+ ast('a = 1').should == [[:assign, [:id, "a"], [:int, 1]]]
+ end
+end
+
+describe 'Return' do
+ behaves_like :parser
+
+ it 'parses return statement' do
+ ast('return').should == [[:ret, nil]]
+ ast('return 1').should == [[:ret, [:int, 1]]]
+ end
+
+ it 'parses local_return statement' do
+ ast('return_local').should == [[:retl, nil]]
+ ast('return_local 1').should == [[:retl, [:int, 1]]]
+ end
+end

0 comments on commit abcd6c5

Please sign in to comment.