Fixed behaviour of character classes, so that the escaping works like Ruby single...
[treetop.git] / spec / spec_helper.rb
blob3921d95e0aa9ddaf04ceef59b6a75160fc979410
1 dir = File.dirname(__FILE__)
2 require 'rubygems'
3 require 'benchmark'
4 require 'spec'
6 unless $bootstrapped_gen_1_metagrammar
7   load File.join(dir, '..', 'lib', 'treetop', 'bootstrap_gen_1_metagrammar.rb')
8 end
9 include Treetop
11 Spec::Runner.configure do |config|
12   config.mock_with :rr
13 end
15 module Treetop
16   class TreetopExampleGroup < Spec::Example::ExampleGroup
17     class << self
18       attr_accessor :parser_class_under_test
20       def testing_expression(expression_under_test)
21         testing_grammar(%{
22           grammar Test
23             rule expression_under_test
24               }+expression_under_test+%{
25             end
26           end
27         }.tabto(0))
28       end
30       def testing_grammar(grammar_under_test)
31         grammar_node = parse_with_metagrammar(grammar_under_test.strip, :grammar)
32         parser_code = grammar_node.compile
33         class_eval(parser_code)
34         self.parser_class_under_test = const_get(grammar_node.parser_name.to_sym)
35       end
37       def parse_with_metagrammar(input, root)
38         parser = Treetop::Compiler::MetagrammarParser.new
39         parser.root = root
40         node = parser.parse(input)
41         raise parser.failure_reason unless node
42         node
43       end
45     end
47     attr_reader :parser
49     def parse_with_metagrammar(input, root)
50       self.class.parse_with_metagrammar(input, root)
51     end
53     def parser_class_under_test
54       self.class.parser_class_under_test
55     end
57     def parse(input, options = {})
58       @parser = parser_class_under_test.new
59       unless options[:consume_all_input].nil?
60         parser.consume_all_input = options.delete(:consume_all_input)
61       end
62       result = parser.parse(input, options)
63       yield result if block_given?
64       result
65     end
67     def compiling_grammar(grammar_under_test)
68       lambda {
69         grammar_node = parse_with_metagrammar(grammar_under_test.strip, :grammar)
70         parser_code = grammar_node.compile
71         [grammar_node, parser_code]
72       }
73     end
75     def compiling_expression(expression_under_test)
76       compiling_grammar(%{
77         grammar Test
78           rule expression_under_test
79             #{expression_under_test}
80           end
81         end
82       }.tabto(0))
83     end
85     def optionally_benchmark(&block)
86       if BENCHMARK
87         Benchmark.bm do |x|
88           x.report(&block)
89         end
90       else
91         yield
92       end
93     end
95     Spec::Example::ExampleGroupFactory.register(:compiler, self)
96     Spec::Example::ExampleGroupFactory.register(:runtime, self)
97   end
98 end
100 class Symbol
101   def to_proc
102     lambda do |x|
103       x.send(self)
104     end
105   end