From b35b7a1ef25347735a6bb7c28ab7e77afea1d856 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 18 Oct 2021 08:50:10 -0700 Subject: Allow Kernel#load to load code into a specified module Instead of always using a new anonymous module for Kernel#load if the wrap argument is not false/nil, use the given module if a module is provided. Implements [Feature #6210] --- test/ruby/test_require.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'test') diff --git a/test/ruby/test_require.rb b/test/ruby/test_require.rb index 7a9faf18f9..77cdae64d9 100644 --- a/test/ruby/test_require.rb +++ b/test/ruby/test_require.rb @@ -367,6 +367,38 @@ class TestRequire < Test::Unit::TestCase } end + def test_load_into_module + Tempfile.create(["test_ruby_test_require", ".rb"]) {|t| + t.puts "def b; 1 end" + t.puts "class Foo" + t.puts " def c; 2 end" + t.puts "end" + t.close + + m = Module.new + load(t.path, m) + assert_equal([:b], m.private_instance_methods(false)) + c = Class.new do + include m + public :b + end + assert_equal(1, c.new.b) + assert_equal(2, m::Foo.new.c) + } + end + + def test_load_wrap_nil + Dir.mktmpdir do |tmp| + File.write("#{tmp}/1.rb", "class LoadWrapNil; end\n") + assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") + path = ""#{tmp.dump}"/1.rb" + begin; + load path, nil + assert_instance_of(Class, LoadWrapNil) + end; + end + end + def test_load_ospath bug = '[ruby-list:49994] path in ospath' base = "test_load\u{3042 3044 3046 3048 304a}".encode(Encoding::Windows_31J) -- cgit v1.2.3