From 1a096110a85b9f49b68152d82a0b907a64fb0243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Sat, 7 Sep 2019 22:13:53 +0300 Subject: [PATCH] basic require_relative (hack) opted to hack require to be getting on need require for the test helper and the files in lib/parfait General require mechanism would still be ok, but require_ralative means implementing file source, which needs to be dragged around. I'll make an issue --- lib/ruby/send_statement.rb | 20 +++++++++++++++++--- test/ruby/test_require.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 test/ruby/test_require.rb diff --git a/lib/ruby/send_statement.rb b/lib/ruby/send_statement.rb index ce781d92..bda73ad8 100644 --- a/lib/ruby/send_statement.rb +++ b/lib/ruby/send_statement.rb @@ -6,13 +6,27 @@ module Ruby class SendStatement < CallStatement def to_vool - return super unless @receiver.is_a?(ModuleName) and @receiver.name == :X - args = @arguments.collect { |arg| arg.to_vool } - Vool::MacroExpression.new(name , args) + if @receiver.is_a?(ModuleName) and @receiver.name == :X + args = @arguments.collect { |arg| arg.to_vool } + return Vool::MacroExpression.new(name , args) + end + return require_file if( @name == :require_relative ) + return super end def to_s(depth = 0) at_depth( depth , "#{receiver}.#{name}(#{arguments.join(', ')})") end + def require_file + target = @arguments.first.value + if(target == 'helper') + file = "/test/rubyx/rt_parfait/helper.rb" + else + file = "/lib/parfait/#{target}.rb" + end + path = File.expand_path( "../../../#{file}" , __FILE__) + source = File.read(path) + RubyCompiler.compile( source ).to_vool + end end class SuperStatement < SendStatement def initialize(args) diff --git a/test/ruby/test_require.rb b/test/ruby/test_require.rb new file mode 100644 index 00000000..294fa118 --- /dev/null +++ b/test/ruby/test_require.rb @@ -0,0 +1,34 @@ +require_relative "helper" + +module Ruby + class TestSendRequireHelper < MiniTest::Test + include RubyTests + def setup + @lst = compile( "require_relative 'helper'").to_vool + end + def test_helper_class + assert_equal Vool::ClassExpression , @lst.class + assert_equal :ParfaitTest , @lst.name + end + def test_methods + assert_equal Vool::Statements , @lst.body.class + assert_equal Vool::MethodExpression , @lst.body.first.class + assert_equal :setup , @lst.body.first.name + end + end + class TestSendRequireparfait < MiniTest::Test + include RubyTests + def setup + @lst = compile( "require_relative 'object'").to_vool + end + def test_helper_class + assert_equal Vool::ClassExpression , @lst.class + assert_equal :Object , @lst.name + end + def test_methods + assert_equal Vool::Statements , @lst.body.class + assert_equal Vool::MethodExpression , @lst.body.first.class + assert_equal :type , @lst.body.first.name + end + end +end