1require 'tempfile'
2require 'open3'
3
4def 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
11end
12
13assert('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 <<-])
16end
17
18assert('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
24end
25
26assert '$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
40end
41
42assert '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)])
45end
46
47assert('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!
52end
53
54assert '__END__', '8.6' do
55  script = Tempfile.new('test.rb')
56
57  script.write <<EOS
58p 'test'
59  __END__ = 'fin'
60p __END__
61__END__
62p 'legend'
63EOS
64  script.flush
65  assert_equal "\"test\"\n\"fin\"\n", `#{cmd('mruby')} #{script.path}`
66end
67
68assert('garbage collecting built-in classes') do
69  script = Tempfile.new('test.rb')
70
71  script.write <<RUBY
72NilClass = nil
73GC.start
74Array.dup
75print nil.class.to_s
76RUBY
77  script.flush
78  assert_equal "NilClass", `#{cmd('mruby')} #{script.path}`
79  assert_equal 0, $?.exitstatus
80end
81
82assert('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."])
85end
86
87assert('mruby -d option') do
88  assert_mruby("false\n", "", true, ["-e", "p $DEBUG"])
89  assert_mruby("true\n", "", true, ["-dep $DEBUG"])
90end
91
92assert('mruby -e option (no code specified)') do
93  assert_mruby("", /\A.*: No code specified for -e\n\z/, false, %w[-e])
94end
95
96assert('mruby -h option') do
97  assert_mruby(/\AUsage: #{Regexp.escape cmd("mruby")} .*/m, "", true, %w[-h])
98end
99
100assert('mruby -r option') do
101  lib = Tempfile.new('lib.rb')
102  lib.write <<EOS
103class Hoge
104  def hoge
105    :hoge
106  end
107end
108EOS
109  lib.flush
110
111  script = Tempfile.new('test.rb')
112  script.write <<EOS
113print Hoge.new.hoge
114EOS
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
121end
122
123assert('mruby -r option (no library specified)') do
124  assert_mruby("", /\A.*: No library specified for -r\n\z/, false, %w[-r])
125end
126
127assert('mruby -r option (file not found)') do
128  assert_mruby("", /\A.*: Cannot open library file: .*\n\z/, false, %w[-r _no_exists_])
129end
130
131assert('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)])
135end
136
137assert('mruby --verbose option') do
138  assert_mruby(/\A[^\n]*NODE.*\n:end\n\z/m, "", true, %w[--verbose -e p(:end)])
139end
140
141assert('mruby --') do
142  assert_mruby(%{["-x", "1"]\n}, "", true, %w[-e p(ARGV) -- -x 1])
143end
144
145assert('mruby invalid short option') do
146  assert_mruby("", /\A.*: invalid option -1 .*\n\z/, false, %w[-1])
147end
148
149assert('mruby invalid long option') do
150  assert_mruby("", /\A.*: invalid option --longopt .*\n\z/, false, %w[--longopt])
151end
152
153assert('unhandled exception') do
154  assert_mruby("", /\bEXCEPTION\b.*\n\z/, false, %w[-e raise("EXCEPTION")])
155end
156
157assert('program file not found') do
158  assert_mruby("", /\A.*: Cannot open program file: .*\n\z/, false, %w[_no_exists_])
159end
160
161assert('codegen error') do
162  code = "def f(#{(1..100).map{|n| "a#{n}"} * ","}); end"
163  assert_mruby("", /\Acodegen error:.*\n\z/, false, ["-e", code])
164end
165