2012-11-05 19:49:57 -05:00
|
|
|
# coding: US-ASCII
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2008-04-13 11:52:14 -04:00
|
|
|
require 'test/unit'
|
2008-05-01 08:47:07 -04:00
|
|
|
require 'stringio'
|
2008-04-13 11:52:14 -04:00
|
|
|
|
|
|
|
class TestParse < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@verbose = $VERBOSE
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$VERBOSE = @verbose
|
|
|
|
end
|
|
|
|
|
2019-07-07 05:17:51 -04:00
|
|
|
def test_error_line
|
|
|
|
assert_syntax_error('------,,', /\n\z/, 'Message to pipe should end with a newline')
|
|
|
|
end
|
|
|
|
|
2008-04-13 11:52:14 -04:00
|
|
|
def test_else_without_rescue
|
2018-03-22 21:09:57 -04:00
|
|
|
assert_syntax_error(<<-END, %r":#{__LINE__+2}: else without rescue"o, [__FILE__, __LINE__+1])
|
2008-04-13 11:52:14 -04:00
|
|
|
begin
|
|
|
|
else
|
|
|
|
42
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-04-13 11:52:14 -04:00
|
|
|
def test_alias_backref
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /can't make alias/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
alias $foo $1
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_command_call
|
|
|
|
t = Object.new
|
|
|
|
def t.foo(x); x; end
|
|
|
|
|
|
|
|
a = false
|
|
|
|
b = c = d = true
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
a &&= t.foo 42
|
|
|
|
b &&= t.foo 42
|
|
|
|
c &&= t.foo nil
|
|
|
|
d &&= t.foo false
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal([false, 42, nil, false], [a, b, c, d])
|
|
|
|
|
|
|
|
a = 3
|
|
|
|
assert_nothing_raised { eval("a &= t.foo 5") }
|
|
|
|
assert_equal(1, a)
|
|
|
|
|
|
|
|
a = [nil, nil, true, true]
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
a[0] ||= t.foo 42
|
|
|
|
a[1] &&= t.foo 42
|
|
|
|
a[2] ||= t.foo 42
|
|
|
|
a[3] &&= t.foo 42
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal([42, nil, true, 42], a)
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
class << o
|
|
|
|
attr_accessor :foo, :bar, :Foo, :Bar, :baz, :qux
|
|
|
|
end
|
|
|
|
o.foo = o.Foo = o::baz = nil
|
|
|
|
o.bar = o.Bar = o::qux = 1
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
o.foo ||= t.foo 42
|
|
|
|
o.bar &&= t.foo 42
|
|
|
|
o.Foo ||= t.foo 42
|
|
|
|
o.Bar &&= t.foo 42
|
|
|
|
o::baz ||= t.foo 42
|
|
|
|
o::qux &&= t.foo 42
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal([42, 42], [o.foo, o.bar])
|
|
|
|
assert_equal([42, 42], [o.Foo, o.Bar])
|
|
|
|
assert_equal([42, 42], [o::baz, o::qux])
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /Can't set variable/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
$1 ||= t.foo 42
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def t.bar(x); x + yield; end
|
|
|
|
|
|
|
|
a = b = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
a = t.bar "foo" do
|
|
|
|
"bar"
|
|
|
|
end.gsub "ob", "OB"
|
|
|
|
b = t.bar "foo" do
|
|
|
|
"bar"
|
|
|
|
end::gsub "ob", "OB"
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal("foOBar", a)
|
|
|
|
assert_equal("foOBar", b)
|
|
|
|
|
|
|
|
a = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
t.instance_eval <<-END, __FILE__, __LINE__+1
|
2008-09-06 18:14:28 -04:00
|
|
|
a = bar "foo" do "bar" end
|
2008-04-13 11:52:14 -04:00
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal("foobar", a)
|
|
|
|
|
|
|
|
a = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-09-06 18:14:28 -04:00
|
|
|
a = t::bar "foo" do "bar" end
|
2008-04-13 11:52:14 -04:00
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal("foobar", a)
|
|
|
|
|
|
|
|
def t.baz(*r)
|
|
|
|
@baz = r + (block_given? ? [yield] : [])
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
t.instance_eval "baz (1), 2"
|
|
|
|
end
|
|
|
|
assert_equal([1, 2], t.instance_eval { @baz })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mlhs_node
|
|
|
|
c = Class.new
|
|
|
|
class << c
|
|
|
|
attr_accessor :foo, :bar, :Foo, :Bar
|
|
|
|
FOO = BAR = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
c::foo, c::bar = 1, 2
|
|
|
|
c.Foo, c.Bar = 1, 2
|
|
|
|
c::FOO, c::BAR = 1, 2
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal([1, 2], [c::foo, c::bar])
|
|
|
|
assert_equal([1, 2], [c.Foo, c.Bar])
|
|
|
|
assert_equal([1, 2], [c::FOO, c::BAR])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dynamic_constant_assignment
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /dynamic constant/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def foo
|
|
|
|
self::FOO, self::BAR = 1, 2
|
|
|
|
::FOO, ::BAR = 1, 2
|
|
|
|
end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /Can't set variable/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
$1, $2 = 1, 2
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /dynamic constant/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def foo
|
|
|
|
::FOO = 1
|
|
|
|
end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
c = Class.new
|
2017-09-26 08:21:51 -04:00
|
|
|
c.freeze
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_valid_syntax("#{<<~"begin;"}\n#{<<~'end;'}") do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
c::FOO &= 1
|
|
|
|
::FOO &= 1
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /Can't set variable/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
$1 &= 1
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_module
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /must be CONSTANT/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
class foo; end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /in method body/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def foo
|
|
|
|
class Foo; end
|
|
|
|
module Bar; end
|
|
|
|
end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_valid_syntax("#{<<~"begin;"}\n#{<<~'end;'}") do
|
|
|
|
begin;
|
2015-08-10 22:53:31 -04:00
|
|
|
class Foo 1; end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_op_name
|
|
|
|
o = Object.new
|
|
|
|
def o.>(x); x; end
|
|
|
|
def o./(x); x; end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
o.instance_eval <<-END, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
undef >, /
|
|
|
|
END
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_arg
|
|
|
|
o = Object.new
|
|
|
|
class << o
|
|
|
|
attr_accessor :foo, :bar, :Foo, :Bar, :baz, :qux
|
|
|
|
end
|
|
|
|
o.foo = o.Foo = o::baz = nil
|
|
|
|
o.bar = o.Bar = o::qux = 1
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
o.foo ||= 42
|
|
|
|
o.bar &&= 42
|
|
|
|
o.Foo ||= 42
|
|
|
|
o.Bar &&= 42
|
|
|
|
o::baz ||= 42
|
|
|
|
o::qux &&= 42
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal([42, 42], [o.foo, o.bar])
|
|
|
|
assert_equal([42, 42], [o.Foo, o.Bar])
|
|
|
|
assert_equal([42, 42], [o::baz, o::qux])
|
|
|
|
|
|
|
|
a = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
a = -2.0 ** 2
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal(-4.0, a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_block_variable
|
|
|
|
o = Object.new
|
|
|
|
def o.foo(*r); yield(*r); end
|
|
|
|
|
|
|
|
a = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-09-06 18:14:28 -04:00
|
|
|
o.foo 1 do|; a| a = 42 end
|
2008-04-13 11:52:14 -04:00
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_nil(a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_arg
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be a constant/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def foo(FOO); end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be an instance variable/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def foo(@foo); end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be a global variable/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def foo($foo); end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be a class variable/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def foo(@@foo); end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be an instance variable/) do
|
|
|
|
begin;
|
|
|
|
o.foo {|; @a| @a = 42 }
|
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_lambda
|
|
|
|
a = b = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
a = -> do
|
|
|
|
b = 42
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
a.call
|
|
|
|
assert_equal(42, b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_block_call_colon2
|
|
|
|
o = Object.new
|
|
|
|
def o.foo(x); x + yield; end
|
|
|
|
|
|
|
|
a = b = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
o.instance_eval <<-END, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
a = foo 1 do 42 end.to_s
|
|
|
|
b = foo 1 do 42 end::to_s
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal("43", a)
|
|
|
|
assert_equal("43", b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_call_method
|
|
|
|
a = b = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
a = proc {|x| x + "bar" }.("foo")
|
|
|
|
b = proc {|x| x + "bar" }::("foo")
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal("foobar", a)
|
|
|
|
assert_equal("foobar", b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xstring
|
|
|
|
assert_raise(Errno::ENOENT) do
|
|
|
|
eval("``")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_words
|
|
|
|
assert_equal([], %W( ))
|
2019-01-24 06:43:11 -05:00
|
|
|
assert_syntax_error('%w[abc', /unterminated list/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-04-13 11:52:14 -04:00
|
|
|
def test_dstr
|
|
|
|
@@foo = 1
|
|
|
|
assert_equal("foo 1 bar", "foo #@@foo bar")
|
|
|
|
"1" =~ /(.)/
|
|
|
|
assert_equal("foo 1 bar", "foo #$1 bar")
|
2019-11-14 02:37:25 -05:00
|
|
|
assert_equal('foo #@1 bar', eval('"foo #@1 bar"'))
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2013-11-29 09:03:26 -05:00
|
|
|
def test_dstr_disallowed_variable
|
2013-05-10 10:56:40 -04:00
|
|
|
bug8375 = '[ruby-core:54885] [Bug #8375]'
|
2019-03-17 01:21:18 -04:00
|
|
|
%w[@ @. @@ @@1 @@. $ $%].each do |src|
|
2013-05-10 10:56:40 -04:00
|
|
|
src = '#'+src+' '
|
|
|
|
str = assert_nothing_raised(SyntaxError, "#{bug8375} #{src.dump}") do
|
|
|
|
break eval('"'+src+'"')
|
|
|
|
end
|
|
|
|
assert_equal(src, str, bug8375)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-13 11:52:14 -04:00
|
|
|
def test_dsym
|
|
|
|
assert_nothing_raised { eval(':""') }
|
|
|
|
end
|
|
|
|
|
2019-03-14 04:13:46 -04:00
|
|
|
def assert_disallowed_variable(type, noname, invalid)
|
|
|
|
noname.each do |name|
|
2019-03-17 01:21:18 -04:00
|
|
|
assert_syntax_error("proc{a = #{name} }", "`#{noname[0]}' without identifiers is not allowed as #{type} variable name")
|
2019-03-14 04:13:46 -04:00
|
|
|
end
|
2015-05-12 22:13:43 -04:00
|
|
|
invalid.each do |name|
|
2019-03-17 01:21:18 -04:00
|
|
|
assert_syntax_error("proc {a = #{name} }", "`#{name}' is not allowed as #{type} variable name")
|
2015-05-12 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_disallowed_instance_variable
|
2019-03-17 01:21:18 -04:00
|
|
|
assert_disallowed_variable("an instance", %w[@ @.], %w[])
|
2015-05-12 22:13:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_disallowed_class_variable
|
2019-03-14 04:13:46 -04:00
|
|
|
assert_disallowed_variable("a class", %w[@@ @@.], %w[@@1])
|
2015-05-12 22:13:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_disallowed_gloal_variable
|
2019-03-14 04:13:46 -04:00
|
|
|
assert_disallowed_variable("a global", %w[$], %w[$%])
|
2015-05-12 22:13:43 -04:00
|
|
|
end
|
|
|
|
|
2008-04-13 11:52:14 -04:00
|
|
|
def test_arg2
|
|
|
|
o = Object.new
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
def o.foo(a=42,*r,z,&b); b.call(r.inject(a*1000+z*100, :+)); end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal(-1405, o.foo(1,2,3,4) {|x| -x })
|
|
|
|
assert_equal(-1302, o.foo(1,2,3) {|x| -x })
|
|
|
|
assert_equal(-1200, o.foo(1,2) {|x| -x })
|
|
|
|
assert_equal(-42100, o.foo(1) {|x| -x })
|
|
|
|
assert_raise(ArgumentError) { o.foo() }
|
|
|
|
|
2020-12-17 06:06:18 -05:00
|
|
|
o = Object.new
|
2008-04-13 11:52:14 -04:00
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
def o.foo(a=42,z,&b); b.call(a*1000+z*100); end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal(-1200, o.foo(1,2) {|x| -x } )
|
|
|
|
assert_equal(-42100, o.foo(1) {|x| -x } )
|
|
|
|
assert_raise(ArgumentError) { o.foo() }
|
|
|
|
|
2020-12-17 06:06:18 -05:00
|
|
|
o = Object.new
|
2008-04-13 11:52:14 -04:00
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
def o.foo(*r,z,&b); b.call(r.inject(z*100, :+)); end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal(-303, o.foo(1,2,3) {|x| -x } )
|
|
|
|
assert_equal(-201, o.foo(1,2) {|x| -x } )
|
|
|
|
assert_equal(-100, o.foo(1) {|x| -x } )
|
|
|
|
assert_raise(ArgumentError) { o.foo() }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_duplicate_argument
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", '') do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
1.times {|&b?| }
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /duplicated argument/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
1.times {|a, a|}
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /duplicated argument/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def foo(a, a); end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_define_singleton_error
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /singleton method for literals/) do
|
|
|
|
begin;
|
2008-04-13 11:52:14 -04:00
|
|
|
def ("foo").foo; end
|
2019-11-10 19:16:00 -05:00
|
|
|
end;
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-15 01:25:42 -05:00
|
|
|
def test_op_asgn1_with_block
|
|
|
|
t = Object.new
|
|
|
|
a = []
|
|
|
|
blk = proc {|x| a << x }
|
|
|
|
def t.[](_)
|
|
|
|
yield(:aref)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
def t.[]=(_, _)
|
|
|
|
yield(:aset)
|
|
|
|
end
|
|
|
|
def t.dummy(_)
|
|
|
|
end
|
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
|
|
|
t[42, &blk] ||= 42
|
|
|
|
END
|
|
|
|
assert_equal([:aref, :aset], a)
|
|
|
|
a.clear
|
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
|
|
|
t[42, &blk] ||= t.dummy 42 # command_asgn test
|
|
|
|
END
|
|
|
|
assert_equal([:aref, :aset], a)
|
2019-06-29 20:34:41 -04:00
|
|
|
blk
|
2018-01-15 01:25:42 -05:00
|
|
|
end
|
|
|
|
|
2008-04-13 11:52:14 -04:00
|
|
|
def test_backquote
|
|
|
|
t = Object.new
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
def t.`(x); "foo" + x + "bar"; end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
a = b = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
a = t.` "zzz"
|
|
|
|
1.times {|;z| t.` ("zzz") }
|
|
|
|
END
|
2012-09-15 05:26:21 -04:00
|
|
|
t.instance_eval <<-END, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
b = `zzz`
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal("foozzzbar", a)
|
|
|
|
assert_equal("foozzzbar", b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_carrige_return
|
|
|
|
assert_equal(2, eval("1 +\r\n1"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_string
|
2017-07-15 04:29:03 -04:00
|
|
|
mesg = 'from the backslash through the invalid char'
|
|
|
|
|
2017-07-16 07:35:27 -04:00
|
|
|
e = assert_syntax_error('"\xg1"', /hex escape/)
|
2019-07-07 05:17:51 -04:00
|
|
|
assert_equal(' ^~'"\n", e.message.lines.last, mesg)
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2017-07-16 20:11:38 -04:00
|
|
|
e = assert_syntax_error('"\u{1234"', 'unterminated Unicode escape')
|
2019-07-07 05:17:51 -04:00
|
|
|
assert_equal(' ^'"\n", e.message.lines.last, mesg)
|
2017-07-16 19:45:48 -04:00
|
|
|
|
|
|
|
e = assert_syntax_error('"\u{xxxx}"', 'invalid Unicode escape')
|
2019-07-07 05:17:51 -04:00
|
|
|
assert_equal(' ^'"\n", e.message.lines.last, mesg)
|
2017-07-16 20:11:38 -04:00
|
|
|
|
|
|
|
e = assert_syntax_error('"\u{xxxx', 'Unicode escape')
|
|
|
|
assert_pattern_list([
|
|
|
|
/.*: invalid Unicode escape\n.*\n/,
|
|
|
|
/ \^/,
|
|
|
|
/\n/,
|
|
|
|
/.*: unterminated Unicode escape\n.*\n/,
|
|
|
|
/ \^/,
|
|
|
|
/\n/,
|
|
|
|
/.*: unterminated string.*\n.*\n/,
|
2019-07-07 05:17:51 -04:00
|
|
|
/ \^\n/,
|
2017-07-16 20:11:38 -04:00
|
|
|
], e.message)
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2017-07-16 07:35:27 -04:00
|
|
|
e = assert_syntax_error('"\M1"', /escape character syntax/)
|
2019-07-07 05:17:51 -04:00
|
|
|
assert_equal(' ^~~'"\n", e.message.lines.last, mesg)
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2017-07-16 07:35:27 -04:00
|
|
|
e = assert_syntax_error('"\C1"', /escape character syntax/)
|
2019-07-07 05:17:51 -04:00
|
|
|
assert_equal(' ^~~'"\n", e.message.lines.last, mesg)
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2017-07-16 09:39:18 -04:00
|
|
|
src = '"\xD0\u{90'"\n""000000000000000000000000"
|
|
|
|
assert_syntax_error(src, /:#{__LINE__}: unterminated/o)
|
|
|
|
|
2017-07-25 04:30:11 -04:00
|
|
|
assert_syntax_error('"\u{100000000}"', /invalid Unicode escape/)
|
2017-11-14 10:37:48 -05:00
|
|
|
assert_equal("", eval('"\u{}"'))
|
|
|
|
assert_equal("", eval('"\u{ }"'))
|
2017-07-25 04:30:11 -04:00
|
|
|
|
2008-04-13 11:52:14 -04:00
|
|
|
assert_equal("\x81", eval('"\C-\M-a"'))
|
|
|
|
assert_equal("\177", eval('"\c?"'))
|
2019-05-28 08:39:13 -04:00
|
|
|
|
|
|
|
assert_warning(/use \\C-\\s/) {assert_equal("\x00", eval('"\C- "'))}
|
|
|
|
assert_warning(/use \\M-\\s/) {assert_equal("\xa0", eval('"\M- "'))}
|
|
|
|
assert_warning(/use \\M-\\C-\\s/) {assert_equal("\x80", eval('"\M-\C- "'))}
|
|
|
|
assert_warning(/use \\C-\\M-\\s/) {assert_equal("\x80", eval('"\C-\M- "'))}
|
|
|
|
assert_warning(/use \\t/) {assert_equal("\x09", eval("\"\\C-\t\""))}
|
|
|
|
assert_warning(/use \\M-\\t/) {assert_equal("\x89", eval("\"\\M-\t\""))}
|
|
|
|
assert_warning(/use \\M-\\t/) {assert_equal("\x89", eval("\"\\M-\\C-\t\""))}
|
|
|
|
assert_warning(/use \\M-\\t/) {assert_equal("\x89", eval("\"\\C-\\M-\t\""))}
|
|
|
|
assert_syntax_error("\"\\C-\x01\"", 'Invalid escape character syntax')
|
|
|
|
assert_syntax_error("\"\\M-\x01\"", 'Invalid escape character syntax')
|
|
|
|
assert_syntax_error("\"\\M-\\C-\x01\"", 'Invalid escape character syntax')
|
|
|
|
assert_syntax_error("\"\\C-\\M-\x01\"", 'Invalid escape character syntax')
|
2021-05-12 23:30:48 -04:00
|
|
|
|
|
|
|
e = assert_syntax_error('"\c\u0000"', 'Invalid escape character syntax')
|
|
|
|
assert_equal(' ^~~~'"\n", e.message.lines.last)
|
|
|
|
e = assert_syntax_error('"\c\U0000"', 'Invalid escape character syntax')
|
|
|
|
assert_equal(' ^~~~'"\n", e.message.lines.last)
|
|
|
|
|
|
|
|
e = assert_syntax_error('"\C-\u0000"', 'Invalid escape character syntax')
|
|
|
|
assert_equal(' ^~~~~'"\n", e.message.lines.last)
|
|
|
|
e = assert_syntax_error('"\C-\U0000"', 'Invalid escape character syntax')
|
|
|
|
assert_equal(' ^~~~~'"\n", e.message.lines.last)
|
|
|
|
|
|
|
|
e = assert_syntax_error('"\M-\u0000"', 'Invalid escape character syntax')
|
|
|
|
assert_equal(' ^~~~~'"\n", e.message.lines.last)
|
|
|
|
e = assert_syntax_error('"\M-\U0000"', 'Invalid escape character syntax')
|
|
|
|
assert_equal(' ^~~~~'"\n", e.message.lines.last)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_question
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('?', /incomplete/)
|
|
|
|
assert_syntax_error('? ', /unexpected/)
|
|
|
|
assert_syntax_error("?\n", /unexpected/)
|
|
|
|
assert_syntax_error("?\t", /unexpected/)
|
|
|
|
assert_syntax_error("?\v", /unexpected/)
|
|
|
|
assert_syntax_error("?\r", /unexpected/)
|
|
|
|
assert_syntax_error("?\f", /unexpected/)
|
|
|
|
assert_syntax_error(" ?a\x8a".force_encoding("utf-8"), /invalid multibyte/)
|
2008-04-13 11:52:14 -04:00
|
|
|
assert_equal("\u{1234}", eval("?\u{1234}"))
|
|
|
|
assert_equal("\u{1234}", eval('?\u{1234}'))
|
2019-05-24 03:10:59 -04:00
|
|
|
assert_equal("\u{1234}", eval('?\u1234'))
|
2019-07-05 09:18:08 -04:00
|
|
|
assert_syntax_error('?\u{41 42}', 'Multiple codepoints at single character literal')
|
2019-05-24 03:10:59 -04:00
|
|
|
e = assert_syntax_error('"#{?\u123}"', 'invalid Unicode escape')
|
|
|
|
assert_not_match(/end-of-input/, e.message)
|
2019-05-28 08:39:13 -04:00
|
|
|
|
|
|
|
assert_warning(/use ?\\C-\\s/) {assert_equal("\x00", eval('?\C- '))}
|
|
|
|
assert_warning(/use ?\\M-\\s/) {assert_equal("\xa0", eval('?\M- '))}
|
|
|
|
assert_warning(/use ?\\M-\\C-\\s/) {assert_equal("\x80", eval('?\M-\C- '))}
|
|
|
|
assert_warning(/use ?\\C-\\M-\\s/) {assert_equal("\x80", eval('?\C-\M- '))}
|
|
|
|
assert_warning(/use ?\\t/) {assert_equal("\x09", eval("?\\C-\t"))}
|
|
|
|
assert_warning(/use ?\\M-\\t/) {assert_equal("\x89", eval("?\\M-\t"))}
|
|
|
|
assert_warning(/use ?\\M-\\t/) {assert_equal("\x89", eval("?\\M-\\C-\t"))}
|
|
|
|
assert_warning(/use ?\\M-\\t/) {assert_equal("\x89", eval("?\\C-\\M-\t"))}
|
|
|
|
assert_syntax_error("?\\C-\x01", 'Invalid escape character syntax')
|
|
|
|
assert_syntax_error("?\\M-\x01", 'Invalid escape character syntax')
|
|
|
|
assert_syntax_error("?\\M-\\C-\x01", 'Invalid escape character syntax')
|
|
|
|
assert_syntax_error("?\\C-\\M-\x01", 'Invalid escape character syntax')
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_percent
|
|
|
|
assert_equal(:foo, eval('%s(foo)'))
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('%s', /unterminated quoted string/)
|
|
|
|
assert_syntax_error('%ss', /unknown type/)
|
|
|
|
assert_syntax_error('%z()', /unknown type/)
|
2020-08-26 08:36:22 -04:00
|
|
|
assert_syntax_error("%\u3042", /unknown type/)
|
|
|
|
assert_syntax_error("%q\u3042", /unknown type/)
|
2021-01-03 22:11:37 -05:00
|
|
|
assert_syntax_error("%", /unterminated quoted string/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_symbol
|
2010-05-29 14:40:48 -04:00
|
|
|
bug = '[ruby-dev:41447]'
|
|
|
|
sym = "foo\0bar".to_sym
|
|
|
|
assert_nothing_raised(SyntaxError, bug) do
|
|
|
|
assert_equal(sym, eval(":'foo\0bar'"))
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
2010-05-29 14:40:48 -04:00
|
|
|
assert_nothing_raised(SyntaxError, bug) do
|
|
|
|
assert_equal(sym, eval(':"foo\u0000bar"'))
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
2010-05-29 14:40:48 -04:00
|
|
|
assert_nothing_raised(SyntaxError, bug) do
|
|
|
|
assert_equal(sym, eval(':"foo\u{0}bar"'))
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
2017-11-14 10:37:48 -05:00
|
|
|
assert_nothing_raised(SyntaxError) do
|
|
|
|
assert_equal(:foobar, eval(':"foo\u{}bar"'))
|
|
|
|
assert_equal(:foobar, eval(':"foo\u{ }bar"'))
|
|
|
|
end
|
2019-05-27 19:44:28 -04:00
|
|
|
|
|
|
|
assert_syntax_error(':@@', /is not allowed/)
|
|
|
|
assert_syntax_error(':@@1', /is not allowed/)
|
|
|
|
assert_syntax_error(':@', /is not allowed/)
|
|
|
|
assert_syntax_error(':@1', /is not allowed/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_string
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("/\n", /unterminated/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_here_document
|
|
|
|
x = nil
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("<\<FOO\n", /can't find string "FOO"/)
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2013-05-10 10:56:40 -04:00
|
|
|
assert_nothing_raised(SyntaxError) do
|
|
|
|
x = eval %q(
|
2008-04-13 11:52:14 -04:00
|
|
|
<<FOO
|
|
|
|
#$
|
|
|
|
FOO
|
|
|
|
)
|
|
|
|
end
|
2013-05-10 10:56:40 -04:00
|
|
|
assert_equal "\#$\n", x
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("<\<\"\n", /unterminated here document identifier/)
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("<<``\n", /can't find string ""/)
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("<<--\n", /unexpected <</)
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2013-05-10 10:56:40 -04:00
|
|
|
assert_nothing_raised(SyntaxError) do
|
|
|
|
x = eval %q(
|
2008-04-13 11:52:14 -04:00
|
|
|
<<FOO
|
|
|
|
#$
|
|
|
|
foo
|
|
|
|
FOO
|
|
|
|
)
|
|
|
|
end
|
2013-05-10 10:56:40 -04:00
|
|
|
assert_equal "\#$\nfoo\n", x
|
2008-04-13 11:52:14 -04:00
|
|
|
|
|
|
|
assert_nothing_raised do
|
2012-10-25 10:06:31 -04:00
|
|
|
eval "x = <<""FOO\r\n1\r\nFOO"
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
assert_equal("1\n", x)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_magic_comment
|
|
|
|
x = nil
|
2021-03-23 03:44:55 -04:00
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
|
|
|
# coding: utf-8
|
|
|
|
x = __ENCODING__
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal(Encoding.find("UTF-8"), x)
|
|
|
|
|
2008-04-13 11:52:14 -04:00
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
# coding = utf-8
|
|
|
|
x = __ENCODING__
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal(Encoding.find("UTF-8"), x)
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
# coding = foobarbazquxquux_dummy_enconding
|
|
|
|
x = __ENCODING__
|
|
|
|
END
|
|
|
|
end
|
2021-03-23 04:20:19 -04:00
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
|
|
|
# xxxx : coding sjis
|
|
|
|
x = __ENCODING__
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal(__ENCODING__, x)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_utf8_bom
|
|
|
|
x = nil
|
|
|
|
assert_nothing_raised do
|
|
|
|
eval "\xef\xbb\xbf x = __ENCODING__"
|
|
|
|
end
|
|
|
|
assert_equal(Encoding.find("UTF-8"), x)
|
|
|
|
assert_raise(NameError) { eval "\xef" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dot_in_next_line
|
|
|
|
x = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
x = 1
|
|
|
|
.to_s
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal("1", x)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pow_asgn
|
|
|
|
x = 3
|
|
|
|
assert_nothing_raised { eval("x **= 2") }
|
|
|
|
assert_equal(9, x)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_embedded_rd
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_valid_syntax("=begin\n""=end")
|
2019-11-10 19:35:37 -05:00
|
|
|
assert_valid_syntax("=begin\n""=end\0")
|
|
|
|
assert_valid_syntax("=begin\n""=end\C-d")
|
|
|
|
assert_valid_syntax("=begin\n""=end\C-z")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_embedded_rd_error
|
|
|
|
error = 'embedded document meets end of file'
|
|
|
|
assert_syntax_error("=begin\n", error)
|
|
|
|
assert_syntax_error("=begin", error)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_float
|
2020-12-17 06:06:18 -05:00
|
|
|
assert_predicate(assert_warning(/out of range/) {eval("1e10000")}, :infinite?)
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('1_E', /trailing `_'/)
|
|
|
|
assert_syntax_error('1E1E1', /unexpected constant/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_global_variable
|
2020-12-17 06:06:18 -05:00
|
|
|
assert_equal(nil, assert_warning(/not initialized/) {eval('$-x')})
|
2008-04-14 11:47:51 -04:00
|
|
|
assert_equal(nil, eval('alias $preserve_last_match $&'))
|
2008-04-13 11:52:14 -04:00
|
|
|
assert_equal(nil, eval('alias $& $test_parse_foobarbazqux'))
|
|
|
|
$test_parse_foobarbazqux = nil
|
2008-04-14 11:47:51 -04:00
|
|
|
assert_equal(nil, $&)
|
|
|
|
assert_equal(nil, eval('alias $& $preserve_last_match'))
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('a = $#', /as a global variable name\na = \$\#\n \^~$/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_instance_variable
|
2019-03-14 20:44:51 -04:00
|
|
|
pattern = /without identifiers is not allowed as an instance variable name/
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('@%', pattern)
|
|
|
|
assert_syntax_error('@', pattern)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_class_variable
|
2019-03-14 20:44:51 -04:00
|
|
|
pattern = /without identifiers is not allowed as a class variable name/
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('@@%', pattern)
|
|
|
|
assert_syntax_error('@@', pattern)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_char
|
2014-08-07 12:07:25 -04:00
|
|
|
bug10117 = '[ruby-core:64243] [Bug #10117]'
|
|
|
|
invalid_char = /Invalid char `\\x01'/
|
2017-09-26 08:21:51 -04:00
|
|
|
x = 1
|
2014-08-07 12:07:25 -04:00
|
|
|
assert_in_out_err(%W"-e \x01x", "", [], invalid_char, bug10117)
|
|
|
|
assert_syntax_error("\x01x", invalid_char, bug10117)
|
2008-04-13 11:52:14 -04:00
|
|
|
assert_equal(nil, eval("\x04x"))
|
2017-09-26 08:21:51 -04:00
|
|
|
assert_equal 1, x
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_literal_concat
|
2017-09-26 08:21:51 -04:00
|
|
|
x = "baz"
|
2008-04-13 11:52:14 -04:00
|
|
|
assert_equal("foobarbaz", eval('"foo" "bar#{x}"'))
|
2017-09-26 08:21:51 -04:00
|
|
|
assert_equal("baz", x)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unassignable
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error(%q(self = 1), /Can't change the value of self/)
|
|
|
|
assert_syntax_error(%q(nil = 1), /Can't assign to nil/)
|
|
|
|
assert_syntax_error(%q(true = 1), /Can't assign to true/)
|
|
|
|
assert_syntax_error(%q(false = 1), /Can't assign to false/)
|
|
|
|
assert_syntax_error(%q(__FILE__ = 1), /Can't assign to __FILE__/)
|
|
|
|
assert_syntax_error(%q(__LINE__ = 1), /Can't assign to __LINE__/)
|
|
|
|
assert_syntax_error(%q(__ENCODING__ = 1), /Can't assign to __ENCODING__/)
|
|
|
|
assert_syntax_error("def foo; FOO = 1; end", /dynamic constant assignment/)
|
|
|
|
assert_syntax_error("x, true", /Can't assign to true/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_block_dup
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("foo(&proc{}) {}", /both block arg and actual block/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_set_backref
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("$& = 1", /Can't set variable/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_arg_concat
|
|
|
|
o = Object.new
|
|
|
|
class << o; self; end.instance_eval do
|
|
|
|
define_method(:[]=) {|*r, &b| b.call(r) }
|
|
|
|
end
|
|
|
|
r = nil
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
o[&proc{|x| r = x }] = 1
|
|
|
|
END
|
|
|
|
end
|
|
|
|
assert_equal([1], r)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_void_expr_stmts_value
|
2017-09-26 08:21:51 -04:00
|
|
|
x = 1
|
2019-10-22 13:30:14 -04:00
|
|
|
useless_use = /useless use/
|
|
|
|
unused = /unused/
|
|
|
|
assert_nil assert_warning(useless_use) {eval("x; nil")}
|
|
|
|
assert_nil assert_warning(useless_use) {eval("1+1; nil")}
|
|
|
|
assert_nil assert_warning('') {eval("1.+(1); nil")}
|
|
|
|
assert_nil assert_warning(useless_use) {eval("TestParse; nil")}
|
|
|
|
assert_nil assert_warning(useless_use) {eval("::TestParse; nil")}
|
|
|
|
assert_nil assert_warning(useless_use) {eval("x..x; nil")}
|
|
|
|
assert_nil assert_warning(useless_use) {eval("x...x; nil")}
|
|
|
|
assert_nil assert_warning(unused) {eval("self; nil")}
|
|
|
|
assert_nil assert_warning(unused) {eval("nil; nil")}
|
|
|
|
assert_nil assert_warning(unused) {eval("true; nil")}
|
|
|
|
assert_nil assert_warning(unused) {eval("false; nil")}
|
|
|
|
assert_nil assert_warning(useless_use) {eval("defined?(1); nil")}
|
2017-09-26 08:21:51 -04:00
|
|
|
assert_equal 1, x
|
2008-04-13 11:52:14 -04:00
|
|
|
|
2019-10-22 13:30:14 -04:00
|
|
|
assert_syntax_error("1; next; 2", /Invalid next/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_in_conditional
|
2020-12-17 06:06:18 -05:00
|
|
|
assert_warning(/`= literal' in conditional/) do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
(x, y = 1, 2) ? 1 : 2
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2020-12-17 06:06:18 -05:00
|
|
|
assert_warning(/`= literal' in conditional/) do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
if @x = true
|
|
|
|
1
|
|
|
|
else
|
|
|
|
2
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_literal_in_conditional
|
2020-12-17 06:06:18 -05:00
|
|
|
assert_warning(/string literal in condition/) do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
"foo" ? 1 : 2
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2020-12-17 06:06:18 -05:00
|
|
|
assert_warning(/regex literal in condition/) do
|
2008-04-13 11:52:14 -04:00
|
|
|
x = "bar"
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
/foo#{x}baz/ ? 1 : 2
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
(true..false) ? 1 : 2
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2020-12-17 06:06:18 -05:00
|
|
|
assert_warning(/string literal in flip-flop/) do
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
("foo".."bar") ? 1 : 2
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2020-12-17 06:06:18 -05:00
|
|
|
assert_warning(/literal in condition/) do
|
2017-09-26 08:21:51 -04:00
|
|
|
x = "bar"
|
2012-09-15 05:26:21 -04:00
|
|
|
eval <<-END, nil, __FILE__, __LINE__+1
|
2008-04-13 11:52:14 -04:00
|
|
|
:"foo#{"x"}baz" ? 1 : 2
|
|
|
|
END
|
2017-09-26 08:21:51 -04:00
|
|
|
assert_equal "bar", x
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_blockarg
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("yield(&:+)", /block argument should not be given/)
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|
|
|
|
|
2011-12-13 10:01:20 -05:00
|
|
|
def test_method_block_location
|
|
|
|
bug5614 = '[ruby-core:40936]'
|
|
|
|
expected = nil
|
|
|
|
e = assert_raise(NoMethodError) do
|
|
|
|
1.times do
|
|
|
|
expected = __LINE__+1
|
|
|
|
end.print do
|
|
|
|
#
|
|
|
|
end
|
|
|
|
end
|
|
|
|
actual = e.backtrace.first[/\A#{Regexp.quote(__FILE__)}:(\d+):/o, 1].to_i
|
|
|
|
assert_equal(expected, actual, bug5614)
|
|
|
|
end
|
2014-08-13 08:17:57 -04:00
|
|
|
|
2018-10-25 23:10:02 -04:00
|
|
|
def test_no_shadowing_variable_warning
|
|
|
|
assert_no_warning(/shadowing outer local variable/) {eval("a=1; tap {|a|}")}
|
2014-08-13 08:17:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unused_variable
|
|
|
|
o = Object.new
|
|
|
|
assert_warning(/assigned but unused variable/) {o.instance_eval("def foo; a=1; nil; end")}
|
2017-08-13 09:34:26 -04:00
|
|
|
assert_warning(/assigned but unused variable/) {o.instance_eval("def bar; a=1; a(); end")}
|
2014-08-13 08:20:13 -04:00
|
|
|
a = "\u{3042}"
|
2019-06-29 05:45:24 -04:00
|
|
|
assert_warning(/#{a}/) {o.instance_eval("def foo0; #{a}=1; nil; end")}
|
|
|
|
assert_warning(/assigned but unused variable/) {o.instance_eval("def foo1; tap {a=1; a()}; end")}
|
|
|
|
assert_warning('') {o.instance_eval("def bar1; a=a=1; nil; end")}
|
|
|
|
assert_warning(/assigned but unused variable/) {o.instance_eval("def bar2; a, = 1, 2; end")}
|
|
|
|
assert_warning('') {o.instance_eval("def marg1(a); nil; end")}
|
|
|
|
assert_warning('') {o.instance_eval("def marg2((a)); nil; end")}
|
2014-08-13 08:17:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_capture_conflict
|
|
|
|
a = 1
|
2016-06-13 07:50:09 -04:00
|
|
|
assert_warning('') {eval("a = 1; /(?<a>)/ =~ ''")}
|
2014-08-13 08:20:13 -04:00
|
|
|
a = "\u{3042}"
|
2016-06-13 07:50:09 -04:00
|
|
|
assert_warning('') {eval("#{a} = 1; /(?<#{a}>)/ =~ ''")}
|
2014-08-13 08:17:57 -04:00
|
|
|
end
|
2014-12-24 22:47:46 -05:00
|
|
|
|
2016-08-10 04:26:46 -04:00
|
|
|
def test_rescue_in_command_assignment
|
|
|
|
bug = '[ruby-core:75621] [Bug #12402]'
|
2016-08-12 21:52:22 -04:00
|
|
|
all_assertions(bug) do |a|
|
|
|
|
a.for("lhs = arg") do
|
|
|
|
v = bug
|
|
|
|
v = raise(bug) rescue "ok"
|
|
|
|
assert_equal("ok", v)
|
|
|
|
end
|
|
|
|
a.for("lhs op_asgn arg") do
|
|
|
|
v = 0
|
|
|
|
v += raise(bug) rescue 1
|
|
|
|
assert_equal(1, v)
|
|
|
|
end
|
|
|
|
a.for("lhs[] op_asgn arg") do
|
|
|
|
v = [0]
|
|
|
|
v[0] += raise(bug) rescue 1
|
|
|
|
assert_equal([1], v)
|
|
|
|
end
|
|
|
|
a.for("lhs.m op_asgn arg") do
|
|
|
|
k = Struct.new(:m)
|
|
|
|
v = k.new(0)
|
|
|
|
v.m += raise(bug) rescue 1
|
|
|
|
assert_equal(k.new(1), v)
|
|
|
|
end
|
|
|
|
a.for("lhs::m op_asgn arg") do
|
|
|
|
k = Struct.new(:m)
|
|
|
|
v = k.new(0)
|
|
|
|
v::m += raise(bug) rescue 1
|
|
|
|
assert_equal(k.new(1), v)
|
|
|
|
end
|
|
|
|
a.for("lhs.C op_asgn arg") do
|
|
|
|
k = Struct.new(:C)
|
|
|
|
v = k.new(0)
|
|
|
|
v.C += raise(bug) rescue 1
|
|
|
|
assert_equal(k.new(1), v)
|
|
|
|
end
|
|
|
|
a.for("lhs::C op_asgn arg") do
|
|
|
|
v = Class.new
|
|
|
|
v::C ||= raise(bug) rescue 1
|
|
|
|
assert_equal(1, v::C)
|
|
|
|
end
|
|
|
|
a.for("lhs = command") do
|
|
|
|
v = bug
|
|
|
|
v = raise bug rescue "ok"
|
|
|
|
assert_equal("ok", v)
|
|
|
|
end
|
|
|
|
a.for("lhs op_asgn command") do
|
|
|
|
v = 0
|
|
|
|
v += raise bug rescue 1
|
|
|
|
assert_equal(1, v)
|
|
|
|
end
|
|
|
|
a.for("lhs[] op_asgn command") do
|
|
|
|
v = [0]
|
|
|
|
v[0] += raise bug rescue 1
|
|
|
|
assert_equal([1], v)
|
|
|
|
end
|
|
|
|
a.for("lhs.m op_asgn command") do
|
|
|
|
k = Struct.new(:m)
|
|
|
|
v = k.new(0)
|
|
|
|
v.m += raise bug rescue 1
|
|
|
|
assert_equal(k.new(1), v)
|
|
|
|
end
|
|
|
|
a.for("lhs::m op_asgn command") do
|
|
|
|
k = Struct.new(:m)
|
|
|
|
v = k.new(0)
|
|
|
|
v::m += raise bug rescue 1
|
|
|
|
assert_equal(k.new(1), v)
|
|
|
|
end
|
|
|
|
a.for("lhs.C op_asgn command") do
|
|
|
|
k = Struct.new(:C)
|
|
|
|
v = k.new(0)
|
|
|
|
v.C += raise bug rescue 1
|
|
|
|
assert_equal(k.new(1), v)
|
|
|
|
end
|
|
|
|
a.for("lhs::C op_asgn command") do
|
|
|
|
v = Class.new
|
|
|
|
v::C ||= raise bug rescue 1
|
|
|
|
assert_equal(1, v::C)
|
|
|
|
end
|
|
|
|
end
|
2016-08-10 04:26:46 -04:00
|
|
|
end
|
|
|
|
|
2016-12-13 23:35:28 -05:00
|
|
|
def test_yyerror_at_eol
|
|
|
|
assert_syntax_error(" 0b", /\^/)
|
|
|
|
assert_syntax_error(" 0b\n", /\^/)
|
|
|
|
end
|
|
|
|
|
2017-04-24 02:17:55 -04:00
|
|
|
def test_error_def_in_argument
|
|
|
|
assert_separately([], "#{<<-"begin;"}\n#{<<~"end;"}")
|
|
|
|
begin;
|
|
|
|
assert_syntax_error("def f r:def d; def f 0end", /unexpected/)
|
|
|
|
end;
|
2017-08-17 08:33:41 -04:00
|
|
|
|
|
|
|
assert_syntax_error("def\nf(000)end", /^ \^~~/)
|
|
|
|
assert_syntax_error("def\nf(&)end", /^ \^/)
|
2017-04-24 02:17:55 -04:00
|
|
|
end
|
|
|
|
|
2017-04-28 04:06:33 -04:00
|
|
|
def test_method_location_in_rescue
|
|
|
|
bug = '[ruby-core:79388] [Bug #13181]'
|
|
|
|
obj, line = Object.new, __LINE__+1
|
|
|
|
def obj.location
|
|
|
|
#
|
|
|
|
raise
|
|
|
|
rescue
|
|
|
|
caller_locations(1, 1)[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(line, obj.location.lineno, bug)
|
|
|
|
end
|
|
|
|
|
2017-04-28 22:58:53 -04:00
|
|
|
def test_negative_line_number
|
|
|
|
bug = '[ruby-core:80920] [Bug #13523]'
|
|
|
|
obj = Object.new
|
|
|
|
obj.instance_eval("def t(e = false);raise if e; __LINE__;end", "test", -100)
|
|
|
|
assert_equal(-100, obj.t, bug)
|
|
|
|
assert_equal(-100, obj.method(:t).source_location[1], bug)
|
|
|
|
e = assert_raise(RuntimeError) {obj.t(true)}
|
|
|
|
assert_equal(-100, e.backtrace_locations.first.lineno, bug)
|
|
|
|
end
|
|
|
|
|
2017-05-04 02:54:11 -04:00
|
|
|
def test_file_in_indented_heredoc
|
|
|
|
name = '[ruby-core:80987] [Bug #13540]' # long enough to be shared
|
|
|
|
assert_equal(name+"\n", eval("#{<<-"begin;"}\n#{<<-'end;'}", nil, name))
|
|
|
|
begin;
|
|
|
|
<<~HEREDOC
|
|
|
|
#{__FILE__}
|
|
|
|
HEREDOC
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2021-07-20 13:53:22 -04:00
|
|
|
def test_heredoc_interpolation
|
|
|
|
var = 1
|
|
|
|
|
|
|
|
v1 = <<~HEREDOC
|
|
|
|
something
|
|
|
|
#{"/#{var}"}
|
|
|
|
HEREDOC
|
|
|
|
|
|
|
|
v2 = <<~HEREDOC
|
|
|
|
something
|
2021-07-27 00:41:10 -04:00
|
|
|
#{_other = "/#{var}"}
|
2021-07-20 13:53:22 -04:00
|
|
|
HEREDOC
|
|
|
|
|
|
|
|
v3 = <<~HEREDOC
|
|
|
|
something
|
|
|
|
#{("/#{var}")}
|
|
|
|
HEREDOC
|
|
|
|
|
|
|
|
assert_equal "something\n/1\n", v1
|
|
|
|
assert_equal "something\n/1\n", v2
|
|
|
|
assert_equal "something\n/1\n", v3
|
|
|
|
assert_equal v1, v2
|
|
|
|
assert_equal v2, v3
|
|
|
|
assert_equal v1, v3
|
|
|
|
end
|
|
|
|
|
2017-06-27 22:02:38 -04:00
|
|
|
def test_unexpected_token_error
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('"x"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', /unexpected/)
|
2017-06-27 22:02:38 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 00:08:51 -04:00
|
|
|
def test_unexpected_token_after_numeric
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('0000xyz', /^ \^~~\Z/)
|
|
|
|
assert_syntax_error('1.2i1.1', /^ \^~~\Z/)
|
|
|
|
assert_syntax_error('1.2.3', /^ \^~\Z/)
|
2017-06-28 00:08:51 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 01:38:27 -04:00
|
|
|
def test_truncated_source_line
|
2019-11-10 19:16:00 -05:00
|
|
|
e = assert_syntax_error("'0123456789012345678901234567890123456789' abcdefghijklmnopqrstuvwxyz0123456789 0123456789012345678901234567890123456789",
|
|
|
|
/unexpected local variable or method/)
|
2017-06-28 01:38:27 -04:00
|
|
|
line = e.message.lines[1]
|
|
|
|
assert_operator(line, :start_with?, "...")
|
|
|
|
assert_operator(line, :end_with?, "...\n")
|
|
|
|
end
|
|
|
|
|
2017-06-28 02:13:00 -04:00
|
|
|
def test_unterminated_regexp_error
|
2019-11-10 19:16:00 -05:00
|
|
|
e = assert_syntax_error("/x", /unterminated regexp meets end of file/)
|
|
|
|
assert_not_match(/unexpected tSTRING_END/, e.message)
|
2017-06-28 02:13:00 -04:00
|
|
|
end
|
|
|
|
|
2017-09-18 20:22:56 -04:00
|
|
|
def test_lparenarg
|
|
|
|
o = Struct.new(:x).new
|
|
|
|
def o.i(x)
|
|
|
|
self.x = x
|
|
|
|
end
|
|
|
|
o.instance_eval {i (-1.3).abs}
|
|
|
|
assert_equal(1.3, o.x)
|
2019-06-29 20:40:55 -04:00
|
|
|
o.i(nil)
|
2019-06-29 20:34:41 -04:00
|
|
|
o.instance_eval {i = 0; i (-1.3).abs; i}
|
2017-09-18 20:22:56 -04:00
|
|
|
assert_equal(1.3, o.x)
|
|
|
|
end
|
|
|
|
|
2017-10-18 09:08:53 -04:00
|
|
|
def test_serial_comparison
|
|
|
|
assert_warning(/comparison '<' after/) do
|
|
|
|
$VERBOSE = true
|
|
|
|
x = 1
|
|
|
|
eval("if false; 0 < x < 2; end")
|
2019-06-29 20:34:41 -04:00
|
|
|
x
|
2017-10-18 09:08:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-10 19:16:00 -05:00
|
|
|
def test_eof
|
|
|
|
assert_equal(42, eval("42\0""end"))
|
|
|
|
assert_equal(42, eval("42\C-d""end"))
|
|
|
|
assert_equal(42, eval("42\C-z""end"))
|
|
|
|
end
|
|
|
|
|
2017-12-19 19:09:51 -05:00
|
|
|
def test_eof_in_def
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("def m\n\0""end", /unexpected/)
|
|
|
|
assert_syntax_error("def m\n\C-d""end", /unexpected/)
|
|
|
|
assert_syntax_error("def m\n\C-z""end", /unexpected/)
|
2017-12-19 19:09:51 -05:00
|
|
|
end
|
|
|
|
|
2021-10-05 03:44:43 -04:00
|
|
|
def test_unexpected_eof
|
|
|
|
assert_syntax_error('unless', /^ \^\Z/)
|
|
|
|
end
|
|
|
|
|
2018-01-01 22:00:13 -05:00
|
|
|
def test_location_of_invalid_token
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error('class xxx end', /^ \^~~\Z/)
|
2018-01-01 22:00:13 -05:00
|
|
|
end
|
|
|
|
|
2018-03-05 03:32:46 -05:00
|
|
|
def test_whitespace_warning
|
2019-11-10 19:16:00 -05:00
|
|
|
assert_syntax_error("\\foo", /backslash/)
|
|
|
|
assert_syntax_error("\\ ", /escaped space/)
|
|
|
|
assert_syntax_error("\\\t", /escaped horizontal tab/)
|
|
|
|
assert_syntax_error("\\\f", /escaped form feed/)
|
|
|
|
assert_syntax_error("\\\r", /escaped carriage return/)
|
|
|
|
assert_warn(/middle of line/) {eval(" \r ")}
|
|
|
|
assert_syntax_error("\\\v", /escaped vertical tab/)
|
2018-03-05 03:32:46 -05:00
|
|
|
end
|
|
|
|
|
2018-03-28 23:42:32 -04:00
|
|
|
def test_command_def_cmdarg
|
|
|
|
assert_valid_syntax("\n#{<<~"begin;"}\n#{<<~'end;'}")
|
|
|
|
begin;
|
|
|
|
m def x(); end
|
|
|
|
1.tap do end
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2018-04-09 20:41:47 -04:00
|
|
|
NONASCII_CONSTANTS = [
|
|
|
|
*%W"\u{00de} \u{00C0}".flat_map {|c| [c, c.encode("iso-8859-15")]},
|
|
|
|
"\u{1c4}", "\u{1f2}", "\u{1f88}", "\u{370}",
|
|
|
|
*%W"\u{391} \u{ff21}".flat_map {|c| [c, c.encode("cp932"), c.encode("euc-jp")]},
|
|
|
|
]
|
|
|
|
|
|
|
|
def assert_nonascii_const
|
|
|
|
assert_all_assertions_foreach("NONASCII_CONSTANTS", *NONASCII_CONSTANTS) do |n|
|
|
|
|
m = Module.new
|
|
|
|
assert_not_operator(m, :const_defined?, n)
|
|
|
|
assert_raise_with_message(NameError, /uninitialized/) do
|
|
|
|
m.const_get(n)
|
|
|
|
end
|
|
|
|
assert_nil(eval("defined?(m::#{n})"))
|
|
|
|
|
|
|
|
v = yield m, n
|
|
|
|
|
|
|
|
assert_operator(m, :const_defined?, n)
|
|
|
|
assert_equal("constant", eval("defined?(m::#{n})"))
|
|
|
|
assert_same(v, m.const_get(n))
|
|
|
|
|
|
|
|
m.__send__(:remove_const, n)
|
|
|
|
assert_not_operator(m, :const_defined?, n)
|
|
|
|
assert_nil(eval("defined?(m::#{n})"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nonascii_const_set
|
|
|
|
assert_nonascii_const do |m, n|
|
|
|
|
m.const_set(n, 42)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nonascii_constant
|
|
|
|
assert_nonascii_const do |m, n|
|
|
|
|
m.module_eval("class #{n}; self; end")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-16 21:22:26 -04:00
|
|
|
def test_cdmarg_after_command_args_and_tlbrace_arg
|
|
|
|
assert_valid_syntax('let () { m(a) do; end }')
|
|
|
|
end
|
|
|
|
|
2021-10-07 11:10:32 -04:00
|
|
|
def test_void_value_in_rhs
|
2020-02-09 22:15:48 -05:00
|
|
|
w = "void value expression"
|
2021-10-07 11:10:32 -04:00
|
|
|
["x = return 1", "x = return, 1", "x = 1, return", "x, y = return"].each do |code|
|
|
|
|
ex = assert_syntax_error(code, w)
|
|
|
|
assert_equal(1, ex.message.scan(w).size, ->{"same #{w.inspect} warning should be just once\n#{w.message}"})
|
|
|
|
end
|
2020-02-09 22:15:48 -05:00
|
|
|
end
|
|
|
|
|
2020-12-21 01:04:06 -05:00
|
|
|
def eval_separately(code)
|
|
|
|
Class.new.class_eval(code)
|
|
|
|
end
|
|
|
|
|
2020-12-21 18:26:42 -05:00
|
|
|
def assert_raise_separately(error, message, code)
|
|
|
|
assert_raise_with_message(error, message) do
|
2020-12-21 01:04:06 -05:00
|
|
|
eval_separately(code)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_ractor_shareable(obj)
|
|
|
|
assert Ractor.shareable?(obj), ->{"Expected #{mu_pp(obj)} to be ractor shareable"}
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_ractor_shareable(obj)
|
|
|
|
assert !Ractor.shareable?(obj), ->{"Expected #{mu_pp(obj)} not to be ractor shareable"}
|
|
|
|
end
|
|
|
|
|
2020-12-21 01:15:35 -05:00
|
|
|
def test_shareable_constant_value_invalid
|
2020-10-09 07:06:30 -04:00
|
|
|
assert_warning(/invalid value/) do
|
|
|
|
assert_valid_syntax("# shareable_constant_value: invalid-option", verbose: true)
|
|
|
|
end
|
2020-12-21 01:15:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_shareable_constant_value_ignored
|
2020-11-02 05:48:17 -05:00
|
|
|
assert_warning(/ignored/) do
|
|
|
|
assert_valid_syntax("nil # shareable_constant_value: true", verbose: true)
|
|
|
|
end
|
2020-12-21 01:15:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_shareable_constant_value_simple
|
2020-12-23 20:59:27 -05:00
|
|
|
obj = [['unsharable_value']]
|
2020-12-21 01:04:06 -05:00
|
|
|
a, b, c = eval_separately("#{<<~"begin;"}\n#{<<~'end;'}")
|
2020-10-09 10:06:13 -04:00
|
|
|
begin;
|
2020-11-13 00:03:25 -05:00
|
|
|
# shareable_constant_value: experimental_everything
|
2020-10-21 01:11:18 -04:00
|
|
|
A = [[1]]
|
2020-11-13 00:03:25 -05:00
|
|
|
# shareable_constant_value: none
|
2020-10-22 03:24:38 -04:00
|
|
|
B = [[2]]
|
2020-11-26 04:10:42 -05:00
|
|
|
# shareable_constant_value: literal
|
|
|
|
C = [["shareable", "constant#{nil}"]]
|
2020-12-18 10:34:14 -05:00
|
|
|
D = A
|
2020-10-22 03:24:38 -04:00
|
|
|
|
2020-11-26 04:10:42 -05:00
|
|
|
[A, B, C]
|
2020-10-09 10:06:13 -04:00
|
|
|
end;
|
2020-12-21 01:04:06 -05:00
|
|
|
assert_ractor_shareable(a)
|
|
|
|
assert_not_ractor_shareable(b)
|
|
|
|
assert_ractor_shareable(c)
|
2020-10-21 01:11:18 -04:00
|
|
|
assert_equal([1], a[0])
|
2020-12-21 01:04:06 -05:00
|
|
|
assert_ractor_shareable(a[0])
|
2020-12-23 20:59:27 -05:00
|
|
|
|
|
|
|
a, obj = eval_separately(<<~'end;')
|
|
|
|
# shareable_constant_value: experimental_copy
|
|
|
|
obj = [["unshareable"]]
|
|
|
|
A = obj
|
|
|
|
[A, obj]
|
|
|
|
end;
|
|
|
|
|
|
|
|
assert_ractor_shareable(a)
|
|
|
|
assert_not_ractor_shareable(obj)
|
|
|
|
assert_equal obj, a
|
|
|
|
assert !obj.equal?(a)
|
2020-12-21 01:15:35 -05:00
|
|
|
end
|
2020-12-21 01:04:06 -05:00
|
|
|
|
2020-12-21 01:15:35 -05:00
|
|
|
def test_shareable_constant_value_nested
|
2020-12-21 01:04:06 -05:00
|
|
|
a, b = eval_separately("#{<<~"begin;"}\n#{<<~'end;'}")
|
2020-10-09 10:06:13 -04:00
|
|
|
begin;
|
2020-11-13 00:03:25 -05:00
|
|
|
# shareable_constant_value: none
|
2020-11-02 05:48:17 -05:00
|
|
|
class X
|
2020-11-13 00:03:25 -05:00
|
|
|
# shareable_constant_value: experimental_everything
|
2020-12-21 01:08:22 -05:00
|
|
|
var = [[1]]
|
|
|
|
A = var
|
2020-10-09 10:06:13 -04:00
|
|
|
end
|
|
|
|
B = []
|
|
|
|
[X::A, B]
|
|
|
|
end;
|
2020-12-21 01:04:06 -05:00
|
|
|
assert_ractor_shareable(a)
|
|
|
|
assert_not_ractor_shareable(b)
|
2020-10-21 01:11:18 -04:00
|
|
|
assert_equal([1], a[0])
|
2020-12-21 01:04:06 -05:00
|
|
|
assert_ractor_shareable(a[0])
|
2020-12-21 01:15:35 -05:00
|
|
|
end
|
2020-11-26 04:10:42 -05:00
|
|
|
|
2020-12-21 01:15:35 -05:00
|
|
|
def test_shareable_constant_value_unshareable_literal
|
2020-12-21 18:26:42 -05:00
|
|
|
assert_raise_separately(Ractor::IsolationError, /unshareable/,
|
|
|
|
"#{<<~"begin;"}\n#{<<~'end;'}")
|
2020-12-21 01:04:06 -05:00
|
|
|
begin;
|
|
|
|
# shareable_constant_value: literal
|
|
|
|
C = ["Not " + "shareable"]
|
|
|
|
end;
|
2020-12-21 01:15:35 -05:00
|
|
|
end
|
2020-12-21 01:08:22 -05:00
|
|
|
|
2020-12-21 01:15:35 -05:00
|
|
|
def test_shareable_constant_value_nonliteral
|
2020-12-22 08:10:11 -05:00
|
|
|
assert_raise_separately(Ractor::IsolationError, /unshareable/, "#{<<~"begin;"}\n#{<<~'end;'}")
|
2020-12-21 01:08:22 -05:00
|
|
|
begin;
|
|
|
|
# shareable_constant_value: literal
|
|
|
|
var = [:not_frozen]
|
|
|
|
C = var
|
2020-12-22 08:10:11 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
assert_raise_separately(Ractor::IsolationError, /unshareable/, "#{<<~"begin;"}\n#{<<~'end;'}")
|
|
|
|
begin;
|
|
|
|
# shareable_constant_value: literal
|
2020-12-21 01:08:22 -05:00
|
|
|
D = begin [] end
|
|
|
|
end;
|
2020-12-21 01:15:35 -05:00
|
|
|
end
|
2020-12-21 01:08:22 -05:00
|
|
|
|
2020-12-21 01:15:35 -05:00
|
|
|
def test_shareable_constant_value_unfrozen
|
2020-12-21 18:26:42 -05:00
|
|
|
assert_raise_separately(Ractor::Error, /does not freeze object correctly/,
|
|
|
|
"#{<<~"begin;"}\n#{<<~'end;'}")
|
2020-12-21 01:08:22 -05:00
|
|
|
begin;
|
|
|
|
# shareable_constant_value: experimental_everything
|
|
|
|
o = Object.new
|
|
|
|
def o.freeze; self; end
|
|
|
|
C = [o]
|
|
|
|
end;
|
2020-10-09 07:06:30 -04:00
|
|
|
end
|
|
|
|
|
2014-12-31 02:48:47 -05:00
|
|
|
=begin
|
2014-12-24 22:47:46 -05:00
|
|
|
def test_past_scope_variable
|
|
|
|
assert_warning(/past scope/) {catch {|tag| eval("BEGIN{throw tag}; tap {a = 1}; a")}}
|
|
|
|
end
|
2014-12-31 02:48:47 -05:00
|
|
|
=end
|
2008-04-13 11:52:14 -04:00
|
|
|
end
|