File: mruby.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (178 lines) | stat: -rw-r--r-- 5,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'tempfile'
require 'open3'

def assert_mruby(exp_out, exp_err, exp_success, args)
  out, err, stat = Open3.capture3( *(cmd_list("mruby") + args))
  assert "assert_mruby" do
    assert_operator(exp_out, :===, out, "standard output")
    assert_operator(exp_err, :===, err, "standard error")
    assert_equal(exp_success, stat.success?, "exit success?")
  end
end

assert('regression for #1564') do
  assert_mruby("", /\A-e:1:2: syntax error, .*\n\z/, false, %w[-e <<])
  assert_mruby("", /\A-e:1:3: syntax error, .*\n\z/, false, %w[-e <<-])
end

assert('regression for #1572') do
  script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb')
  File.write script.path, 'p "ok"'
  system "#{cmd('mrbc')} -g -o #{bin.path} #{script.path}"
  o = `#{cmd('mruby')} #{bin.path}`.strip
  assert_equal '"ok"', o
end

assert '$0 value' do
  script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb')

  # .rb script
  script.write "p $0\n"
  script.flush
  assert_equal "\"#{script.path}\"", `#{cmd('mruby')} "#{script.path}"`.chomp

  # .mrb file
  `#{cmd('mrbc')} -o "#{bin.path}" "#{script.path}"`
  assert_equal "\"#{bin.path}\"", `#{cmd('mruby')} "#{bin.path}"`.chomp

  # one liner
  assert_equal '"-e"', `#{cmd('mruby')} -e #{shellquote('p $0')}`.chomp
end

assert 'ARGV value' do
  assert_mruby(%{["ab", "cde"]\n}, "", true, %w[-e p(ARGV) ab cde])
  assert_mruby("[]\n", "", true, %w[-e p(ARGV)])
end

assert '__END__', '8.6' do
  script = Tempfile.new('test.rb')

  script.write <<EOS
p 'test'
  __END__ = 'fin'
p __END__
__END__
p 'legend'
EOS
  script.flush
  assert_equal "\"test\"\n\"fin\"\n", `#{cmd('mruby')} #{script.path}`
end

assert('garbage collecting built-in classes') do
  script = Tempfile.new('test.rb')

  script.write <<RUBY
NilClass = nil
GC.start
Array.dup
print nil.class.to_s
RUBY
  script.flush
  assert_equal "NilClass", `#{cmd('mruby')} #{script.path}`
  assert_equal 0, $?.exitstatus
end

assert('mruby -c option') do
  assert_mruby("Syntax OK\n", "", true, ["-c", "-e", "p 1"])
  assert_mruby("", /\A-e:1:7: syntax error, .*\n\z/, false, ["-c", "-e", "p 1; 1."])
end

assert('mruby -d option') do
  assert_mruby("false\n", "", true, ["-e", "p $DEBUG"])
  assert_mruby("true\n", "", true, ["-dep $DEBUG"])
end

assert('mruby -e option (no code specified)') do
  assert_mruby("", /\A.*: No code specified for -e\n\z/, false, %w[-e])
end

assert('mruby -h option') do
  assert_mruby(/\AUsage: #{Regexp.escape cmd_bin("mruby")} .*/m, "", true, %w[-h])
end

assert('mruby -r option') do
  lib = Tempfile.new('lib.rb')
  lib.write <<EOS
class Hoge
  def hoge
    :hoge
  end
end
EOS
  lib.flush

  script = Tempfile.new('test.rb')
  script.write <<EOS
print Hoge.new.hoge
EOS
  script.flush
  assert_equal 'hoge', `#{cmd('mruby')} -r #{lib.path} #{script.path}`
  assert_equal 0, $?.exitstatus

  assert_equal 'hogeClass', `#{cmd('mruby')} -r #{lib.path} -r #{script.path} -e #{shellquote('print Hoge.class')}`
  assert_equal 0, $?.exitstatus
end

assert('mruby -r option (no library specified)') do
  assert_mruby("", /\A.*: No library specified for -r\n\z/, false, %w[-r])
end

assert('mruby -r option (file not found)') do
  assert_mruby("", /\A.*: Cannot open library file: .*\n\z/, false, %w[-r _no_exists_])
end

assert('mruby -v option') do
  ver_re = '\Amruby \d+\.\d+\.\d+.* \(\d+-\d+-\d+\)\n'
  assert_mruby(/#{ver_re}\z/, "", true, %w[-v])
  assert_mruby(/#{ver_re}^[^\n]*NODE.*\n:end\n\z/m, "", true, %w[-v -e p(:end)])
end

assert('mruby --verbose option') do
  assert_mruby(/\A[^\n]*NODE.*\n:end\n\z/m, "", true, %w[--verbose -e p(:end)])
end

assert('mruby --') do
  assert_mruby(%{["-x", "1"]\n}, "", true, %w[-e p(ARGV) -- -x 1])
end

assert('mruby invalid short option') do
  assert_mruby("", /\A.*: invalid option -1 .*\n\z/, false, %w[-1])
end

assert('mruby invalid long option') do
  assert_mruby("", /\A.*: invalid option --longopt .*\n\z/, false, %w[--longopt])
end

assert('unhandled exception') do
  assert_mruby("", /\bEXCEPTION\b.*\n\z/, false, %w[-e raise("EXCEPTION")])
end

assert('program file not found') do
  assert_mruby("", /\A.*: Cannot open program file: .*\n\z/, false, %w[_no_exists_])
end

assert('codegen error') do
  code = "def f(#{(1..100).map{|n| "a#{n}"} * ","}); end"
  assert_mruby("", /\A.*\n\z/, false, ["-e", code])
end

assert('top level local variables are in file scope') do
  arb, amrb = Tempfile.new('a.rb'), Tempfile.new('a.mrb')
  brb, bmrb = Tempfile.new('b.rb'), Tempfile.new('b.mrb')
  crb, cmrb = Tempfile.new('c.rb'), Tempfile.new('c.mrb')
  drb, dmrb = Tempfile.new('d.rb'), Tempfile.new('d.mrb')

  File.write arb.path, 'a = 1'
  system "#{cmd('mrbc')} -g -o #{amrb.path} #{arb.path}"
  File.write brb.path, 'p a'
  system "#{cmd('mrbc')} -g -o #{bmrb.path} #{brb.path}"
  assert_mruby("", /:1: undefined method 'a' .*\(NoMethodError\)\n\z/, false, ["-r", arb.path, brb.path])
  assert_mruby("", /:1: undefined method 'a' .*\(NoMethodError\)\n\z/, false, ["-b", "-r", amrb.path, bmrb.path])

  File.write crb.path, 'a, b, c = 1, 2, 3; A = -> { b = -2; [a, b, c] }'
  system "#{cmd('mrbc')} -g -o #{cmrb.path} #{crb.path}"
  File.write drb.path, 'a, b = 5, 6; p A.call; p a, b'
  system "#{cmd('mrbc')} -g -o #{dmrb.path} #{drb.path}"
  assert_mruby("[1, -2, 3]\n5\n6\n", "", true, ["-r", crb.path, drb.path])
  assert_mruby("[1, -2, 3]\n5\n6\n", "", true, ["-b", "-r", cmrb.path, dmrb.path])
end