rubyx/test/parser/helper.rb

60 lines
1.7 KiB
Ruby
Raw Normal View History

require_relative "../helper"
2014-05-08 17:38:10 +02:00
require "parslet/convenience"
# Included in parser test will create tests methods
module ParserHelper
def self.included(base)
base.send :include, InstanceMethods #provides helpers and setup
base.send :extend, ClassMethods #gets the method creation going
end
module InstanceMethods
def setup
2014-05-10 10:03:23 +02:00
@parser = Parser::Crystal.new
@transform = Parser::Transform.new
end
# check that @string_input parses correctly to @parse_output
def check_parse
2014-05-08 17:38:10 +02:00
is = @parser.parse_with_debug(@string_input)
#puts is.inspect
assert_equal @parse_output , is
end
#check that @parse_output transforms to @transform_output
def check_transform
is = @transform.apply @parse_output
#puts is.transform
assert_equal @transform_output , is
end
# check that @string_input parses and transforms to @transform_output
def check_ast
syntax = @parser.parse(@string_input)
tree = @transform.apply(syntax)
# puts tree.inspect
assert_equal @transform_output , tree
end
end
module ClassMethods
2014-04-29 15:21:28 +02:00
# this creates test methods dynamically. For each test_* method we create
# three test_*[ast/parse/transf] methods that in turn check the three phases.
# runnable_methods is called by minitest to determine which tests to run
def runnable_methods
tests = []
2014-04-29 15:21:28 +02:00
public_instance_methods(true).grep(/^test_/).map(&:to_s).each do |test|
["ast" , "transform" , "parse"].each do |what|
2014-04-29 15:21:28 +02:00
name = "#{test}_#{what}"
tests << name
self.send(:define_method, name ) do
2014-04-29 15:21:28 +02:00
send(test)
send("check_#{what}")
end
end
end
tests
end
end
end