source: EcnlProtoTool/trunk/mrbgems/mruby-require/test/test2.rb@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 3.3 KB
Line 
1$dir = File.join(Dir.tmpdir, "mruby-require-test-#{Time.now.to_i}.#{Time.now.usec}")
2
3def 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
25end
26
27def test_reset
28 $require_test_variable = nil
29 $test_dir = nil
30 $test_dir2 = nil
31 $test_conf = nil
32 $LOAD_PATH = [$dir]
33 $" = []
34end
35
36def 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
46end
47
48def test_cleanup
49 if $dir && File.exist?($dir)
50 remove_file_recursive $dir
51 end
52end
53
54#####
55test_setup
56#####
57
58assert("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")], $"
72end
73
74assert("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")], $"
82end
83
84assert("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"))
92end
93
94assert("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
101end
102
103assert("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
112end
113
114assert("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"))
119end
120
121assert("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?
127end
128
129assert("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
134end
135
136
137#####
138test_cleanup
139#####
Note: See TracBrowser for help on using the repository browser.