[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)
|
---|
| 5 |
|
---|
| 6 | File.open(File.join($dir, "loop1.rb"), "w") do |fp|
|
---|
| 7 | fp.puts "require 'loop2.rb'"
|
---|
| 8 | fp.puts "$loop1 = 'loop1'"
|
---|
| 9 | end
|
---|
| 10 | File.open(File.join($dir, "loop2.rb"), "w") do |fp|
|
---|
| 11 | fp.puts "require 'loop1.rb'"
|
---|
| 12 | fp.puts "$loop2 = 'loop2'"
|
---|
| 13 | end
|
---|
| 14 |
|
---|
| 15 | $require_test_count = 10
|
---|
| 16 | (1..$require_test_count-1).each do |i|
|
---|
| 17 | File.open(File.join($dir, "#{i+1}.rb"), "w") do |fp|
|
---|
| 18 | fp.puts "require '#{i}'"
|
---|
| 19 | fp.puts "s = 0"
|
---|
| 20 | (0..100).each{|num| fp.puts "s += #{num}" }
|
---|
| 21 | end
|
---|
| 22 | end
|
---|
| 23 | File.open(File.join($dir, "1.rb"), "w") do |fp|
|
---|
| 24 | fp.puts "$require_test_0 = 123"
|
---|
| 25 | end
|
---|
| 26 |
|
---|
| 27 | $LOAD_PATH = [$dir]
|
---|
| 28 | end
|
---|
| 29 |
|
---|
| 30 | def test_cleanup
|
---|
| 31 | if $dir && File.exist?($dir)
|
---|
| 32 | Dir.entries($dir).each do |e|
|
---|
| 33 | next if ['.', '..'].include? e
|
---|
| 34 | File.unlink File.join($dir,e)
|
---|
| 35 | end
|
---|
| 36 | Dir.unlink $dir
|
---|
| 37 | end
|
---|
| 38 | end
|
---|
| 39 |
|
---|
| 40 | #####
|
---|
| 41 | test_setup
|
---|
| 42 | #####
|
---|
| 43 |
|
---|
| 44 | assert("require loop check") do
|
---|
| 45 | require 'loop1'
|
---|
| 46 | assert_equal 'loop1', $loop1
|
---|
| 47 | assert_equal 'loop2', $loop2
|
---|
| 48 | end
|
---|
| 49 |
|
---|
| 50 | assert("require nest") do
|
---|
| 51 | before = $".size
|
---|
| 52 | require "#{$require_test_count}"
|
---|
| 53 | assert_equal before + $require_test_count, $".size
|
---|
| 54 | end
|
---|
| 55 |
|
---|
| 56 | #####
|
---|
| 57 | test_cleanup
|
---|
| 58 | #####
|
---|