Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-bin-mruby / bintest / mruby.rb
1 require 'tempfile'
2 require 'open3'
3
4 def assert_mruby(exp_out, exp_err, exp_success, args)
5   out, err, stat = Open3.capture3(cmd("mruby"), *args)
6   assert "assert_mruby" do
7     assert_operator(exp_out, :===, out, "standard output")
8     assert_operator(exp_err, :===, err, "standard error")
9     assert_equal(exp_success, stat.success?, "exit success?")
10   end
11 end
12
13 assert('regression for #1564') do
14   assert_mruby("", /\A-e:1:2: syntax error, .*\n\z/, false, %w[-e <<])
15   assert_mruby("", /\A-e:1:3: syntax error, .*\n\z/, false, %w[-e <<-])
16 end
17
18 assert('regression for #1572') do
19   script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb')
20   File.write script.path, 'p "ok"'
21   system "#{cmd('mrbc')} -g -o #{bin.path} #{script.path}"
22   o = `#{cmd('mruby')} -b #{bin.path}`.strip
23   assert_equal '"ok"', o
24 end
25
26 assert '$0 value' do
27   script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb')
28
29   # .rb script
30   script.write "p $0\n"
31   script.flush
32   assert_equal "\"#{script.path}\"", `#{cmd('mruby')} "#{script.path}"`.chomp
33
34   # .mrb file
35   `#{cmd('mrbc')} -o "#{bin.path}" "#{script.path}"`
36   assert_equal "\"#{bin.path}\"", `#{cmd('mruby')} -b "#{bin.path}"`.chomp
37
38   # one liner
39   assert_equal '"-e"', `#{cmd('mruby')} -e #{shellquote('p $0')}`.chomp
40 end
41
42 assert 'ARGV value' do
43   assert_mruby(%{["ab", "cde"]\n}, "", true, %w[-e p(ARGV) ab cde])
44   assert_mruby("[]\n", "", true, %w[-e p(ARGV)])
45 end
46
47 assert('float literal') do
48   script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb')
49   File.write script.path, 'p [3.21, 2e308.infinite?, -2e308.infinite?]'
50   system "#{cmd('mrbc')} -g -o #{bin.path} #{script.path}"
51   assert_equal "[3.21, 1, -1]", `#{cmd('mruby')} -b #{bin.path}`.chomp!
52 end
53
54 assert '__END__', '8.6' do
55   script = Tempfile.new('test.rb')
56
57   script.write <<EOS
58 p 'test'
59   __END__ = 'fin'
60 p __END__
61 __END__
62 p 'legend'
63 EOS
64   script.flush
65   assert_equal "\"test\"\n\"fin\"\n", `#{cmd('mruby')} #{script.path}`
66 end
67
68 assert('garbage collecting built-in classes') do
69   script = Tempfile.new('test.rb')
70
71   script.write <<RUBY
72 NilClass = nil
73 GC.start
74 Array.dup
75 print nil.class.to_s
76 RUBY
77   script.flush
78   assert_equal "NilClass", `#{cmd('mruby')} #{script.path}`
79   assert_equal 0, $?.exitstatus
80 end
81
82 assert('mruby -c option') do
83   assert_mruby("Syntax OK\n", "", true, ["-c", "-e", "p 1"])
84   assert_mruby("", /\A-e:1:7: syntax error, .*\n\z/, false, ["-c", "-e", "p 1; 1."])
85 end
86
87 assert('mruby -d option') do
88   assert_mruby("false\n", "", true, ["-e", "p $DEBUG"])
89   assert_mruby("true\n", "", true, ["-dep $DEBUG"])
90 end
91
92 assert('mruby -e option (no code specified)') do
93   assert_mruby("", /\A.*: No code specified for -e\n\z/, false, %w[-e])
94 end
95
96 assert('mruby -h option') do
97   assert_mruby(/\AUsage: #{Regexp.escape cmd("mruby")} .*/m, "", true, %w[-h])
98 end
99
100 assert('mruby -r option') do
101   lib = Tempfile.new('lib.rb')
102   lib.write <<EOS
103 class Hoge
104   def hoge
105     :hoge
106   end
107 end
108 EOS
109   lib.flush
110
111   script = Tempfile.new('test.rb')
112   script.write <<EOS
113 print Hoge.new.hoge
114 EOS
115   script.flush
116   assert_equal 'hoge', `#{cmd('mruby')} -r #{lib.path} #{script.path}`
117   assert_equal 0, $?.exitstatus
118
119   assert_equal 'hogeClass', `#{cmd('mruby')} -r #{lib.path} -r #{script.path} -e #{shellquote('print Hoge.class')}`
120   assert_equal 0, $?.exitstatus
121 end
122
123 assert('mruby -r option (no library specified)') do
124   assert_mruby("", /\A.*: No library specified for -r\n\z/, false, %w[-r])
125 end
126
127 assert('mruby -r option (file not found)') do
128   assert_mruby("", /\A.*: Cannot open library file: .*\n\z/, false, %w[-r _no_exists_])
129 end
130
131 assert('mruby -v option') do
132   ver_re = '\Amruby \d+\.\d+\.\d+ \(\d+-\d+-\d+\)\n'
133   assert_mruby(/#{ver_re}\z/, "", true, %w[-v])
134   assert_mruby(/#{ver_re}^[^\n]*NODE.*\n:end\n\z/m, "", true, %w[-v -e p(:end)])
135 end
136
137 assert('mruby --verbose option') do
138   assert_mruby(/\A[^\n]*NODE.*\n:end\n\z/m, "", true, %w[--verbose -e p(:end)])
139 end
140
141 assert('mruby --') do
142   assert_mruby(%{["-x", "1"]\n}, "", true, %w[-e p(ARGV) -- -x 1])
143 end
144
145 assert('mruby invalid short option') do
146   assert_mruby("", /\A.*: invalid option -1 .*\n\z/, false, %w[-1])
147 end
148
149 assert('mruby invalid long option') do
150   assert_mruby("", /\A.*: invalid option --longopt .*\n\z/, false, %w[--longopt])
151 end
152
153 assert('unhandled exception') do
154   assert_mruby("", /\bEXCEPTION\b.*\n\z/, false, %w[-e raise("EXCEPTION")])
155 end
156
157 assert('program file not found') do
158   assert_mruby("", /\A.*: Cannot open program file: .*\n\z/, false, %w[_no_exists_])
159 end
160
161 assert('codegen error') do
162   code = "def f(#{(1..100).map{|n| "a#{n}"} * ","}); end"
163   assert_mruby("", /\Acodegen error:.*\n\z/, false, ["-e", code])
164 end