diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-07-17 10:35:41 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-07-17 10:35:41 +0900 |
| commit | d605b72c1d6fa4564a0a5e88535504b6850463b5 (patch) | |
| tree | 774fc0de56002abb3bb2b1c3387ff08f91876d17 /test | |
| parent | 2af92d0ebcbeca6d3d85a27c8193273080a63090 (diff) | |
| parent | 9af3b7c6258de327218dd04e69d76ae68caf17b1 (diff) | |
| download | mruby-d605b72c1d6fa4564a0a5e88535504b6850463b5.tar.gz mruby-d605b72c1d6fa4564a0a5e88535504b6850463b5.zip | |
Merge branch 'master' into i110/inspect-recursion
Diffstat (limited to 'test')
| -rw-r--r-- | test/assert.rb | 373 | ||||
| -rw-r--r-- | test/bintest.rb | 11 | ||||
| -rw-r--r-- | test/report.rb | 4 | ||||
| -rw-r--r-- | test/t/array.rb | 66 | ||||
| -rw-r--r-- | test/t/bs_block.rb | 63 | ||||
| -rw-r--r-- | test/t/class.rb | 60 | ||||
| -rw-r--r-- | test/t/codegen.rb | 6 | ||||
| -rw-r--r-- | test/t/enumerable.rb | 6 | ||||
| -rw-r--r-- | test/t/exception.rb | 4 | ||||
| -rw-r--r-- | test/t/float.rb | 57 | ||||
| -rw-r--r-- | test/t/hash.rb | 11 | ||||
| -rw-r--r-- | test/t/integer.rb | 70 | ||||
| -rw-r--r-- | test/t/kernel.rb | 145 | ||||
| -rw-r--r-- | test/t/literals.rb | 2 | ||||
| -rw-r--r-- | test/t/module.rb | 425 | ||||
| -rw-r--r-- | test/t/numeric.rb | 93 | ||||
| -rw-r--r-- | test/t/proc.rb | 2 | ||||
| -rw-r--r-- | test/t/range.rb | 21 | ||||
| -rw-r--r-- | test/t/string.rb | 132 | ||||
| -rw-r--r-- | test/t/symbol.rb | 8 | ||||
| -rw-r--r-- | test/t/syntax.rb | 199 |
21 files changed, 1020 insertions, 738 deletions
diff --git a/test/assert.rb b/test/assert.rb index a9baae5e1..c493cbbc0 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -1,17 +1,47 @@ +$undefined = Object.new $ok_test = 0 $ko_test = 0 $kill_test = 0 +$skip_test = 0 $asserts = [] $test_start = Time.now if Object.const_defined?(:Time) -# Implementation of print due to the reason that there might be no print -def t_print(*args) - i = 0 - len = args.size - while i < len - str = args[i].to_s - __t_printstr__ str rescue print str - i += 1 +unless RUBY_ENGINE == "mruby" + # For bintest on Ruby + def t_print(*args) + print(*args) + $stdout.flush + nil + 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 @@ -19,16 +49,17 @@ 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 != '' - msg += " => #{e.cause}" if e && e.respond_to?(:cause) - msg += " => #{e.message}" if e && !e.respond_to?(:cause) - msg += " (mrbgems: #{GEMNAME})" if Object.const_defined?(:GEMNAME) - if $mrbtest_assert && $mrbtest_assert.size > 0 + msg += " [#{iso}]" if iso && !iso.empty? + msg += " => #{e}" if e && !e.to_s.empty? + msg += " (#{GEMNAME == 'mruby-test' ? 'core' : "mrbgems: #{GEMNAME}"})" + if $mrbtest_assert $mrbtest_assert.each do |idx, assert_msg, diff| - msg += "\n - Assertion[#{idx}] Failed: #{assert_msg}\n#{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\t#{bt.join("\n\t")}" if bt + msg += "\nbacktrace:\n #{bt.join("\n ")}" if bt msg end @@ -43,206 +74,292 @@ end def assert(str = 'Assertion failed', 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 = [] - $mrbtest_assert_idx = 0 + + 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) - $asserts.push(assertion_string('Fail: ', str, iso, nil)) - $ko_test += 1 - t_print('F') + if $mrbtest_assert.size > 0 + if $mrbtest_assert.size == $mrbtest_child_noassert[-1] + $asserts.push(assertion_string('Info: ', str, iso)) + $mrbtest_child_noassert[-2] += 1 + $ok_test += 1 + t_print('.') + else + $asserts.push(assertion_string('Fail: ', str, iso)) + $ko_test += 1 + t_print('F') + end 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 bt = e.backtrace if $mrbtest_verbose - if e.class.to_s == 'MRubyTestSkip' - $asserts.push(assertion_string('Skip: ', str, iso, e, nil)) - t_print('?') + $asserts.push(assertion_string("#{e.class}: ", str, iso, e, bt)) + $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 - $asserts.push(assertion_string("#{e.class}: ", str, iso, e, bt)) - $kill_test += 1 - t_print('X') + parent_asserts.concat $asserts end - ensure - $mrbtest_assert = nil + $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" + + " Expected: #{exp.inspect}\n" \ " Actual: #{act.inspect}" end -def assert_true(ret, msg = nil, diff = nil) - if $mrbtest_assert - $mrbtest_assert_idx += 1 - unless ret - msg = "Expected #{ret.inspect} to be true" unless msg - diff = assertion_diff(true, ret) unless diff - $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff]) +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 - ret + obj end -def assert_false(ret, msg = nil, diff = nil) - if $mrbtest_assert - $mrbtest_assert_idx += 1 - if ret - msg = "Expected #{ret.inspect} to be false" unless msg - diff = assertion_diff(false, ret) unless diff +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 - $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff]) +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 - !ret + assert_true(ret, msg, diff) end -def assert_equal(arg1, arg2 = nil, arg3 = nil) - if block_given? - exp, act, msg = arg1, yield, arg2 - else - exp, act, msg = arg1, arg2, arg3 +def assert_nil(obj, msg = nil) + unless ret = obj.nil? + diff = " Expected #{obj.inspect} to be nil." end + assert_true(ret, msg, diff) +end - msg = "Expected to be equal" unless msg - diff = assertion_diff(exp, act) - assert_true(exp == act, msg, diff) +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_not_equal(arg1, arg2 = nil, arg3 = nil) - if block_given? - exp, act, msg = arg1, yield, arg2 - else - exp, act, msg = arg1, arg2, arg3 +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 - msg = "Expected to be not equal" unless msg - diff = assertion_diff(exp, act) - assert_false(exp == act, msg, diff) +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 -def assert_nil(obj, msg = nil) - msg = "Expected #{obj.inspect} to be nil" unless msg - diff = assertion_diff(nil, obj) - assert_true(obj.nil?, msg, diff) +## +# 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) + receiver, *args = RUBY_ENGINE == "mruby" ? + [self, :_str_match?, pattern, str] : + [File, :fnmatch?, pattern, str, File::FNM_EXTGLOB|File::FNM_DOTMATCH] + unless ret = !receiver.__send__(*args) == !affirmed + diff = " Expected #{pattern.inspect} to #{'not ' unless affirmed}match #{str.inspect}." + end + assert_true(ret, msg, diff) end -def assert_include(collection, obj, msg = nil) - msg = "Expected #{collection.inspect} to include #{obj.inspect}" unless msg - diff = " Collection: #{collection.inspect}\n" + - " Object: #{obj.inspect}" - assert_true(collection.include?(obj), msg, diff) +## +# 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 -def assert_not_include(collection, obj, msg = nil) - msg = "Expected #{collection.inspect} to not include #{obj.inspect}" unless msg - diff = " Collection: #{collection.inspect}\n" + - " Object: #{obj.inspect}" - assert_false(collection.include?(obj), msg, diff) +## +# 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) - return true unless $mrbtest_assert - $mrbtest_assert_idx += 1 - msg = (exc.last.is_a? String) ? exc.pop : nil - + exc = exc.empty? ? StandardError : exc.size == 1 ? exc[0] : exc begin yield - msg ||= "Expected to raise #{exc} but nothing was raised." - diff = nil - $mrbtest_assert.push [$mrbtest_assert_idx, msg, diff] - false rescue *exc - true + pass rescue Exception => e - msg ||= "Expected to raise #{exc}, not" - diff = " Class: <#{e.class}>\n" + - " Message: #{e.message}" - $mrbtest_assert.push [$mrbtest_assert_idx, msg, diff] - false + 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) - return true unless $mrbtest_assert - $mrbtest_assert_idx += 1 - begin yield - true rescue Exception => e - msg ||= "Expected not to raise #{exc.join(', ')} but it raised" - diff = " Class: <#{e.class}>\n" + - " Message: #{e.message}" - $mrbtest_assert.push [$mrbtest_assert_idx, msg, diff] - false + diff = " Exception raised:\n" \ + " Class: <#{e.class}>\n" \ + " Message: <#{e}>" + flunk(msg, diff) + else + pass end end -## -# Fails unless +obj+ is a kind of +cls+. -def assert_kind_of(cls, obj, msg = nil) - msg = "Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}" unless msg - diff = assertion_diff(cls, obj.class) - assert_true(obj.kind_of?(cls), msg, diff) +def pass + assert_true(true) end -## -# Fails unless +exp+ is equal to +act+ in terms of a Float -def assert_float(exp, act, msg = nil) - msg = "Float #{exp} expected to be equal to float #{act}" unless msg - diff = assertion_diff(exp, act) - assert_true check_float(exp, act), msg, diff +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() +def report t_print("\n") $asserts.each do |msg| - t_print "#{msg}\n" + t_print("#{msg}\n") end - $total_test = $ok_test+$ko_test+$kill_test + $total_test = $ok_test + $ko_test + $kill_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(" 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 -## -# Performs fuzzy check for equality on methods returning floats -def check_float(a, b) - tolerance = Mrbtest::FLOAT_TOLERANCE - a = a.to_f - b = b.to_f - if a.finite? and b.finite? - (a-b).abs < tolerance +def _eval_assertion(meth, exp, act_or_msg, msg, block) + if block + exp, act, msg = exp, block.call, act_or_msg else - true + exp, act, msg = exp, act_or_msg, msg end + return exp.__send__(meth, act), exp, act, msg end ## # Skip the test -class MRubyTestSkip < NotImplementedError - attr_accessor :cause - def initialize(cause) - @cause = cause - end -end +class MRubyTestSkip < NotImplementedError; end def skip(cause = "") raise MRubyTestSkip.new(cause) diff --git a/test/bintest.rb b/test/bintest.rb index 12971a9d9..ed71e57fd 100644 --- a/test/bintest.rb +++ b/test/bintest.rb @@ -1,6 +1,8 @@ $:.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/ @@ -19,15 +21,22 @@ def shellquote(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 -load 'test/report.rb' +exit report diff --git a/test/report.rb b/test/report.rb deleted file mode 100644 index fb77fd0aa..000000000 --- a/test/report.rb +++ /dev/null @@ -1,4 +0,0 @@ -report -if $ko_test > 0 or $kill_test > 0 - raise "mrbtest failed (KO:#{$ko_test}, Crash:#{$kill_test})" -end diff --git a/test/t/array.rb b/test/t/array.rb index 3ed3d54ec..eec31d751 100644 --- a/test/t/array.rb +++ b/test/t/array.rb @@ -55,9 +55,10 @@ assert('Array#[]', '15.2.12.5.4') do assert_equal(nil, [1,2,3].[](-4)) a = [ "a", "b", "c", "d", "e" ] - assert_equal("b", a[1.1]) if class_defined?("Float") 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 @@ -238,7 +239,7 @@ assert('Array#pop', '15.2.12.5.21') do assert_equal([1,2], a) assert_equal(3, b) - assert_raise(RuntimeError) { [].freeze.pop } + assert_raise(FrozenError) { [].freeze.pop } end assert('Array#push', '15.2.12.5.22') do @@ -287,7 +288,7 @@ assert('Array#shift', '15.2.12.5.27') do assert_equal([2,3], a) assert_equal(1, b) - assert_raise(RuntimeError) { [].freeze.shift } + assert_raise(FrozenError) { [].freeze.shift } end assert('Array#size', '15.2.12.5.28') do @@ -297,11 +298,38 @@ assert('Array#size', '15.2.12.5.28') do end assert('Array#slice', '15.2.12.5.29') do - a = "12345".slice(1, 3) - b = a.slice(0) - - assert_equal("2:", "#{b}:") - assert_equal(2, [1,2,3].[](1)) + 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 @@ -361,13 +389,6 @@ end # Not ISO specified -assert("Array (Shared Array Corruption)") do - a = [ "a", "b", "c", "d", "e", "f" ] - b = a.slice(1, 3) - a.clear - b.clear -end - 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) @@ -387,16 +408,15 @@ assert("Array#rindex") do 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(RuntimeError) do + assert_raise(FrozenError) do a[0] = 1 end end - -assert('shared array replace') do - a = [0] * 40 - b = [0, 1, 2] - b.replace a[1, 20].dup - assert_equal 20, b.size -end diff --git a/test/t/bs_block.rb b/test/t/bs_block.rb index 62eb7e32e..995e52559 100644 --- a/test/t/bs_block.rb +++ b/test/t/bs_block.rb @@ -408,42 +408,43 @@ assert('BS Block 32') do end assert('BS Block [ruby-core:14395]') 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 + 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 render(&block) + "in render" + end end - def respond - @response.call + 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 - t = Controller.new - assert_true t.test_for_bug end assert("BS Block 33") do diff --git a/test/t/class.rb b/test/t/class.rb index a5118fa93..e2839111c 100644 --- a/test/t/class.rb +++ b/test/t/class.rb @@ -36,7 +36,7 @@ end assert('Class#new', '15.2.3.3.3') do assert_raise(TypeError, 'Singleton should raise TypeError') do - "a".singleton_class.new + (class <<"a"; self; end).new end class TestClass @@ -236,6 +236,11 @@ assert('class to return the last value') do 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 @@ -293,15 +298,7 @@ assert('singleton tests') do end end - assert_false baz.singleton_methods.include? :run_foo_mod - assert_false baz.singleton_methods.include? :run_baz - - assert_raise(NoMethodError, 'should raise NoMethodError') do - baz.run_foo_mod - end - assert_raise(NoMethodError, 'should raise NoMethodError') do - baz.run_baz - end + assert_equal :run_baz, baz assert_raise(NoMethodError, 'should raise NoMethodError') do bar.run_foo_mod @@ -318,8 +315,8 @@ assert('singleton tests') do self end - assert_true baz.singleton_methods.include? :run_baz - assert_true baz.singleton_methods.include? :run_foo_mod + 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 @@ -358,7 +355,14 @@ assert('singleton tests') do 7 end end - end if class_defined?("Float") + 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 @@ -368,7 +372,7 @@ assert('clone Class') do end end - Foo.clone.new.func + assert_true(Foo.clone.new.func) end assert('class variable and class << self style class method') do @@ -436,16 +440,26 @@ assert('overriding class variable with a module (#3235)') do 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 - -assert("remove_method doesn't segfault if the passed in argument isn't a symbol") do - klass = Class.new - assert_raise(TypeError) { klass.remove_method nil } - assert_raise(TypeError) { klass.remove_method 123 } - assert_raise(TypeError) { klass.remove_method 1.23 } - assert_raise(NameError) { klass.remove_method "hello" } - assert_raise(TypeError) { klass.remove_method Class.new } -end diff --git a/test/t/codegen.rb b/test/t/codegen.rb index 4c9e2c594..acb9e1bf5 100644 --- a/test/t/codegen.rb +++ b/test/t/codegen.rb @@ -184,14 +184,14 @@ assert('register window of calls (#3783)') do # NODE_UNDEF assert_nothing_raised do class << Object.new - undef send + undef inspect end end # NODE_ALIAS assert_nothing_raised do class << Object.new - alias send2 send + alias inspect2 inspect end end -end
\ No newline at end of file +end diff --git a/test/t/enumerable.rb b/test/t/enumerable.rb index 359c3451b..9e7602db7 100644 --- a/test/t/enumerable.rb +++ b/test/t/enumerable.rb @@ -45,7 +45,7 @@ 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 } + assert_equal 'a', [1,2,3].detect(->{"a"}) { false } end assert('Array#each_with_index', '15.3.2.2.5') do @@ -64,11 +64,11 @@ 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 } + assert_equal 'a', [1,2,3].find(->{"a"}) { false } end assert('Enumerable#find_all', '15.3.2.2.8') do - assert_true [1,2,3,4,5,6,7,8,9].find_all() {|i| i%2 == 0}, [2,4,6,8] + 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 diff --git a/test/t/exception.rb b/test/t/exception.rb index ce7b5841e..bdf277c1e 100644 --- a/test/t/exception.rb +++ b/test/t/exception.rb @@ -263,10 +263,10 @@ assert('Exception 13') do end assert('Exception 14') do - def exception_test14; UnknownConstant; end + def (o = Object.new).exception_test14; UnknownConstant end a = :ng begin - send(:exception_test14) + o.__send__(:exception_test14) rescue a = :ok end diff --git a/test/t/float.rb b/test/t/float.rb index 92f7a15f1..63bf83f40 100644 --- a/test/t/float.rb +++ b/test/t/float.rb @@ -1,7 +1,7 @@ ## # Float ISO Test -if class_defined?("Float") +if Object.const_defined?(:Float) assert('Float', '15.2.9') do assert_equal Class, Float.class @@ -82,8 +82,8 @@ assert('Float#ceil', '15.2.9.3.8') do end assert('Float#finite?', '15.2.9.3.9') do - assert_true 3.123456789.finite? - assert_false (1.0 / 0.0).finite? + assert_predicate 3.123456789, :finite? + assert_not_predicate 1.0 / 0.0, :finite? end assert('Float#floor', '15.2.9.3.10') do @@ -139,7 +139,7 @@ assert('Float#round', '15.2.9.3.12') do nan = 0.0/0.0 assert_raise(FloatDomainError){ nan.round } assert_raise(FloatDomainError){ nan.round(-1) } - assert_true(nan.round(1).nan?) + assert_predicate(nan.round(1), :nan?) end assert('Float#to_f', '15.2.9.3.13') do @@ -178,10 +178,10 @@ assert('Float#divmod') do end assert('Float#nan?') do - assert_true (0.0/0.0).nan? - assert_false 0.0.nan? - assert_false (1.0/0.0).nan? - assert_false (-1.0/0.0).nan? + 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 @@ -206,4 +206,43 @@ assert('Float#>>') do assert_equal(-1, -23.0 >> 128) end -end # class_defined?("Float") +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.0.to_s) + assert_equal("-0.0", -0.0.to_s) + assert_equal("-3.25", -3.25.to_s) + assert_equal("50.0", 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.0", 100000.0.to_s) + assert_equal("-100000.0", -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.0", 100000000000000.0.to_s) + assert_equal("-100000000000000.0", -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) diff --git a/test/t/hash.rb b/test/t/hash.rb index 63029be42..cd47d251d 100644 --- a/test/t/hash.rb +++ b/test/t/hash.rb @@ -8,8 +8,9 @@ end assert('Hash#==', '15.2.13.4.1') do assert_true({ 'abc' => 'abc' } == { 'abc' => 'abc' }) assert_false({ 'abc' => 'abc' } == { 'cba' => 'cba' }) - assert_true({ :equal => 1 } == { :equal => 1.0 }) if class_defined?("Float") 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 @@ -82,12 +83,12 @@ assert('Hash#default_proc', '15.2.13.4.7') do end assert('Hash#delete', '15.2.13.4.8') do - a = { 'abc' => 'abc' } - b = { 'abc' => 'abc' } + a = { 'abc' => 'ABC' } + b = { 'abc' => 'ABC' } b_tmp_1 = false b_tmp_2 = false - a.delete('abc') + assert_equal 'ABC', a.delete('abc') b.delete('abc') do |k| b_tmp_1 = true end @@ -371,7 +372,7 @@ end assert('Hash#freeze') do h = {}.freeze - assert_raise(RuntimeError) do + assert_raise(FrozenError) do h[:a] = 'b' end end diff --git a/test/t/integer.rb b/test/t/integer.rb index cea97a1e6..4ab49eb0a 100644 --- a/test/t/integer.rb +++ b/test/t/integer.rb @@ -7,10 +7,10 @@ end assert('Integer#+', '15.2.8.3.1') do a = 1+1 - b = 1+1.0 if class_defined?("Float") + b = 1+1.0 if Object.const_defined?(:Float) assert_equal 2, a - assert_equal 2.0, b if class_defined?("Float") + assert_equal 2.0, b if Object.const_defined?(:Float) assert_raise(TypeError){ 0+nil } assert_raise(TypeError){ 1+nil } @@ -18,40 +18,38 @@ assert('Integer#+', '15.2.8.3.1') do c = Mrbtest::FIXNUM_MAX + 1 d = Mrbtest::FIXNUM_MAX.__send__(:+, 1) - if class_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 + 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 class_defined?("Float") + b = 2-1.0 if Object.const_defined?(:Float) assert_equal 1, a - assert_equal 1.0, b if class_defined?("Float") + assert_equal 1.0, b if Object.const_defined?(:Float) c = Mrbtest::FIXNUM_MIN - 1 d = Mrbtest::FIXNUM_MIN.__send__(:-, 1) - if class_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 + 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 class_defined?("Float") + b = 1*1.0 if Object.const_defined?(:Float) assert_equal 1, a - assert_equal 1.0, b if class_defined?("Float") + assert_equal 1.0, b if Object.const_defined?(:Float) assert_raise(TypeError){ 0*nil } assert_raise(TypeError){ 1*nil } @@ -59,13 +57,12 @@ assert('Integer#*', '15.2.8.3.3') do c = Mrbtest::FIXNUM_MAX * 2 d = Mrbtest::FIXNUM_MAX.__send__(:*, 2) - if class_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 + 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 @@ -226,8 +223,9 @@ assert('Integer#times', '15.2.8.3.22') do 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 if class_defined?("Float") +end assert('Integer#to_i', '15.2.8.3.24') do assert_equal 1, 1.to_i @@ -259,19 +257,3 @@ assert('Integer#divmod', '15.2.8.3.30') do assert_equal [-2, -1], 25.divmod(-13) assert_equal [ 1, -6], -13.divmod(-7) end - -# Not ISO specified - -assert('Integer#step') do - a = [] - b = [] - 1.step(3) do |i| - a << i - end - 1.step(6, 2) do |i| - b << i - end - - assert_equal [1, 2, 3], a - assert_equal [1, 3, 5], b -end diff --git a/test/t/kernel.rb b/test/t/kernel.rb index 561118302..ecfb863a8 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -31,10 +31,6 @@ end # Kernel.eval is provided by the mruby-gem mrbgem. '15.3.1.2.3' -assert('Kernel.global_variables', '15.3.1.2.4') do - assert_equal Array, Kernel.global_variables.class -end - assert('Kernel.iterator?', '15.3.1.2.5') do assert_false Kernel.iterator? end @@ -103,7 +99,7 @@ assert('Kernel#__send__', '15.3.1.3.4') do # test with argument assert_true __send__(:respond_to?, :nil?) # test without argument and without block - assert_equal Array, __send__(:public_methods).class + assert_equal String, __send__(:to_s).class end assert('Kernel#block_given?', '15.3.1.3.6') do @@ -171,7 +167,7 @@ assert('Kernel#clone', '15.3.1.3.8') do 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? @@ -244,6 +240,9 @@ assert('Kernel#extend', '15.3.1.3.13') do 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 @@ -261,10 +260,23 @@ assert('Kernel#freeze') do 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#global_variables', '15.3.1.3.14') do - assert_equal Array, global_variables.class +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 @@ -278,30 +290,6 @@ assert('Kernel#inspect', '15.3.1.3.17') do assert_equal "main", s end -assert('Kernel#instance_variable_defined?', '15.3.1.3.20') do - o = Object.new - o.instance_variable_set(:@a, 1) - - assert_true o.instance_variable_defined?("@a") - assert_false o.instance_variable_defined?("@b") - assert_true o.instance_variable_defined?("@a"[0,2]) - assert_true o.instance_variable_defined?("@abc"[0,2]) -end - -assert('Kernel#instance_variables', '15.3.1.3.23') do - o = Object.new - o.instance_eval do - @a = 11 - @b = 12 - end - ivars = o.instance_variables - - assert_equal Array, ivars.class, - assert_equal(2, ivars.size) - assert_true ivars.include?(:@a) - assert_true ivars.include?(:@b) -end - assert('Kernel#is_a?', '15.3.1.3.24') do assert_true is_a?(Kernel) assert_false is_a?(Array) @@ -381,10 +369,6 @@ assert('Kernel#method_missing', '15.3.1.3.30') do end end -assert('Kernel#methods', '15.3.1.3.31') do - assert_equal Array, methods.class -end - assert('Kernel#nil?', '15.3.1.3.32') do assert_false nil? end @@ -408,23 +392,6 @@ end # Kernel#print is defined in mruby-print mrbgem. '15.3.1.3.35' -assert('Kernel#private_methods', '15.3.1.3.36') do - assert_equal Array, private_methods.class -end - -assert('Kernel#protected_methods', '15.3.1.3.37') do - assert_equal Array, protected_methods.class -end - -assert('Kernel#public_methods', '15.3.1.3.38') do - assert_equal Array, public_methods.class - class Foo - def foo - end - end - assert_equal [:foo], Foo.new.public_methods(false) -end - # Kernel#puts is defined in mruby-print mrbgem. '15.3.1.3.39' assert('Kernel#raise', '15.3.1.3.40') do @@ -450,11 +417,12 @@ assert('Kernel#remove_instance_variable', '15.3.1.3.41') do tri = Test4RemoveInstanceVar.new assert_equal 99, tri.var - tri.remove + assert_equal 99, tri.remove assert_equal nil, tri.var - assert_raise NameError do - tri.remove - end + 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' @@ -485,57 +453,10 @@ assert('Kernel#respond_to?', '15.3.1.3.43') do assert_false Test4RespondTo.new.respond_to?(:test_method) end -assert('Kernel#send', '15.3.1.3.44') do - # test with block - l = send(:lambda) do - true - end - - assert_true l.call - assert_equal l.class, Proc - # test with argument - assert_true send(:respond_to?, :nil?) - # test without argument and without block - assert_equal send(:public_methods).class, Array -end - -assert('Kernel#singleton_methods', '15.3.1.3.45') do - assert_equal singleton_methods.class, Array -end - assert('Kernel#to_s', '15.3.1.3.46') do assert_equal to_s.class, String end -assert('Kernel#to_s on primitives') do - begin - Fixnum.alias_method :to_s_, :to_s - Fixnum.remove_method :to_s - - assert_nothing_raised do - # segfaults if mrb_cptr is used - 1.to_s - end - ensure - Fixnum.alias_method :to_s, :to_s_ - Fixnum.remove_method :to_s_ - end -end - -assert('Kernel.local_variables', '15.3.1.2.7') do - a, b = 0, 1 - a += b - - vars = Kernel.local_variables.sort - assert_equal [:a, :b, :vars], vars - - assert_equal [:a, :b, :c, :vars], Proc.new { |a, b| - c = 2 - # Kernel#local_variables: 15.3.1.3.28 - local_variables.sort - }.call(-1, -2) -end - assert('Kernel#!=') do str1 = "hello" str2 = str1 @@ -578,22 +499,6 @@ assert('Kernel#respond_to_missing?') do assert_false Test4RespondToMissing.new.respond_to?(:no_method) end -assert('Kernel#global_variables') do - variables = global_variables - 1.upto(9) do |i| - assert_equal variables.include?(:"$#{i}"), true - end -end - -assert('Kernel#define_singleton_method') do - o = Object.new - ret = o.define_singleton_method(:test_method) do - :singleton_method_ok - end - assert_equal :test_method, ret - assert_equal :singleton_method_ok, o.test_method -end - assert('stack extend') do def recurse(count, stop) return count if count > stop diff --git a/test/t/literals.rb b/test/t/literals.rb index 51a37c32d..6344219aa 100644 --- a/test/t/literals.rb +++ b/test/t/literals.rb @@ -22,7 +22,7 @@ assert('Literals Numerical', '8.7.6.2') do # decimal assert_equal 999, 0d999 assert_equal 999, 0D999 - # decimal seperator + # decimal separator assert_equal 10000000, 10_000_000 assert_equal 10, 1_0 # integer with exponent diff --git a/test/t/module.rb b/test/t/module.rb index 5a46c24ff..7f869bf1f 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -3,7 +3,7 @@ def labeled_module(name, &block) Module.new do - singleton_class.class_eval do + (class <<self; self end).class_eval do define_method(:to_s) { name } alias_method :inspect, :to_s end @@ -13,7 +13,7 @@ end def labeled_class(name, supklass = Object, &block) Class.new(supklass) do - singleton_class.class_eval do + (class <<self; self end).class_eval do define_method(:to_s) { name } alias_method :inspect, :to_s end @@ -25,26 +25,22 @@ assert('Module', '15.2.2') do assert_equal Class, Module.class end -# TODO not implemented ATM assert('Module.constants', '15.2.2.3.1') do - -# TODO not implemented ATM assert('Module.nesting', '15.2.2.3.2') do - -assert('Module.nesting', '15.2.2.2.2') do - module Test4ModuleNesting - module Test4ModuleNesting2 - assert_equal [Test4ModuleNesting2, Test4ModuleNesting], - Module.nesting +assert('Module#alias_method', '15.2.2.4.8') do + cls = Class.new do + def foo + "FOO" end end - module Test4ModuleNesting::Test4ModuleNesting2 - assert_equal [Test4ModuleNesting::Test4ModuleNesting2], Module.nesting - 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 - sc = Test4ModuleAncestors.singleton_class r = String.ancestors assert_equal Array, r.class @@ -63,6 +59,7 @@ assert('Module#append_features', '15.2.2.4.10') do end assert_equal Test4AppendFeatures2, Test4AppendFeatures2.const_get(:Const4AppendFeatures2) + assert_raise(FrozenError) { Module.new.append_features Class.new.freeze } end assert('Module#attr NameError') do @@ -213,56 +210,9 @@ assert('Module#class_eval', '15.2.2.4.15') do def method1 end end - r = Test4ClassEval.instance_methods - assert_equal 11, Test4ClassEval.class_eval{ @a } assert_equal 12, Test4ClassEval.class_eval{ @b } - assert_equal Array, r.class - assert_true r.include?(:method1) -end - -assert('Module#class_variable_defined?', '15.2.2.4.16') do - class Test4ClassVariableDefined - @@cv = 99 - end - - assert_true Test4ClassVariableDefined.class_variable_defined?(:@@cv) - assert_false Test4ClassVariableDefined.class_variable_defined?(:@@noexisting) -end - -assert('Module#class_variable_get', '15.2.2.4.17') do - class Test4ClassVariableGet - @@cv = 99 - end - - assert_equal 99, Test4ClassVariableGet.class_variable_get(:@@cv) -end - -assert('Module#class_variable_set', '15.2.2.4.18') do - class Test4ClassVariableSet - @@foo = 100 - def foo - @@foo - end - end - - assert_true Test4ClassVariableSet.class_variable_set(:@@cv, 99) - assert_true Test4ClassVariableSet.class_variable_set(:@@foo, 101) - assert_true Test4ClassVariableSet.class_variables.include? :@@cv - assert_equal 99, Test4ClassVariableSet.class_variable_get(:@@cv) - assert_equal 101, Test4ClassVariableSet.new.foo -end - -assert('Module#class_variables', '15.2.2.4.19') do - class Test4ClassVariables1 - @@var1 = 1 - end - class Test4ClassVariables2 < Test4ClassVariables1 - @@var2 = 2 - end - - assert_equal [:@@var1], Test4ClassVariables1.class_variables - assert_equal [:@@var2, :@@var1], Test4ClassVariables2.class_variables + assert_equal true, Test4ClassEval.new.respond_to?(:method1) end assert('Module#const_defined?', '15.2.2.4.20') do @@ -272,6 +222,7 @@ assert('Module#const_defined?', '15.2.2.4.20') do assert_true Test4ConstDefined.const_defined?(:Const4Test4ConstDefined) assert_false Test4ConstDefined.const_defined?(:NotExisting) + assert_raise(NameError){ Test4ConstDefined.const_defined?(:wrong_name) } end assert('Module#const_get', '15.2.2.4.21') do @@ -286,16 +237,7 @@ assert('Module#const_get', '15.2.2.4.21') do assert_raise(TypeError){ Test4ConstGet.const_get(123) } assert_raise(NameError){ Test4ConstGet.const_get(:I_DO_NOT_EXIST) } assert_raise(NameError){ Test4ConstGet.const_get("I_DO_NOT_EXIST::ME_NEITHER") } -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) + assert_raise(NameError){ Test4ConstGet.const_get(:wrong_name) } end assert('Module#const_set', '15.2.2.4.23') do @@ -303,23 +245,47 @@ assert('Module#const_set', '15.2.2.4.23') do Const4Test4ConstSet = 42 end - assert_true Test4ConstSet.const_set(:Const4Test4ConstSet, 23) + assert_equal 23, Test4ConstSet.const_set(:Const4Test4ConstSet, 23) assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet) + ["", "wrongNAME", "Wrong-Name"].each do |n| + assert_raise(NameError) { Test4ConstSet.const_set(n, 1) } + end end -assert('Module#constants', '15.2.2.4.24') do - $n = [] - module TestA - C = 1 +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(NameError) { Test4RemoveConst.remove_const(:NonExistingConst) } + %i[x X!].each do |n| + assert_raise(NameError) { Test4RemoveConst.remove_const(n) } end - class TestB - include TestA - C2 = 1 - $n = constants.sort + 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 [ :C ], TestA.constants - assert_equal [ :C, :C2 ], $n + 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 @@ -335,6 +301,7 @@ assert('Module#include', '15.2.2.4.27') do 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 @@ -366,48 +333,16 @@ assert('Module#included', '15.2.2.4.29') do assert_equal Test4Included2, Test4Included2.const_get(:Const4Included2) end -assert('Module#included_modules', '15.2.2.4.30') do - module Test4includedModules - end - module Test4includedModules2 - include Test4includedModules - end - r = Test4includedModules2.included_modules - - assert_equal Array, r.class - assert_true r.include?(Test4includedModules) -end - assert('Module#initialize', '15.2.2.4.31') do assert_kind_of Module, Module.new mod = Module.new { def hello; "hello"; end } - assert_equal [:hello], mod.instance_methods + 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#instance_methods', '15.2.2.4.33') do - module Test4InstanceMethodsA - def method1() end - end - class Test4InstanceMethodsB - def method2() end - end - class Test4InstanceMethodsC < Test4InstanceMethodsB - def method3() end - end - - r = Test4InstanceMethodsC.instance_methods(true) - - assert_equal [:method1], Test4InstanceMethodsA.instance_methods - assert_equal [:method2], Test4InstanceMethodsB.instance_methods(false) - assert_equal [:method3], Test4InstanceMethodsC.instance_methods(false) - assert_equal Array, r.class - assert_true r.include?(:method3) - assert_true r.include?(:method2) -end - assert('Module#method_defined?', '15.2.2.4.34') do module Test4MethodDefined module A @@ -431,7 +366,6 @@ assert('Module#method_defined?', '15.2.2.4.34') do assert_false Test4MethodDefined::C.method_defined? "method4" end - assert('Module#module_eval', '15.2.2.4.35') do module Test4ModuleEval @a = 11 @@ -442,55 +376,6 @@ assert('Module#module_eval', '15.2.2.4.35') do assert_equal 12, Test4ModuleEval.module_eval{ @b } end -assert('Module#remove_class_variable', '15.2.2.4.39') do - class Test4RemoveClassVariable - @@cv = 99 - end - - assert_equal 99, Test4RemoveClassVariable.remove_class_variable(:@@cv) - assert_false Test4RemoveClassVariable.class_variables.include? :@@cv -end - -assert('Module#remove_const', '15.2.2.4.40') do - module Test4RemoveConst - ExistingConst = 23 - end - - result = Test4RemoveConst.module_eval { remove_const :ExistingConst } - - name_error = false - begin - Test4RemoveConst.module_eval { remove_const :NonExistingConst } - rescue NameError - name_error = true - end - - # Constant removed from Module - assert_false Test4RemoveConst.const_defined? :ExistingConst - # Return value of binding - assert_equal 23, result - # Name Error raised when Constant doesn't exist - assert_true name_error -end - -assert('Module#remove_method', '15.2.2.4.41') do - module Test4RemoveMethod - class Parent - def hello - end - end - - class Child < Parent - def hello - end - end - end - - assert_true Test4RemoveMethod::Child.class_eval{ remove_method :hello } - assert_true Test4RemoveMethod::Child.instance_methods.include? :hello - assert_false Test4RemoveMethod::Child.instance_methods(false).include? :hello -end - assert('Module#undef_method', '15.2.2.4.42') do module Test4UndefMethod class Parent @@ -511,7 +396,6 @@ assert('Module#undef_method', '15.2.2.4.42') do assert_true Test4UndefMethod::Parent.new.respond_to?(:hello) assert_false Test4UndefMethod::Child.new.respond_to?(:hello) assert_false Test4UndefMethod::GrandChild.new.respond_to?(:hello) - assert_false Test4UndefMethod::Child.instance_methods(false).include? :hello end # Not ISO specified @@ -528,6 +412,15 @@ assert('Module#define_method') do 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 @@ -608,41 +501,6 @@ end assert_kind_of(b, c.new, bug8357) end - assert('Moduler#prepend + #instance_methods') do - bug6655 = '[ruby-core:45915]' - assert_equal(Object.instance_methods, Class.new {prepend Module.new}.instance_methods, bug6655) - end - - assert 'Module#prepend + #singleton_methods' do - o = Object.new - o.singleton_class.class_eval {prepend Module.new} - assert_equal([], o.singleton_methods) - end - - assert 'Module#prepend + #remove_method' do - c = Class.new do - prepend Module.new { def foo; end } - end - assert_raise(NameError) do - c.class_eval do - remove_method(:foo) - end - end - c.class_eval do - def foo; end - end - removed = nil - c.singleton_class.class_eval do - define_method(:method_removed) {|id| removed = id} - end - assert_nothing_raised('[Bug #7843]') do - c.class_eval do - remove_method(:foo) - end - end - assert_equal(:foo, removed) - end - assert 'Module#prepend + Class#ancestors' do bug6658 = '[ruby-core:45919]' m = labeled_module("m") @@ -683,12 +541,6 @@ end assert_equal([m3, m0, m1], m3.ancestors) end - assert 'Module#prepend #instance_methods(false)' do - bug6660 = '[ruby-dev:45863]' - assert_equal([:m1], Class.new{ prepend Module.new; def m1; end }.instance_methods(false), bug6660) - assert_equal([:m1], Class.new(Class.new{def m2;end}){ prepend Module.new; def m1; end }.instance_methods(false), bug6660) - end - assert 'cyclic Module#prepend' do bug7841 = '[ruby-core:52205] [Bug #7841]' m1 = Module.new @@ -699,7 +551,7 @@ end end end - # these assertions will not run without a #assert_seperately method + # these assertions will not run without a #assert_separately method #assert 'test_prepend_optmethod' do # bug7983 = '[ruby-dev:47124] [Bug #7983]' # assert_separately [], %{ @@ -715,76 +567,61 @@ end #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 + # 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 + #included_modules' do - bug8025 = '[ruby-core:53158] [Bug #8025]' - mixin = labeled_module("mixin") - c = labeled_module("c") {prepend mixin} - im = c.included_modules - assert_not_include(im, c, bug8025) - assert_include(im, mixin, bug8025) - c1 = labeled_class("c1") {prepend mixin} - c2 = labeled_class("c2", c1) - im = c2.included_modules - assert_not_include(im, c1, bug8025) - assert_not_include(im, c2, bug8025) - assert_include(im, mixin, bug8025) - 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 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") @@ -807,7 +644,7 @@ end assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should accesisble prepended module in superclass") end - # requires #assert_seperately + # requires #assert_separately #assert 'Module#prepend call super' do # assert_separately([], <<-'end;') #do # bug10847 = '[ruby-core:68093] [Bug #10847]' @@ -818,6 +655,10 @@ end # 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 @@ -838,11 +679,12 @@ assert('Module#to_s') do assert_equal 'SetOuter', SetOuter.to_s assert_equal 'SetOuter::SetInner', SetOuter::SetInner.to_s - mod = Module.new - cls = Class.new + assert_match "#<Module:0x*>", Module.new.to_s + assert_match "#<Class:0x*>", Class.new.to_s - assert_equal "#<Module:0x", mod.to_s[0,11] - assert_equal "#<Class:0x", cls.to_s[0,10] + 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 @@ -870,8 +712,8 @@ assert('Issue 1467') do include M1 end - C1.new - C2.new + assert_kind_of(M1, C1.new) + assert_kind_of(M1, C2.new) end assert('clone Module') do @@ -885,7 +727,7 @@ assert('clone Module') do include M1.clone end - B.new.foo + assert_true(B.new.foo) end assert('Module#module_function') do @@ -902,6 +744,15 @@ assert('module with non-class/module outer raises TypeError') do 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" diff --git a/test/t/numeric.rb b/test/t/numeric.rb index 38c62a669..bb95f8ef3 100644 --- a/test/t/numeric.rb +++ b/test/t/numeric.rb @@ -1,8 +1,21 @@ ## # 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 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 + assert_equal(Class, Numeric.class) end assert('Numeric#+@', '15.2.7.4.1') do @@ -15,15 +28,8 @@ end assert('Numeric#abs', '15.2.7.4.3') do assert_equal(1, 1.abs) - assert_equal(1.0, -1.abs) if class_defined?("Float") -end - -assert('Numeric#pow') do - assert_equal(8, 2 ** 3) - assert_equal(-8, -2 ** 3) - assert_equal(1, 2 ** 0) - assert_equal(1, 2.2 ** 0) - assert_equal(0.5, 2 ** -1) + skip unless Object.const_defined?(:Float) + assert_equal(1.0, -1.abs) end assert('Numeric#/', '15.2.8.3.4') do @@ -39,5 +45,70 @@ end # Not ISO specified assert('Numeric#**') do - assert_equal 8.0, 2.0**3 + 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 diff --git a/test/t/proc.rb b/test/t/proc.rb index 42ac3b941..b17b21e8c 100644 --- a/test/t/proc.rb +++ b/test/t/proc.rb @@ -157,7 +157,7 @@ assert('&obj call to_proc if defined') do def mock(&b) b end - assert_equal pr.object_id, mock(&pr).object_id + assert_same pr, mock(&pr) assert_equal pr, mock(&pr) obj = Object.new diff --git a/test/t/range.rb b/test/t/range.rb index 3e67fcc1c..106c2866e 100644 --- a/test/t/range.rb +++ b/test/t/range.rb @@ -8,7 +8,8 @@ end assert('Range#==', '15.2.14.4.1') do assert_true (1..10) == (1..10) assert_false (1..10) == (1..100) - assert_true (1..10) == Range.new(1.0, 10.0) if class_defined?("Float") + 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 @@ -59,7 +60,7 @@ assert('Range#initialize', '15.2.14.4.9') do assert_equal (1..10), b assert_false b.exclude_end? - assert_raise(NameError) { (0..1).send(:initialize, 1, 3) } + assert_raise(NameError) { (0..1).__send__(:initialize, 1, 3) } end assert('Range#last', '15.2.14.4.10') do @@ -93,3 +94,19 @@ assert('Range#eql?', '15.2.14.4.14') do 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 diff --git a/test/t/string.rb b/test/t/string.rb index e91b915fe..cf145f97e 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -2,7 +2,7 @@ ## # String ISO Test -UTF8STRING = ("\343\201\202".size == 1) +UTF8STRING = __ENCODING__ == "UTF-8" assert('String', '15.2.10') do assert_equal Class, String.class @@ -37,11 +37,14 @@ end assert('String#*', '15.2.10.5.5') do assert_equal 'aaaaa', 'a' * 5 assert_equal '', 'a' * 0 - assert_raise(ArgumentError) do - 'a' * -1 - end + assert_equal 'aa', 'a' * 2.1 + assert_raise(ArgumentError) { 'a' * -1 } + assert_raise(RangeError) { '' * 1e30 } + assert_raise(RangeError) { '' * Float::INFINITY } + assert_raise(RangeError) { '' * Float::NAN } + assert_raise(TypeError) { 'a' * '1' } + assert_raise(TypeError) { 'a' * nil } end - assert('String#[]', '15.2.10.5.6') do # length of args is 1 a = 'abc'[0] @@ -155,12 +158,15 @@ assert('String#[]=') do d[-10] = 'X' end - if class_defined?("Float") + 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 @@ -197,8 +203,62 @@ assert('String#[]=') do 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 @@ -253,19 +313,6 @@ assert('String#chomp!', '15.2.10.5.10') do assert_equal 'abc', e end -assert('String#chomp! uses the correct length') do - class A - def to_str - $s.replace("AA") - "A" - end - end - - $s = "AAA" - $s.chomp!(A.new) - assert_equal $s, "A" -end - assert('String#chop', '15.2.10.5.11') do a = ''.chop b = 'abc'.chop @@ -421,6 +468,17 @@ assert('String#index', '15.2.10.5.22') do assert_equal nil, "hello".index("", 6) 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("", 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') @@ -477,12 +535,12 @@ assert('String#reverse', '15.2.10.5.29') do end assert('String#reverse(UTF-8)', '15.2.10.5.29') do - assert_equal "ち", "こんにちは世界"[3] - assert_equal nil, "こんにちは世界"[20] - assert_equal "世", "こんにちは世界"[-2] - assert_equal "世界", "こんにちは世界"[-2..-1] - assert_equal "んに", "こんにちは世界"[1,2] - assert_equal "世", "こんにちは世界"["世"] + a = 'こんにちは世界!' + a.reverse + + assert_equal 'こんにちは世界!', a + assert_equal '!界世はちにんこ', 'こんにちは世界!'.reverse + assert_equal 'あ', 'あ'.reverse end if UTF8STRING assert('String#reverse!', '15.2.10.5.30') do @@ -499,6 +557,10 @@ assert('String#reverse!(UTF-8)', '15.2.10.5.30') do assert_equal '!界世はちにんこ', a assert_equal '!界世はちにんこ', 'こんにちは世界!'.reverse! + + b = 'あ' + b.reverse! + assert_equal 'あ', b end if UTF8STRING assert('String#rindex', '15.2.10.5.31') do @@ -516,7 +578,9 @@ assert('String#rindex(UTF-8)', '15.2.10.5.31') do assert_equal nil, str.index("さ") end if UTF8STRING -# 'String#scan', '15.2.10.5.32' will be tested in mrbgems. +# 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 @@ -592,7 +656,7 @@ assert('String#sub', '15.2.10.5.36') do str = "abc" miss = str.sub("X", "Z") assert_equal str, miss - assert_not_equal str.object_id, miss.object_id + assert_not_same str, miss a = [] assert_equal '.abc', "abc".sub("") { |i| a << i; "." } @@ -631,7 +695,7 @@ assert('String#to_f', '15.2.10.5.38') do assert_float(12345.6789, c) assert_float(0, d) assert_float(Float::INFINITY, e) -end if class_defined?("Float") +end if Object.const_defined?(:Float) assert('String#to_i', '15.2.10.5.39') do a = ''.to_i @@ -696,10 +760,6 @@ assert('String interpolation (mrb_str_concat for shared strings)') do assert_equal "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:", "#{a}:" end -assert('Check the usage of a NUL character') do - "qqq\0ppp" -end - assert('String#bytes') do str1 = "hello" bytes1 = [104, 101, 108, 108, 111] @@ -725,5 +785,11 @@ assert('String#freeze') do str = "hello" str.freeze - assert_raise(RuntimeError) { str.upcase! } + 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 diff --git a/test/t/symbol.rb b/test/t/symbol.rb index 9059f45c2..5c674a9cb 100644 --- a/test/t/symbol.rb +++ b/test/t/symbol.rb @@ -13,8 +13,8 @@ assert('Symbol', '15.2.11') do end assert('Symbol#===', '15.2.11.3.1') do - assert_true :abc == :abc - assert_false :abc == :cba + assert_true :abc === :abc + assert_false :abc === :cba end assert('Symbol#id2name', '15.2.11.3.2') do @@ -28,3 +28,7 @@ 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 diff --git a/test/t/syntax.rb b/test/t/syntax.rb index 299394557..afb735e7f 100644 --- a/test/t/syntax.rb +++ b/test/t/syntax.rb @@ -403,6 +403,9 @@ assert('External command execution.') do 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 @@ -420,10 +423,11 @@ assert('parenthesed do-block in cmdarg') do end assert('method definition in cmdarg') do - if false + result = class MethodDefinitionInCmdarg + def self.bar(arg); arg end bar def foo; self.each do end end end - true + assert_equal(:foo, result) end assert('optional argument in the rhs default expressions') do @@ -451,18 +455,203 @@ assert('multiline comments work correctly') do =begin this is a comment with nothing after begin and end =end -=begin this is a comment +=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 +=end =begin this is a comment this is a comment that has extra after =begin and =end with spaces after it -=end +=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 +end |
