summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/driver.c10
-rw-r--r--test/init_mrbtest.c3
-rw-r--r--test/t/array.rb44
-rw-r--r--test/t/class.rb111
-rw-r--r--test/t/exception.rb12
-rw-r--r--test/t/float.rb7
-rw-r--r--test/t/kernel.rb108
-rw-r--r--test/t/localjumperror.rb8
-rw-r--r--test/t/module.rb24
-rw-r--r--test/t/string.rb4
-rw-r--r--test/t/syntax.rb143
11 files changed, 444 insertions, 30 deletions
diff --git a/test/driver.c b/test/driver.c
index 707794ff9..0116f4584 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(mrb, "$ko_test", 8));
+ mrb_value kill_test = mrb_gv_get(mrb, mrb_intern(mrb, "$kill_test", 10));
return mrb_fixnum_p(ko_test) && mrb_fixnum(ko_test) == 0 && mrb_fixnum_p(kill_test) && mrb_fixnum(kill_test) == 0;
}
@@ -104,7 +102,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(mrb, "$mrbtest_verbose", 16), mrb_true_value());
}
krn = mrb->kernel_module;
diff --git a/test/init_mrbtest.c b/test/init_mrbtest.c
index 3b8f9129e..8d01624f0 100644
--- a/test/init_mrbtest.c
+++ b/test/init_mrbtest.c
@@ -1,9 +1,6 @@
#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_irep[];
diff --git a/test/t/array.rb b/test/t/array.rb
index 1125ee98c..1398bdc6e 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" ]
+ a[1.1] == "b" and
+ a[1,2] == ["b", "c"] and
+ a[1..-2] == ["b", "c", "d"]
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/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..0c67f510a 100644
--- a/test/t/float.rb
+++ b/test/t/float.rb
@@ -143,3 +143,10 @@ assert('Float#truncate', '15.2.9.3.15') do
assert_equal( 3, 3.123456789.truncate)
assert_equal(-3, -3.1.truncate)
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/kernel.rb b/test/t/kernel.rb
index 81c111053..2d409940b 100644
--- a/test/t/kernel.rb
+++ b/test/t/kernel.rb
@@ -281,6 +281,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 +325,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 +385,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 +479,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 +510,22 @@ 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('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/string.rb b/test/t/string.rb
index 4c3689b3a..c42fa006f 100644
--- a/test/t/string.rb
+++ b/test/t/string.rb
@@ -28,6 +28,8 @@ 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
@@ -36,8 +38,6 @@ assert('String#*', '15.2.10.5.5') do
assert_equal 'aaaaa', 'a' * 5
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]
diff --git a/test/t/syntax.rb b/test/t/syntax.rb
index c87a81e06..13cd1198e 100644
--- a/test/t/syntax.rb
+++ b/test/t/syntax.rb
@@ -42,6 +42,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 +166,54 @@ 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