first commit
CI / Build (clang, clang++, ubuntu-22.04, linux, 3.10, master, x86,x86_64) (push) Canceled after 0s
CI / Build (clang, clang++, windows-2022, windows, 3.10, master, x86,x86_64) (push) Canceled after 0s
CI / Release (push) Canceled after 0s

This commit is contained in:
2026-09-13 22:18:11 +01:00
commit 08c03ac533
6701 changed files with 1577314 additions and 0 deletions
+404
View File
@@ -0,0 +1,404 @@
$undefined = Object.new
$ok_test = 0
$ko_test = 0
$kill_test = 0
$warning_test = 0
$skip_test = 0
$asserts = []
$test_start = Time.now if Object.const_defined?(:Time)
# For bintest on Ruby
unless RUBY_ENGINE == "mruby"
def t_print(*args)
print(*args)
$stdout.flush
nil
end
def _str_match?(pattern, str)
File.fnmatch?(pattern, str, File::FNM_EXTGLOB|File::FNM_DOTMATCH)
end
end
class Array
def _assertion_join
join("-")
end
end
class String
def _assertion_indent(indent)
indent = indent.to_s
off = 0
str = self
while nl = index("\n", off)
nl += 1
nl += 1 while slice(nl) == "\n"
break if nl >= size
str = indent.dup if off == 0
str += slice(off, nl - off) + indent
off = nl
end
if off == 0
str = indent + self
else
str += slice(off..-1)
end
str
end
end
##
# Create the assertion in a readable way
def assertion_string(err, str, iso=nil, e=nil, bt=nil)
msg = "#{err}#{str}"
msg += " [#{iso}]" if iso && !iso.empty?
msg += " => #{e}" if e && !e.to_s.empty?
if Object.const_defined?(:GEMNAME)
msg += " (#{GEMNAME == 'mruby-test' ? 'core' : "mrbgems: #{GEMNAME}"})"
end
if $mrbtest_assert
$mrbtest_assert.each do |idx, assert_msg, diff|
msg += "\n - Assertion[#{idx}]"
msg += " #{assert_msg}." if assert_msg && !assert_msg.empty?
msg += "\n#{diff}" if diff && !diff.empty?
end
end
msg += "\nbacktrace:\n #{bt.join("\n ")}" if bt && !bt.empty?
msg
end
##
# Verify a code block.
#
# str : A remark which will be printed in case
# this assertion fails
# iso : The ISO reference code of the feature
# which will be tested by this
# assertion
def assert(str = 'assert', iso = '')
t_print(str, (iso != '' ? " [#{iso}]" : ''), ' : ') if $mrbtest_verbose
begin
$mrbtest_child_noassert ||= [0]
$mrbtest_child_noassert << 0
parent_asserts = $asserts
$asserts = []
parent_mrbtest_assert = $mrbtest_assert
$mrbtest_assert = []
if $mrbtest_assert_idx && !$mrbtest_assert_idx.empty?
$mrbtest_assert_idx[-1] += 1
$mrbtest_assert_idx << 0
else
$mrbtest_assert_idx = [0]
class << $mrbtest_assert_idx
alias to_s _assertion_join
end
end
yield
if $mrbtest_assert.size > 0
if $mrbtest_assert.size == $mrbtest_child_noassert[-1]
$asserts.push(assertion_string('Skip: ', str, iso))
$mrbtest_child_noassert[-2] += 1
$skip_test += 1
t_print('?')
else
$asserts.push(assertion_string('Fail: ', str, iso))
$ko_test += 1
t_print('F')
end
elsif $mrbtest_assert_idx[-1] == 0
$asserts.push(assertion_string('Warn: ', str, iso, 'no assertion'))
$warning_test += 1
t_print('W')
else
$ok_test += 1
t_print('.')
end
rescue MRubyTestSkip => e
$asserts.push(assertion_string('Skip: ', str, iso, e))
$skip_test += 1
$mrbtest_child_noassert[-2] += 1
t_print('?')
rescue Exception => e
$asserts.push(assertion_string("#{e.class}: ", str, iso, e, e.backtrace))
$kill_test += 1
t_print('X')
ensure
if $mrbtest_assert_idx.size > 1
$asserts.each do |mesg|
idx = $mrbtest_assert_idx[0..-2]._assertion_join
mesg = mesg._assertion_indent(" ")
# Give `mesg` as a `diff` argument to avoid adding extra periods.
parent_mrbtest_assert << [idx, nil, mesg]
end
else
parent_asserts.concat $asserts
end
$asserts = parent_asserts
$mrbtest_assert = parent_mrbtest_assert
$mrbtest_assert_idx.pop
$mrbtest_assert_idx = nil if $mrbtest_assert_idx.empty?
$mrbtest_child_noassert.pop
nil
end
t_print("\n") if $mrbtest_verbose
end
def assertion_diff(exp, act)
" Expected: #{exp.inspect}\n" \
" Actual: #{act.inspect}"
end
def assert_true(obj, msg = nil, diff = nil)
if $mrbtest_assert_idx && $mrbtest_assert_idx.size > 0
$mrbtest_assert_idx[-1] += 1
unless obj == true
diff ||= " Expected #{obj.inspect} to be true."
$mrbtest_assert.push([$mrbtest_assert_idx.to_s, msg, diff])
end
end
obj
end
def assert_false(obj, msg = nil, diff = nil)
unless obj == false
diff ||= " Expected #{obj.inspect} to be false."
end
assert_true(!obj, msg, diff)
end
def assert_equal(exp, act_or_msg = nil, msg = nil, &block)
ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
unless ret
diff = assertion_diff(exp, act)
end
assert_true(ret, msg, diff)
end
def assert_not_equal(exp, act_or_msg = nil, msg = nil, &block)
ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
if ret
diff = " Expected #{act.inspect} to not be equal to #{exp.inspect}."
end
assert_true(!ret, msg, diff)
end
def assert_same(*args); _assert_same(true, *args) end
def assert_not_same(*args); _assert_same(false, *args) end
def _assert_same(affirmed, exp, act, msg = nil)
unless ret = exp.equal?(act) == affirmed
exp_str, act_str = [exp, act].map do |o|
"#{o.inspect} (class=#{o.class}, oid=#{o.__id__})"
end
diff = " Expected #{act_str} to #{'not ' unless affirmed}be the same as #{exp_str}."
end
assert_true(ret, msg, diff)
end
def assert_nil(obj, msg = nil)
unless ret = obj.nil?
diff = " Expected #{obj.inspect} to be nil."
end
assert_true(ret, msg, diff)
end
def assert_not_nil(obj, msg = nil)
if ret = obj.nil?
diff = " Expected #{obj.inspect} to not be nil."
end
assert_false(ret, msg, diff)
end
def assert_include(*args); _assert_include(true, *args) end
def assert_not_include(*args); _assert_include(false, *args) end
def _assert_include(affirmed, collection, obj, msg = nil)
unless ret = collection.include?(obj) == affirmed
diff = " Expected #{collection.inspect} to #{'not ' unless affirmed}include #{obj.inspect}."
end
assert_true(ret, msg, diff)
end
def assert_predicate(*args); _assert_predicate(true, *args) end
def assert_not_predicate(*args); _assert_predicate(false, *args) end
def _assert_predicate(affirmed, obj, op, msg = nil)
unless ret = obj.__send__(op) == affirmed
diff = " Expected #{obj.inspect} to #{'not ' unless affirmed}be #{op}."
end
assert_true(ret, msg, diff)
end
def assert_operator(*args); _assert_operator(true, *args) end
def assert_not_operator(*args); _assert_operator(false, *args) end
def _assert_operator(affirmed, obj1, op, obj2 = $undefined, msg = nil)
return _assert_predicate(affirmed, obj1, op, msg) if $undefined.equal?(obj2)
unless ret = obj1.__send__(op, obj2) == affirmed
diff = " Expected #{obj1.inspect} to #{'not ' unless affirmed}be #{op} #{obj2.inspect}."
end
assert_true(ret, msg, diff)
end
##
# Fail unless +str+ matches against +pattern+.
#
# +pattern+ is interpreted as pattern for File.fnmatch?. It may contain the
# following metacharacters:
#
# <code>*</code> ::
# Matches any string.
#
# <code>?</code> ::
# Matches any one character.
#
# <code>[_SET_]</code>, <code>[^_SET_]</code> (<code>[!_SET_]</code>) ::
# Matches any one character in _SET_. Behaves like character sets in
# Regexp, including set negation (<code>[^a-z]</code>).
#
# <code>{_A_,_B_}</code> ::
# Matches pattern _A_ or pattern _B_.
#
# <code> \ </code> ::
# Escapes the next character.
def assert_match(*args); _assert_match(true, *args) end
def assert_not_match(*args); _assert_match(false, *args) end
def _assert_match(affirmed, pattern, str, msg = nil)
unless ret = _str_match?(pattern, str) == affirmed
diff = " Expected #{pattern.inspect} to #{'not ' unless affirmed}match #{str.inspect}."
end
assert_true(ret, msg, diff)
end
##
# Fails unless +obj+ is a kind of +cls+.
def assert_kind_of(cls, obj, msg = nil)
unless ret = obj.kind_of?(cls)
diff = " Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}."
end
assert_true(ret, msg, diff)
end
##
# Fails unless +exp+ is equal to +act+ in terms of a Float
def assert_float(exp, act, msg = nil)
e, a = exp.to_f, act.to_f
if e.finite? && a.finite? && (n = (e - a).abs) > Mrbtest::FLOAT_TOLERANCE
flunk(msg, " Expected |#{exp} - #{act}| (#{n}) to be <= #{Mrbtest::FLOAT_TOLERANCE}.")
elsif (e.infinite? || a.infinite?) && e != a ||
e.nan? && !a.nan? || !e.nan? && a.nan?
flunk(msg, " Expected #{act} to be #{exp}.")
else
pass
end
end
def assert_raise(*exc)
msg = (exc.last.is_a? String) ? exc.pop : nil
exc = exc.empty? ? StandardError : exc.size == 1 ? exc[0] : exc
begin
yield
rescue *exc => e
pass
e
rescue Exception => e
diff = " #{exc} exception expected, not\n" \
" Class: <#{e.class}>\n" \
" Message: <#{e}>"
flunk(msg, diff)
else
diff = " #{exc} expected but nothing was raised."
flunk(msg, diff)
end
end
def assert_nothing_raised(msg = nil)
begin
yield
rescue Exception => e
diff = " Exception raised:\n" \
" Class: <#{e.class}>\n" \
" Message: <#{e}>"
flunk(msg, diff)
else
pass
end
end
def assert_raise_with_message(*args, &block)
_assert_raise_with_message(:plain, *args, &block)
end
def assert_raise_with_message_pattern(*args, &block)
_assert_raise_with_message(:pattern, *args, &block)
end
def _assert_raise_with_message(type, exc, exp_msg, msg = nil, &block)
e = msg ? assert_raise(exc, msg, &block) : assert_raise(exc, &block)
e ? ($mrbtest_assert_idx[-1]-=1) : (return e)
err_msg = e.message
unless ret = type == :pattern ? _str_match?(exp_msg, err_msg) : exp_msg == err_msg
diff = " Expected Exception(#{exc}) was raised, but the message doesn't match.\n"
if type == :pattern
diff += " Expected #{exp_msg.inspect} to match #{err_msg.inspect}."
else
diff += assertion_diff(exp_msg, err_msg)
end
end
assert_true(ret, msg, diff)
end
def pass
assert_true(true)
end
def flunk(msg = "Epic Fail!", diff = "")
assert_true(false, msg, diff)
end
##
# Report the test result and print all assertions
# which were reported broken.
def report
t_print("\n")
$asserts.each do |msg|
t_print("#{msg}\n")
end
$total_test = $ok_test + $ko_test + $kill_test + $warning_test + $skip_test
t_print(" Total: #{$total_test}\n")
t_print(" OK: #{$ok_test}\n")
t_print(" KO: #{$ko_test}\n")
t_print(" Crash: #{$kill_test}\n")
t_print("Warning: #{$warning_test}\n")
t_print(" Skip: #{$skip_test}\n")
if Object.const_defined?(:Time)
t_time = Time.now - $test_start
t_print(" Time: #{t_time.round(2)} seconds\n")
end
$ko_test == 0 && $kill_test == 0
end
def _eval_assertion(meth, exp, act_or_msg, msg, block)
if block
exp, act, msg = exp, block.call, act_or_msg
else
exp, act, msg = exp, act_or_msg, msg
end
return exp.__send__(meth, act), exp, act, msg
end
##
# Skip the test
class MRubyTestSkip < NotImplementedError; end
def skip(cause = "")
raise MRubyTestSkip.new(cause)
end
+42
View File
@@ -0,0 +1,42 @@
$:.unshift File.dirname(File.dirname(File.expand_path(__FILE__)))
require 'test/assert.rb'
GEMNAME = ""
def cmd(s)
case RbConfig::CONFIG['host_os']
when /mswin(?!ce)|mingw|bccwin/
"bin\\#{s}.exe"
else
"bin/#{s}"
end
end
def shellquote(s)
case RbConfig::CONFIG['host_os']
when /mswin(?!ce)|mingw|bccwin/
"\"#{s}\""
else
"'#{s}'"
end
end
print "bintest - Command Binary Test\n\n"
ARGV.each do |gem|
case gem
when '-v'; $mrbtest_verbose = true
end
case RbConfig::CONFIG['host_os']
when /mswin(?!ce)|mingw|bccwin/
gem = gem.gsub('\\', '/')
end
Dir["#{gem}/bintest/**/*.rb"].each do |file|
GEMNAME.replace(File.basename(gem))
load file
end
end
exit report
+16
View File
@@ -0,0 +1,16 @@
##
# ArgumentError ISO Test
assert('ArgumentError', '15.2.24') do
e2 = nil
a = []
begin
# this will cause an exception due to the wrong arguments
a[]
rescue => e1
e2 = e1
end
assert_equal(Class, ArgumentError.class)
assert_equal(ArgumentError, e2.class)
end
+422
View File
@@ -0,0 +1,422 @@
##
# Array ISO Test
assert('Array', '15.2.12') do
assert_equal(Class, Array.class)
end
assert('Array inclueded modules', '15.2.12.3') do
assert_true(Array.include?(Enumerable))
end
assert('Array.[]', '15.2.12.4.1') do
assert_equal([1, 2, 3], Array.[](1,2,3))
end
class SubArray < Array
end
assert('SubArray.[]') do
a = SubArray[1, 2, 3]
assert_equal(SubArray, a.class)
end
assert('Array#+', '15.2.12.5.1') do
assert_equal([1, 1], [1].+([1]))
end
assert('Array#*', '15.2.12.5.2') do
assert_raise(ArgumentError) do
# this will cause an exception due to the wrong argument
[1].*(-1)
end
assert_equal([1, 1, 1], [1].*(3))
assert_equal([], [1].*(0))
end
assert('Array#<<', '15.2.12.5.3') do
assert_equal([1, 1], [1].<<(1))
end
assert('Array#[]', '15.2.12.5.4') do
a = Array.new
assert_raise(ArgumentError) do
# this will cause an exception due to the wrong arguments
a.[]()
end
assert_raise(ArgumentError) do
# this will cause an exception due to the wrong arguments
a.[](1,2,3)
end
assert_equal(2, [1,2,3].[](1))
assert_equal(nil, [1,2,3].[](4))
assert_equal(3, [1,2,3].[](-1))
assert_equal(nil, [1,2,3].[](-4))
a = [ "a", "b", "c", "d", "e" ]
assert_equal(["b", "c"], a[1,2])
assert_equal(["b", "c", "d"], a[1..-2])
skip unless Object.const_defined?(:Float)
assert_equal("b", a[1.1])
end
assert('Array#[]=', '15.2.12.5.5') do
a = Array.new
assert_raise(ArgumentError) do
# this will cause an exception due to the wrong arguments
a.[]=()
end
assert_raise(ArgumentError) do
# this will cause an exception due to the wrong arguments
a.[]=(1,2,3,4)
end
assert_raise(IndexError) do
# this will cause an exception due to the wrong arguments
a = [1,2,3,4,5]
a[1, -1] = 10
end
assert_equal(4, [1,2,3].[]=(1,4))
assert_equal(3, [1,2,3].[]=(1,2,3))
a = [1,2,3,4,5]
a[3..-1] = 6
assert_equal([1,2,3,6], a)
a = [1,2,3,4,5]
a[3..-1] = []
assert_equal([1,2,3], a)
a = [1,2,3,4,5]
a[2...4] = 6
assert_equal([1,2,6,5], a)
# passing self (#3274)
a = [1,2,3]
a[1,0] = a
assert_equal([1,1,2,3,2,3], a)
a = [1,2,3]
a[-1,0] = a
assert_equal([1,2,1,2,3,3], a)
end
assert('Array#clear', '15.2.12.5.6') do
a = [1]
a.clear
assert_equal([], a)
end
assert('Array#collect!', '15.2.12.5.7') do
a = [1,2,3]
a.collect! { |i| i + i }
assert_equal([2,4,6], a)
end
assert('Array#concat', '15.2.12.5.8') do
assert_equal([1,2,3,4], [1, 2].concat([3, 4]))
# passing self (#3302)
a = [1,2,3]
a.concat(a)
assert_equal([1,2,3,1,2,3], a)
end
assert('Array#delete_at', '15.2.12.5.9') do
a = [1,2,3]
assert_equal(2, a.delete_at(1))
assert_equal([1,3], a)
assert_equal(nil, a.delete_at(3))
assert_equal([1,3], a)
assert_equal(nil, a.delete_at(-3))
assert_equal([1,3], a)
assert_equal(3, a.delete_at(-1))
assert_equal([1], a)
end
assert('Array#each', '15.2.12.5.10') do
a = [1,2,3]
b = 0
a.each {|i| b += i}
assert_equal(6, b)
end
assert('Array#each_index', '15.2.12.5.11') do
a = [1]
b = nil
a.each_index {|i| b = i}
assert_equal(0, b)
end
assert('Array#empty?', '15.2.12.5.12') do
a = []
b = [b]
assert_true([].empty?)
assert_false([1].empty?)
end
assert('Array#first', '15.2.12.5.13') do
assert_raise(ArgumentError) do
# this will cause an exception due to the wrong argument
[1,2,3].first(-1)
end
assert_raise(ArgumentError) do
# this will cause an exception due to the wrong argument
[1,2,3].first(1,2)
end
assert_nil([].first)
b = [1,2,3]
assert_equal(1, b.first)
assert_equal([], b.first(0))
assert_equal([1], b.first(1))
assert_equal([1,2,3], b.first(4))
end
assert('Array#index', '15.2.12.5.14') do
a = [1,2,3]
assert_equal(1, a.index(2))
assert_equal(nil, a.index(0))
end
assert('Array#initialize', '15.2.12.5.15') do
a = [].initialize(1)
b = [].initialize(2)
c = [].initialize(2, 1)
d = [].initialize(2) {|i| i}
assert_equal([nil], a)
assert_equal([nil,nil], b)
assert_equal([1,1], c)
assert_equal([0,1], d)
end
assert('Array#initialize_copy', '15.2.12.5.16') do
a = [1,2,3]
b = [].initialize_copy(a)
assert_equal([1,2,3], b)
end
assert('Array#join', '15.2.12.5.17') do
a = [1,2,3].join
b = [1,2,3].join(',')
assert_equal('123', a)
assert_equal('1,2,3', b)
end
assert('Array#last', '15.2.12.5.18') do
assert_raise(ArgumentError) do
# this will cause an exception due to the wrong argument
[1,2,3].last(-1)
end
a = [1,2,3]
assert_equal(3, a.last)
assert_nil([].last)
end
assert('Array#length', '15.2.12.5.19') do
a = [1,2,3]
assert_equal(3, a.length)
end
assert('Array#map!', '15.2.12.5.20') do
a = [1,2,3]
a.map! { |i| i + i }
assert_equal([2,4,6], a)
end
assert('Array#pop', '15.2.12.5.21') do
a = [1,2,3]
b = a.pop
assert_nil([].pop)
assert_equal([1,2], a)
assert_equal(3, b)
assert_raise(FrozenError) { [].freeze.pop }
end
assert('Array#push', '15.2.12.5.22') do
a = [1,2,3]
b = a.push(4)
assert_equal([1,2,3,4], a)
assert_equal([1,2,3,4], b)
end
assert('Array#replace', '15.2.12.5.23') do
a = [1,2,3]
b = [].replace(a)
assert_equal([1,2,3], b)
end
assert('Array#reverse', '15.2.12.5.24') do
a = [1,2,3]
b = a.reverse
assert_equal([1,2,3], a)
assert_equal([3,2,1], b)
end
assert('Array#reverse!', '15.2.12.5.25') do
a = [1,2,3]
b = a.reverse!
assert_equal([3,2,1], a)
assert_equal([3,2,1], b)
end
assert('Array#rindex', '15.2.12.5.26') do
a = [1,2,3]
assert_equal(1, a.rindex(2))
assert_equal(nil, a.rindex(0))
end
assert('Array#shift', '15.2.12.5.27') do
a = [1,2,3]
b = a.shift
assert_nil([].shift)
assert_equal([2,3], a)
assert_equal(1, b)
assert_raise(FrozenError) { [].freeze.shift }
end
assert('Array#size', '15.2.12.5.28') do
a = [1,2,3]
assert_equal(3, a.size)
end
assert('Array#slice', '15.2.12.5.29') do
a = [*(1..100)]
b = a.dup
assert_equal(1, a.slice(0))
assert_equal(100, a.slice(99))
assert_nil(a.slice(100))
assert_equal(100, a.slice(-1))
assert_equal(99, a.slice(-2))
assert_equal(1, a.slice(-100))
assert_nil(a.slice(-101))
assert_equal([1], a.slice(0,1))
assert_equal([100], a.slice(99,1))
assert_equal([], a.slice(100,1))
assert_equal([100], a.slice(99,100))
assert_equal([100], a.slice(-1,1))
assert_equal([99], a.slice(-2,1))
assert_equal([10, 11, 12], a.slice(9, 3))
assert_equal([10, 11, 12], a.slice(-91, 3))
assert_nil(a.slice(-101, 2))
assert_equal([1], a.slice(0..0))
assert_equal([100], a.slice(99..99))
assert_equal([], a.slice(100..100))
assert_equal([100], a.slice(99..200))
assert_equal([100], a.slice(-1..-1))
assert_equal([99], a.slice(-2..-2))
assert_equal([10, 11, 12], a.slice(9..11))
assert_equal([10, 11, 12], a.slice(-91..-89))
assert_equal([10, 11, 12], a.slice(-91..-89))
assert_nil(a.slice(-101..-1))
assert_nil(a.slice(10, -3))
assert_equal([], a.slice(10..7))
assert_equal(b, a)
end
assert('Array#unshift', '15.2.12.5.30') do
a = [2,3]
b = a.unshift(1)
c = [2,3]
d = c.unshift(0, 1)
assert_equal([1,2,3], a)
assert_equal([1,2,3], b)
assert_equal([0,1,2,3], c)
assert_equal([0,1,2,3], d)
end
assert('Array#to_s', '15.2.12.5.31 / 15.2.12.5.32') do
a = [2, 3, 4, 5]
a[4] = a
r1 = a.to_s
r2 = a.inspect
assert_equal(r2, r1)
assert_equal("[2, 3, 4, 5, [...]]", r1)
end
assert('Array#==', '15.2.12.5.33') do
assert_false(["a", "c"] == ["a", "c", 7])
assert_true(["a", "c", 7] == ["a", "c", 7])
assert_false(["a", "c", 7] == ["a", "d", "f"])
end
assert('Array#eql?', '15.2.12.5.34') do
a1 = [ 1, 2, 3 ]
a2 = [ 1, 2, 3 ]
a3 = [ 1.0, 2.0, 3.0 ]
assert_true(a1.eql? a2)
assert_false(a1.eql? a3)
end
assert('Array#hash', '15.2.12.5.35') do
a = [ 1, 2, 3 ]
#assert_true(a.hash.is_a? Integer)
assert_true(a.hash.is_a? Integral) # mruby special
assert_equal([1,2].hash, [1,2].hash)
end
assert('Array#<=>', '15.2.12.5.36') do
r1 = [ "a", "a", "c" ] <=> [ "a", "b", "c" ] #=> -1
r2 = [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1
r3 = [ "a", "b", "c" ] <=> [ "a", "b", "c" ] #=> 0
assert_equal(-1, r1)
assert_equal(+1, r2)
assert_equal(0, r3)
end
# Not ISO specified
assert("Array (Longish inline array)") do
ary = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9], [10, 10], [11, 11], [12, 12], [13, 13], [14, 14], [15, 15], [16, 16], [17, 17], [18, 18], [19, 19], [20, 20], [21, 21], [22, 22], [23, 23], [24, 24], [25, 25], [26, 26], [27, 27], [28, 28], [29, 29], [30, 30], [31, 31], [32, 32], [33, 33], [34, 34], [35, 35], [36, 36], [37, 37], [38, 38], [39, 39], [40, 40], [41, 41], [42, 42], [43, 43], [44, 44], [45, 45], [46, 46], [47, 47], [48, 48], [49, 49], [50, 50], [51, 51], [52, 52], [53, 53], [54, 54], [55, 55], [56, 56], [57, 57], [58, 58], [59, 59], [60, 60], [61, 61], [62, 62], [63, 63], [64, 64], [65, 65], [66, 66], [67, 67], [68, 68], [69, 69], [70, 70], [71, 71], [72, 72], [73, 73], [74, 74], [75, 75], [76, 76], [77, 77], [78, 78], [79, 79], [80, 80], [81, 81], [82, 82], [83, 83], [84, 84], [85, 85], [86, 86], [87, 87], [88, 88], [89, 89], [90, 90], [91, 91], [92, 92], [93, 93], [94, 94], [95, 95], [96, 96], [97, 97], [98, 98], [99, 99], [100, 100], [101, 101], [102, 102], [103, 103], [104, 104], [105, 105], [106, 106], [107, 107], [108, 108], [109, 109], [110, 110], [111, 111], [112, 112], [113, 113], [114, 114], [115, 115], [116, 116], [117, 117], [118, 118], [119, 119], [120, 120], [121, 121], [122, 122], [123, 123], [124, 124], [125, 125], [126, 126], [127, 127], [128, 128], [129, 129], [130, 130], [131, 131], [132, 132], [133, 133], [134, 134], [135, 135], [136, 136], [137, 137], [138, 138], [139, 139], [140, 140], [141, 141], [142, 142], [143, 143], [144, 144], [145, 145], [146, 146], [147, 147], [148, 148], [149, 149], [150, 150], [151, 151], [152, 152], [153, 153], [154, 154], [155, 155], [156, 156], [157, 157], [158, 158], [159, 159], [160, 160], [161, 161], [162, 162], [163, 163], [164, 164], [165, 165], [166, 166], [167, 167], [168, 168], [169, 169], [170, 170], [171, 171], [172, 172], [173, 173], [174, 174], [175, 175], [176, 176], [177, 177], [178, 178], [179, 179], [180, 180], [181, 181], [182, 182], [183, 183], [184, 184], [185, 185], [186, 186], [187, 187], [188, 188], [189, 189], [190, 190], [191, 191], [192, 192], [193, 193], [194, 194], [195, 195], [196, 196], [197, 197], [198, 198], [199, 199]]
h = Hash.new(0)
ary.each {|p| h[p.class] += 1}
assert_equal({Array=>200}, h)
end
assert("Array#rindex") do
class Sneaky
def ==(*)
$a.clear
$a.replace([1])
false
end
end
$a = [2, 3, 4, 5, 6, 7, 8, 9, 10, Sneaky.new]
assert_equal 0, $a.rindex(1)
end
assert('Array#sort!') do
a = [3, 2, 1]
assert_equal a, a.sort! # sort! returns self.
assert_equal [1, 2, 3], a # it is sorted.
end
assert('Array#freeze') do
a = [].freeze
assert_raise(FrozenError) do
a[0] = 1
end
end
+11
View File
@@ -0,0 +1,11 @@
##
# BasicObject
assert('BasicObject') do
assert_equal(Class, BasicObject.class)
end
assert('BasicObject superclass') do
assert_nil(BasicObject.superclass)
end
+522
View File
@@ -0,0 +1,522 @@
##
# Bootstrap tests for blocks
assert('BS Block 1') do
assert_equal(1) do
1.times{
begin
a = 1
ensure
foo = nil
end
}
end
end
assert('BS Block 2') do
assert_equal 2, [1,2,3].find{|x| x == 2}
end
assert('BS Block 3') do
class E
include Enumerable
def each(&block)
[1, 2, 3].each(&block)
end
end
assert_equal 2, E.new.find {|x| x == 2 }
end
assert('BS Block 3') do
sum = 0
for x in [1, 2, 3]
sum += x
end
assert_equal 6, sum
end
assert('BS Block 4') do
sum = 0
for x in (1..5)
sum += x
end
assert_equal 15, sum
end
assert('BS Block 5') do
sum = 0
for x in []
sum += x
end
assert_equal 0, sum
end
assert('BS Block 6') do
ans = []
assert_equal(1) do
1.times{
for n in 1..3
a = n
ans << a
end
}
end
end
assert('BS Block 7') do
ans = []
assert_equal((1..3)) do
for m in 1..3
for n in 2..4
a = [m, n]
ans << a
end
end
end
end
assert('BS Block 8') do
assert_equal [1, 2, 3], (1..3).to_a
end
assert('BS Block 9') do
assert_equal([4, 8, 12]) do
(1..3).map{|e|
e * 4
}
end
end
assert('BS Block 10') do
def m
yield
end
def n
yield
end
assert_equal(100) do
m{
n{
100
}
}
end
end
assert('BS Block 11') do
def m
yield 1
end
assert_equal(20) do
m{|ib|
m{|jb|
i = 20
}
}
end
end
assert('BS Block 12') do
def m
yield 1
end
assert_equal(2) do
m{|ib|
m{|jb|
ib = 20
kb = 2
}
}
end
end
assert('BS Block 13') do
def iter1
iter2{
yield
}
end
def iter2
yield
end
assert_equal(3) do
iter1{
jb = 2
iter1{
jb = 3
}
jb
}
end
end
assert('BS Block 14') do
def iter1
iter2{
yield
}
end
def iter2
yield
end
assert_equal(2) do
iter1{
jb = 2
iter1{
jb
}
jb
}
end
end
assert('BS Block 15') do
def m
yield 1
end
assert_equal(2) do
m{|ib|
ib*2
}
end
end
assert('BS Block 16') do
def m
yield 12345, 67890
end
assert_equal(92580) do
m{|ib,jb|
ib*2+jb
}
end
end
assert('BS Block 17') do
def iter
yield 10
end
a = nil
assert_equal [10, nil] do
[iter{|a|
a
}, a]
end
end
assert('BS Block 18') do
def iter
yield 10
end
assert_equal(21) do
iter{|a|
iter{|a|
a + 1
} + a
}
end
end
assert('BS Block 19') do
def iter
yield 10, 20, 30, 40
end
a = b = c = d = nil
assert_equal([10, 20, 30, 40, nil, nil, nil, nil]) do
iter{|a, b, c, d|
[a, b, c, d]
} + [a, b, c, d]
end
end
assert('BS Block 20') do
def iter
yield 10, 20, 30, 40
end
a = b = nil
assert_equal([10, 20, 30, 40, nil, nil]) do
iter{|a, b, c, d|
[a, b, c, d]
} + [a, b]
end
end
assert('BS Block 21') do
def iter
yield 1, 2
end
assert_equal([1, [2]]) do
iter{|a, *b|
[a, b]
}
end
end
assert('BS Block 22') do
def iter
yield 1, 2
end
assert_equal([[1, 2]]) do
iter{|*a|
[a]
}
end
end
assert('BS Block 23') do
def iter
yield 1, 2
end
assert_equal([1, 2, []]) do
iter{|a, b, *c|
[a, b, c]
}
end
end
assert('BS Block 24') do
def m
yield
end
assert_equal(1) do
m{
1
}
end
end
assert('BS Block 25') do
def m
yield 123
end
assert_equal(15129) do
m{|ib|
m{|jb|
ib*jb
}
}
end
end
assert('BS Block 26') do
def m a
yield a
end
assert_equal(2) do
m(1){|ib|
m(2){|jb|
ib*jb
}
}
end
end
assert('BS Block 27') do
sum = 0
3.times{|ib|
2.times{|jb|
sum += ib + jb
}}
assert_equal sum, 9
end
assert('BS Block 28') do
assert_equal(10) do
3.times{
break 10
}
end
end
assert('BS Block 29') do
def iter
yield 1,2,3
end
assert_equal([1, 2]) do
iter{|i, j|
[i, j]
}
end
end
assert('BS Block 30') do
def iter
yield 1
end
assert_equal([1, nil]) do
iter{|i, j|
[i, j]
}
end
end
assert('BS Block [ruby-dev:31147]') do
def m
yield
end
assert_nil m{|&b| b}
end
assert('BS Block [ruby-dev:31160]') do
def m()
yield
end
assert_nil m {|(v,(*))|}
end
assert('BS Block [issue #750]') do
def m(a, *b)
yield
end
args = [1, 2, 3]
assert_equal m(*args){ 1 }, 1
end
assert('BS Block 31') do
def m()
yield
end
assert_nil m {|((*))|}
end
assert('BS Block [ruby-dev:31440]') do
def m
yield [0]
end
assert_equal m{|v, &b| v}, [0]
end
assert('BS Block 32') do
r = false; 1.times{|&b| r = b}
assert_equal NilClass, r.class
end
assert('BS Block [ruby-core:14395]') do
assert_nothing_raised do
class Controller
def respond_to(&block)
responder = Responder.new
block.call(responder)
responder.respond
end
def test_for_bug
respond_to{|format|
format.js{
"in test"
render{|obj|
obj
}
}
}
end
def render(&block)
"in render"
end
end
class Responder
def method_missing(symbol, &block)
"enter method_missing"
@response = Proc.new{
'in method missing'
block.call
}
"leave method_missing"
end
def respond
@response.call
end
end
t = Controller.new
t.test_for_bug
end
end
assert("BS Block 33") do
module TestReturnFromNestedBlock
def self.test
1.times do
1.times do
return :ok
end
end
:bad
end
end
assert_equal :ok, TestReturnFromNestedBlock.test
end
assert("BS Block 34") do
module TestReturnFromNestedBlock_BSBlock34
def self.test
1.times do
while true
return :ok
end
end
:bad
end
end
assert_equal :ok, TestReturnFromNestedBlock_BSBlock34.test
end
assert("BS Block 35") do
module TestReturnFromNestedBlock_BSBlock35
def self.test
1.times do
until false
return :ok
end
end
:bad
end
end
assert_equal :ok, TestReturnFromNestedBlock_BSBlock35.test
end
assert('BS Block 36') do
def iter
yield 1, 2, 3, 4, 5
end
assert_equal([1, 2, [3, 4], 5]) do
iter{|a, b, *c, d|
[a, b, c, d]
}
end
end
assert('BS Block 37') do
def iter
yield 1, 2, 3
end
assert_equal([1, 2, [], 3]) do
iter{|a, b, *c, d|
[a, b, c, d]
}
end
end
assert('BS Block 38') do
def iter
yield 1,2,3,4,5,6
end
assert_equal [1,2,3,4,5], iter{|a,b,c=:c,d,e| [a,b,c,d,e]}
end
+38
View File
@@ -0,0 +1,38 @@
##
# Bootstrap test for literals
assert('BS Literal 1') do
assert_true true
end
assert('BS Literal 2') do
assert_equal TrueClass, true.class
end
assert('BS Literal 3') do
assert_false false
end
assert('BS Literal 4') do
assert_equal FalseClass, false.class
end
assert('BS Literal 5') do
assert_equal 'nil', nil.inspect
end
assert('BS Literal 6') do
assert_equal NilClass, nil.class
end
assert('BS Literal 7') do
assert_equal Symbol, :sym.class
end
assert('BS Literal 8') do
assert_equal 1234, 1234
end
assert('BS Literal 9') do
assert_equal Fixnum, 1234.class
end
+465
View File
@@ -0,0 +1,465 @@
##
# Class ISO Test
assert('Class', '15.2.3') do
assert_equal(Class, Class.class)
end
assert('Class#initialize', '15.2.3.3.1') do
c = Class.new do
def test
:test
end
end.new
assert_equal(c.test, :test)
end
assert('Class#initialize_copy', '15.2.3.3.2') do
class TestClass
attr_accessor :n
def initialize(n)
@n = n
end
def initialize_copy(obj)
@n = n
end
end
c1 = TestClass.new('Foo')
c2 = c1.dup
c3 = TestClass.new('Bar')
assert_equal(c1.n, c2.n)
assert_not_equal(c1.n, c3.n)
end
assert('Class#new', '15.2.3.3.3') do
assert_raise(TypeError, 'Singleton should raise TypeError') do
(class <<"a"; self; end).new
end
class TestClass
def initialize args, &block
@result = if not args.nil? and block.nil?
# only arguments
:only_args
elsif not args.nil? and not block.nil?
# args and block is given
:args_and_block
else
# this should never happen
:broken
end
end
def result; @result; end
end
assert_equal(:only_args, TestClass.new(:arg).result)
# with block doesn't work yet
end
assert('Class#superclass', '15.2.3.3.4') do
class SubClass < String; end
assert_equal(String, SubClass.superclass)
end
# Not ISO specified
assert('Class 1') do
class C1; end
assert_equal(Class, C1.class)
end
assert('Class 2') do
class C2; end
assert_equal(C2, C2.new.class)
end
assert('Class 3') do
class C3; end
assert_equal(Class, C3.new.class.class)
end
assert('Class 4') do
class C4_A; end
class C4 < C4_A; end
assert_equal(Class, C4.class)
end
assert('Class 5') do
class C5_A; end
class C5 < C5_A; end
assert_equal(C5, C5.new.class)
end
assert('Class 6') do
class C6_A; end
class C6 < C6_A; end
assert_equal(Class, C6.new.class.class)
end
assert('Class 7') do
class C7_A; end
class C7_B; end
class C7 < C7_A; end
assert_raise(TypeError) do
# Different superclass.
class C7 < C7_B; end
end
end
assert('Class 8') do
class C8_A; end
class C8; end # superclass is Object
assert_raise(TypeError) do
# Different superclass.
class C8 < C8_A; end
end
end
assert('Class 9') do
Class9Const = "a"
assert_raise(TypeError) do
class Class9Const; end
end
end
assert('Class Module 1') do
module M; end
assert_equal(Module, M.class)
end
assert('Class Module 2') do
module M; end
class C; include M; end
assert_equal(C, C.new.class)
end
# nested class
assert('Class Nested 1') do
class A; end
class A::B; end
assert_equal(A::B, A::B)
end
assert('Class Nested 2') do
class A; end
class A::B; end
assert_equal(A::B, A::B.new.class)
end
assert('Class Nested 3') do
class A; end
class A::B; end
assert_equal(Class, A::B.new.class.class)
end
assert('Class Nested 4') do
class A; end
class A::B; end
class A::B::C; end
assert_equal(A::B::C, A::B::C)
end
assert('Class Nested 5') do
class A; end
class A::B; end
class A::B::C; end
assert_equal(Class, A::B::C.class)
end
assert('Class Nested 6') do
class A; end
class A::B; end
class A::B::C; end
assert_equal(A::B::C, A::B::C.new.class)
end
assert('Class Nested 7') do
class A; end
class A::B; end
class A::B2 < A::B; end
assert_equal(A::B2, A::B2)
end
assert('Class Nested 8') do
class A; end
class A::B; end
class A::B2 < A::B; end
assert_equal(Class, A::B2.class)
end
assert('Class Colon 1') do
class A; end
A::C = 1
assert_equal(1, A::C)
end
assert('Class Colon 2') do
class A; class ::C; end end
assert_equal(C, C)
end
assert('Class Colon 3') do
class A; class ::C; end end
assert_equal(Class, C.class)
end
assert('Class Dup 1') do
class C; end
assert_equal(Class, C.dup.class)
end
assert('Class Dup 2') do
module M; end
assert_equal(Module, M.dup.class)
end
assert('Class.new') do
assert_equal(Class, Class.new.class)
a = []
klass = Class.new do |c|
a << c
end
assert_equal([klass], a)
end
assert('class to return the last value') do
m = class C; :m end
assert_equal(m, :m)
end
assert('class to return nil if body is empty') do
assert_nil(class C end)
assert_nil(class << self; end)
end
assert('raise when superclass is not a class') do
module FirstModule; end
assert_raise(TypeError, 'should raise TypeError') do
class FirstClass < FirstModule; end
end
class SecondClass; end
assert_raise(TypeError, 'should raise TypeError') do
class SecondClass < false; end
end
class ThirdClass; end
assert_raise(TypeError, 'should raise TypeError') do
class ThirdClass < ThirdClass; end
end
end
assert('Class#inherited') do
class Foo
@@subclass_name = nil
def self.inherited(subclass)
@@subclass_name = subclass
end
def self.subclass_name
@@subclass_name
end
end
assert_equal(nil, Foo.subclass_name)
class Bar < Foo
end
assert_equal(Bar, Foo.subclass_name)
class Baz < Bar
end
assert_equal(Baz, Foo.subclass_name)
end
assert('singleton tests') do
module FooMod
def run_foo_mod
100
end
end
bar = String.new
baz = class << bar
extend FooMod
def self.run_baz
200
end
end
assert_equal :run_baz, baz
assert_raise(NoMethodError, 'should raise NoMethodError') do
bar.run_foo_mod
end
assert_raise(NoMethodError, 'should raise NoMethodError') do
bar.run_baz
end
baz = class << bar
extend FooMod
def self.run_baz
300
end
self
end
assert_true baz.respond_to? :run_baz
assert_true baz.respond_to? :run_foo_mod
assert_equal 100, baz.run_foo_mod
assert_equal 300, baz.run_baz
assert_raise(NoMethodError, 'should raise NoMethodError') do
bar.run_foo_mod
end
assert_raise(NoMethodError, 'should raise NoMethodError') do
bar.run_baz
end
fv = false
class << fv
def self.run_false
5
end
end
nv = nil
class << nv
def self.run_nil
6
end
end
tv = true
class << tv
def self.run_nil
7
end
end
assert_raise(TypeError, 'should raise TypeError') do
num = 1.0
class << num
def self.run_nil
7
end
end
end if Object.const_defined?(:Float)
o = Object.new
sc = class << o; self end
o.freeze
assert_predicate(sc, :frozen?)
assert_predicate(class << Object.new.freeze; self end, :frozen?)
end
assert('clone Class') do
class Foo
def func
true
end
end
assert_true(Foo.clone.new.func)
end
assert('class variable and class << self style class method') do
class ClassVariableTest
@@class_variable = "value"
class << self
def class_variable
@@class_variable
end
end
end
assert_equal("value", ClassVariableTest.class_variable)
end
assert('class variable definition in singleton_class') do
class ClassVariableDefinitionInSingletonTest
class << self
@@class_variable = "value"
end
def class_variable
@@class_variable
end
end
assert_equal("value", ClassVariableDefinitionInSingletonTest.new.class_variable)
end
assert('class variable in module and class << self style class method') do
module ClassVariableInModuleTest
@@class_variable = "value"
class << self
def class_variable
@@class_variable
end
end
end
assert_equal("value", ClassVariableInModuleTest.class_variable)
end
assert('child class/module defined in singleton class get parent constant') do
actual = module GetParentConstantTest
EXPECT = "value"
class << self
class CHILD
class << self
EXPECT
end
end
end
end
assert_equal("value", actual)
end
assert('overriding class variable with a module (#3235)') do
module ModuleWithCVar
@@class_variable = 1
end
class CVarOverrideTest
@@class_variable = 2
include ModuleWithCVar
assert_equal(1, @@class_variable)
end
end
assert('class variable for frozen class/module') do
module CVarForFrozenModule
freeze
assert_raise(FrozenError) { @@cv = 1 }
end
class CVarForFrozenClassA
@@a = nil
freeze
end
class CVarForFrozenClassB < CVarForFrozenClassA
def a=(v)
@@a = v
end
end
b = CVarForFrozenClassB.new
assert_raise(FrozenError) { b.a = 1 }
end
assert('class with non-class/module outer raises TypeError') do
assert_raise(TypeError) { class 0::C1; end }
assert_raise(TypeError) { class []::C2; end }
end
+197
View File
@@ -0,0 +1,197 @@
##
# Codegen tests
assert('peephole optimization does not eliminate move whose result is reused') do
assert_raise LocalJumpError do
def method
yield
end
method(&a &&= 0)
end
end
assert('empty condition in ternary expression parses correctly') do
assert_equal(() ? 1 : 2, 2)
end
assert('method call with exactly 127 arguments') do
def args_to_ary(*args)
args
end
assert_equal [0]*127, args_to_ary(
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
)
end
assert('nested empty heredoc') do
_, a = nil, <<B
#{<<A}
A
B
assert_equal "\n", a
end
assert('splat in case splat') do
a = *case
when 0
* = 1
end
assert_equal [1], a
end
assert('undef with 127 or more arguments') do
assert_raise NameError do
undef
a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,
a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,
a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,
a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a
end
end
assert('next in normal loop with 127 arguments') do
assert_raise NameError do
while true
next A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A
end
end
end
assert('negate literal register alignment') do
a = *case
when 0
-0.0
2
end
assert_equal [2], a
end
assert('register window of calls (#3783)') do
# NODE_FOR
assert_nothing_raised do
for i in []; end
end
# NODE_SYMBOLS
assert_nothing_raised do
%i(sym)
end
# NODE_SCALL
assert_nothing_raised do
Object.new&.__id__
end
# NODE_RESCUE with splat
assert_nothing_raised do
begin
raise
rescue *[Exception]
end
end
# NODE_CASE
assert_nothing_raised do
case 1
when nil
end
end
# NODE_CASE with splat
assert_nothing_raised do
case 1
when *nil
end
end
# NODE_HASH
assert_nothing_raised do
{}.merge(
0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, 7=>7, 8=>8, 9=>9,
10=>10, 11=>11, 12=>12, 13=>13, 14=>14, 15=>15, 16=>16, 17=>17, 18=>18, 19=>19,
20=>20, 21=>21, 22=>22, 23=>23, 24=>24, 25=>25, 26=>26, 27=>27, 28=>28, 29=>29,
30=>30, 31=>31, 32=>32, 33=>33, 34=>34, 35=>35, 36=>36, 37=>37, 38=>38, 39=>39,
40=>40, 41=>41, 42=>42, 43=>43, 44=>44, 45=>45, 46=>46, 47=>47, 48=>48, 49=>49,
50=>50, 51=>51, 52=>52, 53=>53, 54=>54, 55=>55, 56=>56, 57=>57, 58=>58, 59=>59,
60=>60, 61=>61, 62=>62, 63=>63, 64=>64, 65=>65, 66=>66, 67=>67, 68=>68, 69=>69,
70=>70, 71=>71, 72=>72, 73=>73, 74=>74, 75=>75, 76=>76, 77=>77, 78=>78, 79=>79,
80=>80, 81=>81, 82=>82, 83=>83, 84=>84, 85=>85, 86=>86, 87=>87, 88=>88, 89=>89,
90=>90, 91=>91, 92=>92, 93=>93, 94=>94, 95=>95, 96=>96, 97=>97, 98=>98, 99=>99,
100=>100, 101=>101, 102=>102, 103=>103, 104=>104, 105=>105, 106=>106, 107=>107, 108=>108, 109=>109,
110=>110, 111=>111, 112=>112, 113=>113, 114=>114, 115=>115, 116=>116, 117=>117, 118=>118, 119=>119,
120=>120, 121=>121, 122=>122, 123=>123, 124=>124, 125=>125, 126=>126)
end
# NODE_OP_ASGN
o = Object.new
class << o
attr_accessor :a
end
o.a = 1
assert_nothing_raised{ o.a += 1 }
o.a = 1
assert_nothing_raised{ o.a <<= 1 }
o.a = 1
assert_nothing_raised{ o.a &&= 1 }
o = { k: 1 }
assert_nothing_raised{ o[:k] += 1 }
o = { k: 1 }
assert_nothing_raised{ o[:k] <<= 1 }
o = { k: 1 }
assert_nothing_raised{ o[:k] &&= 1 }
o = { k: 1 }
assert_nothing_raised{ o[*[:k]] += 1 }
o = { k: 1 }
assert_nothing_raised{ o[*[:k]] <<= 1 }
o = { k: 1 }
assert_nothing_raised{ o[*[:k]] &&= 1 }
# NODE_YIELD
def check_node_yield
yield
end
assert_nothing_raised do
check_node_yield{}
end
# NODE_DXSTR
assert_raise(NotImplementedError){ `#{:dynamic}` }
# NODE_XSTR
assert_raise(NotImplementedError){ `static` }
# NODE_DREGX
class Regexp; end
assert_raise(NoMethodError){ /#{'dynamic'}tail/ }
assert_raise(NoMethodError){ /#{'dynamic'}tail/iu }
# NODE_REGX
assert_raise(NoMethodError){ /static/ }
assert_raise(NoMethodError){ /static/iu }
Object.remove_const :Regexp
# NODE_UNDEF
assert_nothing_raised do
class << Object.new
undef inspect
end
end
# NODE_ALIAS
assert_nothing_raised do
class << Object.new
alias inspect2 inspect
end
end
end
+80
View File
@@ -0,0 +1,80 @@
assert('Comparable#<', '15.3.3.2.1') do
class Foo
include Comparable
def <=>(x)
x
end
end
assert_false(Foo.new < 0)
assert_false(Foo.new < 1)
assert_true(Foo.new < -1)
assert_raise(ArgumentError){ Foo.new < nil }
end
assert('Comparable#<=', '15.3.3.2.2') do
class Foo
include Comparable
def <=>(x)
x
end
end
assert_true(Foo.new <= 0)
assert_false(Foo.new <= 1)
assert_true(Foo.new <= -1)
assert_raise(ArgumentError){ Foo.new <= nil }
end
assert('Comparable#==', '15.3.3.2.3') do
class Foo
include Comparable
def <=>(x)
0
end
end
assert_true(Foo.new == Foo.new)
end
assert('Comparable#>', '15.3.3.2.4') do
class Foo
include Comparable
def <=>(x)
x
end
end
assert_false(Foo.new > 0)
assert_true(Foo.new > 1)
assert_false(Foo.new > -1)
assert_raise(ArgumentError){ Foo.new > nil }
end
assert('Comparable#>=', '15.3.3.2.5') do
class Foo
include Comparable
def <=>(x)
x
end
end
assert_true(Foo.new >= 0)
assert_true(Foo.new >= 1)
assert_false(Foo.new >= -1)
assert_raise(ArgumentError){ Foo.new >= nil }
end
assert('Comparable#between?', '15.3.3.2.6') do
class Foo
include Comparable
def <=>(x)
x
end
end
c = Foo.new
assert_false(c.between?(-1, 1))
assert_false(c.between?(-1, -1))
assert_false(c.between?( 1, 1))
assert_true(c.between?( 1, -1))
assert_true(c.between?(0, 0))
end
+36
View File
@@ -0,0 +1,36 @@
##
# ensure Test
class EnsureYieldBreak
attr_reader :ensure_context
def try
yield
ensure
@ensure_context = self
end
end
assert('ensure - context - yield') do
yielder = EnsureYieldBreak.new
yielder.try do
end
assert_equal yielder, yielder.ensure_context
end
assert('ensure - context - yield and break') do
yielder = EnsureYieldBreak.new
yielder.try do
break
end
assert_equal yielder, yielder.ensure_context
end
assert('ensure - context - yield and return') do
yielder = EnsureYieldBreak.new
lambda do
yielder.try do
return
end
end.call
assert_equal yielder, yielder.ensure_context
end
+134
View File
@@ -0,0 +1,134 @@
##
# Enumerable ISO Test
assert('Enumerable', '15.3.2') do
assert_equal(Module, Enumerable.class)
end
assert('Enumerable#all?', '15.3.2.2.1') do
assert_true([1,2,3].all?)
assert_false([1,false,3].all?)
a = [2,4,6]
all = a.all? do |e|
e % 2 == 0
end
assert_true(all)
a = [2,4,7]
all = a.all? do |e|
e % 2 == 0
end
assert_false(all)
end
assert('Enumerable#any?', '15.3.2.2.2') do
assert_true([false,true,false].any?)
assert_false([false,false,false].any?)
a = [1,3,6]
any = a.any? do |e|
e % 2 == 0
end
assert_true(any)
a = [1,3,5]
any = a.any? do |e|
e % 2 == 0
end
assert_false(any)
end
assert('Enumerable#collect', '15.3.2.2.3') do
assert_true [1,2,3].collect { |i| i + i } == [2,4,6]
end
assert('Enumerable#detect', '15.3.2.2.4') do
assert_equal 1, [1,2,3].detect() { true }
assert_equal 'a', [1,2,3].detect(->{"a"}) { false }
end
assert('Array#each_with_index', '15.3.2.2.5') do
a = nil
b = nil
[1].each_with_index {|e,i| a = e; b = i}
assert_equal(1, a)
assert_equal(0, b)
end
assert('Enumerable#entries', '15.3.2.2.6') do
assert_equal([1], [1].entries)
end
assert('Enumerable#find', '15.3.2.2.7') do
assert_equal 1, [1,2,3].find() { true }
assert_equal 'a', [1,2,3].find(->{"a"}) { false }
end
assert('Enumerable#find_all', '15.3.2.2.8') do
assert_equal [2,4,6,8], [1,2,3,4,5,6,7,8,9].find_all() {|i| i%2 == 0}
end
assert('Enumerable#grep', '15.3.2.2.9') do
assert_equal [4,5,6], [1,2,3,4,5,6,7,8,9].grep(4..6)
end
assert('Enumerable#include?', '15.3.2.2.10') do
assert_true [1,2,3,4,5,6,7,8,9].include?(5)
assert_false [1,2,3,4,5,6,7,8,9].include?(0)
end
assert('Enumerable#inject', '15.3.2.2.11') do
assert_equal 21, [1,2,3,4,5,6].inject() {|s, n| s + n}
assert_equal 22, [1,2,3,4,5,6].inject(1) {|s, n| s + n}
end
assert('Enumerable#map', '15.3.2.2.12') do
assert_equal [2,4,6], [1,2,3].map { |i| i + i }
end
assert('Enumerable#max', '15.3.2.2.13') do
a = ['aaa', 'bb', 'c']
assert_equal 'c', a.max
assert_equal 'aaa', a.max {|i1,i2| i1.length <=> i2.length}
end
assert('Enumerable#min', '15.3.2.2.14') do
a = ['aaa', 'bb', 'c']
assert_equal 'aaa', a.min
assert_equal 'c', a.min {|i1,i2| i1.length <=> i2.length}
end
assert('Enumerable#member?', '15.3.2.2.15') do
assert_true [1,2,3,4,5,6,7,8,9].member?(5)
assert_false [1,2,3,4,5,6,7,8,9].member?(0)
end
assert('Enumerable#partition', '15.3.2.2.16') do
partition = [0,1,2,3,4,5,6,7,8,9].partition do |i|
i % 2 == 0
end
assert_equal [[0,2,4,6,8], [1,3,5,7,9]], partition
end
assert('Enumerable#reject', '15.3.2.2.17') do
reject = [0,1,2,3,4,5,6,7,8,9].reject do |i|
i % 2 == 0
end
assert_equal [1,3,5,7,9], reject
end
assert('Enumerable#select', '15.3.2.2.18') do
assert_equal [2,4,6,8], [1,2,3,4,5,6,7,8,9].select() {|i| i%2 == 0}
end
assert('Enumerable#sort', '15.3.2.2.19') do
assert_equal [1,2,3,4,6,7], [7,3,1,2,6,4].sort
assert_equal [7,6,4,3,2,1], [7,3,1,2,6,4].sort {|e1,e2|e2<=>e1}
end
assert('Enumerable#to_a', '15.3.2.2.20') do
assert_equal [1], [1].to_a
end
+424
View File
@@ -0,0 +1,424 @@
##
# Exception ISO Test
assert('Exception', '15.2.22') do
assert_equal Class, Exception.class
end
assert('Exception.exception', '15.2.22.4.1') do
e = Exception.exception('a')
assert_equal Exception, e.class
end
assert('Exception#exception', '15.2.22.5.1') do
e = Exception.new
re = RuntimeError.new
assert_equal e, e.exception
assert_equal e, e.exception(e)
assert_equal re, re.exception(re)
changed_re = re.exception('message has changed')
assert_not_equal re, changed_re
assert_equal 'message has changed', changed_re.message
end
assert('Exception#message', '15.2.22.5.2') do
e = Exception.exception('a')
assert_equal 'a', e.message
end
assert('Exception#to_s', '15.2.22.5.3') do
e = Exception.exception('a')
assert_equal 'a', e.to_s
end
assert('Exception.exception', '15.2.22.4.1') do
e = Exception.exception()
e.initialize('a')
assert_equal 'a', e.message
end
assert('NameError', '15.2.31') do
assert_raise(NameError) do
raise NameError.new
end
e = NameError.new "msg", "name"
assert_equal "msg", e.message
assert_equal "name", e.name
end
assert('ScriptError', '15.2.37') do
assert_raise(ScriptError) do
raise ScriptError.new
end
end
assert('SyntaxError', '15.2.38') do
assert_raise(SyntaxError) do
raise SyntaxError.new
end
end
# Not ISO specified
assert('Exception 1') do
r=begin
1+1
ensure
2+2
end
assert_equal 2, r
end
assert('Exception 2') do
r=begin
1+1
begin
2+2
ensure
3+3
end
ensure
4+4
end
assert_equal 4, r
end
assert('Exception 3') do
r=begin
1+1
begin
2+2
ensure
3+3
end
ensure
4+4
begin
5+5
ensure
6+6
end
end
assert_equal 4, r
end
assert('Exception 4') do
a = nil
1.times{|e|
begin
rescue => err
end
a = err.class
}
assert_equal NilClass, a
end
assert('Exception 5') do
$ans = []
def m
$!
end
def m2
1.times{
begin
return
ensure
$ans << m
end
}
end
m2
assert_equal [nil], $ans
end
assert('Exception 6') do
$i = 0
def m
iter{
begin
$i += 1
begin
$i += 2
break
ensure
end
ensure
$i += 4
end
$i = 0
}
end
def iter
yield
end
m
assert_equal 7, $i
end
assert('Exception 7') do
$i = 0
def m
begin
$i += 1
begin
$i += 2
return
ensure
$i += 3
end
ensure
$i += 4
end
p :end
end
m
assert_equal 10, $i
end
assert('Exception 8') do
r=begin
1
rescue
2
else
3
end
assert_equal 3, r
end
assert('Exception 9') do
r=begin
1+1
rescue
2+2
else
3+3
ensure
4+4
end
assert_equal 6, r
end
assert('Exception 10') do
r=begin
1+1
begin
2+2
rescue
3+3
else
4+4
end
rescue
5+5
else
6+6
ensure
7+7
end
assert_equal 12, r
end
assert('Exception 11') do
a = :ok
begin
begin
raise Exception
rescue
a = :ng
end
rescue Exception
end
assert_equal :ok, a
end
assert('Exception 12') do
a = :ok
begin
raise Exception rescue a = :ng
rescue Exception
end
assert_equal :ok, a
end
assert('Exception 13') do
a = :ng
begin
raise StandardError
rescue TypeError, ArgumentError
a = :ng
rescue
a = :ok
else
a = :ng
end
assert_equal :ok, a
end
assert('Exception 14') do
def (o = Object.new).exception_test14; UnknownConstant end
a = :ng
begin
o.__send__(:exception_test14)
rescue
a = :ok
end
assert_equal :ok, a
end
assert('Exception 15') do
a = begin
:ok
rescue
:ko
end
assert_equal :ok, a
end
assert('Exception 16') do
begin
raise "foo"
false
rescue => e
assert_equal "foo", e.message
end
end
assert('Exception 17') do
r=begin
raise "a" # RuntimeError
rescue ArgumentError
1
rescue StandardError
2
else
3
ensure
4
end
assert_equal 2, r
end
assert('Exception 18') do
r=begin
0
rescue ArgumentError
1
rescue StandardError
2
else
3
ensure
4
end
assert_equal 3, r
end
assert('Exception 19') do
class Class4Exception19
def a
r = @e = false
begin
b
rescue TypeError
r = self.z
end
[ r, @e ]
end
def b
begin
1 * "b"
ensure
@e = self.zz
end
end
def zz
true
end
def z
true
end
end
assert_equal [true, true], Class4Exception19.new.a
end
assert('Exception#inspect') do
assert_equal "Exception", Exception.new.inspect
assert_equal "Exception", Exception.new("").inspect
assert_equal "error! (Exception)", Exception.new("error!").inspect
end
assert('Exception#backtrace') do
assert_nothing_raised do
begin
raise "get backtrace"
rescue => e
e.backtrace
end
end
end
assert('Raise in ensure') do
assert_raise(ArgumentError) do
begin
raise "" # RuntimeError
ensure
raise ArgumentError
end
end
end
def backtrace_available?
begin
raise "XXX"
rescue => exception
not exception.backtrace.empty?
end
end
assert('GC in rescue') do
skip "backtrace isn't available" unless backtrace_available?
line = nil
begin
[1].each do
[2].each do
[3].each do
line = __LINE__; raise "XXX"
end
end
end
rescue => exception
GC.start
assert_equal("#{__FILE__}:#{line}",
exception.backtrace.first)
end
end
assert('Method call in rescue') do
skip "backtrace isn't available" unless backtrace_available?
line = nil
begin
[1].each do
[2].each do
line = __LINE__; raise "XXX"
end
end
rescue => exception
[3].each do
end
assert_equal("#{__FILE__}:#{line}",
exception.backtrace.first)
end
end
+31
View File
@@ -0,0 +1,31 @@
##
# FalseClass ISO Test
assert('FalseClass', '15.2.6') do
assert_equal Class, FalseClass.class
end
assert('FalseClass false', '15.2.6.1') do
assert_false false
assert_equal FalseClass, false.class
assert_false FalseClass.method_defined? :new
end
assert('FalseClass#&', '15.2.6.3.1') do
assert_false false.&(true)
assert_false false.&(false)
end
assert('FalseClass#^', '15.2.6.3.2') do
assert_true false.^(true)
assert_false false.^(false)
end
assert('FalseClass#to_s', '15.2.6.3.3') do
assert_equal 'false', false.to_s
end
assert('FalseClass#|', '15.2.6.3.4') do
assert_true false.|(true)
assert_false false.|(false)
end
+248
View File
@@ -0,0 +1,248 @@
##
# Float ISO Test
if Object.const_defined?(:Float)
assert('Float', '15.2.9') do
assert_equal Class, Float.class
end
assert('Float#+', '15.2.9.3.1') do
a = 3.123456788 + 0.000000001
b = 3.123456789 + 1
assert_float(3.123456789, a)
assert_float(4.123456789, b)
assert_raise(TypeError){ 0.0+nil }
assert_raise(TypeError){ 1.0+nil }
end
assert('Float#-', '15.2.9.3.2') do
a = 3.123456790 - 0.000000001
b = 5.123456789 - 1
assert_float(3.123456789, a)
assert_float(4.123456789, b)
end
assert('Float#*', '15.2.9.3.3') do
a = 3.125 * 3.125
b = 3.125 * 1
assert_float(9.765625, a)
assert_float(3.125 , b)
end
assert('Float#/', '15.2.9.3.4') do
a = 3.123456789 / 3.123456789
b = 3.123456789 / 1
assert_float(1.0 , a)
assert_float(3.123456789, b)
end
assert('Float#%', '15.2.9.3.5') do
a = 3.125 % 3.125
b = 3.125 % 1
assert_float(0.0 , a)
assert_float(0.125, b)
end
assert('Float#<=>', '15.2.9.3.6') do
a = 3.125 <=> 3.123
b = 3.125 <=> 3.125
c = 3.125 <=> 3.126
a2 = 3.125 <=> 3
c2 = 3.125 <=> 4
assert_equal( 1, a)
assert_equal( 0, b)
assert_equal(-1, c)
assert_equal( 1, a2)
assert_equal(-1, c2)
end
assert('Float#==', '15.2.9.3.7') do
assert_true 3.1 == 3.1
assert_false 3.1 == 3.2
end
assert('Float#ceil', '15.2.9.3.8') do
a = 3.123456789.ceil
b = 3.0.ceil
c = -3.123456789.ceil
d = -3.0.ceil
assert_equal( 4, a)
assert_equal( 3, b)
assert_equal(-3, c)
assert_equal(-3, d)
end
assert('Float#finite?', '15.2.9.3.9') do
assert_predicate 3.123456789, :finite?
assert_not_predicate 1.0 / 0.0, :finite?
end
assert('Float#floor', '15.2.9.3.10') do
a = 3.123456789.floor
b = 3.0.floor
c = -3.123456789.floor
d = -3.0.floor
assert_equal( 3, a)
assert_equal( 3, b)
assert_equal(-4, c)
assert_equal(-3, d)
end
assert('Float#infinite?', '15.2.9.3.11') do
a = 3.123456789.infinite?
b = (1.0 / 0.0).infinite?
c = (-1.0 / 0.0).infinite?
assert_nil a
assert_equal( 1, b)
assert_equal(-1, c)
end
assert('Float#round', '15.2.9.3.12') do
a = 3.123456789.round
b = 3.5.round
c = 3.4999.round
d = (-3.123456789).round
e = (-3.5).round
f = 12345.67.round(-1)
g = 3.423456789.round(0)
h = 3.423456789.round(1)
i = 3.423456789.round(3)
assert_equal( 3, a)
assert_equal( 4, b)
assert_equal( 3, c)
assert_equal( -3, d)
assert_equal( -4, e)
assert_equal(12350, f)
assert_equal( 3, g)
assert_float( 3.4, h)
assert_float(3.423, i)
assert_equal(42.0, 42.0.round(307))
assert_equal(1.0e307, 1.0e307.round(2))
inf = 1.0/0.0
assert_raise(FloatDomainError){ inf.round }
assert_raise(FloatDomainError){ inf.round(-1) }
assert_equal(inf, inf.round(1))
nan = 0.0/0.0
assert_raise(FloatDomainError){ nan.round }
assert_raise(FloatDomainError){ nan.round(-1) }
assert_predicate(nan.round(1), :nan?)
end
assert('Float#to_f', '15.2.9.3.13') do
a = 3.123456789
assert_float(a, a.to_f)
end
assert('Float#to_i', '15.2.9.3.14') do
assert_equal(3, 3.123456789.to_i)
assert_raise(FloatDomainError) { Float::INFINITY.to_i }
assert_raise(FloatDomainError) { (-Float::INFINITY).to_i }
assert_raise(FloatDomainError) { Float::NAN.to_i }
end
assert('Float#truncate', '15.2.9.3.15') do
assert_equal( 3, 3.123456789.truncate)
assert_equal(-3, -3.1.truncate)
end
assert('Float#divmod') do
def check_floats exp, act
assert_float exp[0], act[0]
assert_float exp[1], act[1]
end
# Note: quotients are Float because mruby does not have Bignum.
check_floats [ 0, 0.0], 0.0.divmod(1)
check_floats [ 0, 1.1], 1.1.divmod(3)
check_floats [ 3, 0.2], 3.2.divmod(1)
check_floats [ 2, 6.3], 20.3.divmod(7)
check_floats [-1, 1.6], -3.4.divmod(5)
check_floats [-2, -0.5], 25.5.divmod(-13)
check_floats [ 1, -6.6], -13.6.divmod(-7)
check_floats [ 3, 0.2], 9.8.divmod(3.2)
end
assert('Float#nan?') do
assert_predicate(0.0/0.0, :nan?)
assert_not_predicate(0.0, :nan?)
assert_not_predicate(1.0/0.0, :nan?)
assert_not_predicate(-1.0/0.0, :nan?)
end
assert('Float#<<') do
# Left Shift by one
assert_equal 46, 23.0 << 1
# Left Shift by a negative is Right Shift
assert_equal 23, 46.0 << -1
end
assert('Float#>>') do
# Right Shift by one
assert_equal 23, 46.0 >> 1
# Right Shift by a negative is Left Shift
assert_equal 46, 23.0 >> -1
# Don't raise on large Right Shift
assert_equal 0, 23.0 >> 128
# Don't raise on large Right Shift
assert_equal(-1, -23.0 >> 128)
end
assert('Float#to_s') do
uses_float = 4e38.infinite? # enable MRB_USE_FLOAT?
assert_equal("Infinity", Float::INFINITY.to_s)
assert_equal("-Infinity", (-Float::INFINITY).to_s)
assert_equal("NaN", Float::NAN.to_s)
assert_equal("0", 0.0.to_s)
assert_equal("-0", -0.0.to_s)
assert_equal("-3.25", -3.25.to_s)
assert_equal("50", 50.0.to_s)
assert_equal("0.0125", 0.0125.to_s)
assert_equal("-0.0125", -0.0125.to_s)
assert_equal("1.0e-10", 0.0000000001.to_s)
assert_equal("-1.0e-10", -0.0000000001.to_s)
assert_equal("1.0e+20", 1e20.to_s)
assert_equal("-1.0e+20", -1e20.to_s)
assert_equal("1.0e+16", 10000000000000000.0.to_s)
assert_equal("-1.0e+16", -10000000000000000.0.to_s)
assert_equal("100000", 100000.0.to_s)
assert_equal("-100000", -100000.0.to_s)
if uses_float
assert_equal("1.0e+08", 100000000.0.to_s)
assert_equal("-1.0e+08", -100000000.0.to_s)
assert_equal("1.0e+07", 10000000.0.to_s)
assert_equal("-1.0e+07", -10000000.0.to_s)
else
assert_equal("1.0e+15", 1000000000000000.0.to_s)
assert_equal("-1.0e+15", -1000000000000000.0.to_s)
assert_equal("100000000000000", 100000000000000.0.to_s)
assert_equal("-100000000000000", -100000000000000.0.to_s)
end
end
assert('Float#eql?') do
assert_operator(5.0, :eql?, 5.0)
assert_not_operator(5.0, :eql?, 5)
assert_not_operator(5.0, :eql?, "5.0")
end
end # const_defined?(:Float)
+45
View File
@@ -0,0 +1,45 @@
# Not ISO specified
assert('GC.enable') do
assert_false GC.disable
assert_true GC.enable
assert_false GC.enable
end
assert('GC.disable') do
begin
assert_false GC.disable
assert_true GC.disable
ensure
GC.enable
end
end
assert('GC.interval_ratio=') do
origin = GC.interval_ratio
begin
assert_equal 150, (GC.interval_ratio = 150)
ensure
GC.interval_ratio = origin
end
end
assert('GC.step_ratio=') do
origin = GC.step_ratio
begin
assert_equal 150, (GC.step_ratio = 150)
ensure
GC.step_ratio = origin
end
end
assert('GC.generational_mode=') do
origin = GC.generational_mode
begin
assert_false (GC.generational_mode = false)
assert_true (GC.generational_mode = true)
assert_true (GC.generational_mode = true)
ensure
GC.generational_mode = origin
end
end
+378
View File
@@ -0,0 +1,378 @@
##
# Hash ISO Test
assert('Hash', '15.2.13') do
assert_equal Class, Hash.class
end
assert('Hash#==', '15.2.13.4.1') do
assert_true({ 'abc' => 'abc' } == { 'abc' => 'abc' })
assert_false({ 'abc' => 'abc' } == { 'cba' => 'cba' })
assert_false({ :a => 1 } == true)
skip unless Object.const_defined?(:Float)
assert_true({ :equal => 1 } == { :equal => 1.0 })
end
assert('Hash#[]', '15.2.13.4.2') do
a = { 'abc' => 'abc' }
assert_equal 'abc', a['abc']
# Hash#[] should call #default (#3272)
hash = {}
def hash.default(k); self[k] = 1; end
hash[:foo] += 1
assert_equal 2, hash[:foo]
end
assert('Hash#[]=', '15.2.13.4.3') do
a = Hash.new
a['abc'] = 'abc'
assert_equal 'abc', a['abc']
end
assert('Hash#clear', '15.2.13.4.4') do
a = { 'abc' => 'abc' }
a.clear
assert_equal({ }, a)
end
assert('Hash#dup') do
a = { 'a' => 1 }
b = a.dup
a['a'] = 2
assert_equal({'a' => 1}, b)
c = Hash.new { |h, k| h[k] = k.upcase }
d = c.dup
assert_equal("FOO", d["foo"])
end
assert('Hash#default', '15.2.13.4.5') do
a = Hash.new
b = Hash.new('abc')
c = Hash.new {|s,k| s[k] = k}
assert_nil a.default
assert_equal 'abc', b.default
assert_nil c.default
assert_equal 'abc', c.default('abc')
end
assert('Hash#default=', '15.2.13.4.6') do
a = { 'abc' => 'abc' }
a.default = 'cba'
assert_equal 'abc', a['abc']
assert_equal 'cba', a['notexist']
end
assert('Hash#default_proc', '15.2.13.4.7') do
a = Hash.new
b = Hash.new {|s,k| s[k] = k + k}
c = b[2]
d = b['cat']
assert_nil a.default_proc
assert_equal Proc, b.default_proc.class
assert_equal 4, c
assert_equal 'catcat', d
end
assert('Hash#delete', '15.2.13.4.8') do
a = { 'abc' => 'ABC' }
b = { 'abc' => 'ABC' }
b_tmp_1 = false
b_tmp_2 = false
assert_equal 'ABC', a.delete('abc')
b.delete('abc') do |k|
b_tmp_1 = true
end
b.delete('abc') do |k|
b_tmp_2 = true
end
assert_nil a.delete('cba')
assert_false a.has_key?('abc')
assert_false b_tmp_1
assert_true b_tmp_2
end
assert('Hash#each', '15.2.13.4.9') do
a = { 'abc_key' => 'abc_value' }
key = nil
value = nil
a.each do |k,v|
key = k
value = v
end
assert_equal 'abc_key', key
assert_equal 'abc_value', value
end
assert('Hash#each_key', '15.2.13.4.10') do
a = { 'abc_key' => 'abc_value' }
key = nil
a.each_key do |k|
key = k
end
assert_equal 'abc_key', key
end
assert('Hash#each_value', '15.2.13.4.11') do
a = { 'abc_key' => 'abc_value' }
value = nil
a.each_value do |v|
value = v
end
assert_equal 'abc_value', value
end
assert('Hash#empty?', '15.2.13.4.12') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_false a.empty?
assert_true b.empty?
end
assert('Hash#has_key?', '15.2.13.4.13') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_true a.has_key?('abc_key')
assert_false b.has_key?('cba')
end
assert('Hash#has_value?', '15.2.13.4.14') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_true a.has_value?('abc_value')
assert_false b.has_value?('cba')
end
assert('Hash#include?', '15.2.13.4.15') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_true a.include?('abc_key')
assert_false b.include?('cba')
end
assert('Hash#initialize', '15.2.13.4.16') do
# Testing initialize by new.
h = Hash.new
h2 = Hash.new(:not_found)
assert_true h.is_a? Hash
assert_equal({ }, h)
assert_nil h["hello"]
assert_equal :not_found, h2["hello"]
end
assert('Hash#initialize_copy', '15.2.13.4.17') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new.initialize_copy(a)
assert_equal({ 'abc_key' => 'abc_value' }, b)
end
assert('Hash#key?', '15.2.13.4.18') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_true a.key?('abc_key')
assert_false b.key?('cba')
end
assert('Hash#keys', '15.2.13.4.19') do
a = { 'abc_key' => 'abc_value' }
assert_equal ['abc_key'], a.keys
end
assert('Hash#length', '15.2.13.4.20') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_equal 1, a.length
assert_equal 0, b.length
end
assert('Hash#member?', '15.2.13.4.21') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_true a.member?('abc_key')
assert_false b.member?('cba')
end
assert('Hash#merge', '15.2.13.4.22') do
a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
b = { 'cba_key' => 'XXX', 'xyz_key' => 'xyz_value' }
result_1 = a.merge b
result_2 = a.merge(b) do |key, original, new|
original
end
assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'XXX',
'xyz_key' => 'xyz_value' }, result_1)
assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value',
'xyz_key' => 'xyz_value' }, result_2)
assert_raise(TypeError) do
{ 'abc_key' => 'abc_value' }.merge "a"
end
end
assert('Hash#replace', '15.2.13.4.23') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new.replace(a)
assert_equal({ 'abc_key' => 'abc_value' }, b)
a = Hash.new(42)
b = {}
b.replace(a)
assert_equal(42, b[1])
a = Hash.new{|h,x| x}
b.replace(a)
assert_equal(127, b[127])
assert_raise(TypeError) do
{ 'abc_key' => 'abc_value' }.replace "a"
end
end
assert('Hash#shift', '15.2.13.4.24') do
a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
b = a.shift
assert_equal Array, b.class
assert_equal 2, b.size
assert_equal 1, a.size
b = a.shift
assert_equal Array, b.class
assert_equal 2, b.size
assert_equal 0, a.size
end
assert('Hash#size', '15.2.13.4.25') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_equal 1, a.size
assert_equal 0, b.size
end
assert('Hash#store', '15.2.13.4.26') do
a = Hash.new
a.store('abc', 'abc')
assert_equal 'abc', a['abc']
end
assert('Hash#value?', '15.2.13.4.27') do
a = { 'abc_key' => 'abc_value' }
b = Hash.new
assert_true a.value?('abc_value')
assert_false b.value?('cba')
end
assert('Hash#values', '15.2.13.4.28') do
a = { 'abc_key' => 'abc_value' }
assert_equal ['abc_value'], a.values
end
# Not ISO specified
assert('Hash#eql?') do
a = { 'a' => 1, 'b' => 2, 'c' => 3 }
b = { 'a' => 1, 'b' => 2, 'c' => 3 }
c = { 'a' => 1.0, 'b' => 2, 'c' => 3 }
assert_true(a.eql?(b))
assert_false(a.eql?(c))
assert_false(a.eql?(true))
end
assert('Hash#reject') do
h = {:one => 1, :two => 2, :three => 3, :four => 4}
ret = h.reject do |k,v|
v % 2 == 0
end
assert_equal({:one => 1, :three => 3}, ret)
assert_equal({:one => 1, :two => 2, :three => 3, :four => 4}, h)
end
assert('Hash#reject!') do
h = {:one => 1, :two => 2, :three => 3, :four => 4}
ret = h.reject! do |k,v|
v % 2 == 0
end
assert_equal({:one => 1, :three => 3}, ret)
assert_equal({:one => 1, :three => 3}, h)
end
assert('Hash#select') do
h = {:one => 1, :two => 2, :three => 3, :four => 4}
ret = h.select do |k,v|
v % 2 == 0
end
assert_equal({:two => 2, :four => 4}, ret)
assert_equal({:one => 1, :two => 2, :three => 3, :four => 4}, h)
end
assert('Hash#select!') do
h = {:one => 1, :two => 2, :three => 3, :four => 4}
ret = h.select! do |k,v|
v % 2 == 0
end
assert_equal({:two => 2, :four => 4}, ret)
assert_equal({:two => 2, :four => 4}, h)
end
# Not ISO specified
assert('Hash#inspect') do
h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
h["recur"] = h
ret = h.to_s
assert_include ret, '"c"=>300'
assert_include ret, '"a"=>100'
assert_include ret, '"d"=>400'
assert_include ret, '"recur"=>{...}'
end
assert('Hash#rehash') do
h = {[:a] => "b"}
# hash key modified
h.keys[0][0] = :b
# h[[:b]] => nil
h.rehash
assert_equal("b", h[[:b]])
end
assert('Hash#freeze') do
h = {}.freeze
assert_raise(FrozenError) do
h[:a] = 'b'
end
end
+6
View File
@@ -0,0 +1,6 @@
##
# IndexError ISO Test
assert('IndexError', '15.2.33') do
assert_equal Class, IndexError.class
end
+267
View File
@@ -0,0 +1,267 @@
##
# Integer ISO Test
assert('Integer', '15.2.8') do
assert_equal Class, Integer.class
end
assert('Integer#+', '15.2.8.3.1') do
a = 1+1
b = 1+1.0 if Object.const_defined?(:Float)
assert_equal 2, a
assert_equal 2.0, b if Object.const_defined?(:Float)
assert_raise(TypeError){ 0+nil }
assert_raise(TypeError){ 1+nil }
c = Mrbtest::FIXNUM_MAX + 1
d = Mrbtest::FIXNUM_MAX.__send__(:+, 1)
skip unless Object.const_defined?(:Float)
e = Mrbtest::FIXNUM_MAX + 1.0
assert_equal Float, c.class
assert_equal Float, d.class
assert_float e, c
assert_float e, d
end
assert('Integer#-', '15.2.8.3.2') do
a = 2-1
b = 2-1.0 if Object.const_defined?(:Float)
assert_equal 1, a
assert_equal 1.0, b if Object.const_defined?(:Float)
c = Mrbtest::FIXNUM_MIN - 1
d = Mrbtest::FIXNUM_MIN.__send__(:-, 1)
skip unless Object.const_defined?(:Float)
e = Mrbtest::FIXNUM_MIN - 1.0
assert_equal Float, c.class
assert_equal Float, d.class
assert_float e, c
assert_float e, d
end
assert('Integer#*', '15.2.8.3.3') do
a = 1*1
b = 1*1.0 if Object.const_defined?(:Float)
assert_equal 1, a
assert_equal 1.0, b if Object.const_defined?(:Float)
assert_raise(TypeError){ 0*nil }
assert_raise(TypeError){ 1*nil }
c = Mrbtest::FIXNUM_MAX * 2
d = Mrbtest::FIXNUM_MAX.__send__(:*, 2)
skip unless Object.const_defined?(:Float)
e = Mrbtest::FIXNUM_MAX * 2.0
assert_equal Float, c.class
assert_equal Float, d.class
assert_float e, c
assert_float e, d
end
assert('Integer#/', '15.2.8.3.4') do
a = 2/1
b = 2/1.0
assert_equal 2, a
assert_equal 2.0, b
end
assert('Integer#%', '15.2.8.3.5') do
a = 1%1
b = 1%1.0
c = 2%4
d = 2%5
e = 2%-5
f = -2%5
g = -2%-5
h = 2%-2
i = -2%2
j = -2%-2
assert_equal 0, a
assert_equal 0.0, b
assert_equal 2, c
assert_equal 2, d
assert_equal(-3, e)
assert_equal 3, f
assert_equal(-2, g)
assert_equal 0, h
assert_equal 0, i
assert_equal 0, j
end
assert('Integer#<=>', '15.2.9.3.6') do
a = 1<=>0
b = 1<=>1
c = 1<=>2
assert_equal 1, a
assert_equal 0, b
assert_equal(-1, c)
end
assert('Integer#==', '15.2.8.3.7') do
a = 1==0
b = 1==1
assert_false a
assert_true b
end
assert('Integer#~', '15.2.8.3.8') do
# Complement
assert_equal(-1, ~0)
assert_equal(-3, ~2)
end
assert('Integer#&', '15.2.8.3.9') do
# Bitwise AND
# 0101 (5)
# & 0011 (3)
# = 0001 (1)
assert_equal 1, 5 & 3
end
assert('Integer#|', '15.2.8.3.10') do
# Bitwise OR
# 0101 (5)
# | 0011 (3)
# = 0111 (7)
assert_equal 7, 5 | 3
end
assert('Integer#^', '15.2.8.3.11') do
# Bitwise XOR
# 0101 (5)
# ^ 0011 (3)
# = 0110 (6)
assert_equal 6, 5 ^ 3
end
assert('Integer#<<', '15.2.8.3.12') do
# Left Shift by one
# 00010111 (23)
# = 00101110 (46)
assert_equal 46, 23 << 1
# Left Shift by a negative is Right Shift
assert_equal 23, 46 << -1
skip unless Object.const_defined?(:Float)
# Overflow to Fixnum
assert_float 9223372036854775808.0, 1 << 63
assert_float(-13835058055282163712.0, -3 << 62)
end
assert('Integer#>>', '15.2.8.3.13') do
# Right Shift by one
# 00101110 (46)
# = 00010111 (23)
assert_equal 23, 46 >> 1
# Right Shift by a negative is Left Shift
assert_equal 46, 23 >> -1
# Don't raise on large Right Shift
assert_equal 0, 23 >> 128
end
assert('Integer#ceil', '15.2.8.3.14') do
assert_equal 10, 10.ceil
end
assert('Integer#downto', '15.2.8.3.15') do
a = 0
3.downto(1) do |i|
a += i
end
assert_equal 6, a
end
assert('Integer#eql?', '15.2.8.3.16') do
a = 1.eql?(1)
b = 1.eql?(2)
c = 1.eql?(nil)
assert_true a
assert_false b
assert_false c
end
assert('Integer#floor', '15.2.8.3.17') do
a = 1.floor
assert_equal 1, a
end
assert('Integer#next', '15.2.8.3.19') do
assert_equal 2, 1.next
end
assert('Integer#round', '15.2.8.3.20') do
assert_equal 1, 1.round
end
assert('Integer#succ', '15.2.8.3.21') do
assert_equal 2, 1.succ
end
assert('Integer#times', '15.2.8.3.22') do
a = 0
3.times do
a += 1
end
assert_equal 3, a
end
assert('Integer#to_f', '15.2.8.3.23') do
skip unless Object.const_defined?(:Float)
assert_equal 1.0, 1.to_f
end
assert('Integer#to_i', '15.2.8.3.24') do
assert_equal 1, 1.to_i
end
assert('Integer#to_s', '15.2.8.3.25') do
assert_equal "1", 1.to_s
assert_equal "-1", -1.to_s
assert_equal "1010", 10.to_s(2)
assert_equal "a", 10.to_s(36)
assert_equal "-a", -10.to_s(36)
assert_equal "30071", 12345.to_s(8)
assert_raise(ArgumentError) { 10.to_s(-1) }
assert_raise(ArgumentError) { 10.to_s(0) }
assert_raise(ArgumentError) { 10.to_s(1) }
assert_raise(ArgumentError) { 10.to_s(37) }
end
assert('Integer#truncate', '15.2.8.3.26') do
assert_equal 1, 1.truncate
end
assert('Integer#upto', '15.2.8.3.27') do
a = 0
1.upto(3) do |i|
a += i
end
assert_equal 6, a
end
assert('Integer#divmod', '15.2.8.3.30') do
assert_equal [ 0, 0], 0.divmod(1)
assert_equal [ 0, 1], 1.divmod(3)
assert_equal [ 3, 0], 3.divmod(1)
assert_equal [ 2, 6], 20.divmod(7)
assert_equal [-1, 2], -3.divmod(5)
assert_equal [-2, -1], 25.divmod(-13)
assert_equal [ 1, -6], -13.divmod(-7)
end
+61
View File
@@ -0,0 +1,61 @@
assert('while expression', '11.5.2.3.2') do
idx = 10
all = []
res = while idx > 0
all << idx
idx -= 1
end
assert_equal nil, res
assert_equal [10,9,8,7,6,5,4,3,2,1], all
end
assert('until expression', '11.5.2.3.3') do
idx = 10
all = []
res = until idx == 0
all << idx
idx -= 1
end
assert_equal nil, res
assert_equal [10,9,8,7,6,5,4,3,2,1], all
end
assert('break expression', '11.5.2.4.3') do
assert_equal :result do
while true
break :result
end
end
assert_equal :result do
until false
break :result
end
end
end
assert('next expression', '11.5.2.4.4') do
assert_equal [8,6,4,2,0] do
all = []
idx = 10
while idx > 0
idx -= 1
next if (idx % 2) == 1
all << idx
end
all
end
assert_equal [8,6,4,2,0] do
all = []
idx = 10
until idx == 0
idx -= 1
next if (idx % 2) == 1
all << idx
end
all
end
end
+498
View File
@@ -0,0 +1,498 @@
##
# Kernel ISO Test
assert('Kernel', '15.3.1') do
assert_equal Module, Kernel.class
end
assert('Kernel.block_given?', '15.3.1.2.2') do
def bg_try(&b)
if Kernel.block_given?
yield
else
"no block"
end
end
assert_false Kernel.block_given?
# test without block
assert_equal "no block", bg_try
# test with block
assert_equal "block" do
bg_try { "block" }
end
# test with block
assert_equal "block" do
bg_try do
"block"
end
end
end
# Kernel.eval is provided by the mruby-eval mrbgem. '15.3.1.2.3'
assert('Kernel.iterator?', '15.3.1.2.5') do
assert_false Kernel.iterator?
end
assert('Kernel.lambda', '15.3.1.2.6') do
l = Kernel.lambda do
true
end
m = Kernel.lambda(&l)
assert_true l.call
assert_equal Proc, l.class
assert_true m.call
assert_equal Proc, m.class
end
assert('Kernel.loop', '15.3.1.2.8') do
i = 0
Kernel.loop do
i += 1
break if i == 100
end
assert_equal 100, i
end
# Kernel.p is provided by the mruby-print mrbgem. '15.3.1.2.9'
# Kernel.print is provided by the mruby-print mrbgem. '15.3.1.2.10'
# Kernel.puts is provided by the mruby-print mrbgem. '15.3.1.2.11'
assert('Kernel.raise', '15.3.1.2.12') do
assert_raise RuntimeError do
Kernel.raise
end
assert_raise RuntimeError do
Kernel.raise RuntimeError.new
end
end
assert('Kernel#__id__', '15.3.1.3.3') do
assert_equal Fixnum, __id__.class
end
assert('Kernel#__send__', '15.3.1.3.4') do
# test with block
l = __send__(:lambda) do
true
end
assert_true l.call
assert_equal Proc, l.class
# test with argument
assert_true __send__(:respond_to?, :nil?)
# test without argument and without block
assert_equal String, __send__(:to_s).class
args = [:respond_to?, :nil?]
assert_true __send__(*args)
assert_equal [:respond_to?, :nil?], args
end
assert('Kernel#block_given?', '15.3.1.3.6') do
def bg_try(&b)
if block_given?
yield
else
"no block"
end
end
assert_false block_given?
assert_equal "no block", bg_try
assert_equal "block" do
bg_try { "block" }
end
assert_equal "block" do
bg_try do
"block"
end
end
def bg_try_in_block
-> { block_given? }[]
end
assert_false bg_try_in_block
assert_true bg_try_in_block{}
end
assert('Kernel#class', '15.3.1.3.7') do
assert_equal Module, Kernel.class
end
assert('Kernel#clone', '15.3.1.3.8') do
class KernelCloneTest
def initialize
@v = 0
end
def get
@v
end
def set(v)
@v = v
end
end
a = KernelCloneTest.new
a.set(1)
b = a.clone
def a.test
end
a.set(2)
c = a.clone
immutables = [ 1, :foo, true, false, nil ]
error_count = 0
immutables.each do |i|
begin
i.clone
rescue TypeError
error_count += 1
end
end
assert_equal 2, a.get
assert_equal 1, b.get
assert_equal 2, c.get
assert_true a.respond_to?(:test)
assert_false b.respond_to?(:test)
assert_true c.respond_to?(:test)
a.freeze
d = a.clone
assert_true d.frozen?
end
assert('Kernel#dup', '15.3.1.3.9') do
class KernelDupTest
def initialize
@v = 0
end
def get
@v
end
def set(v)
@v = v
end
end
a = KernelDupTest.new
a.set(1)
b = a.dup
def a.test
end
a.set(2)
c = a.dup
assert_equal 2, a.get
assert_equal 1, b.get
assert_equal 2, c.get
assert_true a.respond_to?(:test)
assert_false b.respond_to?(:test)
assert_false c.respond_to?(:test)
end
assert('Kernel#dup class') do
assert_nothing_raised do
Array.dup.new(200)
Range.dup.new(2, 3)
String.dup.new("a"*50)
end
end
# Kernel#eval is provided by mruby-eval mrbgem '15.3.1.3.12'
assert('Kernel#extend', '15.3.1.3.13') do
class Test4ExtendClass
end
module Test4ExtendModule
def test_method; end
end
a = Test4ExtendClass.new
a.extend(Test4ExtendModule)
b = Test4ExtendClass.new
assert_true a.respond_to?(:test_method)
assert_false b.respond_to?(:test_method)
assert_raise(FrozenError) { Object.new.freeze.extend(Test4ExtendModule) }
assert_raise(FrozenError, TypeError) { :sym.extend(Test4ExtendModule) }
end
assert('Kernel#extend works on toplevel', '15.3.1.3.13') do
module Test4ExtendModule
def test_method; end
end
# This would crash...
extend(Test4ExtendModule)
assert_true respond_to?(:test_method)
end
assert('Kernel#freeze') do
obj = Object.new
assert_equal obj, obj.freeze
assert_equal 0, 0.freeze
assert_equal :a, :a.freeze
assert_equal true, true.freeze
assert_equal false, false.freeze
assert_equal nil, nil.freeze
skip unless Object.const_defined?(:Float)
assert_equal 0.0, 0.0.freeze
end
assert('Kernel#frozen?') do
assert_false "".frozen?
assert_true "".freeze.frozen?
assert_true 0.frozen?
assert_true :a.frozen?
assert_true true.frozen?
assert_true false.frozen?
assert_true nil.frozen?
skip unless Object.const_defined?(:Float)
assert_true 0.0.frozen?
end
assert('Kernel#hash', '15.3.1.3.15') do
assert_equal hash, hash
end
assert('Kernel#inspect', '15.3.1.3.17') do
s = inspect
assert_equal String, s.class
assert_equal "main", s
end
assert('Kernel#is_a?', '15.3.1.3.24') do
assert_true is_a?(Kernel)
assert_false is_a?(Array)
assert_raise TypeError do
42.is_a?(42)
end
end
assert('Kernel#iterator?', '15.3.1.3.25') do
assert_false iterator?
end
assert('Kernel#kind_of?', '15.3.1.3.26') do
assert_true kind_of?(Kernel)
assert_false kind_of?(Array)
end
assert('Kernel#lambda', '15.3.1.3.27') do
l = lambda do
true
end
m = lambda(&l)
assert_true l.call
assert_equal Proc, l.class
assert_true m.call
assert_equal Proc, m.class
end
assert('Kernel#loop', '15.3.1.3.29') do
i = 0
loop do
i += 1
break if i == 100
end
assert_equal i, 100
end
assert('Kernel#method_missing', '15.3.1.3.30') do
class MMTestClass
def method_missing(sym)
"A call to #{sym}"
end
end
mm_test = MMTestClass.new
assert_equal 'A call to no_method_named_this', mm_test.no_method_named_this
class SuperMMTestClass < MMTestClass
def no_super_method_named_this
super
end
end
super_mm_test = SuperMMTestClass.new
assert_equal 'A call to no_super_method_named_this', super_mm_test.no_super_method_named_this
class NoSuperMethodTestClass
def no_super_method_named_this
super
end
end
no_super_test = NoSuperMethodTestClass.new
msg = "undefined method 'no_super_method_named_this'"
assert_raise_with_message(NoMethodError, msg) do
no_super_test.no_super_method_named_this
end
a = String.new
msg = "undefined method 'no_method_named_this'"
assert_raise_with_message(NoMethodError, msg) do
a.no_method_named_this
end
end
assert('Kernel#nil?', '15.3.1.3.32') do
assert_false nil?
end
assert('Kernel#object_id', '15.3.1.3.33') do
a = ""
b = ""
assert_not_equal a.object_id, b.object_id
assert_kind_of Numeric, object_id
assert_kind_of Numeric, "".object_id
assert_kind_of Numeric, true.object_id
assert_kind_of Numeric, false.object_id
assert_kind_of Numeric, nil.object_id
assert_kind_of Numeric, :no.object_id
assert_kind_of Numeric, 1.object_id
assert_kind_of Numeric, 1.0.object_id
end
# Kernel#p is defined in mruby-print mrbgem. '15.3.1.3.34'
# Kernel#print is defined in mruby-print mrbgem. '15.3.1.3.35'
# Kernel#puts is defined in mruby-print mrbgem. '15.3.1.3.39'
assert('Kernel#raise', '15.3.1.3.40') do
assert_raise RuntimeError do
raise
end
assert_raise RuntimeError do
raise RuntimeError.new
end
end
assert('Kernel#remove_instance_variable', '15.3.1.3.41') do
class Test4RemoveInstanceVar
attr_reader :var
def initialize
@var = 99
end
def remove
remove_instance_variable(:@var)
end
end
tri = Test4RemoveInstanceVar.new
assert_equal 99, tri.var
assert_equal 99, tri.remove
assert_equal nil, tri.var
assert_raise(NameError) { tri.remove }
assert_raise(NameError) { tri.remove_instance_variable(:var) }
assert_raise(FrozenError) { tri.freeze.remove }
assert_raise(FrozenError, NameError) { :a.remove_instance_variable(:@v) }
end
# Kernel#require is defined in mruby-require. '15.3.1.3.42'
assert('Kernel#respond_to?', '15.3.1.3.43') do
class Test4RespondTo
def valid_method; end
def test_method; end
undef test_method
end
assert_raise TypeError do
Test4RespondTo.new.respond_to?(1)
end
assert_raise ArgumentError do
Test4RespondTo.new.respond_to?
end
assert_raise ArgumentError do
Test4RespondTo.new.respond_to? :a, true, :aa
end
assert_true respond_to?(:nil?)
assert_true Test4RespondTo.new.respond_to?(:valid_method)
assert_true Test4RespondTo.new.respond_to?('valid_method')
assert_false Test4RespondTo.new.respond_to?(:test_method)
end
assert('Kernel#to_s', '15.3.1.3.46') do
assert_equal to_s.class, String
end
assert('Kernel#!=') do
str1 = "hello"
str2 = str1
str3 = "world"
assert_false (str1[1] != 'e')
assert_true (str1 != str3)
assert_false (str2 != str1)
end
# operator "!~" is defined in ISO Ruby 11.4.4.
assert('Kernel#!~') do
x = "x"
def x.=~(other)
other == "x"
end
assert_false x !~ "x"
assert_true x !~ "z"
y = "y"
def y.=~(other)
other == "y"
end
def y.!~(other)
other == "not y"
end
assert_false y !~ "y"
assert_false y !~ "z"
assert_true y !~ "not y"
end
assert('Kernel#respond_to_missing?') do
class Test4RespondToMissing
def respond_to_missing?(method_name, include_private = false)
method_name == :a_method
end
end
assert_true Test4RespondToMissing.new.respond_to?(:a_method)
assert_false Test4RespondToMissing.new.respond_to?(:no_method)
end
assert('stack extend') do
def recurse(count, stop)
return count if count > stop
recurse(count+1, stop)
end
assert_equal 6, recurse(0, 5)
end
+74
View File
@@ -0,0 +1,74 @@
# The aim of these tests is to detect pitfall for optimized VM.
# Test for or/and
#
# You may think instruction fusion(OP_EQ and OP_JMPIF) for avoiding
# generate intermediate boolean value.
# But and/or is pitfall for this fusioning.
#
# For example, the following mruby code:
#
# if i > 0 and i < 10
#
# compiles to the following byte code:
#
# 1 000 OP_LOADI R1 0 ; R1:i
# 2 001 OP_MOVE R2 R1 ; R1:i
# 2 002 OP_LOADI R3 0
# 2 003 OP_GT R2 :> 1
# 2 004 OP_JMPNOT R2 008
# 2 005 OP_MOVE R2 R1 ; R1:i
# 2 006 OP_LOADI R3 10
# 2 007 OP_LT R2 :< 1
# 2 008 OP_JMPNOT R2 (The address of end of then part)
#
# When the instruction fusion the OP_GT and OP_JMPNOT you fell into the pitfalls.
# The deleted intermediate boolean value is used in OP_JMPNOT (address 008).
assert('and', '11.2.3') do
a = 1
if a > 0 and a < 10
b = 1
else
b = 0
end
assert_equal 1, b
if a < 0 and a < 10
b = 1
else
b = 0
end
assert_equal 0, b
if a < 0 and a > 10
b = 1
else
b = 0
end
assert_equal 0, b
end
assert('or','11.2.4') do
a = 1
if a > 0 or a < 10
b = 1
else
b = 0
end
assert_equal 1, b
if a < 0 or a < 10
b = 1
else
b = 0
end
assert_equal 1, b
if a < 0 or a > 10
b = 1
else
b = 0
end
assert_equal 0, b
end
+337
View File
@@ -0,0 +1,337 @@
##
# Literals ISO Test
assert('Literals Numerical', '8.7.6.2') do
# signed and unsigned integer
assert_equal 1, 1
assert_equal(-1, -1)
assert_equal(+1, +1)
# signed and unsigned float
assert_equal 1.0, 1.0
assert_equal(-1.0, -1.0)
# binary
assert_equal 128, 0b10000000
assert_equal 128, 0B10000000
# octal
assert_equal 8, 0o10
assert_equal 8, 0O10
assert_equal 8, 0_10
# hex
assert_equal 255, 0xff
assert_equal 255, 0Xff
# decimal
assert_equal 999, 0d999
assert_equal 999, 0D999
# decimal separator
assert_equal 10000000, 10_000_000
assert_equal 10, 1_0
# integer with exponent
assert_equal 10.0, 1e1
assert_equal(0.1, 1e-1)
assert_equal 10.0, 1e+1
# float with exponent
assert_equal 10.0, 1.0e1
assert_equal(0.1, 1.0e-1)
assert_equal 10.0, 1.0e+1
end
assert('Literals Strings Single Quoted', '8.7.6.3.2') do
assert_equal 'abc', 'abc'
assert_equal '\'', '\''
assert_equal '\\', '\\'
end
assert('Literals Strings Double Quoted', '8.7.6.3.3') do
a = "abc"
assert_equal "abc", "abc"
assert_equal "\"", "\""
assert_equal "\\", "\\"
assert_equal "abc", "#{a}"
end
assert('Literals Strings Quoted Non-Expanded', '8.7.6.3.4') do
a = %q{abc}
b = %q(abc)
c = %q[abc]
d = %q<abc>
e = %q/abc/
f = %q/ab\/c/
g = %q{#{a}}
assert_equal 'abc', a
assert_equal 'abc', b
assert_equal 'abc', c
assert_equal 'abc', d
assert_equal 'abc', e
assert_equal 'ab/c', f
assert_equal '#{a}', g
end
assert('Literals Strings Quoted Expanded', '8.7.6.3.5') do
a = %Q{abc}
b = %Q(abc)
c = %Q[abc]
d = %Q<abc>
e = %Q/abc/
f = %Q/ab\/c/
g = %Q{#{a}}
assert_equal 'abc', a
assert_equal 'abc', b
assert_equal 'abc', c
assert_equal 'abc', d
assert_equal 'abc', e
assert_equal 'ab/c', f
assert_equal 'abc', g
end
assert('Literals Strings Here documents', '8.7.6.3.6') do
a = <<AAA
aaa
AAA
b = <<b_b
bbb
b_b
c = [<<CCC1, <<"CCC2", <<'CCC3']
c1
CCC1
c 2
CCC2
c 3
CCC3
d = <<DDD
d#{1+2}DDD
d\t
DDD\n
DDD
e = <<'EEE'
e#{1+2}EEE
e\t
EEE\n
EEE
f = <<"FFF"
F
FF#{"f"}FFF
F
FFF
g = <<-GGG
ggg
GGG
h = <<-"HHH"
hhh
HHH
i = <<-'III'
iii
III
j = [<<-JJJ1 , <<-"JJJ2" , <<-'JJJ3' ]
j#{1}j
JJJ1
j#{2}j
JJJ2
j#{3}j
JJJ3
k = <<'KKK'.to_i
123
KKK
m = [<<MM1, <<MM2]
x#{m2 = {x:<<MM3}}y
mm3
MM3
mm1
MM1
mm2
MM2
n = [1, "#{<<NN1}", 3,
nn1
NN1
4]
qqq = Proc.new {|*x| x.join(' $ ')}
q1 = qqq.call("a", <<QQ1, "c",
q
QQ1
"d")
q2 = qqq.call("l", "m#{<<QQ2}n",
qq
QQ2
"o")
w = %W( 1 #{<<WWW} 3
www
WWW
4 5 )
x = [1, <<XXX1,
foo #{<<XXX2} bar
222 #{<<XXX3} 444
333
XXX3
5
XXX2
6
XXX1
9]
z = <<'ZZZ'
ZZZ
assert_equal "aaa\n", a
assert_equal "bbb\n", b
assert_equal ["c1\n", "c 2\n", "c 3\n"], c
assert_equal "d3DDD\nd\t\nDDD\n\n", d
assert_equal "e\#{1+2}EEE\ne\\t\nEEE\\n\n", e
assert_equal "F\nFFfFFF\nF\n", f
assert_equal " ggg\n", g
assert_equal " hhh\n", h
assert_equal " iii\n", i
assert_equal [" j1j\n", " j2j\n", " j\#{3}j\n"], j
assert_equal 123, k
assert_equal ["x{:x=>\"mm3\\n\"}y\nmm1\n", "mm2\n"], m
assert_equal ({:x=>"mm3\n"}), m2
assert_equal [1, "nn1\n", 3, 4], n
assert_equal "a $ q\n $ c $ d", q1
assert_equal "l $ mqq\nn $ o", q2
assert_equal ["1", "www\n", "3", "4", "5"], w
assert_equal [1, "foo 222 333\n 444\n5\n bar\n6\n", 9], x
assert_equal "", z
end
assert('Literals Array', '8.7.6.4') do
a = %W{abc#{1+2}def \}g}
b = %W(abc #{2+3} def \(g)
c = %W[#{3+4}]
d = %W< #{4+5} >
e = %W//
f = %W[[ab cd][ef]]
g = %W{
ab
#{-1}1
2#{2}
}
h = %W(a\nb
test\ abc
c\
d
x\y x\\y x\\\y)
assert_equal ['abc3def', '}g'], a
assert_equal ['abc', '5', 'def', '(g'], b
assert_equal ['7'],c
assert_equal ['9'], d
assert_equal [], e
assert_equal ['[ab', 'cd][ef]'], f
assert_equal ['ab', '-11', '22'], g
assert_equal ["a\nb", 'test abc', "c\nd", "xy", "x\\y", "x\\y"], h
a = %w{abc#{1+2}def \}g}
b = %w(abc #{2+3} def \(g)
c = %w[#{3+4}]
d = %w< #{4+5} >
e = %w//
f = %w[[ab cd][ef]]
g = %w{
ab
#{-1}1
2#{2}
}
h = %w(a\nb
test\ abc
c\
d
x\y x\\y x\\\y)
assert_equal ['abc#{1+2}def', '}g'], a
assert_equal ['abc', '#{2+3}', 'def', '(g'], b
assert_equal ['#{3+4}'], c
assert_equal ['#{4+5}'], d
assert_equal [], e
assert_equal ['[ab', 'cd][ef]'], f
assert_equal ['ab', '#{-1}1', '2#{2}'], g
assert_equal ["a\\nb", "test abc", "c\nd", "x\\y", "x\\y", "x\\\\y"], h
end
assert('Literals Array of symbols') do
a = %I{abc#{1+2}def \}g}
b = %I(abc #{2+3} def \(g)
c = %I[#{3+4}]
d = %I< #{4+5} >
e = %I//
f = %I[[ab cd][ef]]
g = %I{
ab
#{-1}1
2#{2}
}
assert_equal [:'abc3def', :'}g'], a
assert_equal [:'abc', :'5', :'def', :'(g'], b
assert_equal [:'7'],c
assert_equal [:'9'], d
assert_equal [], e
assert_equal [:'[ab', :'cd][ef]'], f
assert_equal [:'ab', :'-11', :'22'], g
a = %i{abc#{1+2}def \}g}
b = %i(abc #{2+3} def \(g)
c = %i[#{3+4}]
d = %i< #{4+5} >
e = %i//
f = %i[[ab cd][ef]]
g = %i{
ab
#{-1}1
2#{2}
}
assert_equal [:'abc#{1+2}def', :'}g'], a
assert_equal [:'abc', :'#{2+3}', :'def', :'(g'], b
assert_equal [:'#{3+4}'], c
assert_equal [:'#{4+5}'], d
assert_equal [] ,e
assert_equal [:'[ab', :'cd][ef]'], f
assert_equal [:'ab', :'#{-1}1', :'2#{2}'], g
end
assert('Literals Symbol', '8.7.6.6') do
# do not compile error
:$asd
:@asd
:@@asd
:asd=
:asd!
:asd?
:+
:+@
:if
:BEGIN
a = :"asd qwe"
b = :'foo bar'
c = :"a#{1+2}b"
d = %s(asd)
e = %s( foo \))
f = %s[asd \[
qwe]
g = %s/foo#{1+2}bar/
h = %s{{foo bar}}
assert_equal :'asd qwe', a
assert_equal :"foo bar", b
assert_equal :a3b, c
assert_equal :asd, d
assert_equal :' foo )', e
assert_equal :"asd [\nqwe", f
assert_equal :'foo#{1+2}bar', g
assert_equal :'{foo bar}', h
end
# Not Implemented ATM assert('Literals Regular expression', '8.7.6.5') do
+13
View File
@@ -0,0 +1,13 @@
##
# LocalJumpError ISO Test
assert('LocalJumpError', '15.2.25') do
assert_equal Class, LocalJumpError.class
# assert_raise LocalJumpError do
# # this will cause an exception due to the wrong location
# retry
# end
end
# TODO 15.2.25.2.1 LocalJumpError#exit_value
# TODO 15.2.25.2.2 LocalJumpError#reason
+109
View File
@@ -0,0 +1,109 @@
##
# Chapter 13.3 "Methods" ISO Test
assert('The alias statement', '13.3.6 a) 4)') do
# check aliasing in all possible ways
def alias_test_method_original; true; end
alias alias_test_method_a alias_test_method_original
alias :alias_test_method_b :alias_test_method_original
assert_true(alias_test_method_original)
assert_true(alias_test_method_a)
assert_true(alias_test_method_b)
end
assert('The alias statement (overwrite original)', '13.3.6 a) 4)') do
# check that an aliased method can be overwritten
# without side effect
def alias_test_method_original; true; end
alias alias_test_method_a alias_test_method_original
alias :alias_test_method_b :alias_test_method_original
assert_true(alias_test_method_original)
def alias_test_method_original; false; end
assert_false(alias_test_method_original)
assert_true(alias_test_method_a)
assert_true(alias_test_method_b)
end
assert('The alias statement', '13.3.6 a) 5)') do
# check that alias is raising NameError if
# non-existing method should be undefined
assert_raise(NameError) do
alias new_name_a non_existing_method
end
assert_raise(NameError) do
alias :new_name_b :non_existing_method
end
end
assert('The undef statement', '13.3.7 a) 4)') do
# check that undef is undefining method
# based on the method name
def existing_method_a; true; end
def existing_method_b; true; end
def existing_method_c; true; end
def existing_method_d; true; end
def existing_method_e; true; end
def existing_method_f; true; end
# check that methods are defined
assert_true(existing_method_a, 'Method should be defined')
assert_true(existing_method_b, 'Method should be defined')
assert_true(existing_method_c, 'Method should be defined')
assert_true(existing_method_d, 'Method should be defined')
assert_true(existing_method_e, 'Method should be defined')
assert_true(existing_method_f, 'Method should be defined')
# undefine in all possible ways and check that method
# is undefined
undef existing_method_a
assert_raise(NoMethodError) do
existing_method_a
end
undef :existing_method_b
assert_raise(NoMethodError) do
existing_method_b
end
undef existing_method_c, existing_method_d
assert_raise(NoMethodError) do
existing_method_c
end
assert_raise(NoMethodError) do
existing_method_d
end
undef :existing_method_e, :existing_method_f
assert_raise(NoMethodError) do
existing_method_e
end
assert_raise(NoMethodError) do
existing_method_f
end
end
assert('The undef statement (method undefined)', '13.3.7 a) 5)') do
# check that undef is raising NameError if
# non-existing method should be undefined
assert_raise(NameError) do
undef non_existing_method
end
assert_raise(NameError) do
undef :non_existing_method
end
end
+798
View File
@@ -0,0 +1,798 @@
##
# Module ISO Test
def labeled_module(name, &block)
Module.new do
(class <<self; self end).class_eval do
define_method(:to_s) { name }
alias_method :inspect, :to_s
end
class_eval(&block) if block
end
end
def labeled_class(name, supklass = Object, &block)
Class.new(supklass) do
(class <<self; self end).class_eval do
define_method(:to_s) { name }
alias_method :inspect, :to_s
end
class_eval(&block) if block
end
end
def assert_uninitialized_const(&block)
assert_raise_with_message_pattern(NameError, "uninitialized constant *", &block)
end
def assert_wrong_const_name(&block)
assert_raise_with_message_pattern(NameError, "wrong constant name *", &block)
end
assert('Module', '15.2.2') do
assert_equal Class, Module.class
end
assert('Module#alias_method', '15.2.2.4.8') do
cls = Class.new do
def foo
"FOO"
end
end
assert_same(cls, cls.alias_method(:bar, :foo))
assert_equal("FOO", cls.new.bar)
end
# TODO not implemented ATM assert('Module.constants', '15.2.2.3.1') do
assert('Module#ancestors', '15.2.2.4.9') do
class Test4ModuleAncestors
end
r = String.ancestors
assert_equal Array, r.class
assert_true r.include?(String)
assert_true r.include?(Object)
end
assert('Module#append_features', '15.2.2.4.10') do
module Test4AppendFeatures
def self.append_features(mod)
Test4AppendFeatures2.const_set(:Const4AppendFeatures2, mod)
end
end
module Test4AppendFeatures2
include Test4AppendFeatures
end
assert_equal Test4AppendFeatures2, Test4AppendFeatures2.const_get(:Const4AppendFeatures2)
assert_raise(FrozenError) { Module.new.append_features Class.new.freeze }
end
assert('Module#attr NameError') do
%w[
foo?
@foo
@@foo
$foo
].each do |name|
module NameTest; end
assert_raise(NameError) do
NameTest.module_eval { attr_reader name.to_sym }
end
assert_raise(NameError) do
NameTest.module_eval { attr_writer name.to_sym }
end
assert_raise(NameError) do
NameTest.module_eval { attr name.to_sym }
end
assert_raise(NameError) do
NameTest.module_eval { attr_accessor name.to_sym }
end
end
end
assert('Module#attr', '15.2.2.4.11') do
class AttrTest
class << self
attr :cattr
def cattr_val=(val)
@cattr = val
end
end
attr :iattr
def iattr_val=(val)
@iattr = val
end
end
test = AttrTest.new
assert_true AttrTest.respond_to?(:cattr)
assert_true test.respond_to?(:iattr)
assert_false AttrTest.respond_to?(:cattr=)
assert_false test.respond_to?(:iattr=)
test.iattr_val = 'test'
assert_equal 'test', test.iattr
AttrTest.cattr_val = 'test'
assert_equal 'test', AttrTest.cattr
end
assert('Module#attr_accessor', '15.2.2.4.12') do
class AttrTestAccessor
class << self
attr_accessor :cattr
end
attr_accessor :iattr, 'iattr2'
end
attr_instance = AttrTestAccessor.new
assert_true AttrTestAccessor.respond_to?(:cattr=)
assert_true attr_instance.respond_to?(:iattr=)
assert_true attr_instance.respond_to?(:iattr2=)
assert_true AttrTestAccessor.respond_to?(:cattr)
assert_true attr_instance.respond_to?(:iattr)
assert_true attr_instance.respond_to?(:iattr2)
attr_instance.iattr = 'test'
assert_equal 'test', attr_instance.iattr
AttrTestAccessor.cattr = 'test'
assert_equal 'test', AttrTestAccessor.cattr
end
assert('Module#attr_reader', '15.2.2.4.13') do
class AttrTestReader
class << self
attr_reader :cattr
def cattr_val=(val)
@cattr = val
end
end
attr_reader :iattr, 'iattr2'
def iattr_val=(val)
@iattr = val
end
end
attr_instance = AttrTestReader.new
assert_true AttrTestReader.respond_to?(:cattr)
assert_true attr_instance.respond_to?(:iattr)
assert_true attr_instance.respond_to?(:iattr2)
assert_false AttrTestReader.respond_to?(:cattr=)
assert_false attr_instance.respond_to?(:iattr=)
assert_false attr_instance.respond_to?(:iattr2=)
attr_instance.iattr_val = 'test'
assert_equal 'test', attr_instance.iattr
AttrTestReader.cattr_val = 'test'
assert_equal 'test', AttrTestReader.cattr
end
assert('Module#attr_writer', '15.2.2.4.14') do
class AttrTestWriter
class << self
attr_writer :cattr
def cattr_val
@cattr
end
end
attr_writer :iattr, 'iattr2'
def iattr_val
@iattr
end
end
attr_instance = AttrTestWriter.new
assert_true AttrTestWriter.respond_to?(:cattr=)
assert_true attr_instance.respond_to?(:iattr=)
assert_true attr_instance.respond_to?(:iattr2=)
assert_false AttrTestWriter.respond_to?(:cattr)
assert_false attr_instance.respond_to?(:iattr)
assert_false attr_instance.respond_to?(:iattr2)
attr_instance.iattr = 'test'
assert_equal 'test', attr_instance.iattr_val
AttrTestWriter.cattr = 'test'
assert_equal 'test', AttrTestWriter.cattr_val
end
assert('Module#class_eval', '15.2.2.4.15') do
class Test4ClassEval
@a = 11
@b = 12
end
Test4ClassEval.class_eval do
def method1
end
end
assert_equal 11, Test4ClassEval.class_eval{ @a }
assert_equal 12, Test4ClassEval.class_eval{ @b }
assert_equal true, Test4ClassEval.new.respond_to?(:method1)
end
assert('Module#const_defined?', '15.2.2.4.20') do
module Test4ConstDefined
Const4Test4ConstDefined = true
end
assert_true Test4ConstDefined.const_defined?(:Const4Test4ConstDefined)
assert_false Test4ConstDefined.const_defined?(:NotExisting)
assert_wrong_const_name{ Test4ConstDefined.const_defined?(:wrong_name) }
end
assert('Module#const_get', '15.2.2.4.21') do
module Test4ConstGet
Const4Test4ConstGet = 42
end
assert_equal 42, Test4ConstGet.const_get(:Const4Test4ConstGet)
assert_equal 42, Test4ConstGet.const_get("Const4Test4ConstGet")
assert_equal 42, Object.const_get("Test4ConstGet::Const4Test4ConstGet")
assert_raise(TypeError){ Test4ConstGet.const_get(123) }
assert_uninitialized_const{ Test4ConstGet.const_get(:I_DO_NOT_EXIST) }
assert_uninitialized_const{ Test4ConstGet.const_get("I_DO_NOT_EXIST::ME_NEITHER") }
assert_wrong_const_name{ Test4ConstGet.const_get(:wrong_name) }
end
assert('Module#const_set', '15.2.2.4.23') do
module Test4ConstSet
Const4Test4ConstSet = 42
end
assert_equal 23, Test4ConstSet.const_set(:Const4Test4ConstSet, 23)
assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet)
["", "wrongNAME", "Wrong-Name"].each do |n|
assert_wrong_const_name { Test4ConstSet.const_set(n, 1) }
end
end
assert('Module#remove_const', '15.2.2.4.40') do
module Test4RemoveConst
ExistingConst = 23
end
assert_equal 23, Test4RemoveConst.remove_const(:ExistingConst)
assert_false Test4RemoveConst.const_defined?(:ExistingConst)
assert_raise_with_message_pattern(NameError, "constant * not defined") do
Test4RemoveConst.remove_const(:NonExistingConst)
end
%i[x X!].each do |n|
assert_wrong_const_name { Test4RemoveConst.remove_const(n) }
end
assert_raise(FrozenError) { Test4RemoveConst.freeze.remove_const(:A) }
end
assert('Module#const_missing', '15.2.2.4.22') do
module Test4ConstMissing
def self.const_missing(sym)
42 # the answer to everything
end
end
assert_equal 42, Test4ConstMissing.const_get(:ConstDoesntExist)
end
assert('Module#extend_object', '15.2.2.4.25') do
cls = Class.new
mod = Module.new { def foo; end }
a = cls.new
b = cls.new
mod.extend_object(b)
assert_false a.respond_to?(:foo)
assert_true b.respond_to?(:foo)
assert_raise(FrozenError) { mod.extend_object(cls.new.freeze) }
assert_raise(FrozenError, TypeError) { mod.extend_object(1) }
end
assert('Module#include', '15.2.2.4.27') do
module Test4Include
Const4Include = 42
end
module Test4Include2
@include_result = include Test4Include
class << self
attr_reader :include_result
end
end
assert_equal 42, Test4Include2.const_get(:Const4Include)
assert_equal Test4Include2, Test4Include2.include_result
assert_raise(FrozenError) { Module.new.freeze.include Test4Include }
end
assert('Module#include?', '15.2.2.4.28') do
module Test4IncludeP
end
class Test4IncludeP2
include Test4IncludeP
end
class Test4IncludeP3 < Test4IncludeP2
end
assert_true Test4IncludeP2.include?(Test4IncludeP)
assert_true Test4IncludeP3.include?(Test4IncludeP)
assert_false Test4IncludeP.include?(Test4IncludeP)
end
assert('Module#included', '15.2.2.4.29') do
module Test4Included
Const4Included = 42
def self.included mod
Test4Included.const_set(:Const4Included2, mod)
end
end
module Test4Included2
include Test4Included
end
assert_equal 42, Test4Included2.const_get(:Const4Included)
assert_equal Test4Included2, Test4Included2.const_get(:Const4Included2)
end
assert('Module#initialize', '15.2.2.4.31') do
assert_kind_of Module, Module.new
mod = Module.new { def hello; "hello"; end }
cls = Class.new{include mod}
assert_true cls.new.respond_to?(:hello)
a = nil
mod = Module.new { |m| a = m }
assert_equal mod, a
end
assert('Module#method_defined?', '15.2.2.4.34') do
module Test4MethodDefined
module A
def method1() end
end
class B
def method2() end
end
class C < B
include A
def method3() end
end
end
assert_true Test4MethodDefined::A.method_defined? :method1
assert_true Test4MethodDefined::C.method_defined? :method1
assert_true Test4MethodDefined::C.method_defined? "method2"
assert_true Test4MethodDefined::C.method_defined? "method3"
assert_false Test4MethodDefined::C.method_defined? "method4"
end
assert('Module#module_eval', '15.2.2.4.35') do
module Test4ModuleEval
@a = 11
@b = 12
end
assert_equal 11, Test4ModuleEval.module_eval{ @a }
assert_equal 12, Test4ModuleEval.module_eval{ @b }
end
assert('Module#undef_method', '15.2.2.4.42') do
module Test4UndefMethod
class Parent
def hello
end
end
class Child < Parent
def hello
end
end
class GrandChild < Child
end
end
Test4UndefMethod::Child.class_eval{ undef_method :hello }
assert_true Test4UndefMethod::Parent.new.respond_to?(:hello)
assert_false Test4UndefMethod::Child.new.respond_to?(:hello)
assert_false Test4UndefMethod::GrandChild.new.respond_to?(:hello)
end
# Not ISO specified
assert('Module#dup') do
module TestModuleDup
@@cvar = :cvar
class << self
attr_accessor :cattr
def cmeth; :cmeth end
end
def cvar; @@cvar end
def imeth; :imeth end
self.cattr = :cattr
end
m = TestModuleDup.dup
o = Object.include(m).new
assert_equal(:cattr, m.cattr)
assert_equal(:cmeth, m.cmeth)
assert_equal(:cvar, o.cvar)
assert_equal(:imeth, o.imeth)
assert_match("#<Module:0x*>", m.to_s)
assert_not_predicate(m, :frozen?)
assert_not_predicate(TestModuleDup.freeze.dup, :frozen?)
end
assert('Module#define_method') do
c = Class.new {
define_method(:m1) { :ok }
define_method(:m2, Proc.new { :ok })
}
assert_equal c.new.m1, :ok
assert_equal c.new.m2, :ok
assert_raise(TypeError) do
Class.new { define_method(:n1, nil) }
end
end
assert 'Module#prepend_features' do
mod = Module.new { def m; :mod end }
cls = Class.new { def m; :cls end }
assert_equal :cls, cls.new.m
mod.prepend_features(cls)
assert_equal :mod, cls.new.m
assert_raise(FrozenError) { Module.new.prepend_features(Class.new.freeze) }
end
# @!group prepend
assert('Module#prepend') do
module M0
def m1; [:M0] end
end
module M1
def m1; [:M1, super, :M1] end
end
module M2
def m1; [:M2, super, :M2] end
end
M3 = Module.new do
def m1; [:M3, super, :M3] end
end
module M4
def m1; [:M4, super, :M4] end
end
class P0
include M0
prepend M1
def m1; [:C0, super, :C0] end
end
class P1 < P0
prepend M2, M3
include M4
def m1; [:C1, super, :C1] end
end
obj = P1.new
expected = [:M2,[:M3,[:C1,[:M4,[:M1,[:C0,[:M0],:C0],:M1],:M4],:C1],:M3],:M2]
assert_equal(expected, obj.m1)
end
assert('Module#prepend result') do
module TestPrepended; end
module TestPrependResult
@prepend_result = prepend TestPrepended
class << self
attr_reader :prepend_result
end
end
assert_equal TestPrependResult, TestPrependResult.prepend_result
end
# mruby shouldn't be affected by this since there is
# no visibility control (yet)
assert('Module#prepend public') do
assert_nothing_raised('ruby/ruby #8846') do
Class.new.prepend(Module.new)
end
end
assert('Module#prepend inheritance') do
bug6654 = '[ruby-core:45914]'
a = labeled_module('a')
b = labeled_module('b') { include a }
c = labeled_module('c') { prepend b }
#assert bug6654 do
# the Module#< operator should be used here instead, but we don't have it
assert_include(c.ancestors, a)
assert_include(c.ancestors, b)
#end
bug8357 = '[ruby-core:54736] [Bug #8357]'
b = labeled_module('b') { prepend a }
c = labeled_class('c') { include b }
#assert bug8357 do
# the Module#< operator should be used here instead, but we don't have it
assert_include(c.ancestors, a)
assert_include(c.ancestors, b)
#end
bug8357 = '[ruby-core:54742] [Bug #8357]'
assert_kind_of(b, c.new, bug8357)
end
assert 'Module#prepend + Class#ancestors' do
bug6658 = '[ruby-core:45919]'
m = labeled_module("m")
c = labeled_class("c") {prepend m}
assert_equal([m, c], c.ancestors[0, 2], bug6658)
bug6662 = '[ruby-dev:45868]'
c2 = labeled_class("c2", c)
anc = c2.ancestors
assert_equal([c2, m, c, Object], anc[0..anc.index(Object)], bug6662)
end
assert 'Module#prepend + Module#ancestors' do
bug6659 = '[ruby-dev:45861]'
m0 = labeled_module("m0") { def x; [:m0, *super] end }
m1 = labeled_module("m1") { def x; [:m1, *super] end; prepend m0 }
m2 = labeled_module("m2") { def x; [:m2, *super] end; prepend m1 }
c0 = labeled_class("c0") { def x; [:c0] end }
c1 = labeled_class("c1") { def x; [:c1] end; prepend m2 }
c2 = labeled_class("c2", c0) { def x; [:c2, *super] end; include m2 }
#
assert_equal([m0, m1], m1.ancestors, bug6659)
#
bug6662 = '[ruby-dev:45868]'
assert_equal([m0, m1, m2], m2.ancestors, bug6662)
assert_equal([m0, m1, m2, c1], c1.ancestors[0, 4], bug6662)
assert_equal([:m0, :m1, :m2, :c1], c1.new.x)
assert_equal([c2, m0, m1, m2, c0], c2.ancestors[0, 5], bug6662)
assert_equal([:c2, :m0, :m1, :m2, :c0], c2.new.x)
#
m3 = labeled_module("m3") { include m1; prepend m1 }
assert_equal([m3, m0, m1], m3.ancestors)
m3 = labeled_module("m3") { prepend m1; include m1 }
assert_equal([m0, m1, m3], m3.ancestors)
m3 = labeled_module("m3") { prepend m1; prepend m1 }
assert_equal([m0, m1, m3], m3.ancestors)
m3 = labeled_module("m3") { include m1; include m1 }
assert_equal([m3, m0, m1], m3.ancestors)
end
assert 'cyclic Module#prepend' do
bug7841 = '[ruby-core:52205] [Bug #7841]'
m1 = Module.new
m2 = Module.new
m1.instance_eval { prepend(m2) }
assert_raise(ArgumentError, bug7841) do
m2.instance_eval { prepend(m1) }
end
end
# these assertions will not run without a #assert_separately method
#assert 'test_prepend_optmethod' do
# bug7983 = '[ruby-dev:47124] [Bug #7983]'
# assert_separately [], %{
# module M
# def /(other)
# to_f / other
# end
# end
# Fixnum.send(:prepend, M)
# assert_equal(0.5, 1 / 2, "#{bug7983}")
# }
# assert_equal(0, 1 / 2)
#end
# mruby has no visibility control
# assert 'Module#prepend visibility' do
# bug8005 = '[ruby-core:53106] [Bug #8005]'
# c = Class.new do
# prepend Module.new {}
# def foo() end
# protected :foo
# end
# a = c.new
# assert_true a.respond_to?(:foo), bug8005
# assert_nothing_raised(bug8005) {a.send :foo}
# end
# mruby has no visibility control
# assert 'Module#prepend inherited visibility' do
# bug8238 = '[ruby-core:54105] [Bug #8238]'
# module Test4PrependVisibilityInherited
# class A
# def foo() A; end
# private :foo
# end
# class B < A
# public :foo
# prepend Module.new
# end
# end
# assert_equal(Test4PrependVisibilityInherited::A, Test4PrependVisibilityInherited::B.new.foo, "#{bug8238}")
# end
# assert 'Module#prepend super in alias' do
# skip "super does not currently work in aliased methods"
# bug7842 = '[Bug #7842]'
# p = labeled_module("P") do
# def m; "P"+super; end
# end
# a = labeled_class("A") do
# def m; "A"; end
# end
# b = labeled_class("B", a) do
# def m; "B"+super; end
# alias m2 m
# prepend p
# alias m3 m
# end
# assert_nothing_raised do
# assert_equal("BA", b.new.m2, bug7842)
# end
# assert_nothing_raised do
# assert_equal("PBA", b.new.m3, bug7842)
# end
# end
assert 'Module#prepend each class' do
m = labeled_module("M")
c1 = labeled_class("C1") {prepend m}
c2 = labeled_class("C2", c1) {prepend m}
assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should be able to prepend each class")
end
assert 'Module#prepend no duplication' do
m = labeled_module("M")
c = labeled_class("C") {prepend m; prepend m}
assert_equal([m, c], c.ancestors[0, 2], "should never duplicate")
end
assert 'Module#prepend in superclass' do
m = labeled_module("M")
c1 = labeled_class("C1")
c2 = labeled_class("C2", c1) {prepend m}
c1.class_eval {prepend m}
assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should accesisble prepended module in superclass")
end
# requires #assert_separately
#assert 'Module#prepend call super' do
# assert_separately([], <<-'end;') #do
# bug10847 = '[ruby-core:68093] [Bug #10847]'
# module M; end
# Float.prepend M
# assert_nothing_raised(SystemStackError, bug10847) do
# 0.3.numerator
# end
# end;
#end
assert 'Module#prepend to frozen class' do
assert_raise(FrozenError) { Class.new.freeze.prepend Module.new }
end
# @!endgroup prepend
assert('Module#to_s') do
module Outer
class Inner; end
const_set :SetInner, Class.new
end
assert_equal 'Outer', Outer.to_s
assert_equal 'Outer::Inner', Outer::Inner.to_s
assert_equal 'Outer::SetInner', Outer::SetInner.to_s
outer = Module.new do
const_set :SetInner, Class.new
end
Object.const_set :SetOuter, outer
assert_equal 'SetOuter', SetOuter.to_s
assert_equal 'SetOuter::SetInner', SetOuter::SetInner.to_s
assert_match "#<Module:0x*>", Module.new.to_s
assert_match "#<Class:0x*>", Class.new.to_s
assert_equal "FrozenClassToS", (FrozenClassToS = Class.new.freeze).to_s
assert_equal "Outer::A", (Outer::A = Module.new.freeze).to_s
assert_match "#<Module:0x*>::A", (Module.new::A = Class.new.freeze).to_s
end
assert('Module#inspect') do
module Test4to_sModules
end
assert_equal 'Test4to_sModules', Test4to_sModules.inspect
end
assert('Issue 1467') do
module M1
def initialize()
super()
end
end
class C1
include M1
def initialize()
super()
end
end
class C2
include M1
end
assert_kind_of(M1, C1.new)
assert_kind_of(M1, C2.new)
end
assert('clone Module') do
module M1
def foo
true
end
end
class B
include M1.clone
end
assert_true(B.new.foo)
end
assert('Module#module_function') do
module M
def modfunc; end
module_function :modfunc
end
assert_true M.respond_to?(:modfunc)
end
assert('module with non-class/module outer raises TypeError') do
assert_raise(TypeError) { module 0::M1 end }
assert_raise(TypeError) { module []::M2 end }
end
assert('module to return the last value') do
m = module M; :m end
assert_equal(m, :m)
end
assert('module to return nil if body is empty') do
assert_nil(module M end)
end
assert('get constant of parent module in singleton class; issue #3568') do
actual = module GetConstantInSingletonTest
EXPECTED = "value"
class << self
EXPECTED
end
end
assert_equal("value", actual)
end
+28
View File
@@ -0,0 +1,28 @@
##
# NameError ISO Test
assert('NameError', '15.2.31') do
assert_equal Class, NameError.class
end
assert('NameError#name', '15.2.31.2.1') do
# This check is not duplicate with 15.2.31.2.2 check.
# Because the NameError in this test is generated in
# C API.
class TestDummy
alias foo bar
rescue NameError => e
$test_dummy_result = e.name
end
assert_equal :bar, $test_dummy_result
end
assert('NameError#initialize', '15.2.31.2.2') do
e = NameError.new('a', :foo)
assert_equal NameError, e.class
assert_equal 'a', e.message
assert_equal :foo, e.name
end
+39
View File
@@ -0,0 +1,39 @@
##
# NilClass ISO Test
assert('NilClass', '15.2.4') do
assert_equal Class, NilClass.class
end
assert('NilClass', '15.2.4.1') do
assert_equal NilClass, nil.class
assert_false NilClass.method_defined? :new
end
assert('NilClass#&', '15.2.4.3.1') do
assert_false nil.&(true)
assert_false nil.&(nil)
end
assert('NilClass#^', '15.2.4.3.2') do
assert_true nil.^(true)
assert_false nil.^(false)
end
assert('NilClass#|', '15.2.4.3.3') do
assert_true nil.|(true)
assert_false nil.|(false)
end
assert('NilClass#nil?', '15.2.4.3.4') do
assert_true nil.nil?
end
assert('NilClass#to_s', '15.2.4.3.5') do
assert_equal '', nil.to_s
end
assert('safe navigation') do
assert_nil nil&.size
assert_equal 0, []&.size
end
+22
View File
@@ -0,0 +1,22 @@
##
# NoMethodError ISO Test
assert('NoMethodError', '15.2.32') do
NoMethodError.class == Class
assert_raise NoMethodError do
doesNotExistAsAMethodNameForVerySure("")
end
end
assert('NoMethodError#args', '15.2.32.2.1') do
a = NoMethodError.new 'test', :test, [1, 2]
assert_equal [1, 2], a.args
assert_nothing_raised do
begin
doesNotExistAsAMethodNameForVerySure 3, 1, 4
rescue NoMethodError => e
assert_equal [3, 1, 4], e.args
end
end
end
+114
View File
@@ -0,0 +1,114 @@
##
# Numeric ISO Test
def assert_step(exp, receiver, args, inf: false)
act = []
ret = receiver.step(*args) do |i|
act << i
break if inf && exp.size == act.size
end
expr = "#{receiver.inspect}.step(#{args.map(&:inspect).join(', ')})"
assert "assert_step" do
assert_true(exp.eql?(act), "#{expr}: counters", assertion_diff(exp, act))
assert_same(receiver, ret, "#{expr}: return value") unless inf
end
end
assert('Numeric', '15.2.7') do
assert_equal(Class, Numeric.class)
end
assert('Numeric#+@', '15.2.7.4.1') do
assert_equal(+1, +1)
end
assert('Numeric#-@', '15.2.7.4.2') do
assert_equal(-1, -1)
end
assert('Numeric#abs', '15.2.7.4.3') do
assert_equal(1, 1.abs)
skip unless Object.const_defined?(:Float)
assert_equal(1.0, -1.abs)
end
assert('Numeric#/', '15.2.8.3.4') do
n = Class.new(Numeric){ def /(x); 15.1;end }.new
assert_equal(2, 10/5)
assert_equal(0.0625, 1/16)
assert_equal(15.1, n/10)
assert_raise(TypeError){ 1/n }
assert_raise(TypeError){ 1/nil }
end
# Not ISO specified
assert('Numeric#**') do
assert_equal(8, 2 ** 3)
assert_equal(-8, -2 ** 3)
assert_equal(1, 2 ** 0)
skip unless Object.const_defined?(:Float)
assert_equal(1.0, 2.2 ** 0)
assert_equal(0.5, 2 ** -1)
assert_equal(8.0, 2.0**3)
end
assert('Numeric#step') do
assert_raise(ArgumentError) { 1.step(2, 0) { break } }
assert_step([2, 3, 4], 2, [4])
assert_step([10, 8, 6, 4, 2], 10, [1, -2])
assert_step([], 2, [1, 3])
assert_step([], -2, [-1, -3])
assert_step([10, 11, 12, 13], 10, [], inf: true)
assert_step([10, 7, 4], 10, [nil, -3], inf: true)
skip unless Object.const_defined?(:Float)
inf = Float::INFINITY
assert_raise(ArgumentError) { 1.step(2, 0.0) { break } }
assert_step([2.0, 3.0, 4.0], 2, [4.0])
assert_step([7.0, 4.0, 1.0, -2.0], 7, [-4, -3.0])
assert_step([2.0, 3.0, 4.0], 2.0, [4])
assert_step([10.0, 11.0, 12.0, 13.0], 10.0, [], inf: true)
assert_step([10.0, 7.0, 4.0], 10, [nil, -3.0], inf: true)
assert_step([1.0], 1, [nil, inf])
assert_step([1.0], 1, [nil, -inf])
assert_step([1.0], 1, [3, inf])
assert_step([], 1, [-3, inf])
assert_step([], 1, [3, -inf])
assert_step([1.0], 1, [-3, -inf])
assert_step([1.0], 1, [inf, inf])
assert_step([], 1, [inf, -inf])
assert_step([], 1, [-inf, inf])
assert_step([1.0], 1, [-inf, -inf])
assert_step([], inf, [2])
assert_step([], inf, [-2])
assert_step([], inf, [2, 3])
assert_step([inf, inf, inf], inf, [2, -3], inf: true)
assert_step([], inf, [2, inf])
assert_step([inf], inf, [2, -inf])
assert_step([], inf, [-2, inf])
assert_step([inf], inf, [-2, -inf])
assert_step([], inf, [-2, 3])
assert_step([inf, inf, inf], inf, [-2, -3], inf: true)
assert_step([inf], inf, [inf])
assert_step([], inf, [-inf])
assert_step([inf], inf, [inf, inf])
assert_step([inf], inf, [inf, -inf])
assert_step([inf], inf, [-inf, -inf])
assert_step([-inf, -inf, -inf], -inf, [2], inf: true)
assert_step([-inf, -inf, -inf], -inf, [-2], inf: true)
assert_step([-inf, -inf, -inf], -inf, [2, 3], inf: true)
assert_step([], -inf, [2, -3])
assert_step([-inf], -inf, [2, inf])
assert_step([], -inf, [2, -inf])
assert_step([-inf], -inf, [-2, inf])
assert_step([], -inf, [-2, -inf])
assert_step([-inf, -inf, -inf], -inf, [-2, 3], inf: true)
assert_step([], -inf, [-2, -3])
assert_step([-inf, -inf, -inf], -inf, [inf], inf: true)
assert_step([-inf], -inf, [-inf])
assert_step([-inf], -inf, [inf, inf])
assert_step([], -inf, [inf, -inf])
assert_step([-inf], -inf, [-inf, -inf])
end
+11
View File
@@ -0,0 +1,11 @@
##
# Object ISO Test
assert('Object', '15.2.1') do
assert_equal Class, Object.class
end
assert('Object superclass', '15.2.1.2') do
assert_equal BasicObject, Object.superclass
end
+180
View File
@@ -0,0 +1,180 @@
##
# Proc ISO Test
assert('Proc', '15.2.17') do
assert_equal Class, Proc.class
end
assert('Proc.new', '15.2.17.3.1') do
assert_raise ArgumentError do
Proc.new
end
assert_equal (Proc.new {}).class, Proc
assert_raise LocalJumpError do
Proc.new{ break }.call
end
end
assert('Proc#[]', '15.2.17.4.1') do
a = 0
b = Proc.new { a += 1 }
b.[]
a2 = 0
b2 = Proc.new { |i| a2 += i }
b2.[](5)
assert_equal 1, a
assert_equal 5, a2
end
assert('Proc#arity', '15.2.17.4.2') do
a = Proc.new {|x, y|}.arity
b = Proc.new {|x, *y, z|}.arity
c = Proc.new {|x=0, y|}.arity
d = Proc.new {|(x, y), z=0|}.arity
assert_equal 2, a
assert_equal(-3, b)
assert_equal 1, c
assert_equal 1, d
e = ->(x=0, y){}.arity
f = ->((x, y), z=0){}.arity
g = ->(x=0){}.arity
assert_equal(-2, e)
assert_equal(-2, f)
assert_equal(-1, g)
end
assert('Proc#call', '15.2.17.4.3') do
a = 0
b = Proc.new { a += 1 }
b.call
a2 = 0
b2 = Proc.new { |i| a2 += i }
b2.call(5)
assert_equal 1, a
assert_equal 5, a2
end
assert('Proc#call proc args pos block') do
pr = Proc.new {|a,b,&c|
[a, b, c.class, c&&c.call(:x)]
}
assert_equal [nil, nil, Proc, :proc], (pr.call(){ :proc })
assert_equal [1, nil, Proc, :proc], (pr.call(1){ :proc })
assert_equal [1, 2, Proc, :proc], (pr.call(1, 2){ :proc })
assert_equal [1, 2, Proc, :proc], (pr.call(1, 2, 3){ :proc })
assert_equal [1, 2, Proc, :proc], (pr.call(1, 2, 3, 4){ :proc })
assert_equal [nil, nil, Proc, :x], (pr.call(){|x| x})
assert_equal [1, nil, Proc, :x], (pr.call(1){|x| x})
assert_equal [1, 2, Proc, :x], (pr.call(1, 2){|x| x})
assert_equal [1, 2, Proc, :x], (pr.call(1, 2, 3){|x| x})
assert_equal [1, 2, Proc, :x], (pr.call(1, 2, 3, 4){|x| x})
end
assert('Proc#call proc args pos rest post') do
pr = Proc.new {|a,b,*c,d,e|
[a,b,c,d,e]
}
assert_equal [nil, nil, [], nil, nil], pr.call()
assert_equal [1, nil, [], nil, nil], pr.call(1)
assert_equal [1, 2, [], nil, nil], pr.call(1,2)
assert_equal [1, 2, [], 3, nil], pr.call(1,2,3)
assert_equal [1, 2, [], 3, 4], pr.call(1,2,3,4)
assert_equal [1, 2, [3], 4, 5], pr.call(1,2,3,4,5)
assert_equal [1, 2, [3, 4], 5, 6], pr.call(1,2,3,4,5,6)
assert_equal [1, 2, [3, 4, 5], 6,7], pr.call(1,2,3,4,5,6,7)
assert_equal [nil, nil, [], nil, nil], pr.call([])
assert_equal [1, nil, [], nil, nil], pr.call([1])
assert_equal [1, 2, [], nil, nil], pr.call([1,2])
assert_equal [1, 2, [], 3, nil], pr.call([1,2,3])
assert_equal [1, 2, [], 3, 4], pr.call([1,2,3,4])
assert_equal [1, 2, [3], 4, 5], pr.call([1,2,3,4,5])
assert_equal [1, 2, [3, 4], 5, 6], pr.call([1,2,3,4,5,6])
assert_equal [1, 2, [3, 4, 5], 6,7], pr.call([1,2,3,4,5,6,7])
end
assert('Proc#return_does_not_break_self') do
class TestClass
attr_accessor :block
def initialize
end
def return_array
@block = Proc.new { self }
return []
end
def return_instance_variable
@block = Proc.new { self }
return @block
end
def return_const_fixnum
@block = Proc.new { self }
return 123
end
def return_nil
@block = Proc.new { self }
return nil
end
end
c = TestClass.new
assert_equal [], c.return_array
assert_equal c, c.block.call
c.return_instance_variable
assert_equal c, c.block.call
assert_equal 123, c.return_const_fixnum
assert_equal c, c.block.call
assert_equal nil, c.return_nil
assert_equal c, c.block.call
end
assert('call Proc#initialize if defined') do
a = []
c = Class.new(Proc) do
define_method(:initialize) do
a << :ok
end
end
assert_kind_of c, c.new{}
assert_equal [:ok], a
end
assert('&obj call to_proc if defined') do
pr = Proc.new{}
def mock(&b)
b
end
assert_same pr, mock(&pr)
assert_equal pr, mock(&pr)
obj = Object.new
def obj.to_proc
Proc.new{ :from_to_proc }
end
assert_equal :from_to_proc, mock(&obj).call
assert_raise(TypeError){ mock(&(Object.new)) }
end
assert('Creation of a proc through the block of a method') do
def m(&b) b end
assert_equal m{}.class, Proc
assert_raise LocalJumpError do
m{ break }.call
end
end
+112
View File
@@ -0,0 +1,112 @@
##
# Range ISO Test
assert('Range', '15.2.14') do
assert_equal Class, Range.class
end
assert('Range#==', '15.2.14.4.1') do
assert_true (1..10) == (1..10)
assert_false (1..10) == (1..100)
skip unless Object.const_defined?(:Float)
assert_true (1..10) == Range.new(1.0, 10.0)
end
assert('Range#===', '15.2.14.4.2') do
a = (1..10)
assert_true a === 5
assert_false a === 20
end
assert('Range#begin', '15.2.14.4.3') do
assert_equal 1, (1..10).begin
end
assert('Range#each', '15.2.14.4.4') do
a = (1..3)
b = 0
a.each {|i| b += i}
assert_equal 6, b
end
assert('Range#end', '15.2.14.4.5') do
assert_equal 10, (1..10).end
end
assert('Range#exclude_end?', '15.2.14.4.6') do
assert_true (1...10).exclude_end?
assert_false (1..10).exclude_end?
end
assert('Range#first', '15.2.14.4.7') do
assert_equal 1, (1..10).first
end
assert('Range#include?', '15.2.14.4.8') do
assert_true (1..10).include?(10)
assert_false (1..10).include?(11)
assert_true (1...10).include?(9)
assert_false (1...10).include?(10)
end
assert('Range#initialize', '15.2.14.4.9') do
a = Range.new(1, 10, true)
b = Range.new(1, 10, false)
assert_equal (1...10), a
assert_true a.exclude_end?
assert_equal (1..10), b
assert_false b.exclude_end?
assert_raise(NameError) { (0..1).__send__(:initialize, 1, 3) }
end
assert('Range#last', '15.2.14.4.10') do
assert_equal 10, (1..10).last
end
assert('Range#member?', '15.2.14.4.11') do
a = (1..10)
assert_true a.member?(5)
assert_false a.member?(20)
end
assert('Range#to_s', '15.2.14.4.12') do
assert_equal "0..1", (0..1).to_s
assert_equal "0...1", (0...1).to_s
assert_equal "a..b", ("a".."b").to_s
assert_equal "a...b", ("a"..."b").to_s
end
assert('Range#inspect', '15.2.14.4.13') do
assert_equal "0..1", (0..1).inspect
assert_equal "0...1", (0...1).inspect
assert_equal "\"a\"..\"b\"", ("a".."b").inspect
assert_equal "\"a\"...\"b\"", ("a"..."b").inspect
end
assert('Range#eql?', '15.2.14.4.14') do
assert_true (1..10).eql? (1..10)
assert_false (1..10).eql? (1..100)
assert_false (1..10).eql? (Range.new(1.0, 10.0))
assert_false (1..10).eql? "1..10"
end
assert('Range#initialize_copy', '15.2.14.4.15') do
assert_raise(NameError) { (0..1).__send__(:initialize_copy, 1..3) }
end
assert('Range#dup') do
r = (1..3).dup
assert_equal 1, r.begin
assert_equal 3, r.end
assert_false r.exclude_end?
r = ("a"..."z").dup
assert_equal "a", r.begin
assert_equal "z", r.end
assert_true r.exclude_end?
end
+6
View File
@@ -0,0 +1,6 @@
##
# RangeError ISO Test
assert('RangeError', '15.2.26') do
assert_equal Class, RangeError.class
end
+4
View File
@@ -0,0 +1,4 @@
##
# RegexpError ISO Test
# TODO broken ATM assert('RegexpError', '15.2.27') do
+6
View File
@@ -0,0 +1,6 @@
##
# RuntimeError ISO Test
assert('RuntimeError', '15.2.28') do
assert_equal Class, RuntimeError.class
end
+6
View File
@@ -0,0 +1,6 @@
##
# StandardError ISO Test
assert('StandardError', '15.2.23') do
assert_equal Class, StandardError.class
end
+897
View File
@@ -0,0 +1,897 @@
# coding: utf-8
##
# String ISO Test
UTF8STRING = __ENCODING__ == "UTF-8"
assert('String', '15.2.10') do
assert_equal Class, String.class
end
assert('String#<=>', '15.2.10.5.1') do
a = '' <=> ''
b = '' <=> 'not empty'
c = 'not empty' <=> ''
d = 'abc' <=> 'cba'
e = 'cba' <=> 'abc'
assert_equal 0, a
assert_equal(-1, b)
assert_equal 1, c
assert_equal(-1, d)
assert_equal 1, e
assert_nil 'a' <=> 1024
end
assert('String#==', '15.2.10.5.2') do
assert_equal 'abc', 'abc'
assert_not_equal 'abc', 'cba'
end
# 'String#=~', '15.2.10.5.3' will be tested in mrbgems.
assert('String#+', '15.2.10.5.4') do
assert_equal 'ab', 'a' + 'b'
end
assert('String#*', '15.2.10.5.5') do
assert_equal 'aaaaa', 'a' * 5
assert_equal '', 'a' * 0
assert_raise(ArgumentError) { 'a' * -1 }
assert_raise(TypeError) { 'a' * '1' }
assert_raise(TypeError) { 'a' * nil }
skip unless Object.const_defined?(:Float)
assert_equal 'aa', 'a' * 2.1
assert_raise(RangeError) { '' * 1e30 }
assert_raise(RangeError) { '' * Float::INFINITY }
assert_raise(RangeError) { '' * Float::NAN }
end
assert('String#[]', '15.2.10.5.6') do
# length of args is 1
assert_equal 'a', 'abc'[0]
assert_equal 'c', 'abc'[-1]
assert_nil 'abc'[10]
assert_nil 'abc'[-10]
assert_equal 'b', 'abc'[1.1] if Object.const_defined?(:Float)
# length of args is 2
assert_nil 'abc'[0, -1]
assert_nil 'abc'[10, 0]
assert_nil 'abc'[-10, 0]
assert_equal '', 'abc'[0, 0]
assert_equal 'bc', 'abc'[1, 2]
# args is String
assert_equal 'bc', 'abc'['bc']
assert_nil 'abc'['XX']
assert_raise(TypeError) { 'abc'[nil] }
end
assert('String#[](UTF-8)', '15.2.10.5.6') do
assert_equal "", "こんにちは世界"[3]
assert_equal nil, "こんにちは世界"[20]
assert_equal "", "こんにちは世界"[-2]
assert_equal "世界", "こんにちは世界"[-2..-1]
assert_equal "んに", "こんにちは世界"[1,2]
assert_equal "", "こんにちは世界"[""]
end if UTF8STRING
assert('String#[] with Range') do
a1 = 'abc'[1..0]
b1 = 'abc'[1..1]
c1 = 'abc'[1..2]
d1 = 'abc'[1..3]
e1 = 'abc'[1..4]
f1 = 'abc'[0..-2]
g1 = 'abc'[-2..3]
h1 = 'abc'[3..4]
i1 = 'abc'[4..5]
j1 = 'abcdefghijklmnopqrstuvwxyz'[1..3]
a2 = 'abc'[1...0]
b2 = 'abc'[1...1]
c2 = 'abc'[1...2]
d2 = 'abc'[1...3]
e2 = 'abc'[1...4]
f2 = 'abc'[0...-2]
g2 = 'abc'[-2...3]
h2 = 'abc'[3...4]
i2 = 'abc'[4...5]
j2 = 'abcdefghijklmnopqrstuvwxyz'[1...3]
assert_equal '', a1
assert_equal 'b', b1
assert_equal 'bc', c1
assert_equal 'bc', d1
assert_equal 'bc', e1
assert_equal 'ab', f1
assert_equal 'bc', g1
assert_equal '', h1
assert_nil i2
assert_equal 'bcd', j1
assert_equal '', a2
assert_equal '', b2
assert_equal 'b', c2
assert_equal 'bc', d2
assert_equal 'bc', e2
assert_equal 'a', f2
assert_equal 'bc', g2
assert_equal '', h2
assert_nil i2
assert_equal 'bc', j2
end
assert('String#[]=') do
# length of args is 1
a = 'abc'
a[0] = 'X'
assert_equal 'Xbc', a
b = 'abc'
b[-1] = 'X'
assert_equal 'abX', b
c = 'abc'
assert_raise(IndexError) do
c[10] = 'X'
end
d = 'abc'
assert_raise(IndexError) do
d[-10] = 'X'
end
if Object.const_defined?(:Float)
e = 'abc'
e[1.1] = 'X'
assert_equal 'aXc', e
end
assert_raise(TypeError) { 'a'[0] = 1 }
assert_raise(TypeError) { 'a'[:a] = '1' }
# length of args is 2
a1 = 'abc'
assert_raise(IndexError) do
a1[0, -1] = 'X'
end
b1 = 'abc'
assert_raise(IndexError) do
b1[10, 0] = 'X'
end
c1 = 'abc'
assert_raise(IndexError) do
c1[-10, 0] = 'X'
end
d1 = 'abc'
d1[0, 0] = 'X'
assert_equal 'Xabc', d1
e1 = 'abc'
e1[1, 3] = 'X'
assert_equal 'aX', e1
# args is RegExp
# It will be tested in mrbgems.
# args is String
a3 = 'abc'
a3['bc'] = 'X'
assert_equal a3, 'aX'
b3 = 'abc'
assert_raise(IndexError) do
b3['XX'] = 'Y'
end
assert_raise(TypeError) { 'a'[:a, 0] = '1' }
assert_raise(TypeError) { 'a'[0, :a] = '1' }
assert_raise(TypeError) { 'a'[0, 1] = 1 }
end
assert('String[]=(UTF-8)') do
a = "➀➁➂➃➄"
a[3] = ""
assert_equal "➀➁➂⚃➄", a
b = "➀➁➂➃➄"
b[3, 0] = ""
assert_equal "➀➁➂⛄➃➄", b
c = "➀➁➂➃➄"
c[3, 2] = "⚃⚄"
assert_equal "➀➁➂⚃⚄", c
d = "➀➁➂➃➄"
d[5] = ""
assert_equal "➀➁➂➃➄⛄", d
e = "➀➁➂➃➄"
e[5, 0] = ""
assert_equal "➀➁➂➃➄⛄", e
f = "➀➁➂➃➄"
f[5, 2] = ""
assert_equal "➀➁➂➃➄⛄", f
g = "➀➁➂➃➄"
assert_raise(IndexError) { g[6] = "" }
h = "➀➁➂➃➄"
assert_raise(IndexError) { h[6, 0] = "" }
i = "➀➁➂➃➄"
assert_raise(IndexError) { i[6, 2] = "" }
j = "➀➁➂➃➄"
j[""] = ""
assert_equal "➀➁➂⚃➄", j
k = "➀➁➂➃➄"
assert_raise(IndexError) { k[""] = "" }
l = "➀➁➂➃➄"
assert_nothing_raised { l[""] = "" }
assert_equal "➀➁➃➄", l
m = "➀➁➂➃➄"
assert_raise(TypeError) { m[""] = nil }
assert_equal "➀➁➂➃➄", m
end if UTF8STRING
assert('String#capitalize', '15.2.10.5.7') do
a = 'abc'
a.capitalize
assert_equal 'abc', a
assert_equal 'Abc', 'abc'.capitalize
end
assert('String#capitalize!', '15.2.10.5.8') do
a = 'abc'
a.capitalize!
assert_equal 'Abc', a
assert_equal nil, 'Abc'.capitalize!
end
assert('String#chomp', '15.2.10.5.9') do
a = 'abc'.chomp
b = ''.chomp
c = "abc\n".chomp
d = "abc\n\n".chomp
e = "abc\t".chomp("\t")
f = "abc\n"
f.chomp
assert_equal 'abc', a
assert_equal '', b
assert_equal 'abc', c
assert_equal "abc\n", d
assert_equal 'abc', e
assert_equal "abc\n", f
end
assert('String#chomp!', '15.2.10.5.10') do
a = 'abc'
b = ''
c = "abc\n"
d = "abc\n\n"
e = "abc\t"
a.chomp!
b.chomp!
c.chomp!
d.chomp!
e.chomp!("\t")
assert_equal 'abc', a
assert_equal '', b
assert_equal 'abc', c
assert_equal "abc\n", d
assert_equal 'abc', e
end
assert('String#chop', '15.2.10.5.11') do
a = ''.chop
b = 'abc'.chop
c = 'abc'
c.chop
assert_equal '', a
assert_equal 'ab', b
assert_equal 'abc', c
end
assert('String#chop(UTF-8)', '15.2.10.5.11') do
a = ''.chop
b = 'あいう'.chop
c = "\n".chop.chop
assert_equal '', a
assert_equal 'あい', b
assert_equal 'あ', c
end if UTF8STRING
assert('String#chop!', '15.2.10.5.12') do
a = ''
b = 'abc'
a.chop!
b.chop!
assert_equal a, ''
assert_equal b, 'ab'
end
assert('String#chop!(UTF-8)', '15.2.10.5.12') do
a = ''
b = "あいうえ\n"
c = "あいうえ\n"
a.chop!
b.chop!
c.chop!
c.chop!
assert_equal a, ''
assert_equal b, 'あいうえ'
assert_equal c, 'あいう'
end if UTF8STRING
assert('String#downcase', '15.2.10.5.13') do
a = 'ABC'.downcase
b = 'ABC'
b.downcase
assert_equal 'abc', a
assert_equal 'ABC', b
end
assert('String#downcase!', '15.2.10.5.14') do
a = 'ABC'
a.downcase!
assert_equal 'abc', a
assert_equal nil, 'abc'.downcase!
end
assert('String#each_line', '15.2.10.5.15') do
a = "first line\nsecond line\nthird line"
list = ["first line\n", "second line\n", "third line"]
n_list = []
a.each_line do |line|
n_list << line
end
assert_equal list, n_list
n_list.clear
a.each_line("li") do |line|
n_list << line
end
assert_equal ["first li", "ne\nsecond li", "ne\nthird li", "ne"], n_list
end
assert('String#empty?', '15.2.10.5.16') do
a = ''
b = 'not empty'
assert_true a.empty?
assert_false b.empty?
end
assert('String#eql?', '15.2.10.5.17') do
assert_true 'abc'.eql?('abc')
assert_false 'abc'.eql?('cba')
end
assert('String#gsub', '15.2.10.5.18') do
assert_equal('aBcaBc', 'abcabc'.gsub('b', 'B'), 'gsub without block')
assert_equal('aBcaBc', 'abcabc'.gsub('b'){|w| w.capitalize }, 'gsub with block')
assert_equal('$a$a$', '#a#a#'.gsub('#', '$'), 'mruby/mruby#847')
assert_equal('$a$a$', '#a#a#'.gsub('#'){|_w| '$' }, 'mruby/mruby#847 with block')
assert_equal('$$a$$', '##a##'.gsub('##', '$$'), 'mruby/mruby#847 another case')
assert_equal('$$a$$', '##a##'.gsub('##'){|_w| '$$' }, 'mruby/mruby#847 another case with block')
assert_equal('A', 'a'.gsub('a', 'A'))
assert_equal('A', 'a'.gsub('a'){|w| w.capitalize })
assert_equal("<a><><>", 'a'.gsub('a', '<\0><\1><\2>'))
assert_equal(".h.e.l.l.o.", "hello".gsub("", "."))
a = []
assert_equal(".h.e.l.l.o.", "hello".gsub("") { |i| a << i; "." })
assert_equal(["", "", "", "", "", ""], a)
assert_raise(ArgumentError) { "".gsub }
assert_raise(ArgumentError) { "".gsub("", "", "") }
end
assert('String#gsub with backslash') do
s = 'abXcdXef'
assert_equal 'ab<\\>cd<\\>ef', s.gsub('X', '<\\\\>')
assert_equal 'ab<X>cd<X>ef', s.gsub('X', '<\\&>')
assert_equal 'ab<X>cd<X>ef', s.gsub('X', '<\\0>')
assert_equal 'ab<ab>cd<abXcd>ef', s.gsub('X', '<\\`>')
assert_equal 'ab<cdXef>cd<ef>ef', s.gsub('X', '<\\\'>')
end
assert('String#gsub!', '15.2.10.5.19') do
a = 'abcabc'
a.gsub!('b', 'B')
b = 'abcabc'
b.gsub!('b') { |w| w.capitalize }
assert_equal 'aBcaBc', a
assert_equal 'aBcaBc', b
end
assert('String#hash', '15.2.10.5.20') do
a = 'abc'
assert_equal 'abc'.hash, a.hash
end
assert('String#include?', '15.2.10.5.21') do
assert_true 'abc'.include?('a')
assert_false 'abc'.include?('d')
end
assert('String#index', '15.2.10.5.22') do
assert_equal 0, 'abc'.index('a')
assert_nil 'abc'.index('d')
assert_equal 3, 'abcabc'.index('a', 1)
assert_equal 5, "hello".index("", 5)
assert_equal nil, "hello".index("", 6)
assert_equal 3, "hello".index("l", -2)
assert_raise(ArgumentError) { "hello".index }
assert_raise(TypeError) { "hello".index(101) }
end
assert('String#index(UTF-8)', '15.2.10.5.22') do
assert_equal 0, '⓿➊➋➌➍➎'.index('⓿')
assert_nil '⓿➊➋➌➍➎'.index('➓')
assert_equal 6, '⓿➊➋➌➍➎⓿➊➋➌➍➎'.index('⓿', 1)
assert_equal 6, '⓿➊➋➌➍➎⓿➊➋➌➍➎'.index('⓿', -7)
assert_equal 6, "⓿➊➋➌➍➎".index("", 6)
assert_equal nil, "⓿➊➋➌➍➎".index("", 7)
assert_equal 0, '⓿➊➋➌➍➎'.index("\xe2")
assert_equal nil, '⓿➊➋➌➍➎'.index("\xe3")
assert_equal 6, "\xd1\xd1\xd1\xd1\xd1\xd1⓿➊➋➌➍➎".index('⓿')
end if UTF8STRING
assert('String#initialize', '15.2.10.5.23') do
a = ''
a.initialize('abc')
assert_equal 'abc', a
a.initialize('abcdefghijklmnopqrstuvwxyz')
assert_equal 'abcdefghijklmnopqrstuvwxyz', a
end
assert('String#initialize_copy', '15.2.10.5.24') do
a = ''
a.initialize_copy('abc')
assert_equal 'abc', a
end
assert('String#intern', '15.2.10.5.25') do
assert_equal :abc, 'abc'.intern
end
assert('String#length', '15.2.10.5.26') do
assert_equal 3, 'abc'.length
end
# 'String#match', '15.2.10.5.27' will be tested in mrbgems.
assert('String#replace', '15.2.10.5.28') do
a = ''
a.replace('abc')
assert_equal 'abc', a
assert_equal 'abc', 'cba'.replace(a)
b = 'abc' * 10
c = ('cba' * 10).dup
b.replace(c);
c.replace(b);
assert_equal c, b
# shared string
s = "foo" * 100
a = s[10, 90] # create shared string
assert_equal("", s.replace("")) # clear
assert_equal("", s) # s is cleared
assert_not_equal("", a) # a should not be affected
end
assert('String#reverse', '15.2.10.5.29') do
a = 'abc'
a.reverse
assert_equal 'abc', a
assert_equal 'cba', 'abc'.reverse
end
assert('String#reverse(UTF-8)', '15.2.10.5.29') do
a = 'こんにちは世界!'
a.reverse
assert_equal 'こんにちは世界!', a
assert_equal '!界世はちにんこ', 'こんにちは世界!'.reverse
assert_equal 'あ', 'あ'.reverse
end if UTF8STRING
assert('String#reverse!', '15.2.10.5.30') do
a = 'abc'
a.reverse!
assert_equal 'cba', a
assert_equal 'cba', 'abc'.reverse!
end
assert('String#reverse!(UTF-8)', '15.2.10.5.30') do
a = 'こんにちは世界!'
a.reverse!
assert_equal '!界世はちにんこ', a
assert_equal '!界世はちにんこ', 'こんにちは世界!'.reverse!
b = 'あ'
b.reverse!
assert_equal 'あ', b
end if UTF8STRING
assert('String#rindex', '15.2.10.5.31') do
assert_equal 0, 'abc'.rindex('a')
assert_equal 0, 'abc'.rindex('a', 3)
assert_nil 'abc'.rindex('a', -4)
assert_nil 'abc'.rindex('d')
assert_equal 6, 'abcabc'.rindex('')
assert_equal 3, 'abcabc'.rindex('a')
assert_equal 0, 'abcabc'.rindex('a', 1)
assert_equal 3, 'abcabc'.rindex('a', 4)
assert_equal 0, 'abcabc'.rindex('a', -4)
assert_raise(ArgumentError) { "hello".rindex }
assert_raise(TypeError) { "hello".rindex(101) }
end
assert('String#rindex(UTF-8)', '15.2.10.5.31') do
str = "こんにちは世界!\nこんにちは世界!"
assert_nil str.rindex('さ')
assert_equal 12, str.rindex('ち')
assert_equal 3, str.rindex('ち', 10)
assert_equal 3, str.rindex('ち', -6)
broken = "\xf0\xf1\xf2\xf3\xf0\xf1\xf2\xf3"
assert_nil broken.rindex("\x81") # "\x81" is a part of "☁" ("\xe2\x98\x81")
assert_equal 11, broken.rindex("")
assert_equal 11, broken.rindex("", 12)
assert_equal 11, broken.rindex("", 11)
assert_equal 3, broken.rindex("", 10)
end if UTF8STRING
# assert('String#scan', '15.2.10.5.32') do
# # Not implemented yet
# end
assert('String#size', '15.2.10.5.33') do
assert_equal 3, 'abc'.size
end
assert('String#size(UTF-8)', '15.2.10.5.33') do
str = 'こんにちは世界!'
assert_equal 8, str.size
assert_not_equal str.bytesize, str.size
assert_equal 2, str[1, 2].size
end if UTF8STRING
assert('String#slice', '15.2.10.5.34') do
# length of args is 1
a = 'abc'.slice(0)
b = 'abc'.slice(-1)
c = 'abc'.slice(10)
d = 'abc'.slice(-10)
# length of args is 2
a1 = 'abc'.slice(0, -1)
b1 = 'abc'.slice(10, 0)
c1 = 'abc'.slice(-10, 0)
d1 = 'abc'.slice(0, 0)
e1 = 'abc'.slice(1, 2)
# slice of shared string
e11 = e1.slice(0)
# args is RegExp
# It will be tested in mrbgems.
# args is String
a3 = 'abc'.slice('bc')
b3 = 'abc'.slice('XX')
assert_equal 'a', a
assert_equal 'c', b
assert_nil c
assert_nil d
assert_nil a1
assert_nil b1
assert_nil c1
assert_equal '', d1
assert_equal 'bc', e1
assert_equal 'b', e11
assert_equal 'bc', a3
assert_nil b3
end
# TODO Broken ATM
assert('String#split', '15.2.10.5.35') do
# without RegExp behavior is actually unspecified
assert_equal ['abc', 'abc', 'abc'], 'abc abc abc'.split
assert_equal ["a", "b", "c", "", "d"], 'a,b,c,,d'.split(',')
assert_equal ['abc', 'abc', 'abc'], 'abc abc abc'.split(nil)
assert_equal ['a', 'b', 'c'], 'abc'.split("")
end
assert('String#split(UTF-8)', '15.2.10.5.35') do
got = "こんにちは世界!".split('')
assert_equal ['こ', 'ん', 'に', 'ち', 'は', '世', '界', '!'], got
got = "こんにちは世界!".split('に')
assert_equal ['こん', 'ちは世界!'], got
end if UTF8STRING
assert('String#sub', '15.2.10.5.36') do
assert_equal 'aBcabc', 'abcabc'.sub('b', 'B')
assert_equal 'aBcabc', 'abcabc'.sub('b') { |w| w.capitalize }
assert_equal 'aa$', 'aa#'.sub('#', '$')
assert_equal '.abc', "abc".sub("", ".")
str = "abc"
miss = str.sub("X", "Z")
assert_equal str, miss
assert_not_same str, miss
a = []
assert_equal '.abc', "abc".sub("") { |i| a << i; "." }
assert_equal [""], a
end
assert('String#sub with backslash') do
s = 'abXcdXef'
assert_equal 'ab<\\>cdXef', s.sub('X', '<\\\\>')
assert_equal 'ab<X>cdXef', s.sub('X', '<\\&>')
assert_equal 'ab<X>cdXef', s.sub('X', '<\\0>')
assert_equal 'ab<ab>cdXef', s.sub('X', '<\\`>')
assert_equal 'ab<cdXef>cdXef', s.sub('X', '<\\\'>')
end
assert('String#sub!', '15.2.10.5.37') do
a = 'abcabc'
a.sub!('b', 'B')
b = 'abcabc'
b.sub!('b') { |w| w.capitalize }
assert_equal 'aBcabc', a
assert_equal 'aBcabc', b
end
assert('String#to_f', '15.2.10.5.38') do
assert_operator(0.0, :eql?, ''.to_f)
assert_operator(123456789.0, :eql?, '123456789'.to_f)
assert_operator(12345.6789, :eql?, '12345.6789'.to_f)
assert_operator(0.0, :eql?, '1e-2147483648'.to_f)
assert_operator(Float::INFINITY, :eql?, '1e2147483648'.to_f)
assert_operator(0.0, :eql?, 'a'.to_f)
assert_operator(4.0, :eql?, '4a5'.to_f)
assert_operator(12.0, :eql?, '1_2__3'.to_f)
assert_operator(123.0, :eql?, '1_2_3'.to_f)
assert_operator(68.0, :eql?, '68_'.to_f)
assert_operator(68.0, :eql?, '68._7'.to_f)
assert_operator(68.7, :eql?, '68.7_'.to_f)
assert_operator(68.7, :eql?, '68.7_ '.to_f)
assert_operator(6.0, :eql?, '6 8.7'.to_f)
assert_operator(68.0, :eql?, '68. 7'.to_f)
assert_operator(0.0, :eql?, '_68'.to_f)
assert_operator(0.0, :eql?, ' _68'.to_f)
assert_operator(12.34, :eql?, '1_2.3_4'.to_f)
assert_operator(12.3, :eql?, '1_2.3__4'.to_f)
assert_operator(0.9, :eql?, '.9'.to_f)
assert_operator(0.9, :eql?, "\t\r\n\f\v .9 \t\r\n\f\v".to_f)
end if Object.const_defined?(:Float)
assert('String#to_i', '15.2.10.5.39') do
assert_operator 0, :eql?, ''.to_i
assert_operator 32143, :eql?, '32143'.to_i
assert_operator 10, :eql?, 'a'.to_i(16)
assert_operator 4, :eql?, '100'.to_i(2)
assert_operator 1_000, :eql?, '1_000'.to_i
assert_operator 0, :eql?, 'a'.to_i
assert_operator 4, :eql?, '4a5'.to_i
assert_operator 12, :eql?, '1_2__3'.to_i
assert_operator 123, :eql?, '1_2_3'.to_i
assert_operator 68, :eql?, '68_'.to_i
assert_operator 68, :eql?, '68_ '.to_i
assert_operator 0, :eql?, '_68'.to_i
assert_operator 0, :eql?, ' _68'.to_i
assert_operator 68, :eql?, "\t\r\n\f\v 68 \t\r\n\f\v".to_i
assert_operator 6, :eql?, ' 6 8 '.to_i
end
assert('String#to_s', '15.2.10.5.40') do
assert_equal 'abc', 'abc'.to_s
end
assert('String#to_sym', '15.2.10.5.41') do
assert_equal :abc, 'abc'.to_sym
end
assert('String#upcase', '15.2.10.5.42') do
a = 'abc'.upcase
b = 'abc'
b.upcase
assert_equal 'ABC', a
assert_equal 'abc', b
end
assert('String#upcase!', '15.2.10.5.43') do
a = 'abc'
a.upcase!
assert_equal 'ABC', a
assert_equal nil, 'ABC'.upcase!
a = 'abcdefghijklmnopqrstuvwxyz'
b = a.dup
a.upcase!
b.upcase!
assert_equal 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', b
end
assert('String#inspect', '15.2.10.5.46') do
assert_equal "\"\\x00\"", "\0".inspect
assert_equal "\"foo\"", "foo".inspect
if UTF8STRING
assert_equal '"る"', "".inspect
else
assert_equal '"\xe3\x82\x8b"', "".inspect
end
# should not raise an exception - regress #1210
assert_nothing_raised do
("\1" * 100).inspect
end
end
# Not ISO specified
assert('String interpolation (mrb_str_concat for shared strings)') do
a = "A" * 32
assert_equal "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:", "#{a}:"
end
assert('String#bytes') do
str1 = "hello"
bytes1 = [104, 101, 108, 108, 111]
str2 = "\xFF"
bytes2 = [0xFF]
assert_equal bytes1, str1.bytes
assert_equal bytes2, str2.bytes
end
assert('String#each_byte') do
str1 = "hello"
bytes1 = [104, 101, 108, 108, 111]
bytes2 = []
str1.each_byte {|b| bytes2 << b }
assert_equal bytes1, bytes2
end
assert('String#freeze') do
str = "hello"
str.freeze
assert_raise(FrozenError) { str.upcase! }
end
assert('String literal concatenation') do
assert_equal 2, ("A" "B").size
assert_equal 3, ('A' "B" 'C').size
assert_equal 4, (%(A) "B#{?C}" "D").size
end
assert('String#getbyte') do
str1 = "hello"
bytes1 = [104, 101, 108, 108, 111]
assert_equal bytes1[0], str1.getbyte(0)
assert_equal bytes1[-1], str1.getbyte(-1)
assert_equal bytes1[6], str1.getbyte(6)
str2 = "\xFF"
bytes2 = [0xFF]
assert_equal bytes2[0], str2.getbyte(0)
end
assert('String#setbyte') do
str1 = "hello"
h = "H".getbyte(0)
str1.setbyte(0, h)
assert_equal(h, str1.getbyte(0))
assert_equal("Hello", str1)
end
assert('String#byteslice') do
str1 = "hello"
str2 = "\u3042ab" # "\xE3\x81\x82ab"
assert_equal("h", str1.byteslice(0))
assert_equal("e", str1.byteslice(1))
assert_equal(nil, str1.byteslice(5))
assert_equal("o", str1.byteslice(-1))
assert_equal(nil, str1.byteslice(-6))
assert_equal("\xE3", str2.byteslice(0))
assert_equal("\x81", str2.byteslice(1))
assert_equal(nil, str2.byteslice(5))
assert_equal("b", str2.byteslice(-1))
assert_equal(nil, str2.byteslice(-6))
assert_equal("", str1.byteslice(0, 0))
assert_equal(str1, str1.byteslice(0, 6))
assert_equal("el", str1.byteslice(1, 2))
assert_equal("", str1.byteslice(5, 1))
assert_equal("o", str1.byteslice(-1, 6))
assert_equal(nil, str1.byteslice(-6, 1))
assert_equal(nil, str1.byteslice(0, -1))
assert_equal("", str2.byteslice(0, 0))
assert_equal(str2, str2.byteslice(0, 6))
assert_equal("\x81\x82", str2.byteslice(1, 2))
assert_equal("", str2.byteslice(5, 1))
assert_equal("b", str2.byteslice(-1, 6))
assert_equal(nil, str2.byteslice(-6, 1))
assert_equal(nil, str2.byteslice(0, -1))
assert_equal("ell", str1.byteslice(1..3))
assert_equal("el", str1.byteslice(1...3))
assert_equal("h", str1.byteslice(0..0))
assert_equal("", str1.byteslice(5..0))
assert_equal("o", str1.byteslice(4..5))
assert_equal(nil, str1.byteslice(6..0))
assert_equal("", str1.byteslice(-1..0))
assert_equal("llo", str1.byteslice(-3..5))
assert_equal("\x81\x82a", str2.byteslice(1..3))
assert_equal("\x81\x82", str2.byteslice(1...3))
assert_equal("\xE3", str2.byteslice(0..0))
assert_equal("", str2.byteslice(5..0))
assert_equal("b", str2.byteslice(4..5))
assert_equal(nil, str2.byteslice(6..0))
assert_equal("", str2.byteslice(-1..0))
assert_equal("\x82ab", str2.byteslice(-3..5))
assert_raise(ArgumentError) { str1.byteslice }
assert_raise(ArgumentError) { str1.byteslice(1, 2, 3) }
assert_raise(TypeError) { str1.byteslice("1") }
assert_raise(TypeError) { str1.byteslice("1", 2) }
assert_raise(TypeError) { str1.byteslice(1, "2") }
assert_raise(TypeError) { str1.byteslice(1..2, 3) }
skip unless Object.const_defined?(:Float)
assert_equal("o", str1.byteslice(4.0))
assert_equal("\x82ab", str2.byteslice(2.0, 3.0))
end
+47
View File
@@ -0,0 +1,47 @@
[
# [:Object, :implementation_defined_value, '15.2.2.1'],
[:Module, :Object, '15.2.2.2'],
[:Class, :Module, '15.2.3.2'],
[:NilClass, :Object, '15.2.4.2'],
[:TrueClass, :Object, '15.2.5.2'],
[:FalseClass, :Object, '15.2.6.2'],
[:Numeric, :Object, '15.2.7.2'],
[:Integer, :Numeric, '15.2.8.2'],
[:Float, :Numeric, '15.2.9.2'],
[:String, :Object, '15.2.10.2'],
[:Symbol, :Object, '15.2.11.2'],
[:Array, :Object, '15.2.12.2'],
[:Hash, :Object, '15.2.13.2'],
[:Range, :Object, '15.2.14.2'],
# [:Regexp, :Object, '15.2.15.2'], #No Regexp in mruby core
# [:MatchData, :Object, '15.2.16.2'],
[:Proc, :Object, '15.2.17.2'],
# [:Struct, :Object, '15.2.18.2'],
# [:Time, :Object, '15.2.19.2'],
# [:IO, :Object, '15.2.20.2'],
# [:File, :IO, '15.2.21.2'],
[:Exception, :Object, '15.2.22.2'],
[:StandardError, :Exception, '15.2.23.2'],
[:ArgumentError, :StandardError, '15.2.24.2'],
# [:LocalJumpError, :StandardError, '15.2.25.2'],
[:LocalJumpError, :ScriptError, '15.2.25.2'], # mruby specific
[:RangeError, :StandardError, '12.2.26.2'],
[:RegexpError, :StandardError, '12.2.27.2'],
[:RuntimeError, :StandardError, '12.2.28.2'],
[:TypeError, :StandardError, '12.2.29.2'],
# [:ZeroDivisionError, :StandardError, '12.2.30.2'], # No ZeroDivisionError in mruby
[:NameError, :StandardError, '15.2.31.2'],
[:NoMethodError, :NameError, '15.2.32.2'],
[:IndexError, :StandardError, '15.2.33.2'],
# [:IOError, :StandardError, '12.2.34.2'],
# [:EOFError, :IOError, '12.2.35.2'],
# [:SystemCallError, :StandardError, '15.2.36.2'],
[:ScriptError, :Exception, '12.2.37.2'],
[:SyntaxError, :ScriptError, '12.2.38.2'],
# [:LoadError, :ScriptError, '12.2.39,2'],
].each do |cls, super_cls, iso|
assert "Direct superclass of #{cls}", iso do
skip "#{cls} isn't defined" unless Object.const_defined? cls
assert_equal Object.const_get(super_cls), Object.const_get(cls).superclass
end
end
+34
View File
@@ -0,0 +1,34 @@
##
# Symbol ISO Test
assert('Symbol') do
assert_equal :"a", :a
assert_equal :"a#{1}", :a1
assert_equal :'a', :a
assert_equal :'a#{1}', :"a\#{1}"
end
assert('Symbol', '15.2.11') do
assert_equal Class, Symbol.class
end
assert('Symbol#===', '15.2.11.3.1') do
assert_true :abc === :abc
assert_false :abc === :cba
end
assert('Symbol#id2name', '15.2.11.3.2') do
assert_equal 'abc', :abc.id2name
end
assert('Symbol#to_s', '15.2.11.3.3') do
assert_equal 'abc', :abc.to_s
end
assert('Symbol#to_sym', '15.2.11.3.4') do
assert_equal :abc, :abc.to_sym
end
assert('Symbol#to_proc') do
assert_equal 5, :abs.to_proc[-5]
end
+683
View File
@@ -0,0 +1,683 @@
assert('__FILE__') do
file = __FILE__[-9, 9]
assert_equal 'syntax.rb', file
end
assert('__LINE__') do
assert_equal 7, __LINE__
end
assert('super', '11.3.4') do
assert_raise NoMethodError do
super
end
class SuperFoo
def foo
true
end
def bar(*a)
a
end
end
class SuperBar < SuperFoo
def foo
super
end
def bar(*a)
super(*a)
end
end
bar = SuperBar.new
assert_true bar.foo
assert_equal [1,2,3], bar.bar(1,2,3)
end
assert('yield', '11.3.5') do
assert_raise LocalJumpError do
yield
end
assert_raise LocalJumpError do
o = Object.new
def o.foo
yield
end
o.foo
end
end
assert('redo in a for loop (#3275)') do
sum = 0
for i in 1..10
sum += i
i -= 1
if i > 0
redo
end
end
assert_equal 220, sum
end
assert('Abbreviated variable assignment', '11.4.2.3.2') do
a ||= 1
b &&= 1
c = 1
c += 2
assert_equal 1, a
assert_nil b
assert_equal 3, c
end
assert('case expression', '11.5.2.2.4') do
# case-expression-with-expression, one when-clause
x = 0
case "a"
when "a"
x = 1
end
assert_equal 1, x
# case-expression-with-expression, multiple when-clauses
x = 0
case "b"
when "a"
x = 1
when "b"
x = 2
end
assert_equal 2, x
# no matching when-clause
x = 0
case "c"
when "a"
x = 1
when "b"
x = 2
end
assert_equal 0, x
# case-expression-with-expression, one when-clause and one else-clause
a = 0
case "c"
when "a"
x = 1
else
x = 3
end
assert_equal 3, x
# case-expression-without-expression, one when-clause
x = 0
case
when true
x = 1
end
assert_equal 1, x
# case-expression-without-expression, multiple when-clauses
x = 0
case
when 0 == 1
x = 1
when 1 == 1
x = 2
end
assert_equal 2, x
# case-expression-without-expression, one when-clause and one else-clause
x = 0
case
when 0 == 1
x = 1
else
x = 3
end
assert_equal 3, x
# multiple when-arguments
x = 0
case 4
when 1, 3, 5
x = 1
when 2, 4, 6
x = 2
end
assert_equal 2, x
# when-argument with splatting argument
x = :integer
odds = [ 1, 3, 5, 7, 9 ]
evens = [ 2, 4, 6, 8 ]
case 5
when *odds
x = :odd
when *evens
x = :even
end
assert_equal :odd, x
true
end
assert('Nested const reference') do
module Syntax4Const
CONST1 = "hello world"
class Const2
def const1
CONST1
end
end
end
assert_equal "hello world", Syntax4Const::CONST1
assert_equal "hello world", Syntax4Const::Const2.new.const1
end
assert('Abbreviated variable assignment as returns') do
module Syntax4AbbrVarAsgnAsReturns
class A
def b
@c ||= 1
end
end
end
assert_equal 1, Syntax4AbbrVarAsgnAsReturns::A.new.b
end
assert('Splat and multiple assignment') do
*a = *[1,2,3]
b, *c = *[7,8,9]
assert_equal [1,2,3], a
assert_equal 7, b
assert_equal [8,9], c
(a, b), c = [1,2],3
assert_equal [1,2,3], [a,b,c]
(a, b), c = 1,2,3
assert_equal [1,nil,2], [a,b,c]
end
assert('Splat and multiple assignment from variable') do
a = [1, 2, 3]
b, *c = a
assert_equal 1, b
assert_equal [2, 3], c
end
assert('Splat and multiple assignment from variables') do
a = [1, 2, 3]
b = [4, 5, 6, 7]
c, d, *e, f, g = *a, *b
assert_equal 1, c
assert_equal 2, d
assert_equal [3, 4, 5], e
assert_equal 6, f
assert_equal 7, g
end
assert('Splat and multiple assignment in for') do
a = [1, 2, 3, 4, 5, 6, 7]
for b, c, *d, e, f in [a] do
end
assert_equal 1, b
assert_equal 2, c
assert_equal [3, 4, 5], d
assert_equal 6, e
assert_equal 7, f
end
assert('Splat without assignment') do
* = [0]
a, * = [1, 2]
assert_equal 1, a
end
assert('multiple assignment (rest)') do
*a = 0
assert_equal [0], a
end
assert('multiple assignment (rest+post)') do
*a, b = 0, 1, 2
*c, d = 3
assert_equal [0, 1], a
assert_equal 2, b
assert_equal [], c
assert_equal 3, d
end
assert('multiple assignment (nosplat array rhs)') do
a, *b = []
*c, d = [0]
e, *f, g = [1, 2]
assert_nil a
assert_equal [], b
assert_equal [], c
assert_equal 0, d
assert_equal 1, e
assert_equal [], f
assert_equal 2, g
end
assert('multiple assignment (empty array rhs #3236, #3239)') do
a,b,*c = []; assert_equal [nil, nil, []], [a, b, c]
a,b,*c = [1]; assert_equal [1, nil, []], [a, b, c]
a,b,*c = [nil]; assert_equal [nil,nil, []], [a, b, c]
a,b,*c = [[]]; assert_equal [[], nil, []], [a, b, c]
end
assert('Return values of case statements') do
a = [] << case 1
when 3 then 2
when 2 then 2
when 1 then 2
end
b = [] << case 1
when 2 then 2
else
end
def fb
n = 0
Proc.new do
n += 1
case
when n % 15 == 0
else n
end
end
end
assert_equal [2], a
assert_equal [nil], b
assert_equal 1, fb.call
end
assert('Return values of if and case statements') do
true_clause_value =
if true
1
else
case 2
when 3
end
4
end
assert_equal 1, true_clause_value
end
assert('Return values of no expression case statement') do
when_value =
case
when true
1
end
assert_equal 1, when_value
end
assert('splat object in assignment') do
o = Object.new
def o.to_a
nil
end
assert_equal [o], (a = *o)
def o.to_a
1
end
assert_raise(TypeError) { a = *o }
def o.to_a
[2]
end
assert_equal [2], (a = *o)
end
assert('splat object in case statement') do
o = Object.new
def o.to_a
nil
end
a = case o
when *o
1
end
assert_equal 1, a
end
assert('splat in case statement') do
values = [3,5,1,7,8]
testa = [1,2,7]
testb = [5,6]
resulta = []
resultb = []
resultc = []
values.each do |value|
case value
when *testa
resulta << value
when *testb
resultb << value
else
resultc << value
end
end
assert_equal [1,7], resulta
assert_equal [5], resultb
assert_equal [3,8], resultc
end
assert('External command execution.') do
module Kernel
sym = '`'.to_sym
alias_method :old_cmd, sym
results = []
define_method(sym) do |str|
results.push str
str
end
`test` # NOVAL NODE_XSTR
`test dynamic #{sym}` # NOVAL NODE_DXSTR
assert_equal ['test', 'test dynamic `'], results
t = `test` # VAL NODE_XSTR
assert_equal 'test', t
assert_equal ['test', 'test dynamic `', 'test'], results
t = `test dynamic #{sym}` # VAL NODE_DXSTR
assert_equal 'test dynamic `', t
assert_equal ['test', 'test dynamic `', 'test', 'test dynamic `'], results
results = []
assert_equal 'test sym test sym test', `test #{:sym} test #{:sym} test`
alias_method sym, :old_cmd
end
true
end
assert('parenthesed do-block in cmdarg') do
class ParenDoBlockCmdArg
def test(block)
block.call
end
end
x = ParenDoBlockCmdArg.new
result = x.test (Proc.new do :ok; end)
assert_equal :ok, result
end
assert('method definition in cmdarg') do
result = class MethodDefinitionInCmdarg
def self.bar(arg); arg end
bar def foo; self.each do end end
end
assert_equal(:foo, result)
end
assert('optional argument in the rhs default expressions') do
class OptArgInRHS
def foo
"method called"
end
def t(foo = foo)
foo
end
def t2(foo = foo())
foo
end
end
o = OptArgInRHS.new
assert_nil(o.t)
assert_equal("method called", o.t2)
end
assert('optional block argument in the rhs default expressions') do
assert_nil(Proc.new {|foo = foo| foo}.call)
end
assert('local variable definition in default value and subsequent arguments') do
def m(a = b = 1, c) [a, b, c] end
assert_equal([1, 1, :c], m(:c))
assert_equal([:a, nil, :c], m(:a, :c))
def m(a = b = 1, &c) [a, b, c ? true : nil] end
assert_equal([1, 1, nil], m)
assert_equal([1, 1, true], m{})
assert_equal([:a, nil, nil], m(:a))
assert_equal([:a, nil, true], m(:a){})
end
assert('multiline comments work correctly') do
=begin
this is a comment with nothing after begin and end
=end
=begin this is a comment
this is a comment with extra after =begin
=end
=begin
this is a comment that has =end with spaces after it
=end
=begin this is a comment
this is a comment that has extra after =begin and =end with spaces after it
=end
line = __LINE__
=begin this is a comment
this is a comment that has extra after =begin and =end with tabs after it
=end xxxxxxxxxxxxxxxxxxxxxxxxxx
assert_equal(line + 4, __LINE__)
end
assert 'keyword arguments' do
def m(a, b:1) [a, b] end
assert_equal [1, 1], m(1)
assert_equal [1, 2], m(1, b: 2)
def m(a, b:) [a, b] end
assert_equal [1, 2], m(1, b: 2)
assert_raise(ArgumentError) { m b: 1 }
assert_raise(ArgumentError) { m 1 }
def m(a:) a end
assert_equal 1, m(a: 1)
assert_raise(ArgumentError) { m }
assert_raise(ArgumentError) { m 'a' => 1, a: 1 }
h = { a: 1 }
assert_equal 1, m(h)
assert_equal({ a: 1 }, h)
def m(a: 1) a end
assert_equal 1, m
assert_equal 2, m(a: 2)
assert_raise(ArgumentError) { m 1 }
def m(**) end
assert_nil m
assert_nil m a: 1, b: 2
assert_raise(ArgumentError) { m 2 }
def m(a, **) a end
assert_equal 1, m(1)
assert_equal 1, m(1, a: 2, b: 3)
assert_equal({ 'a' => 1, b: 2 }, m('a' => 1, b: 2))
def m(a, **k) [a, k] end
assert_equal [1, {}], m(1)
assert_equal [1, {a: 2, b: 3}], m(1, a: 2, b: 3)
assert_equal [{'a' => 1, b: 2}, {}], m('a' => 1, b: 2)
def m(a=1, **) a end
assert_equal 1, m
assert_equal 2, m(2, a: 1, b: 0)
assert_raise(ArgumentError) { m('a' => 1, a: 2) }
def m(a=1, **k) [a, k] end
assert_equal [1, {}], m
assert_equal [1, {a: 1}], m(a: 1)
assert_equal [2, {a: 1, b: 2}], m(2, a: 1, b: 2)
assert_equal [{a: 1}, {b: 2}], m({a: 1}, {b: 2})
def m(*, a:) a end
assert_equal 1, m(a: 1)
assert_equal 3, m(1, 2, a: 3)
assert_raise(ArgumentError) { m('a' => 1, a: 2) }
def m(*a, b:) [a, b] end
assert_equal [[], 1], m(b: 1)
assert_equal [[1, 2], 3], m(1, 2, b: 3)
assert_raise(ArgumentError) { m('a' => 1, b: 2) }
def m(*a, b: 1) [a, b] end
assert_equal [[], 1], m
assert_equal [[1, 2, 3], 4], m(1, 2, 3, b: 4)
assert_raise(ArgumentError) { m('a' => 1, b: 2) }
def m(*, **) end
assert_nil m()
assert_nil m(a: 1, b: 2)
assert_nil m(1, 2, 3, a: 4, b: 5)
def m(*a, **) a end
assert_equal [], m()
assert_equal [1, 2, 3], m(1, 2, 3, a: 4, b: 5)
assert_raise(ArgumentError) { m("a" => 1, a: 1) }
assert_equal [1], m(1, **{a: 2})
def m(*, **k) k end
assert_equal({}, m())
assert_equal({a: 4, b: 5}, m(1, 2, 3, a: 4, b: 5))
assert_raise(ArgumentError) { m("a" => 1, a: 1) }
def m(a = nil, b = nil, **k) [a, k] end
assert_equal [nil, {}], m()
assert_equal([nil, {a: 1}], m(a: 1))
assert_raise(ArgumentError) { m("a" => 1, a: 1) }
assert_equal([{"a" => 1}, {a: 1}], m({ "a" => 1 }, a: 1))
assert_equal([{a: 1}, {}], m({a: 1}, {}))
assert_equal([nil, {}], m({}))
def m(*a, **k) [a, k] end
assert_equal([[], {}], m())
assert_equal([[1], {}], m(1))
assert_equal([[], {a: 1, b: 2}], m(a: 1, b: 2))
assert_equal([[1, 2, 3], {a: 2}], m(1, 2, 3, a: 2))
assert_raise(ArgumentError) { m("a" => 1, a: 1) }
assert_raise(ArgumentError) { m("a" => 1) }
assert_equal([[], {a: 1}], m(a: 1))
assert_raise(ArgumentError) { m("a" => 1, a: 1) }
assert_equal([[{"a" => 1}], {a: 1}], m({ "a" => 1 }, a: 1))
assert_equal([[{a: 1}], {}], m({a: 1}, {}))
assert_raise(ArgumentError) { m({a: 1}, {"a" => 1}) }
def m(a:, b:) [a, b] end
assert_equal([1, 2], m(a: 1, b: 2))
assert_raise(ArgumentError) { m("a" => 1, a: 1, b: 2) }
def m(a:, b: 1) [a, b] end
assert_equal([1, 1], m(a: 1))
assert_equal([1, 2], m(a: 1, b: 2))
assert_raise(ArgumentError) { m("a" => 1, a: 1, b: 2) }
def m(a:, **) a end
assert_equal(1, m(a: 1))
assert_equal(1, m(a: 1, b: 2))
assert_raise(ArgumentError) { m("a" => 1, a: 1, b: 2) }
def m(a:, **k) [a, k] end
assert_equal([1, {}], m(a: 1))
assert_equal([1, {b: 2, c: 3}], m(a: 1, b: 2, c: 3))
assert_raise(ArgumentError) { m("a" => 1, a: 1, b: 2) }
=begin
def m(a:, &b) [a, b] end
assert_equal([1, nil], m(a: 1))
assert_equal([1, l], m(a: 1, &(l = ->{})))
=end
def m(a: 1, b:) [a, b] end
assert_equal([1, 0], m(b: 0))
assert_equal([3, 2], m(b: 2, a: 3))
assert_raise(ArgumentError) { m a: 1 }
def m(a: def m(a: 1) a end, b:)
[a, b]
end
assert_equal([2, 3], m(a: 2, b: 3))
assert_equal([:m, 1], m(b: 1))
# Note the default value of a: in the original method.
assert_equal(1, m())
def m(a: 1, b: 2) [a, b] end
assert_equal([1, 2], m())
assert_equal([4, 3], m(b: 3, a: 4))
def m(a: 1, **) a end
assert_equal(1, m())
assert_equal(2, m(a: 2, b: 1))
def m(a: 1, **k) [a, k] end
assert_equal([1, {b: 2, c: 3}], m(b: 2, c: 3))
def m(a:, **) yield end
assert_raise(ArgumentError) { m { :blk } }
assert_equal :blk, m(a: 1){ :blk }
def m(a:, **k, &b) [b.call, k] end
assert_raise(ArgumentError) { m { :blk } }
assert_equal [:blk, {b: 2}], m(a: 1, b: 2){ :blk }
def m(**k, &b) [k, b] end
assert_equal([{ a: 1, b: 2}, nil], m(a: 1, b: 2))
assert_equal :blk, m{ :blk }[1].call
def m(hsh = {}) hsh end
assert_equal({ a: 1, b: 2 }, m(a: 1, b: 2))
assert_equal({ a: 1, 'b' => 2 }, m(a: 1, 'b' => 2))
def m(hsh) hsh end
assert_equal({ a: 1, b: 2 }, m(a: 1, b: 2))
assert_equal({ a: 1, 'b' => 2 }, m(a: 1, 'b' => 2))
=begin
def m(a, b=1, *c, (*d, (e)), f: 2, g:, h:, **k, &l)
[a, b, c, d, e, f, g, h, k, l]
end
result = m(9, 8, 7, 6, f: 5, g: 4, h: 3, &(l = ->{}))
assert_equal([9, 8, [7], [], 6, 5, 4, 3, {}, l], result)
def m a, b=1, *c, d, e:, f: 2, g:, **k, &l
[a, b, c, d, e, f, g, k, l]
end
result = m(1, 2, e: 3, g: 4, h: 5, i: 6, &(l = ->{}))
assert_equal([1, 1, [], 2, 3, 2, 4, { h: 5, i: 6 }, l], result)
=end
def m(a: b = 1, c:) [a, b, c] end
assert_equal([1, 1, :c], m(c: :c))
assert_equal([:a, nil, :c], m(a: :a, c: :c))
end
assert('numbered parameters') do
assert_equal(15, [1,2,3,4,5].reduce {_1+_2})
assert_equal(45, Proc.new do _1 + _2 + _3 + _4 + _5 + _6 + _7 + _8 + _9 end.call(*[1, 2, 3, 4, 5, 6, 7, 8, 9]))
end
assert('_0 is not numbered parameter') do
_0 = :l
assert_equal(:l, ->{_0}.call)
end
+31
View File
@@ -0,0 +1,31 @@
##
# TrueClass ISO Test
assert('TrueClass', '15.2.5') do
assert_equal Class, TrueClass.class
end
assert('TrueClass true', '15.2.5.1') do
assert_true true
assert_equal TrueClass, true.class
assert_false TrueClass.method_defined? :new
end
assert('TrueClass#&', '15.2.5.3.1') do
assert_true true.&(true)
assert_false true.&(false)
end
assert('TrueClass#^', '15.2.5.3.2') do
assert_false true.^(true)
assert_true true.^(false)
end
assert('TrueClass#to_s', '15.2.5.3.3') do
assert_equal 'true', true.to_s
end
assert('TrueClass#|', '15.2.5.3.4') do
assert_true true.|(true)
assert_true true.|(false)
end
+6
View File
@@ -0,0 +1,6 @@
##
# TypeError ISO Test
assert('TypeError', '15.2.29') do
assert_equal Class, TypeError.class
end
+39
View File
@@ -0,0 +1,39 @@
# Test of the \u notation
assert('bare \u notation test') do
# Mininum and maximum one byte characters
assert_equal("\x00", "\u0000")
assert_equal("\x7F", "\u007F")
# Mininum and maximum two byte characters
assert_equal("\xC2\x80", "\u0080")
assert_equal("\xDF\xBF", "\u07FF")
# Mininum and maximum three byte characters
assert_equal("\xE0\xA0\x80", "\u0800")
assert_equal("\xEF\xBF\xBF", "\uFFFF")
# Four byte characters require the \U notation
end
assert('braced \u notation test') do
# Mininum and maximum one byte characters
assert_equal("\x00", "\u{0000}")
assert_equal("\x7F", "\u{007F}")
# Mininum and maximum two byte characters
assert_equal("\xC2\x80", "\u{0080}")
assert_equal("\xDF\xBF", "\u{07FF}")
# Mininum and maximum three byte characters
assert_equal("\xE0\xA0\x80", "\u{0800}")
assert_equal("\xEF\xBF\xBF", "\u{FFFF}")
# Mininum and maximum four byte characters
assert_equal("\xF0\x90\x80\x80", "\u{10000}")
assert_equal("\xF4\x8F\xBF\xBF", "\u{10FFFF}")
end
assert('braced multiple \u notation test') do
assert_equal("ABC", "\u{41 42 43}")
end
+58
View File
@@ -0,0 +1,58 @@
# coding: utf-8-emacs
def sclass(v)
class << v
self
end
end
assert('mrb_vformat') do
vf = TestVFormat
assert_equal '', vf.z('')
assert_equal 'No specifier!', vf.z('No specifier!')
assert_equal '`c`: C', vf.c('`c`: %c', ?C)
assert_equal '`d`: 123', vf.d('`d`: %d', 123)
assert_equal '`d`: -79', vf.d('`d`: %d', -79)
assert_equal '`i`: 514', vf.i('`i`: %i', 514)
assert_equal '`i`: -83', vf.i('`i`: %i', -83)
assert_equal '`t`: NilClass', vf.v('`t`: %t', nil)
assert_equal '`t`: FalseClass', vf.v('`t`: %t', false)
assert_equal '`t`: TrueClass', vf.v('`t`: %t', true)
assert_equal '`t`: Fixnum', vf.v('`t`: %t', 0)
assert_equal '`t`: Hash', vf.v('`t`: %t', {k: "value"})
assert_match '#<Class:#<Class:#<Hash:0x*>>>', vf.v('%t', sclass({}))
assert_equal 'string and length', vf.l('string %l length', 'andante', 3)
assert_equal '`n`: sym', vf.n('`n`: %n', :sym)
assert_equal '%C文字列%', vf.s('%s', '%C文字列%')
assert_equal '`C`: Kernel module', vf.C('`C`: %C module', Kernel)
assert_equal '`C`: NilClass', vf.C('`C`: %C', nil.class)
assert_match '#<Class:#<String:0x*>>', vf.C('%C', sclass(""))
assert_equal '`T`: NilClass', vf.v('`T`: %T', nil)
assert_equal '`T`: FalseClass', vf.v('`T`: %T', false)
assert_equal '`T`: TrueClass', vf.v('`T`: %T', true)
assert_equal '`T`: Fixnum', vf.v('`T`: %T', 0)
assert_equal '`T`: Hash', vf.v('`T`: %T', {k: "value"})
assert_match 'Class', vf.v('%T', sclass({}))
assert_equal '`Y`: nil', vf.v('`Y`: %Y', nil)
assert_equal '`Y`: false', vf.v('`Y`: %Y', false)
assert_equal '`Y`: true', vf.v('`Y`: %Y', true)
assert_equal '`Y`: Fixnum', vf.v('`Y`: %Y', 0)
assert_equal '`Y`: Hash', vf.v('`Y`: %Y', {k: "value"})
assert_equal 'Class', vf.v('%Y', sclass({}))
assert_match '#<Class:#<String:0x*>>', vf.v('%v', sclass(""))
assert_equal '`v`: 1...3', vf.v('`v`: %v', 1...3)
assert_equal '`S`: {:a=>1, "b"=>"c"}', vf.v('`S`: %S', {a: 1, "b" => ?c})
assert_equal 'percent: %', vf.z('percent: %%')
assert_equal '"I": inspect char', vf.c('%!c: inspect char', ?I)
assert_equal '709: inspect mrb_int', vf.i('%!d: inspect mrb_int', 709)
assert_equal '"a\x00b\xff"', vf.l('%!l', "a\000b\xFFc\000d", 4)
assert_equal ':"&.": inspect symbol', vf.n('%!n: inspect symbol', :'&.')
assert_equal 'inspect "String"', vf.v('inspect %!v', 'String')
assert_equal 'inspect Array: [1, :x, {}]', vf.v('inspect Array: %!v', [1,:x,{}])
assert_match '`!C`: #<Class:0x*>', vf.C('`!C`: %!C', Class.new)
assert_equal 'escape: \\%a,b,c,d', vf.v('escape: \\\\\%a,b,\c%v', ',d')
skip unless Object.const_defined?(:Float)
assert_equal '`f`: 0.0125', vf.f('`f`: %f', 0.0125)
assert_equal '-Infinity', vf.f('%f', -Float::INFINITY)
assert_equal 'NaN: Not a Number', vf.f('%f: Not a Number', Float::NAN)
end