diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:19:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:20:02 +0000 |
commit | 58daab21cd043e1dc37024a7f99b396788372918 (patch) | |
tree | 96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /web/server/h2o/libh2o/deps/mruby-require/test | |
parent | Releasing debian version 1.43.2-1. (diff) | |
download | netdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz netdata-58daab21cd043e1dc37024a7f99b396788372918.zip |
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'web/server/h2o/libh2o/deps/mruby-require/test')
6 files changed, 311 insertions, 0 deletions
diff --git a/web/server/h2o/libh2o/deps/mruby-require/test/d/required.rb b/web/server/h2o/libh2o/deps/mruby-require/test/d/required.rb new file mode 100644 index 000000000..d6218cd03 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-require/test/d/required.rb @@ -0,0 +1,19 @@ +# global variables +$gvar0 = 1 +$gvar1 = 1 + +# toplevel local variables +lvar0 = 1 +lvar1 = 1 + +# define a procedure +def proc0 + :proc0 +end + +# define a new method of an existing class. +class MrubyRequireClass + def foo + :foo + end +end diff --git a/web/server/h2o/libh2o/deps/mruby-require/test/require.rb b/web/server/h2o/libh2o/deps/mruby-require/test/require.rb new file mode 100644 index 000000000..95e1ef022 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-require/test/require.rb @@ -0,0 +1,28 @@ +assert("Kernel.require") do + # see d/required.rb + $gvar1 = 0 + lvar1 = 0 + class MrubyRequireClass; end + + assert_true require(File.join(File.dirname(__FILE__), "d", "required.rb")) + + # Kernel.require can create a global variable + assert_equal 1, $gvar0 + + # Kernel.require can change value of a global variable + assert_equal 1, $gvar1 + + # Kernel.require cannot create a local variable + assert_raise(NoMethodError) do + lvar0 + end + + # Kernel.require cannot change value of a local variable + assert_equal 0, lvar1 + + # Kernel.require can define a toplevel procedure + assert_equal :proc0, proc0 + + # Kernel.require can add a method to an existing class + assert_equal :foo, MrubyRequireClass.new.foo +end diff --git a/web/server/h2o/libh2o/deps/mruby-require/test/test.rb b/web/server/h2o/libh2o/deps/mruby-require/test/test.rb new file mode 100644 index 000000000..0886cabef --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-require/test/test.rb @@ -0,0 +1,34 @@ +$" = [] # init + +assert "Kernel#_load_rb_str" do + assert_equal true, self.methods.include?(:_load_rb_str) + assert_equal false, Object.const_defined?(:LOAD_RB_STR_TEST) + _load_rb_str("LOAD_RB_STR_TEST = 1") + assert_equal true, Object.const_defined?(:LOAD_RB_STR_TEST) +end + +assert "$LOAD_PATH check" do + assert_equal Array, $LOAD_PATH.class +end + +assert '$" check' do + assert_equal [], $" +end + +assert('load - error check') do + assert_raise TypeError, "load(nil) should raise TypeError" do + load nil + end + assert_raise LoadError, "load('notfound') should raise LoadError" do + load 'notfound' + end +end + +assert('require - error check') do + assert_raise TypeError, "require(nil) should raise TypeError" do + require nil + end + assert_raise LoadError, "require('notfound') should raise LoadError" do + require "notfound" + end +end diff --git a/web/server/h2o/libh2o/deps/mruby-require/test/test2.rb b/web/server/h2o/libh2o/deps/mruby-require/test/test2.rb new file mode 100644 index 000000000..b66b9fdf3 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-require/test/test2.rb @@ -0,0 +1,139 @@ +$dir = File.join(Dir.tmpdir, "mruby-require-test-#{Time.now.to_i}.#{Time.now.usec}") + +def test_setup + Dir.mkdir($dir) unless File.exist?($dir) + + File.open(File.join($dir, "test.rb"), "w") do |fp| + fp.puts "$require_test_variable = 123" + end + + File.open(File.join($dir, "test_dir.rb"), "w") do |fp| + fp.puts "$test_dir = 'test_dir'" + end + Dir.mkdir(File.join($dir, "test_dir")) + File.open(File.join($dir, "test_dir", "test_dir.rb"), "w") do |fp| + fp.puts "$test_dir2 = 'test_dir/test_dir'" + end + + File.open(File.join($dir, "test_conf.conf"), "w") do |fp| + fp.puts "$test_conf = 'test_conf'" + end + + File.open(File.join($dir, "empty.rb"), "w") + + test_reset +end + +def test_reset + $require_test_variable = nil + $test_dir = nil + $test_dir2 = nil + $test_conf = nil + $LOAD_PATH = [$dir] + $" = [] +end + +def remove_file_recursive(path) + if File.directory? path + Dir.entries(path).each do |entry| + next if ['.', '..'].include?(entry) + remove_file_recursive File.join(path, entry) + end + Dir.unlink path + else + File.unlink path + end +end + +def test_cleanup + if $dir && File.exist?($dir) + remove_file_recursive $dir + end +end + +##### +test_setup +##### + +assert("require 'test' should be success") do + test_reset + + assert_true require("test"), "require returns true when success" + assert_equal [File.join($dir, "test.rb")], $" + assert_equal 123, $require_test_variable + $require_test_variable = 789 + assert_false require("test"), "2nd require should returns false" + assert_equal 789, $require_test_variable + + test_reset + + assert_true require("test.rb"), "require should be success with '.rb'" + assert_equal [File.join($dir, "test.rb")], $" +end + +assert("require with absolute path should be success") do + test_reset + assert_true require(File.join($dir, "test")) + assert_equal [File.join($dir, "test.rb")], $" + + test_reset + assert_true require(File.join($dir, "test.rb")) + assert_equal [File.join($dir, "test.rb")], $" +end + +assert("require with absolute path && empty load_path") do + test_reset + $LOAD_PATH = [] + + assert_raise LoadError, "cannot load test.rb" do + require "test" + end + assert_equal true, require(File.join($dir, "test")) +end + +assert("require 'test_dir' should be success") do + test_reset + + assert_true require("test_dir"), "require 'test_dir' should be load 'test_dir.rb'" + assert_equal [File.join($dir, "test_dir.rb")], $" + assert_true require("test_dir/test_dir"), "require 'test_dir/test_dir' should be success" + assert_equal 'test_dir/test_dir', $test_dir2 +end + +assert("require 'test_conf' should be fail") do + test_reset + + assert_raise LoadError, "require 'test_conf.conf' should be fail" do + require("test_conf.conf") + end + assert_raise LoadError, "require method can't load *.conf" do + require File.join($dir, "test_conf.conf") + end +end + +assert("require 'empty' should be success") do + test_reset + + assert_true require("empty") + assert_equal 0, File.size(File.join($dir, "empty.rb")) +end + +assert("load 'test.rb' should be success") do + test_reset + + assert_true load(File.join($dir, "test.rb")) + assert_equal 123, $require_test_variable + assert_true $".empty? +end + +assert("load 'test_conf.conf' should be success") do + test_reset + + assert_equal true, load(File.join($dir, "test_conf.conf")) + assert_equal "test_conf", $test_conf +end + + +##### +test_cleanup +##### diff --git a/web/server/h2o/libh2o/deps/mruby-require/test/test_context.rb b/web/server/h2o/libh2o/deps/mruby-require/test/test_context.rb new file mode 100644 index 000000000..001174748 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-require/test/test_context.rb @@ -0,0 +1,33 @@ +$dir = File.join(Dir.tmpdir, "mruby-require-test-#{Time.now.to_i}.#{Time.now.usec}") + +def test_setup + Dir.mkdir($dir) unless File.exist?($dir) + + File.open(File.join($dir, "foo.rb"), "w") do |f| + f.puts "$require_context = self" + end +end + +def test_cleanup + if $dir && File.exist?($dir) + Dir.entries($dir).each do |e| + next if ['.', '..'].include? e + File.unlink File.join($dir,e) + end + Dir.unlink $dir + end +end + + +##### +test_setup +##### + +assert("require context") do + require File.join($dir, 'foo.rb') + assert_equal self, $require_context +end + +##### +test_cleanup +##### diff --git a/web/server/h2o/libh2o/deps/mruby-require/test/test_nest_loop.rb b/web/server/h2o/libh2o/deps/mruby-require/test/test_nest_loop.rb new file mode 100644 index 000000000..c5a4b18b0 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-require/test/test_nest_loop.rb @@ -0,0 +1,58 @@ +$dir = File.join(Dir.tmpdir, "mruby-require-test-#{Time.now.to_i}.#{Time.now.usec}") + +def test_setup + Dir.mkdir($dir) + + File.open(File.join($dir, "loop1.rb"), "w") do |fp| + fp.puts "require 'loop2.rb'" + fp.puts "$loop1 = 'loop1'" + end + File.open(File.join($dir, "loop2.rb"), "w") do |fp| + fp.puts "require 'loop1.rb'" + fp.puts "$loop2 = 'loop2'" + end + + $require_test_count = 10 + (1..$require_test_count-1).each do |i| + File.open(File.join($dir, "#{i+1}.rb"), "w") do |fp| + fp.puts "require '#{i}'" + fp.puts "s = 0" + (0..100).each{|num| fp.puts "s += #{num}" } + end + end + File.open(File.join($dir, "1.rb"), "w") do |fp| + fp.puts "$require_test_0 = 123" + end + + $LOAD_PATH = [$dir] +end + +def test_cleanup + if $dir && File.exist?($dir) + Dir.entries($dir).each do |e| + next if ['.', '..'].include? e + File.unlink File.join($dir,e) + end + Dir.unlink $dir + end +end + +##### +test_setup +##### + +assert("require loop check") do + require 'loop1' + assert_equal 'loop1', $loop1 + assert_equal 'loop2', $loop2 +end + +assert("require nest") do + before = $".size + require "#{$require_test_count}" + assert_equal before + $require_test_count, $".size +end + +##### +test_cleanup +##### |