diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/assert.rb | 29 | ||||
| -rw-r--r-- | test/bintest.rb | 10 | ||||
| -rw-r--r-- | test/driver.c | 16 | ||||
| -rw-r--r-- | test/init_mrbtest.c | 5 | ||||
| -rw-r--r-- | test/mrbtest.rake | 34 | ||||
| -rw-r--r-- | test/no_mrb_open_test_dummy.rb | 2 | ||||
| -rw-r--r-- | test/t/array.rb | 44 | ||||
| -rw-r--r-- | test/t/class.rb | 111 | ||||
| -rw-r--r-- | test/t/comparable.rb | 32 | ||||
| -rw-r--r-- | test/t/enumerable.rb | 24 | ||||
| -rw-r--r-- | test/t/exception.rb | 12 | ||||
| -rw-r--r-- | test/t/float.rb | 39 | ||||
| -rw-r--r-- | test/t/hash.rb | 38 | ||||
| -rw-r--r-- | test/t/integer.rb | 16 | ||||
| -rw-r--r-- | test/t/kernel.rb | 142 | ||||
| -rw-r--r-- | test/t/localjumperror.rb | 8 | ||||
| -rw-r--r-- | test/t/module.rb | 24 | ||||
| -rw-r--r-- | test/t/nil.rb | 9 | ||||
| -rw-r--r-- | test/t/numeric.rb | 10 | ||||
| -rw-r--r-- | test/t/string.rb | 37 | ||||
| -rw-r--r-- | test/t/syntax.rb | 180 | ||||
| -rw-r--r-- | test/t/true.rb | 10 | ||||
| -rw-r--r-- | test/t/unicode.rb | 35 |
23 files changed, 804 insertions, 63 deletions
diff --git a/test/assert.rb b/test/assert.rb index 3d35fc267..8fecc5b0a 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -9,10 +9,11 @@ def t_print(*args) i = 0 len = args.size while i < len + str = args[i].to_s begin - __printstr__ args[i].to_s + __printstr__ str rescue NoMethodError - __t_printstr__ args[i].to_s + __t_printstr__ str rescue print str end i += 1 end @@ -59,7 +60,7 @@ def assert(str = 'Assertion failed', iso = '') $asserts.push "Skip: #{str} #{iso} #{e.cause}" t_print('?') else - $asserts.push(assertion_string('Error: ', str, iso, e)) + $asserts.push(assertion_string("#{e.class}: ", str, iso, e)) $kill_test += 1 t_print('X') end @@ -77,7 +78,7 @@ end def assert_true(ret, msg = nil, diff = nil) if $mrbtest_assert $mrbtest_assert_idx += 1 - if !ret + 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]) @@ -173,6 +174,24 @@ def assert_raise(*exp) ret end +def assert_nothing_raised(*exp) + ret = true + if $mrbtest_assert + $mrbtest_assert_idx += 1 + msg = exp.last.class == String ? exp.pop : "" + begin + yield + rescue Exception => e + msg = "#{msg} exception raised." + diff = " Class: <#{e.class}>\n" + + " Message: #{e.message}" + $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff]) + ret = false + end + end + ret +end + ## # Fails unless +obj+ is a kind of +cls+. def assert_kind_of(cls, obj, msg = nil) @@ -199,7 +218,7 @@ def report() puts msg end - $total_test = $ok_test.+($ko_test) + $total_test = $ok_test+$ko_test+$kill_test t_print("Total: #{$total_test}\n") t_print(" OK: #{$ok_test}\n") diff --git a/test/bintest.rb b/test/bintest.rb new file mode 100644 index 000000000..0ff3341a0 --- /dev/null +++ b/test/bintest.rb @@ -0,0 +1,10 @@ +$:.unshift File.dirname(File.dirname(File.expand_path(__FILE__))) +require 'test/assert.rb' + +ARGV.each do |gem| + Dir["#{gem}/bintest/*.rb"].each do |file| + load file + end +end + +load 'test/report.rb' diff --git a/test/driver.c b/test/driver.c index 707794ff9..bcac73d25 100644 --- a/test/driver.c +++ b/test/driver.c @@ -24,9 +24,7 @@ mrb_init_mrbtest(mrb_state *); static void print_hint(void) { - printf("mrbtest - Embeddable Ruby Test\n"); - printf("\nThis is a very early version, please test and report errors.\n"); - printf("Thanks :)\n\n"); + printf("mrbtest - Embeddable Ruby Test\n\n"); } static int @@ -34,8 +32,8 @@ check_error(mrb_state *mrb) { /* Error check */ /* $ko_test and $kill_test should be 0 */ - mrb_value ko_test = mrb_gv_get(mrb, mrb_intern2(mrb, "$ko_test", 8)); - mrb_value kill_test = mrb_gv_get(mrb, mrb_intern2(mrb, "$kill_test", 10)); + mrb_value ko_test = mrb_gv_get(mrb, mrb_intern_lit(mrb, "$ko_test")); + mrb_value kill_test = mrb_gv_get(mrb, mrb_intern_lit(mrb, "$kill_test")); return mrb_fixnum_p(ko_test) && mrb_fixnum(ko_test) == 0 && mrb_fixnum_p(kill_test) && mrb_fixnum(kill_test) == 0; } @@ -63,14 +61,12 @@ eval_test(mrb_state *mrb) static void t_printstr(mrb_state *mrb, mrb_value obj) { - struct RString *str; char *s; int len; if (mrb_string_p(obj)) { - str = mrb_str_ptr(obj); - s = str->ptr; - len = str->len; + s = RSTRING_PTR(obj); + len = RSTRING_LEN(obj); fwrite(s, len, 1, stdout); } } @@ -104,7 +100,7 @@ main(int argc, char **argv) if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v') { printf("verbose mode: enable\n\n"); - mrb_gv_set(mrb, mrb_intern2(mrb, "$mrbtest_verbose", 16), mrb_true_value()); + mrb_gv_set(mrb, mrb_intern_lit(mrb, "$mrbtest_verbose"), mrb_true_value()); } krn = mrb->kernel_module; diff --git a/test/init_mrbtest.c b/test/init_mrbtest.c index 3b8f9129e..717578dc8 100644 --- a/test/init_mrbtest.c +++ b/test/init_mrbtest.c @@ -1,10 +1,8 @@ #include <stdlib.h> #include "mruby.h" #include "mruby/irep.h" -#include "mruby/dump.h" -#include "mruby/string.h" -#include "mruby/proc.h" +extern const uint8_t mrbtest_assert_irep[]; extern const uint8_t mrbtest_irep[]; void mrbgemtest_init(mrb_state* mrb); @@ -12,6 +10,7 @@ void mrbgemtest_init(mrb_state* mrb); void mrb_init_mrbtest(mrb_state *mrb) { + mrb_load_irep(mrb, mrbtest_assert_irep); mrb_load_irep(mrb, mrbtest_irep); #ifndef DISABLE_GEMS mrbgemtest_init(mrb); diff --git a/test/mrbtest.rake b/test/mrbtest.rake index 0507981d6..e8eb6addd 100644 --- a/test/mrbtest.rake +++ b/test/mrbtest.rake @@ -8,12 +8,15 @@ MRuby.each_target do mlib = clib.ext(exts.object) mrbs = Dir.glob("#{current_dir}/t/*.rb") init = "#{current_dir}/init_mrbtest.c" - asslib = "#{current_dir}/assert.rb" + ass_c = "#{current_build_dir}/assert.c" + ass_lib = ass_c.ext(exts.object) mrbtest_lib = libfile("#{current_build_dir}/mrbtest") - file mrbtest_lib => [mlib, gems.map(&:test_objs), gems.map { |g| g.test_rbireps.ext(exts.object) }].flatten do |t| + gem_test_files = gems.select { |g| g.run_test_in_other_mrb_state? }.map { |g| g.test_rbireps.ext(exts.object) } + file mrbtest_lib => [mlib, ass_lib, gems.map(&:test_objs), gem_test_files].flatten do |t| archiver.run t.name, t.prerequisites end + file mrbtest_lib => "#{build_dir}/test/no_mrb_open_test.c".ext(exts.object) unless build_mrbtest_lib_only? driver_obj = objfile("#{current_build_dir}/driver") @@ -27,20 +30,41 @@ MRuby.each_target do end end - file mlib => [clib] - file clib => [mrbcfile, init, asslib] + mrbs do |t| + file ass_lib => ass_c + file ass_c => "#{current_dir}/assert.rb" do |t| + FileUtils.mkdir_p File.dirname t.name + open(t.name, 'w') do |f| + mrbc.run f, [t.prerequisites], 'mrbtest_assert_irep' + end + end + + file mlib => clib + file clib => [mrbcfile, init] + mrbs do |t| _pp "GEN", "*.rb", "#{clib.relative_path}" FileUtils.mkdir_p File.dirname(clib) open(clib, 'w') do |f| + f.puts %Q[/*] + f.puts %Q[ * This file contains a list of all] + f.puts %Q[ * test functions.] + f.puts %Q[ *] + f.puts %Q[ * IMPORTANT:] + f.puts %Q[ * This file was generated!] + f.puts %Q[ * All manual changes will get lost.] + f.puts %Q[ */] + f.puts %Q[] f.puts IO.read(init) - mrbc.run f, [asslib] + mrbs, 'mrbtest_irep' + mrbc.run f, mrbs, 'mrbtest_irep' gems.each do |g| + next unless g.run_test_in_other_mrb_state? f.puts %Q[void GENERATED_TMP_mrb_#{g.funcname}_gem_test(mrb_state *mrb);] end + f.puts %Q[void no_mrb_open_mrbgem_test(mrb_state *mrb);] f.puts %Q[void mrbgemtest_init(mrb_state* mrb) {] gems.each do |g| + next unless g.run_test_in_other_mrb_state? f.puts %Q[ GENERATED_TMP_mrb_#{g.funcname}_gem_test(mrb);] end + f.puts %Q[ no_mrb_open_mrbgem_test(mrb);] f.puts %Q[}] end end diff --git a/test/no_mrb_open_test_dummy.rb b/test/no_mrb_open_test_dummy.rb new file mode 100644 index 000000000..5181c0a45 --- /dev/null +++ b/test/no_mrb_open_test_dummy.rb @@ -0,0 +1,2 @@ +#dummy + diff --git a/test/t/array.rb b/test/t/array.rb index 1125ee98c..48f2fe0c4 100644 --- a/test/t/array.rb +++ b/test/t/array.rb @@ -17,7 +17,11 @@ assert('Array.[]', '15.2.12.4.1') do assert_equal([1, 2, 3], Array.[](1,2,3)) end -assert('Array#*', '15.2.12.5.1') do +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) @@ -26,10 +30,6 @@ assert('Array#*', '15.2.12.5.1') do assert_equal([], [1].*(0)) end -assert('Array#+', '15.2.12.5.2') do - assert_equal([1, 1], [1].+([1])) -end - assert('Array#<<', '15.2.12.5.3') do assert_equal([1, 1], [1].<<(1)) end @@ -46,6 +46,14 @@ assert('Array#[]', '15.2.12.5.4') do 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", a[1.1]) + assert_equal(["b", "c"], a[1,2]) + assert_equal(["b", "c", "d"], a[1..-2]) end assert('Array#[]=', '15.2.12.5.5') do @@ -61,6 +69,18 @@ assert('Array#[]=', '15.2.12.5.5') do 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) end assert('Array#clear', '15.2.12.5.6') do @@ -81,8 +101,14 @@ end assert('Array#delete_at', '15.2.12.5.9') do a = [1,2,3] - a.delete_at(1) + 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 @@ -129,6 +155,7 @@ 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 @@ -225,6 +252,7 @@ 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 @@ -290,6 +318,7 @@ assert('Array#hash', '15.2.12.5.35') do a = [ 1, 2, 3 ] assert_true(a.hash.is_a? Integer) + assert_equal([1,2].hash, [1,2].hash) end assert('Array#<=>', '15.2.12.5.36') do @@ -317,6 +346,3 @@ assert("Array (Longish inline array)") do ary.each {|p| h[p.class] += 1} assert_equal({Array=>200}, h) end - - - diff --git a/test/t/class.rb b/test/t/class.rb index d6c0f1c9a..bea20ee24 100644 --- a/test/t/class.rb +++ b/test/t/class.rb @@ -235,6 +235,23 @@ assert('class to return the last value') do assert_equal(m, :m) 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 @@ -258,3 +275,97 @@ assert('Class#inherited') do 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_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_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.singleton_methods.include? :run_baz + assert_true baz.singleton_methods.include? :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 +end + +assert('clone Class') do + class Foo + def func + true + end + end + + Foo.clone.new.func +end diff --git a/test/t/comparable.rb b/test/t/comparable.rb index b5718d2d2..2ee28de7b 100644 --- a/test/t/comparable.rb +++ b/test/t/comparable.rb @@ -3,22 +3,26 @@ assert('Comparable#<', '15.3.3.2.1') do class Foo include Comparable def <=>(x) - 0 + x end end - - assert_false(Foo.new < Foo.new) + 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) - 0 + x end end - - assert_true(Foo.new <= Foo.new) + 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 @@ -36,22 +40,26 @@ assert('Comparable#>', '15.3.3.2.4') do class Foo include Comparable def <=>(x) - 0 + x end end - - assert_false(Foo.new > Foo.new) + 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) - 0 + x end end - - assert_true(Foo.new >= Foo.new) + 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 diff --git a/test/t/enumerable.rb b/test/t/enumerable.rb index ed062823c..4fa615a8f 100644 --- a/test/t/enumerable.rb +++ b/test/t/enumerable.rb @@ -8,11 +8,35 @@ 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 diff --git a/test/t/exception.rb b/test/t/exception.rb index 4239cba8b..8099e911f 100644 --- a/test/t/exception.rb +++ b/test/t/exception.rb @@ -345,3 +345,15 @@ assert('Exception#backtrace') do true end + +assert('Raise in ensure') do + + assert_raise(RuntimeError) do + begin + raise "" + ensure + raise "" + end + end + +end diff --git a/test/t/float.rb b/test/t/float.rb index f70bf2d66..ded434320 100644 --- a/test/t/float.rb +++ b/test/t/float.rb @@ -15,6 +15,9 @@ assert('Float#+', '15.2.9.3.1') do 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 @@ -127,6 +130,18 @@ assert('Float#round', '15.2.9.3.12') do 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_true(nan.round(1).nan?) end assert('Float#to_f', '15.2.9.3.13') do @@ -143,3 +158,27 @@ 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_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? +end diff --git a/test/t/hash.rb b/test/t/hash.rb index 39f9ae9d8..8a10f4026 100644 --- a/test/t/hash.rb +++ b/test/t/hash.rb @@ -12,6 +12,8 @@ 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 }) + assert_false({ :a => 1 } == true) end assert('Hash#[]', '15.2.13.4.2') do @@ -34,6 +36,13 @@ assert('Hash#clear', '15.2.13.4.4') do assert_equal({ }, a) end +assert('Hash#dup') do + a = { 'a' => 1 } + b = a.dup + a['a'] = 2 + assert_equal(b, {'a' => 1}) +end + assert('Hash#default', '15.2.13.4.5') do a = Hash.new b = Hash.new('abc') @@ -221,14 +230,30 @@ assert('Hash#replace', '15.2.13.4.23') do 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]) end assert('Hash#shift', '15.2.13.4.24') do a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } b = a.shift - assert_equal({ 'abc_key' => 'abc_value' }, a) - assert_equal [ 'cba_key', 'cba_value' ], b + 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 @@ -262,6 +287,15 @@ 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| diff --git a/test/t/integer.rb b/test/t/integer.rb index 79ee1e790..66dd61c0b 100644 --- a/test/t/integer.rb +++ b/test/t/integer.rb @@ -15,6 +15,9 @@ assert('Integer#+', '15.2.8.3.1') do assert_equal 2, a assert_equal 2.0, b + + assert_raise(TypeError){ 0+nil } + assert_raise(TypeError){ 1+nil } end assert('Integer#-', '15.2.8.3.2') do @@ -31,6 +34,9 @@ assert('Integer#*', '15.2.8.3.3') do assert_equal 1, a assert_equal 1.0, b + + assert_raise(TypeError){ 0*nil } + assert_raise(TypeError){ 1*nil } end assert('Integer#/', '15.2.8.3.4') do @@ -207,6 +213,16 @@ end # Not ISO specified +assert('Integer#divmod') 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 + assert('Integer#step') do a = [] b = [] diff --git a/test/t/kernel.rb b/test/t/kernel.rb index 81c111053..c6b65ddf7 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -264,6 +264,16 @@ 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 @@ -281,6 +291,10 @@ 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 @@ -321,6 +335,57 @@ assert('Kernel#loop', '15.3.1.3.29') do 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 + + a = String.new + begin + a.no_method_named_this + rescue NoMethodError => e + assert_equal "undefined method 'no_method_named_this' for \"\"", e.message + end + + class ShortInspectClass + def inspect + 'An inspect string' + end + end + b = ShortInspectClass.new + begin + b.no_method_named_this + rescue NoMethodError => e + assert_equal "undefined method 'no_method_named_this' for An inspect string", e.message + end + + class LongInspectClass + def inspect + "A" * 70 + end + end + c = LongInspectClass.new + begin + c.no_method_named_this + rescue NoMethodError => e + assert_equal "undefined method 'no_method_named_this' for #{c.to_s}", e.message + end + + class NoInspectClass + undef inspect + end + d = NoInspectClass.new + begin + d.no_method_named_this + rescue NoMethodError => e + assert_equal "undefined method 'no_method_named_this' for #{d.to_s}", e.message + end +end + assert('Kernel#methods', '15.3.1.3.31') do assert_equal Array, methods.class end @@ -330,7 +395,18 @@ assert('Kernel#nil?', '15.3.1.3.32') do end assert('Kernel#object_id', '15.3.1.3.33') do - assert_equal Fixnum, object_id.class + 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' @@ -413,6 +489,27 @@ assert('Kernel#!=') do 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) @@ -423,3 +520,46 @@ assert('Kernel#respond_to_missing?') do assert_true Test4RespondToMissing.new.respond_to?(:a_method) 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#__method__') do + assert_equal(:m, Class.new {def m; __method__; end}.new.m) + assert_equal(:m, Class.new {define_method(:m) {__method__}}.new.m) + c = Class.new do + [:m1, :m2].each do |m| + define_method(m) do + __method__ + end + end + end + assert_equal(:m1, c.new.m1) + assert_equal(:m2, c.new.m2) +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 + recurse(count+1, stop) + end + + assert_equal 6, recurse(0, 5) + assert_raise RuntimeError do + recurse(0, 100000) + end +end + diff --git a/test/t/localjumperror.rb b/test/t/localjumperror.rb index a7d18b3b1..1780cb518 100644 --- a/test/t/localjumperror.rb +++ b/test/t/localjumperror.rb @@ -3,10 +3,10 @@ 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 +# 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 diff --git a/test/t/module.rb b/test/t/module.rb index 8655db391..48a09f720 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -83,7 +83,7 @@ assert('Module#attr', '15.2.2.4.11') do assert_true AttrTest.respond_to?(:cattr) assert_true test.respond_to?(:iattr) - assert_false AttrTest.respond_to?(:vattr=) + assert_false AttrTest.respond_to?(:cattr=) assert_false test.respond_to?(:iattr=) test.iattr_val = 'test' @@ -276,16 +276,16 @@ end assert('Module.constants', '15.2.2.4.24') do $n = [] module TestA - Const = 1 + C = 1 end class TestB include TestA - Const2 = 1 + C2 = 1 $n = constants.sort end - assert_equal [ :Const ], TestA.constants - assert_equal [ :Const, :Const2 ], $n + assert_equal [ :C ], TestA.constants + assert_equal [ :C, :C2 ], $n end assert('Module#include', '15.2.2.4.27') do @@ -503,3 +503,17 @@ assert('Issue 1467') do C1.new C2.new end + +assert('clone Module') do + module M1 + def foo + true + end + end + + class B + include M1.clone + end + + B.new.foo +end diff --git a/test/t/nil.rb b/test/t/nil.rb index 443178c81..971ce2e8e 100644 --- a/test/t/nil.rb +++ b/test/t/nil.rb @@ -5,6 +5,15 @@ 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 superclass', '15.2.4.2') do + assert_equal Object, NilClass.superclass +end + assert('NilClass#&', '15.2.4.3.1') do assert_false nil.&(true) assert_false nil.&(nil) diff --git a/test/t/numeric.rb b/test/t/numeric.rb index 7dfec3e82..ef977da29 100644 --- a/test/t/numeric.rb +++ b/test/t/numeric.rb @@ -22,6 +22,16 @@ assert('Numeric#abs', '15.2.7.4.3') do 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 diff --git a/test/t/string.rb b/test/t/string.rb index 4c3689b3a..04f90fb45 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -28,16 +28,20 @@ assert('String#==', '15.2.10.5.2') do 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) do + 'a' * -1 + end end -# 'String#=~', '15.2.10.5.5' will be tested in mrbgems. - assert('String#[]', '15.2.10.5.6') do # length of args is 1 a = 'abc'[0] @@ -82,6 +86,7 @@ assert('String#[] with Range') do 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] @@ -91,6 +96,7 @@ assert('String#[] with Range') do g2 = 'abc'[-2...3] h2 = 'abc'[3...4] i2 = 'abc'[4...5] + j2 = 'abcdefghijklmnopqrstuvwxyz'[1...3] assert_equal '', a1 assert_equal 'b', b1 @@ -101,6 +107,7 @@ assert('String#[] with Range') do assert_equal 'bc', g1 assert_equal '', h1 assert_nil i2 + assert_equal 'bcd', j1 assert_equal '', a2 assert_equal '', b2 assert_equal 'b', c2 @@ -110,6 +117,7 @@ assert('String#[] with Range') do assert_equal 'bc', g2 assert_equal '', h2 assert_nil i2 + assert_equal 'bc', j2 end assert('String#capitalize', '15.2.10.5.7') do @@ -125,6 +133,7 @@ assert('String#capitalize!', '15.2.10.5.8') do a.capitalize! assert_equal 'Abc', a + assert_equal nil, 'Abc'.capitalize! end assert('String#chomp', '15.2.10.5.9') do @@ -204,6 +213,7 @@ assert('String#downcase!', '15.2.10.5.14') do a.downcase! assert_equal 'abc', a + assert_equal nil, 'abc'.downcase! end assert('String#each_line', '15.2.10.5.15') do @@ -275,8 +285,10 @@ end 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 @@ -301,6 +313,13 @@ assert('String#replace', '15.2.10.5.28') do 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 end assert('String#reverse', '15.2.10.5.29') do @@ -324,6 +343,9 @@ assert('String#rindex', '15.2.10.5.31') do assert_nil 'abc'.rindex('d') assert_equal 0, 'abcabc'.rindex('a', 1) assert_equal 3, 'abcabc'.rindex('a', 4) + + assert_equal 3, 'abcabc'.rindex(97) + assert_equal nil, 'abcabc'.rindex(0) end # 'String#scan', '15.2.10.5.32' will be tested in mrbgems. @@ -411,11 +433,13 @@ assert('String#to_i', '15.2.10.5.39') do b = '32143'.to_i c = 'a'.to_i(16) d = '100'.to_i(2) + e = '1_000'.to_i assert_equal 0, a assert_equal 32143, b assert_equal 10, c assert_equal 4, d + assert_equal 1_000, e end assert('String#to_s', '15.2.10.5.40') do @@ -442,6 +466,13 @@ assert('String#upcase!', '15.2.10.5.43') do 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 # Not ISO specified diff --git a/test/t/syntax.rb b/test/t/syntax.rb index c87a81e06..fac73aa7b 100644 --- a/test/t/syntax.rb +++ b/test/t/syntax.rb @@ -1,3 +1,12 @@ +assert('__FILE__') do + file = __FILE__ + assert_true 'test/t/syntax.rb' == file || 'test\t\syntax.rb' == file +end + +assert('__LINE__') do + assert_equal 7, __LINE__ +end + assert('super', '11.3.4') do assert_raise NoMethodError do super @@ -42,6 +51,98 @@ assert('Abbreviated variable assignment', '11.4.2.3.2') do 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" @@ -74,3 +175,82 @@ assert('Splat and mass assignment') do assert_equal 7, b assert_equal [8,9], 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('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 + class << 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 + + alias_method sym, :old_cmd + end + true +end diff --git a/test/t/true.rb b/test/t/true.rb index 3aebf43a1..e5da2112c 100644 --- a/test/t/true.rb +++ b/test/t/true.rb @@ -5,12 +5,14 @@ assert('TrueClass', '15.2.5') do assert_equal Class, TrueClass.class end -assert('TrueClass superclass', '15.2.5.2') do - assert_equal Object, TrueClass.superclass -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 superclass', '15.2.5.2') do + assert_equal Object, TrueClass.superclass end assert('TrueClass#&', '15.2.5.3.1') do diff --git a/test/t/unicode.rb b/test/t/unicode.rb new file mode 100644 index 000000000..7edd65ef2 --- /dev/null +++ b/test/t/unicode.rb @@ -0,0 +1,35 @@ +# Test of the \u notation + +assert('bare \u notation test') do + # Mininum and maximum one byte characters + assert_equal("\u0000", "\x00") + assert_equal("\u007F", "\x7F") + + # Mininum and maximum two byte characters + assert_equal("\u0080", "\xC2\x80") + assert_equal("\u07FF", "\xDF\xBF") + + # Mininum and maximum three byte characters + assert_equal("\u0800", "\xE0\xA0\x80") + assert_equal("\uFFFF", "\xEF\xBF\xBF") + + # Four byte characters require the \U notation +end + +assert('braced \u notation test') do + # Mininum and maximum one byte characters + assert_equal("\u{0000}", "\x00") + assert_equal("\u{007F}", "\x7F") + + # Mininum and maximum two byte characters + assert_equal("\u{0080}", "\xC2\x80") + assert_equal("\u{07FF}", "\xDF\xBF") + + # Mininum and maximum three byte characters + assert_equal("\u{0800}", "\xE0\xA0\x80") + assert_equal("\u{FFFF}", "\xEF\xBF\xBF") + + # Mininum and maximum four byte characters + assert_equal("\u{10000}", "\xF0\x90\x80\x80") + assert_equal("\u{10FFFF}", "\xF4\x8F\xBF\xBF") +end |
