2007-02-23 00:23:33 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestRubyLiteral < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_special_const
|
|
|
|
assert_equal 'true', true.inspect
|
|
|
|
assert_instance_of TrueClass, true
|
|
|
|
assert_equal 'false', false.inspect
|
|
|
|
assert_instance_of FalseClass, false
|
|
|
|
assert_equal 'nil', nil.inspect
|
|
|
|
assert_instance_of NilClass, nil
|
|
|
|
assert_equal ':sym', :sym.inspect
|
|
|
|
assert_instance_of Symbol, :sym
|
|
|
|
assert_equal '1234', 1234.inspect
|
|
|
|
assert_instance_of Fixnum, 1234
|
|
|
|
assert_equal '1234', 1_2_3_4.inspect
|
|
|
|
assert_instance_of Fixnum, 1_2_3_4
|
|
|
|
assert_equal '18', 0x12.inspect
|
|
|
|
assert_instance_of Fixnum, 0x12
|
2008-03-01 10:51:04 -05:00
|
|
|
assert_raise(SyntaxError) { eval("0x") }
|
2007-02-23 00:23:33 -05:00
|
|
|
assert_equal '15', 0o17.inspect
|
|
|
|
assert_instance_of Fixnum, 0o17
|
2008-03-01 10:51:04 -05:00
|
|
|
assert_raise(SyntaxError) { eval("0o") }
|
2007-02-23 00:23:33 -05:00
|
|
|
assert_equal '5', 0b101.inspect
|
|
|
|
assert_instance_of Fixnum, 0b101
|
2008-03-01 10:51:04 -05:00
|
|
|
assert_raise(SyntaxError) { eval("0b") }
|
2007-02-23 00:23:33 -05:00
|
|
|
assert_equal '123456789012345678901234567890', 123456789012345678901234567890.inspect
|
|
|
|
assert_instance_of Bignum, 123456789012345678901234567890
|
|
|
|
assert_instance_of Float, 1.3
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_self
|
|
|
|
assert_equal self, self
|
|
|
|
assert_instance_of TestRubyLiteral, self
|
|
|
|
assert_respond_to self, :test_self
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_string
|
|
|
|
assert_instance_of String, ?a
|
|
|
|
assert_equal "a", ?a
|
|
|
|
assert_instance_of String, ?A
|
|
|
|
assert_equal "A", ?A
|
|
|
|
assert_instance_of String, ?\n
|
|
|
|
assert_equal "\n", ?\n
|
|
|
|
assert_equal " ", ?\ # space
|
|
|
|
assert_equal '', ''
|
|
|
|
assert_equal 'string', 'string'
|
|
|
|
assert_equal 'string string', 'string string'
|
|
|
|
assert_equal ' ', ' '
|
|
|
|
assert_equal ' ', " "
|
|
|
|
assert_equal "\0", "\0"
|
|
|
|
assert_equal "\1", "\1"
|
|
|
|
assert_equal "3", "\x33"
|
|
|
|
assert_equal "\n", "\n"
|
2010-04-26 21:54:23 -04:00
|
|
|
bug2500 = '[ruby-core:27228]'
|
|
|
|
%w[c C- M-].each do |pre|
|
2010-05-16 08:54:37 -04:00
|
|
|
["u", %w[u{ }]].each do |open, close|
|
2010-04-26 21:54:23 -04:00
|
|
|
str = "\"\\#{pre}\\#{open}5555#{close}\""
|
|
|
|
assert_raise(SyntaxError, "#{bug2500} eval(#{str})") {eval(str)}
|
|
|
|
end
|
|
|
|
end
|
2010-05-16 08:54:37 -04:00
|
|
|
assert_equal "\x13", "\c\x33"
|
|
|
|
assert_equal "\x13", "\C-\x33"
|
|
|
|
assert_equal "\xB3", "\M-\x33"
|
2007-02-23 00:23:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dstring
|
|
|
|
assert_equal '2', "#{1+1}"
|
|
|
|
assert_equal '16', "#{2 ** 4}"
|
|
|
|
s = "string"
|
|
|
|
assert_equal s, "#{s}"
|
2009-07-20 06:01:05 -04:00
|
|
|
a = 'Foo'
|
|
|
|
b = "#{a}" << 'Bar'
|
|
|
|
assert_equal('Foo', a, 'r3842')
|
|
|
|
assert_equal('FooBar', b, 'r3842')
|
2007-02-23 00:23:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dsymbol
|
|
|
|
assert_equal :a3c, :"a#{1+2}c"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xstring
|
|
|
|
assert_equal "foo\n", `echo foo`
|
|
|
|
s = 'foo'
|
|
|
|
assert_equal "foo\n", `echo #{s}`
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_regexp
|
|
|
|
assert_instance_of Regexp, //
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(//, 'a')
|
|
|
|
assert_match(//, '')
|
2007-02-23 00:23:33 -05:00
|
|
|
assert_instance_of Regexp, /a/
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/a/, 'a')
|
|
|
|
assert_no_match(/test/, 'tes')
|
2007-02-23 00:23:33 -05:00
|
|
|
re = /test/
|
|
|
|
assert_match re, 'test'
|
|
|
|
str = 'test'
|
|
|
|
assert_match re, str
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/test/, str)
|
2007-02-23 00:23:33 -05:00
|
|
|
assert_equal 0, (/test/ =~ 'test')
|
|
|
|
assert_equal 0, (re =~ 'test')
|
|
|
|
assert_equal 0, (/test/ =~ str)
|
|
|
|
assert_equal 0, (re =~ str)
|
|
|
|
assert_equal 0, ('test' =~ /test/)
|
|
|
|
assert_equal 0, ('test' =~ re)
|
|
|
|
assert_equal 0, (str =~ /test/)
|
|
|
|
assert_equal 0, (str =~ re)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dregexp
|
|
|
|
assert_instance_of Regexp, /re#{'ge'}xp/
|
|
|
|
assert_equal(/regexp/, /re#{'ge'}xp/)
|
2010-10-03 23:43:50 -04:00
|
|
|
bug3903 = '[ruby-core:32682]'
|
|
|
|
assert_raise(SyntaxError, bug3903) {eval('/[#{"\x80"}]/')}
|
2007-02-23 00:23:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_array
|
|
|
|
assert_instance_of Array, []
|
|
|
|
assert_equal [], []
|
|
|
|
assert_equal 0, [].size
|
|
|
|
assert_instance_of Array, [0]
|
|
|
|
assert_equal [3], [3]
|
|
|
|
assert_equal 1, [3].size
|
|
|
|
a = [3]
|
|
|
|
assert_equal 3, a[0]
|
|
|
|
assert_instance_of Array, [1,2]
|
|
|
|
assert_equal [1,2], [1,2]
|
|
|
|
assert_instance_of Array, [1,2,3,4,5]
|
|
|
|
assert_equal [1,2,3,4,5], [1,2,3,4,5]
|
|
|
|
assert_equal 5, [1,2,3,4,5].size
|
|
|
|
a = [1,2]
|
|
|
|
assert_equal 1, a[0]
|
|
|
|
assert_equal 2, a[1]
|
|
|
|
a = [1 + 2, 3 + 4, 5 + 6]
|
|
|
|
assert_instance_of Array, a
|
|
|
|
assert_equal [3, 7, 11], a
|
|
|
|
assert_equal 7, a[1]
|
|
|
|
assert_equal 1, ([0][0] += 1)
|
|
|
|
assert_equal 1, ([2][0] -= 1)
|
|
|
|
a = [obj = Object.new]
|
|
|
|
assert_instance_of Array, a
|
|
|
|
assert_equal 1, a.size
|
|
|
|
assert_equal obj, a[0]
|
|
|
|
a = [1,2,3]
|
|
|
|
a[1] = 5
|
|
|
|
assert_equal 5, a[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
assert_instance_of Hash, {}
|
|
|
|
assert_equal({}, {})
|
|
|
|
assert_instance_of Hash, {1 => 2}
|
|
|
|
assert_equal({1 => 2}, {1 => 2})
|
|
|
|
h = {1 => 2}
|
|
|
|
assert_equal 2, h[1]
|
|
|
|
h = {"string" => "literal", "goto" => "hell"}
|
|
|
|
assert_equal h, h
|
|
|
|
assert_equal 2, h.size
|
|
|
|
assert_equal h, h
|
|
|
|
assert_equal "literal", h["string"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_range
|
|
|
|
assert_instance_of Range, (1..2)
|
|
|
|
assert_equal(1..2, 1..2)
|
|
|
|
r = 1..2
|
|
|
|
assert_equal 1, r.begin
|
|
|
|
assert_equal 2, r.end
|
|
|
|
assert_equal false, r.exclude_end?
|
|
|
|
assert_instance_of Range, (1...3)
|
|
|
|
assert_equal(1...3, 1...3)
|
|
|
|
r = 1...3
|
|
|
|
assert_equal 1, r.begin
|
|
|
|
assert_equal 3, r.end
|
|
|
|
assert_equal true, r.exclude_end?
|
|
|
|
r = 1+2 .. 3+4
|
|
|
|
assert_instance_of Range, r
|
|
|
|
assert_equal 3, r.begin
|
|
|
|
assert_equal 7, r.end
|
|
|
|
assert_equal false, r.exclude_end?
|
|
|
|
r = 1+2 ... 3+4
|
|
|
|
assert_instance_of Range, r
|
|
|
|
assert_equal 3, r.begin
|
|
|
|
assert_equal 7, r.end
|
|
|
|
assert_equal true, r.exclude_end?
|
|
|
|
assert_instance_of Range, 'a'..'c'
|
|
|
|
r = 'a'..'c'
|
|
|
|
assert_equal 'a', r.begin
|
|
|
|
assert_equal 'c', r.end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test__FILE__
|
|
|
|
assert_instance_of String, __FILE__
|
|
|
|
assert_equal __FILE__, __FILE__
|
|
|
|
assert_equal 'test_literal.rb', File.basename(__FILE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test__LINE__
|
|
|
|
assert_instance_of Fixnum, __LINE__
|
|
|
|
assert_equal __LINE__, __LINE__
|
|
|
|
end
|
|
|
|
|
2008-03-01 11:16:46 -05:00
|
|
|
def test_integer
|
|
|
|
head = ['', '0x', '0o', '0b', '0d', '-', '+']
|
|
|
|
chars = ['0', '1', '_', '9', 'f']
|
|
|
|
head.each {|h|
|
|
|
|
4.times {|len|
|
|
|
|
a = [h]
|
|
|
|
len.times { a = a.product(chars).map {|x| x.join('') } }
|
|
|
|
a.each {|s|
|
|
|
|
next if s.empty?
|
|
|
|
begin
|
|
|
|
r1 = Integer(s)
|
|
|
|
rescue ArgumentError
|
|
|
|
r1 = :err
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
r2 = eval(s)
|
|
|
|
rescue NameError, SyntaxError
|
|
|
|
r2 = :err
|
|
|
|
end
|
|
|
|
assert_equal(r1, r2, "Integer(#{s.inspect}) != eval(#{s.inspect})")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-29 21:00:14 -05:00
|
|
|
bug2407 = '[ruby-dev:39798]'
|
|
|
|
head.each {|h|
|
|
|
|
if /^0/ =~ h
|
|
|
|
begin
|
|
|
|
eval("#{h}_")
|
|
|
|
rescue SyntaxError => e
|
|
|
|
assert_match(/numeric literal without digits\Z/, e.message, bug2407)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
2008-03-01 11:16:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_float
|
|
|
|
head = ['', '-', '+']
|
|
|
|
chars = ['0', '1', '_', '9', 'f', '.']
|
|
|
|
head.each {|h|
|
|
|
|
6.times {|len|
|
|
|
|
a = [h]
|
|
|
|
len.times { a = a.product(chars).map {|x| x.join('') } }
|
|
|
|
a.each {|s|
|
|
|
|
next if s.empty?
|
|
|
|
next if /\.\z/ =~ s
|
|
|
|
next if /\A[-+]?\./ =~ s
|
|
|
|
next if /\A[-+]?0/ =~ s
|
|
|
|
begin
|
|
|
|
r1 = Float(s)
|
|
|
|
rescue ArgumentError
|
|
|
|
r1 = :err
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
r2 = eval(s)
|
|
|
|
rescue NameError, SyntaxError
|
|
|
|
r2 = :err
|
|
|
|
end
|
|
|
|
r2 = :err if Range === r2
|
|
|
|
assert_equal(r1, r2, "Float(#{s.inspect}) != eval(#{s.inspect})")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2007-02-23 00:23:33 -05:00
|
|
|
end
|