[331] | 1 | $dir = File.join(Dir.tmpdir, "mruby-require-test-#{Time.now.to_i}.#{Time.now.usec}")
|
---|
| 2 |
|
---|
| 3 | def test_setup
|
---|
| 4 | Dir.mkdir($dir) unless File.exist?($dir)
|
---|
| 5 |
|
---|
| 6 | File.open(File.join($dir, "test.rb"), "w") do |fp|
|
---|
| 7 | fp.puts "$require_test_variable = 123"
|
---|
| 8 | end
|
---|
| 9 |
|
---|
| 10 | File.open(File.join($dir, "test_dir.rb"), "w") do |fp|
|
---|
| 11 | fp.puts "$test_dir = 'test_dir'"
|
---|
| 12 | end
|
---|
| 13 | Dir.mkdir(File.join($dir, "test_dir"))
|
---|
| 14 | File.open(File.join($dir, "test_dir", "test_dir.rb"), "w") do |fp|
|
---|
| 15 | fp.puts "$test_dir2 = 'test_dir/test_dir'"
|
---|
| 16 | end
|
---|
| 17 |
|
---|
| 18 | File.open(File.join($dir, "test_conf.conf"), "w") do |fp|
|
---|
| 19 | fp.puts "$test_conf = 'test_conf'"
|
---|
| 20 | end
|
---|
| 21 |
|
---|
| 22 | File.open(File.join($dir, "empty.rb"), "w")
|
---|
| 23 |
|
---|
| 24 | test_reset
|
---|
| 25 | end
|
---|
| 26 |
|
---|
| 27 | def test_reset
|
---|
| 28 | $require_test_variable = nil
|
---|
| 29 | $test_dir = nil
|
---|
| 30 | $test_dir2 = nil
|
---|
| 31 | $test_conf = nil
|
---|
| 32 | $LOAD_PATH = [$dir]
|
---|
| 33 | $" = []
|
---|
| 34 | end
|
---|
| 35 |
|
---|
| 36 | def remove_file_recursive(path)
|
---|
| 37 | if File.directory? path
|
---|
| 38 | Dir.entries(path).each do |entry|
|
---|
| 39 | next if ['.', '..'].include?(entry)
|
---|
| 40 | remove_file_recursive File.join(path, entry)
|
---|
| 41 | end
|
---|
| 42 | Dir.unlink path
|
---|
| 43 | else
|
---|
| 44 | File.unlink path
|
---|
| 45 | end
|
---|
| 46 | end
|
---|
| 47 |
|
---|
| 48 | def test_cleanup
|
---|
| 49 | if $dir && File.exist?($dir)
|
---|
| 50 | remove_file_recursive $dir
|
---|
| 51 | end
|
---|
| 52 | end
|
---|
| 53 |
|
---|
| 54 | #####
|
---|
| 55 | test_setup
|
---|
| 56 | #####
|
---|
| 57 |
|
---|
| 58 | assert("require 'test' should be success") do
|
---|
| 59 | test_reset
|
---|
| 60 |
|
---|
| 61 | assert_true require("test"), "require returns true when success"
|
---|
| 62 | assert_equal [File.join($dir, "test.rb")], $"
|
---|
| 63 | assert_equal 123, $require_test_variable
|
---|
| 64 | $require_test_variable = 789
|
---|
| 65 | assert_false require("test"), "2nd require should returns false"
|
---|
| 66 | assert_equal 789, $require_test_variable
|
---|
| 67 |
|
---|
| 68 | test_reset
|
---|
| 69 |
|
---|
| 70 | assert_true require("test.rb"), "require should be success with '.rb'"
|
---|
| 71 | assert_equal [File.join($dir, "test.rb")], $"
|
---|
| 72 | end
|
---|
| 73 |
|
---|
| 74 | assert("require with absolute path should be success") do
|
---|
| 75 | test_reset
|
---|
| 76 | assert_true require(File.join($dir, "test"))
|
---|
| 77 | assert_equal [File.join($dir, "test.rb")], $"
|
---|
| 78 |
|
---|
| 79 | test_reset
|
---|
| 80 | assert_true require(File.join($dir, "test.rb"))
|
---|
| 81 | assert_equal [File.join($dir, "test.rb")], $"
|
---|
| 82 | end
|
---|
| 83 |
|
---|
| 84 | assert("require with absolute path && empty load_path") do
|
---|
| 85 | test_reset
|
---|
| 86 | $LOAD_PATH = []
|
---|
| 87 |
|
---|
| 88 | assert_raise LoadError, "cannot load test.rb" do
|
---|
| 89 | require "test"
|
---|
| 90 | end
|
---|
| 91 | assert_equal true, require(File.join($dir, "test"))
|
---|
| 92 | end
|
---|
| 93 |
|
---|
| 94 | assert("require 'test_dir' should be success") do
|
---|
| 95 | test_reset
|
---|
| 96 |
|
---|
| 97 | assert_true require("test_dir"), "require 'test_dir' should be load 'test_dir.rb'"
|
---|
| 98 | assert_equal [File.join($dir, "test_dir.rb")], $"
|
---|
| 99 | assert_true require("test_dir/test_dir"), "require 'test_dir/test_dir' should be success"
|
---|
| 100 | assert_equal 'test_dir/test_dir', $test_dir2
|
---|
| 101 | end
|
---|
| 102 |
|
---|
| 103 | assert("require 'test_conf' should be fail") do
|
---|
| 104 | test_reset
|
---|
| 105 |
|
---|
| 106 | assert_raise LoadError, "require 'test_conf.conf' should be fail" do
|
---|
| 107 | require("test_conf.conf")
|
---|
| 108 | end
|
---|
| 109 | assert_raise LoadError, "require method can't load *.conf" do
|
---|
| 110 | require File.join($dir, "test_conf.conf")
|
---|
| 111 | end
|
---|
| 112 | end
|
---|
| 113 |
|
---|
| 114 | assert("require 'empty' should be success") do
|
---|
| 115 | test_reset
|
---|
| 116 |
|
---|
| 117 | assert_true require("empty")
|
---|
| 118 | assert_equal 0, File.size(File.join($dir, "empty.rb"))
|
---|
| 119 | end
|
---|
| 120 |
|
---|
| 121 | assert("load 'test.rb' should be success") do
|
---|
| 122 | test_reset
|
---|
| 123 |
|
---|
| 124 | assert_true load(File.join($dir, "test.rb"))
|
---|
| 125 | assert_equal 123, $require_test_variable
|
---|
| 126 | assert_true $".empty?
|
---|
| 127 | end
|
---|
| 128 |
|
---|
| 129 | assert("load 'test_conf.conf' should be success") do
|
---|
| 130 | test_reset
|
---|
| 131 |
|
---|
| 132 | assert_equal true, load(File.join($dir, "test_conf.conf"))
|
---|
| 133 | assert_equal "test_conf", $test_conf
|
---|
| 134 | end
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | #####
|
---|
| 138 | test_cleanup
|
---|
| 139 | #####
|
---|