summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/driver.c19
-rw-r--r--test/t/exception.rb10
-rw-r--r--test/t/hash.rb6
-rw-r--r--test/t/integer.rb32
-rw-r--r--test/t/kernel.rb42
-rw-r--r--test/t/module.rb6
-rw-r--r--test/t/proc.rb42
7 files changed, 128 insertions, 29 deletions
diff --git a/test/driver.c b/test/driver.c
index f7fb1b6df..f605d7517 100644
--- a/test/driver.c
+++ b/test/driver.c
@@ -10,12 +10,12 @@
#include <stdlib.h>
#include <string.h>
-#include <mruby.h>
-#include <mruby/proc.h>
-#include <mruby/data.h>
-#include <mruby/compile.h>
-#include <mruby/string.h>
-#include <mruby/variable.h>
+#include "mruby.h"
+#include "mruby/proc.h"
+#include "mruby/data.h"
+#include "mruby/compile.h"
+#include "mruby/string.h"
+#include "mruby/variable.h"
void
mrb_init_mrbtest(mrb_state *);
@@ -87,6 +87,7 @@ main(int argc, char **argv)
{
mrb_state *mrb;
struct RClass *krn;
+ struct RClass *mrbtest;
int ret;
print_hint();
@@ -106,6 +107,12 @@ main(int argc, char **argv)
krn = mrb->kernel_module;
mrb_define_method(mrb, krn, "__t_printstr__", mrb_t_printstr, MRB_ARGS_REQ(1));
+ mrbtest = mrb_define_module(mrb, "Mrbtest");
+
+ mrb_define_const(mrb, mrbtest, "FIXNUM_MAX", mrb_fixnum_value(MRB_INT_MAX));
+ mrb_define_const(mrb, mrbtest, "FIXNUM_MIN", mrb_fixnum_value(MRB_INT_MIN));
+ mrb_define_const(mrb, mrbtest, "FIXNUM_BIT", mrb_fixnum_value(MRB_INT_BIT));
+
mrb_init_mrbtest(mrb);
ret = eval_test(mrb);
mrb_close(mrb);
diff --git a/test/t/exception.rb b/test/t/exception.rb
index a77402bc5..816c3241b 100644
--- a/test/t/exception.rb
+++ b/test/t/exception.rb
@@ -42,6 +42,16 @@ assert('Exception.exception', '15.2.22.4.1') do
assert_equal 'a', e.message
end
+assert('NameError', '15.2.31') do
+ assert_raise(NameError) do
+ raise NameError.new
+ end
+
+ e = NameError.new "msg", "name"
+ assert_equal "msg", e.message
+ assert_equal "name", e.name
+end
+
assert('ScriptError', '15.2.37') do
assert_raise(ScriptError) do
raise ScriptError.new
diff --git a/test/t/hash.rb b/test/t/hash.rb
index 2ddd33316..0d8d137c4 100644
--- a/test/t/hash.rb
+++ b/test/t/hash.rb
@@ -40,7 +40,7 @@ assert('Hash#dup') do
a = { 'a' => 1 }
b = a.dup
a['a'] = 2
- assert_equal(b, {'a' => 1})
+ assert_equal({'a' => 1}, b)
end
assert('Hash#default', '15.2.13.4.5') do
@@ -223,6 +223,10 @@ assert('Hash#merge', '15.2.13.4.22') do
'xyz_key' => 'xyz_value' }, result_1)
assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value',
'xyz_key' => 'xyz_value' }, result_2)
+
+ assert_raise(TypeError) do
+ { 'abc_key' => 'abc_value' }.merge "a"
+ end
end
assert('Hash#replace', '15.2.13.4.23') do
diff --git a/test/t/integer.rb b/test/t/integer.rb
index 66dd61c0b..6560dddfe 100644
--- a/test/t/integer.rb
+++ b/test/t/integer.rb
@@ -18,6 +18,14 @@ assert('Integer#+', '15.2.8.3.1') do
assert_raise(TypeError){ 0+nil }
assert_raise(TypeError){ 1+nil }
+
+ c = Mrbtest::FIXNUM_MAX + 1
+ d = Mrbtest::FIXNUM_MAX.__send__(:+, 1)
+ 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
@@ -26,6 +34,14 @@ assert('Integer#-', '15.2.8.3.2') do
assert_equal 1, a
assert_equal 1.0, b
+
+ c = Mrbtest::FIXNUM_MIN - 1
+ d = Mrbtest::FIXNUM_MIN.__send__(:-, 1)
+ 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
@@ -37,6 +53,14 @@ assert('Integer#*', '15.2.8.3.3') do
assert_raise(TypeError){ 0*nil }
assert_raise(TypeError){ 1*nil }
+
+ c = Mrbtest::FIXNUM_MAX * 2
+ d = Mrbtest::FIXNUM_MAX.__send__(:*, 2)
+ 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
@@ -57,7 +81,7 @@ assert('Integer#%', '15.2.8.3.5') do
assert_equal 2, c
end
-assert('Integer#<=>', '15.2.8.3.6') do
+assert('Integer#<=>', '15.2.9.3.6') do
a = 1<=>0
b = 1<=>1
c = 1<=>2
@@ -211,9 +235,7 @@ assert('Integer#upto', '15.2.8.3.27') do
assert_equal 6, a
end
-# Not ISO specified
-
-assert('Integer#divmod') do
+assert('Integer#divmod', '15.2.8.3.30') do
assert_equal [ 0, 0], 0.divmod(1)
assert_equal [ 0, 1], 1.divmod(3)
assert_equal [ 3, 0], 3.divmod(1)
@@ -223,6 +245,8 @@ assert('Integer#divmod') do
assert_equal [ 1, -6], -13.divmod(-7)
end
+# Not ISO specified
+
assert('Integer#step') do
a = []
b = []
diff --git a/test/t/kernel.rb b/test/t/kernel.rb
index c6b65ddf7..be3c99a90 100644
--- a/test/t/kernel.rb
+++ b/test/t/kernel.rb
@@ -437,6 +437,26 @@ assert('Kernel#raise', '15.3.1.3.40') do
end
end
+assert('Kernel#remove_instance_variable', '15.3.1.3.41') do
+ class Test4RemoveInstanceVar
+ attr_reader :var
+ def initialize
+ @var = 99
+ end
+ def remove
+ remove_instance_variable(:@var)
+ end
+ end
+
+ tri = Test4RemoveInstanceVar.new
+ assert_equal 99, tri.var
+ tri.remove
+ assert_equal nil, tri.var
+ assert_raise NameError do
+ tri.remove
+ end
+end
+
# Kernel#require is defined in mruby-require. '15.3.1.3.42'
assert('Kernel#respond_to?', '15.3.1.3.43') do
@@ -451,6 +471,14 @@ assert('Kernel#respond_to?', '15.3.1.3.43') do
Test4RespondTo.new.respond_to?(1)
end
+ assert_raise ArgumentError do
+ Test4RespondTo.new.respond_to?
+ end
+
+ assert_raise ArgumentError do
+ Test4RespondTo.new.respond_to? :a, true, :aa
+ end
+
assert_true respond_to?(:nil?)
assert_true Test4RespondTo.new.respond_to?(:valid_method)
assert_true Test4RespondTo.new.respond_to?('valid_method')
@@ -528,20 +556,6 @@ assert('Kernel#global_variables') do
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
diff --git a/test/t/module.rb b/test/t/module.rb
index 585774a4b..2072f1f3c 100644
--- a/test/t/module.rb
+++ b/test/t/module.rb
@@ -254,7 +254,7 @@ assert('Module#const_get', '15.2.2.4.21') do
assert_equal 42, Test4ConstGet.const_get(:Const4Test4ConstGet)
end
-assert('Module.const_missing', '15.2.2.4.22') do
+assert('Module#const_missing', '15.2.2.4.22') do
module Test4ConstMissing
def self.const_missing(sym)
42 # the answer to everything
@@ -273,7 +273,7 @@ assert('Module#const_get', '15.2.2.4.23') do
assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet)
end
-assert('Module.constants', '15.2.2.4.24') do
+assert('Module#constants', '15.2.2.4.24') do
$n = []
module TestA
C = 1
@@ -444,7 +444,7 @@ assert('Module#remove_method', '15.2.2.4.41') do
assert_false Test4RemoveMethod::Child.instance_methods(false).include? :hello
end
-assert('Module.undef_method', '15.2.2.4.42') do
+assert('Module#undef_method', '15.2.2.4.42') do
module Test4UndefMethod
class Parent
def hello
diff --git a/test/t/proc.rb b/test/t/proc.rb
index 1be73c99a..e871e637e 100644
--- a/test/t/proc.rb
+++ b/test/t/proc.rb
@@ -56,7 +56,7 @@ assert('Proc#call', '15.2.17.4.3') do
end
assert('Proc#call proc args pos block') do
- pr = proc {|a,b,&c|
+ pr = Proc.new {|a,b,&c|
[a, b, c.class, c&&c.call(:x)]
}
assert_equal [nil, nil, Proc, :proc], (pr.call(){ :proc })
@@ -72,6 +72,29 @@ assert('Proc#call proc args pos block') do
assert_equal [1, 2, Proc, :x], (pr.call(1, 2, 3, 4){|x| x})
end
+assert('Proc#call proc args pos rest post') do
+ pr = Proc.new {|a,b,*c,d,e|
+ [a,b,c,d,e]
+ }
+ assert_equal [nil, nil, [], nil, nil], pr.call()
+ assert_equal [1, nil, [], nil, nil], pr.call(1)
+ assert_equal [1, 2, [], nil, nil], pr.call(1,2)
+ assert_equal [1, 2, [], 3, nil], pr.call(1,2,3)
+ assert_equal [1, 2, [], 3, 4], pr.call(1,2,3,4)
+ assert_equal [1, 2, [3], 4, 5], pr.call(1,2,3,4,5)
+ assert_equal [1, 2, [3, 4], 5, 6], pr.call(1,2,3,4,5,6)
+ assert_equal [1, 2, [3, 4, 5], 6,7], pr.call(1,2,3,4,5,6,7)
+
+ assert_equal [nil, nil, [], nil, nil], pr.call([])
+ assert_equal [1, nil, [], nil, nil], pr.call([1])
+ assert_equal [1, 2, [], nil, nil], pr.call([1,2])
+ assert_equal [1, 2, [], 3, nil], pr.call([1,2,3])
+ assert_equal [1, 2, [], 3, 4], pr.call([1,2,3,4])
+ assert_equal [1, 2, [3], 4, 5], pr.call([1,2,3,4,5])
+ assert_equal [1, 2, [3, 4], 5, 6], pr.call([1,2,3,4,5,6])
+ assert_equal [1, 2, [3, 4, 5], 6,7], pr.call([1,2,3,4,5,6,7])
+end
+
assert('Proc#return_does_not_break_self') do
class TestClass
attr_accessor :block
@@ -108,3 +131,20 @@ assert('Proc#return_does_not_break_self') do
assert_equal nil, c.return_nil
assert_equal c, c.block.call
end
+
+assert('&obj call to_proc if defined') do
+ pr = Proc.new{}
+ def mock(&b)
+ b
+ end
+ assert_equal pr.object_id, mock(&pr).object_id
+ assert_equal pr, mock(&pr)
+
+ obj = Object.new
+ def obj.to_proc
+ Proc.new{ :from_to_proc }
+ end
+ assert_equal :from_to_proc, mock(&obj).call
+
+ assert_raise(TypeError){ mock(&(Object.new)) }
+end