1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/racc/helper.rb

116 lines
3.4 KiB
Ruby
Raw Normal View History

2019-06-20 05:20:38 -04:00
verbose = $VERBOSE
2019-05-13 08:25:22 -04:00
$VERBOSE = true
2019-06-20 05:20:38 -04:00
begin
require 'test/unit'
begin
require_relative './lib/core_assertions'
Test::Unit::TestCase.include Test::Unit::CoreAssertions
rescue LoadError
end
2019-05-13 08:25:22 -04:00
require 'racc/static'
require 'fileutils'
require 'tempfile'
require 'timeout'
module Racc
class TestCase < Test::Unit::TestCase
2019-05-13 08:25:22 -04:00
PROJECT_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..'))
2019-05-14 08:18:57 -04:00
test_dir = File.join(PROJECT_DIR, 'test')
test_dir = File.join(PROJECT_DIR, 'racc') unless File.exist?(test_dir)
TEST_DIR = test_dir
2019-05-14 08:18:57 -04:00
racc = File.join(PROJECT_DIR, 'bin', 'racc')
racc = File.join(PROJECT_DIR, '..', 'libexec', 'racc') unless File.exist?(racc)
RACC = racc
ASSET_DIR = File.join(TEST_DIR, 'assets') # test grammars
REGRESS_DIR = File.join(TEST_DIR, 'regress') # known-good generated outputs
2019-05-13 08:25:22 -04:00
INC = [
File.join(PROJECT_DIR, 'lib'),
File.join(PROJECT_DIR, 'ext'),
].join(':')
def setup
2019-07-02 01:10:50 -04:00
@TEMP_DIR = Dir.mktmpdir("racc")
@OUT_DIR = File.join(@TEMP_DIR, 'out')
@TAB_DIR = File.join(@TEMP_DIR, 'tab') # generated parsers go here
@LOG_DIR = File.join(@TEMP_DIR, 'log')
@ERR_DIR = File.join(@TEMP_DIR, 'err')
FileUtils.mkdir_p([@OUT_DIR, @TAB_DIR, @LOG_DIR, @ERR_DIR])
FileUtils.cp File.join(TEST_DIR, "src.intp"), @TEMP_DIR
2019-05-13 08:25:22 -04:00
end
def teardown
2019-07-02 01:10:50 -04:00
FileUtils.rm_f(File.join(@TEMP_DIR, "src.intp"))
FileUtils.rm_rf([@OUT_DIR, @TAB_DIR, @LOG_DIR, @ERR_DIR, @TEMP_DIR])
2019-05-13 08:25:22 -04:00
end
def assert_compile(asset, args = [], **opt)
2019-05-13 08:25:22 -04:00
file = File.basename(asset, '.y')
args = ([args].flatten) + [
"#{ASSET_DIR}/#{file}.y",
'-Do',
2019-07-02 01:10:50 -04:00
"-O#{@OUT_DIR}/#{file}",
"-o#{@TAB_DIR}/#{file}",
2019-05-13 08:25:22 -04:00
]
2019-06-20 04:41:01 -04:00
racc(*args, **opt)
2019-05-13 08:25:22 -04:00
end
def assert_debugfile(asset, ok)
file = File.basename(asset, '.y')
2019-07-02 01:10:50 -04:00
Dir.chdir(@LOG_DIR) do
2019-06-02 07:18:24 -04:00
File.foreach("#{file}.y") do |line|
2019-05-13 08:25:22 -04:00
line.strip!
case line
when /sr/ then assert_equal "sr#{ok[0]}", line
when /rr/ then assert_equal "rr#{ok[1]}", line
when /un/ then assert_equal "un#{ok[2]}", line
when /ur/ then assert_equal "ur#{ok[3]}", line
when /ex/ then assert_equal "ex#{ok[4]}", line
else
raise TestFailed, 'racc outputs unknown debug report???'
end
end
end
end
def assert_exec(asset)
lib_path = File.expand_path("../../lib", __FILE__)
2019-05-13 08:25:22 -04:00
file = File.basename(asset, '.y')
ruby "-I#{lib_path}", "#{@TAB_DIR}/#{file}"
2019-05-13 08:25:22 -04:00
end
def strip_version(source)
source.sub(/This file is automatically generated by Racc \d+\.\d+\.\d+/, '')
end
def assert_output_unchanged(asset)
# racc generates the difference results in GitHub Actions
2019-10-30 17:57:06 -04:00
omit unless RUBY_PLATFORM =~ /darwin/
2019-05-13 08:25:22 -04:00
file = File.basename(asset, '.y')
expected = File.read("#{REGRESS_DIR}/#{file}")
2019-07-02 01:10:50 -04:00
actual = File.read("#{@TAB_DIR}/#{file}")
2019-05-13 08:25:22 -04:00
result = (strip_version(expected) == strip_version(actual))
assert(result, "Output of test/assets/#{file}.y differed from " \
"expectation. Try compiling it and diff with test/regress/#{file}.")
end
def racc(*arg, **opt)
lib_path = File.expand_path("../../lib", __FILE__)
ruby "-I#{lib_path}", "-S", RACC, *arg, **opt
2019-05-13 08:25:22 -04:00
end
def ruby(*arg, **opt)
2019-07-02 01:10:50 -04:00
assert_ruby_status(["-C", @TEMP_DIR, *arg], **opt)
2019-05-13 08:25:22 -04:00
end
end
end
2019-06-20 05:20:38 -04:00
ensure
$VERBOSE = verbose
end