spec_helper.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. require 'rubygems'
  2. require 'benchmark'
  3. require 'rspec'
  4. require 'polyglot'
  5. $LOAD_PATH.unshift File.expand_path('../../lib')
  6. require 'treetop'
  7. include Treetop
  8. module Treetop
  9. module ExampleGroupInstanceMethods
  10. module ClassMethods
  11. attr_accessor :parser_class_under_test
  12. def testing_expression(expression_under_test)
  13. testing_grammar(%{
  14. grammar Test
  15. rule expression_under_test
  16. }+expression_under_test+%{
  17. end
  18. end
  19. }.tabto(0))
  20. end
  21. def testing_grammar(grammar_under_test)
  22. grammar_node = parse_with_metagrammar(grammar_under_test.strip, :module_or_grammar)
  23. parser_code = grammar_node.compile
  24. class_eval(parser_code)
  25. self.parser_class_under_test = class_eval(grammar_node.parser_name)
  26. end
  27. def parse_with_metagrammar(input, root)
  28. parser = Treetop::Compiler::MetagrammarParser.new
  29. parser.root = root
  30. node = parser.parse(input)
  31. raise parser.failure_reason unless node
  32. node
  33. end
  34. end
  35. attr_reader :parser
  36. def parse_with_metagrammar(input, root)
  37. self.class.parse_with_metagrammar(input, root)
  38. end
  39. def parser_class_under_test
  40. self.class.parser_class_under_test
  41. end
  42. def parse(input, options = {})
  43. @parser = parser_class_under_test.new
  44. unless options[:consume_all_input].nil?
  45. parser.consume_all_input = options.delete(:consume_all_input)
  46. end
  47. result = parser.parse(input, options)
  48. yield result if block_given?
  49. result
  50. end
  51. def parse_multibyte(input, options = {})
  52. require 'active_support/all'
  53. if RUBY_VERSION !~ /^1.9/ && 'NONE' == $KCODE then $KCODE = 'UTF8' end
  54. # rspec 1.3 used to do something similar (set it to 'u') that we need
  55. # for activerecord multibyte wrapper to kick in (1.8 only? @todo)
  56. parse(input.mb_chars, options)
  57. end
  58. def compiling_grammar(grammar_under_test)
  59. lambda {
  60. grammar_node = parse_with_metagrammar(grammar_under_test.strip, :grammar)
  61. parser_code = grammar_node.compile
  62. [grammar_node, parser_code]
  63. }
  64. end
  65. def compiling_expression(expression_under_test)
  66. compiling_grammar(%{
  67. grammar Test
  68. rule expression_under_test
  69. #{expression_under_test}
  70. end
  71. end
  72. }.tabto(0))
  73. end
  74. def optionally_benchmark(&block)
  75. if BENCHMARK
  76. Benchmark.bm do |x|
  77. x.report(&block)
  78. end
  79. else
  80. yield
  81. end
  82. end
  83. end
  84. end
  85. RSpec.configure do |c|
  86. c.mock_with :rr
  87. c.extend Treetop::ExampleGroupInstanceMethods::ClassMethods
  88. c.include Treetop::ExampleGroupInstanceMethods
  89. end
  90. class Symbol
  91. def to_proc
  92. lambda do |x|
  93. x.send(self)
  94. end
  95. end unless method_defined?(:to_proc)
  96. end