diff options
| -rw-r--r-- | include/mruby/value.h | 5 | ||||
| -rw-r--r-- | mrbgems/mruby-range-ext/src/range.c | 8 | ||||
| -rw-r--r-- | mrbgems/mruby-sprintf/src/sprintf.c | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 22 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/string.rb | 20 | ||||
| -rw-r--r-- | src/array.c | 31 | ||||
| -rw-r--r-- | src/class.c | 2 | ||||
| -rw-r--r-- | src/codegen.c | 26 | ||||
| -rw-r--r-- | src/gc.c | 13 | ||||
| -rw-r--r-- | src/load.c | 7 | ||||
| -rw-r--r-- | src/numeric.c | 6 | ||||
| -rw-r--r-- | src/object.c | 6 | ||||
| -rw-r--r-- | src/range.c | 10 | ||||
| -rw-r--r-- | src/string.c | 9 | ||||
| -rw-r--r-- | src/vm.c | 15 | ||||
| -rw-r--r-- | tasks/mrbgems_test.rake | 2 | ||||
| -rw-r--r-- | tasks/mruby_build_commands.rake | 4 | ||||
| -rw-r--r-- | tasks/mruby_build_gem.rake | 6 | ||||
| -rw-r--r-- | test/t/array.rb | 13 | ||||
| -rw-r--r-- | test/t/class.rb | 101 | ||||
| -rw-r--r-- | test/t/kernel.rb | 76 | ||||
| -rw-r--r-- | test/t/syntax.rb | 23 |
22 files changed, 329 insertions, 80 deletions
diff --git a/include/mruby/value.h b/include/mruby/value.h index cc0440bbc..13acd039e 100644 --- a/include/mruby/value.h +++ b/include/mruby/value.h @@ -155,7 +155,7 @@ typedef struct mrb_value { #define mrb_tt(o) (((o).value.ttt & 0xfc000)>>14) #define mrb_mktt(tt) (0xfff00000|((tt)<<14)) #define mrb_type(o) ((uint32_t)0xfff00000 < (o).value.ttt ? mrb_tt(o) : MRB_TT_FLOAT) -#define mrb_ptr(o) ((void*)((((intptr_t)0x3fffffffffff)&((intptr_t)((o).value.p)))<<2)) +#define mrb_ptr(o) ((void*)((((uintptr_t)0x3fffffffffff)&((uintptr_t)((o).value.p)))<<2)) #define mrb_float(o) (o).f #define MRB_SET_VALUE(o, tt, attr, v) do {\ @@ -166,7 +166,7 @@ typedef struct mrb_value { case MRB_TT_UNDEF:\ case MRB_TT_FIXNUM:\ case MRB_TT_SYMBOL: (o).attr = (v); break;\ - default: (o).value.i = 0; (o).value.p = (void*)((intptr_t)(o).value.p | (((intptr_t)(v))>>2)); break;\ + default: (o).value.i = 0; (o).value.p = (void*)((uintptr_t)(o).value.p | (((uintptr_t)(v))>>2)); break;\ }\ } while (0) @@ -315,6 +315,7 @@ mrb_float_value(struct mrb_state *mrb, mrb_float f) #define mrb_bool(o) ((o).w != MRB_Qnil && (o).w != MRB_Qfalse) #else + #define mrb_cptr(o) mrb_ptr(o) #define mrb_fixnum_p(o) (mrb_type(o) == MRB_TT_FIXNUM) #define mrb_undef_p(o) (mrb_type(o) == MRB_TT_UNDEF) diff --git a/mrbgems/mruby-range-ext/src/range.c b/mrbgems/mruby-range-ext/src/range.c index 4e17dac8c..c980ecffd 100644 --- a/mrbgems/mruby-range-ext/src/range.c +++ b/mrbgems/mruby-range-ext/src/range.c @@ -7,7 +7,7 @@ r_le(mrb_state *mrb, mrb_value a, mrb_value b) mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */ /* output :a < b => -1, a = b => 0, a > b => +1 */ - if (mrb_type(r) == MRB_TT_FIXNUM) { + if (mrb_fixnum_p(r)) { mrb_int c = mrb_fixnum(r); if (c == 0 || c == -1) return TRUE; } @@ -21,11 +21,7 @@ r_lt(mrb_state *mrb, mrb_value a, mrb_value b) mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* output :a < b => -1, a = b => 0, a > b => +1 */ - if (mrb_type(r) == MRB_TT_FIXNUM) { - if (mrb_fixnum(r) == -1) return TRUE; - } - - return FALSE; + return mrb_fixnum_p(r) && mrb_fixnum(r) == -1; } /* diff --git a/mrbgems/mruby-sprintf/src/sprintf.c b/mrbgems/mruby-sprintf/src/sprintf.c index 6479b19bc..5485f5098 100644 --- a/mrbgems/mruby-sprintf/src/sprintf.c +++ b/mrbgems/mruby-sprintf/src/sprintf.c @@ -15,10 +15,6 @@ #include <math.h> #include <ctype.h> -#ifdef HAVE_IEEEFP_H -#include <ieeefp.h> -#endif - #define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */ #define BITSPERDIG (sizeof(mrb_int)*CHAR_BIT) #define EXTENDSIGN(n, l) (((~0 << (n)) >> (((n)*(l)) % BITSPERDIG)) & ~(~0 << (n))) diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 005438b38..a517aa209 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -49,4 +49,26 @@ class String def casecmp(str) self.downcase <=> str.downcase end + + def partition(sep) + raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String + n = index(sep) + unless n.nil? + m = n + sep.size + [ slice(0, n), sep, slice(m, size - m) ] + else + [ self, "", "" ] + end + end + + def rpartition(sep) + raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String + n = rindex(sep) + unless n.nil? + m = n + sep.size + [ slice(0, n), sep, slice(m, size - m) ] + else + [ "", "", self ] + end + end end diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb index 3ab959437..67ed628c0 100644 --- a/mrbgems/mruby-string-ext/test/string.rb +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -114,3 +114,23 @@ assert('String#end_with?') do assert_true !"ng".end_with?("ing", "mng") assert_raise TypeError do "hello".end_with?(true) end end + +assert('String#partition') do + assert_equal ["a", "x", "axa"], "axaxa".partition("x") + assert_equal ["aaaaa", "", ""], "aaaaa".partition("x") + assert_equal ["", "", "aaaaa"], "aaaaa".partition("") + assert_equal ["", "a", "aaaa"], "aaaaa".partition("a") + assert_equal ["aaaa", "b", ""], "aaaab".partition("b") + assert_equal ["", "b", "aaaa"], "baaaa".partition("b") + assert_equal ["", "", ""], "".partition("a") +end + +assert('String#rpartition') do + assert_equal ["axa", "x", "a"], "axaxa".rpartition("x") + assert_equal ["", "", "aaaaa"], "aaaaa".rpartition("x") + assert_equal ["aaaaa", "", ""], "aaaaa".rpartition("") + assert_equal ["aaaa", "a", ""], "aaaaa".rpartition("a") + assert_equal ["aaaa", "b", ""], "aaaab".rpartition("b") + assert_equal ["", "b", "aaaa"], "baaaa".rpartition("b") + assert_equal ["", "", ""], "".rpartition("a") +end diff --git a/src/array.c b/src/array.c index 0fb257d7e..67351bb9b 100644 --- a/src/array.c +++ b/src/array.c @@ -4,12 +4,7 @@ ** See Copyright Notice in mruby.h */ -#ifndef SIZE_MAX - /* Some versions of VC++ - * has SIZE_MAX in stdint.h - */ -# include <limits.h> -#endif +#include <limits.h> #include "mruby.h" #include "mruby/array.h" #include "mruby/class.h" @@ -318,7 +313,7 @@ mrb_ary_cmp(mrb_state *mrb, mrb_value ary1) for (i=0; i<len; i++) { mrb_value v = ary_elt(ary2, i); r = mrb_funcall_argv(mrb, ary_elt(ary1, i), cmp, 1, &v); - if (mrb_type(r) != MRB_TT_FIXNUM || mrb_fixnum(r) != 0) return r; + if (!mrb_fixnum_p(r) || mrb_fixnum(r) != 0) return r; } } len = a1->len - a2->len; @@ -697,7 +692,7 @@ mrb_ary_aget(mrb_state *mrb, mrb_value self) return mrb_ary_ref(mrb, self, index); case 1: - if (mrb_type(argv[0]) != MRB_TT_FIXNUM) { + if (!mrb_fixnum_p(argv[0])) { mrb_raise(mrb, E_TYPE_ERROR, "expected Fixnum"); } if (index < 0) index += a->len; @@ -854,6 +849,9 @@ mrb_ary_splat(mrb_state *mrb, mrb_value v) if (mrb_array_p(v)) { return v; } + if (mrb_respond_to(mrb, v, mrb_intern_lit(mrb, "to_a"))) { + return mrb_funcall(mrb, v, "to_a", 0); + } else { return mrb_ary_new_from_values(mrb, 1, &v); } @@ -1113,6 +1111,22 @@ mrb_ary_eql(mrb_state *mrb, mrb_value ary1) return mrb_true_value(); } +static mrb_value +mrb_ary_ceqq(mrb_state *mrb, mrb_value ary) +{ + mrb_value v; + mrb_int i, len; + mrb_sym eqq = mrb_intern_lit(mrb, "==="); + + mrb_get_args(mrb, "o", &v); + len = RARRAY_LEN(ary); + for (i=0; i<len; i++) { + mrb_value c = mrb_funcall_argv(mrb, ary_elt(ary, i), eqq, 1, &v); + if (mrb_test(c)) return mrb_true_value(); + } + return mrb_false_value(); +} + void mrb_init_array(mrb_state *mrb) { @@ -1155,4 +1169,5 @@ mrb_init_array(mrb_state *mrb) mrb_define_method(mrb, a, "==", mrb_ary_equal, MRB_ARGS_REQ(1)); /* 15.2.12.5.33 (x) */ mrb_define_method(mrb, a, "eql?", mrb_ary_eql, MRB_ARGS_REQ(1)); /* 15.2.12.5.34 (x) */ mrb_define_method(mrb, a, "<=>", mrb_ary_cmp, MRB_ARGS_REQ(1)); /* 15.2.12.5.36 (x) */ + mrb_define_method(mrb, a, "__case_eqq", mrb_ary_ceqq, MRB_ARGS_REQ(1)); /* internal */ } diff --git a/src/class.c b/src/class.c index 7cb0ca93a..d29089253 100644 --- a/src/class.c +++ b/src/class.c @@ -522,7 +522,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) s = mrb_str_ptr(ss); len = (mrb_int)strlen(s->ptr); if (len < s->len) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "String contains NUL"); + mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte"); } else if (len > s->len) { mrb_str_modify(mrb, s); diff --git a/src/codegen.c b/src/codegen.c index fc83244f2..83018c627 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -421,7 +421,7 @@ new_lit(codegen_scope *s, mrb_value val) case MRB_TT_FIXNUM: for (i=0; i<s->irep->plen; i++) { pv = &s->irep->pool[i]; - if (mrb_type(*pv) != MRB_TT_FIXNUM) continue; + if (!mrb_fixnum_p(*pv)) continue; if (mrb_fixnum(*pv) == mrb_fixnum(val)) return i; } break; @@ -661,7 +661,7 @@ scope_body(codegen_scope *s, node *tree, int val) { codegen_scope *scope = scope_new(s->mrb, s, tree->car); - codegen(scope, tree->cdr, val); + codegen(scope, tree->cdr, VAL); if (!s->iseq) { genop(scope, MKOP_A(OP_STOP, 0)); } @@ -674,9 +674,7 @@ scope_body(codegen_scope *s, node *tree, int val) genop(scope, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL)); } else { - pop(); - genop_peep(scope, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL); - push(); + genop_peep(scope, MKOP_AB(OP_RETURN, scope->sp-1, OP_R_NORMAL), NOVAL); } } scope_finish(scope); @@ -1388,7 +1386,12 @@ codegen(codegen_scope *s, node *tree, int val) if (head) { genop(s, MKOP_AB(OP_MOVE, cursp(), head)); pop(); - genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "===")), 1)); + if ((intptr_t)n->car->car == NODE_SPLAT) { + genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1)); + } + else { + genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "===")), 1)); + } } else { pop(); @@ -1512,7 +1515,14 @@ codegen(codegen_scope *s, node *tree, int val) break; case NODE_SPLAT: - codegen(s, tree, VAL); + { + int idx = new_msym(s, mrb_intern_lit(s->mrb, "to_a")); + + codegen(s, tree, VAL); + pop(); + genop(s, MKOP_ABC(OP_SEND, cursp(), idx, 0)); + push(); + } break; case NODE_ASGN: @@ -2897,7 +2907,7 @@ codedump_recur(mrb_state *mrb, mrb_irep *irep) void codedump_all(mrb_state *mrb, struct RProc *proc) { - return codedump_recur(mrb, proc->body.irep); + codedump_recur(mrb, proc->body.irep); } struct RProc* @@ -4,12 +4,7 @@ ** See Copyright Notice in mruby.h */ -#ifndef SIZE_MAX - /* Some versions of VC++ - * has SIZE_MAX in stdint.h - */ -# include <limits.h> -#endif +#include <limits.h> #include <string.h> #include <stdlib.h> #include "mruby.h" @@ -370,17 +365,19 @@ mrb_free_heap(mrb_state *mrb) static void gc_protect(mrb_state *mrb, struct RBasic *p) { - if (mrb->arena_idx >= MRB_GC_ARENA_SIZE) { #ifdef MRB_GC_FIXED_ARENA + if (mrb->arena_idx >= MRB_GC_ARENA_SIZE) { /* arena overflow error */ mrb->arena_idx = MRB_GC_ARENA_SIZE - 4; /* force room in arena */ mrb_raise(mrb, E_RUNTIME_ERROR, "arena overflow error"); + } #else + if (mrb->arena_idx >= mrb->arena_capa) { /* extend arena */ mrb->arena_capa *= 1.5; mrb->arena = (struct RBasic**)mrb_realloc(mrb, mrb->arena, sizeof(struct RBasic*)*mrb->arena_capa); -#endif } +#endif mrb->arena[mrb->arena_idx++] = p; } diff --git a/src/load.c b/src/load.c index adc2416df..57845b2ca 100644 --- a/src/load.c +++ b/src/load.c @@ -4,12 +4,7 @@ ** See Copyright Notice in mruby.h */ -#ifndef SIZE_MAX - /* Some versions of VC++ - * has SIZE_MAX in stdint.h - */ -# include <limits.h> -#endif +#include <limits.h> #include <stdlib.h> #include <string.h> #include "mruby/dump.h" diff --git a/src/numeric.c b/src/numeric.c index db90d81fb..0b841bf70 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -5,12 +5,6 @@ */ #include <float.h> -#if defined(__FreeBSD__) && __FreeBSD__ < 4 -# include <floatingpoint.h> -#endif -#ifdef HAVE_IEEEFP_H -# include <ieeefp.h> -#endif #include <limits.h> #include <math.h> #include <stdlib.h> diff --git a/src/object.c b/src/object.c index 529c168ee..daa76a03f 100644 --- a/src/object.c +++ b/src/object.c @@ -326,9 +326,9 @@ mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method) { mrb_value v; - if (mrb_type(val) == MRB_TT_FIXNUM) return val; + if (mrb_fixnum_p(val)) return val; v = convert_type(mrb, val, "Integer", method, FALSE); - if (mrb_nil_p(v) || mrb_type(v) != MRB_TT_FIXNUM) { + if (mrb_nil_p(v) || !mrb_fixnum_p(v)) { return mrb_nil_value(); } return v; @@ -404,7 +404,7 @@ mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t) if (mrb_nil_p(x)) { etype = "nil"; } - else if (mrb_type(x) == MRB_TT_FIXNUM) { + else if (mrb_fixnum_p(x)) { etype = "Fixnum"; } else if (mrb_type(x) == MRB_TT_SYMBOL) { diff --git a/src/range.c b/src/range.c index a27ced4cb..0eacc83d5 100644 --- a/src/range.c +++ b/src/range.c @@ -176,7 +176,7 @@ r_le(mrb_state *mrb, mrb_value a, mrb_value b) mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */ /* output :a < b => -1, a = b => 0, a > b => +1 */ - if (mrb_type(r) == MRB_TT_FIXNUM) { + if (mrb_fixnum_p(r)) { mrb_int c = mrb_fixnum(r); if (c == 0 || c == -1) return TRUE; } @@ -190,11 +190,7 @@ r_gt(mrb_state *mrb, mrb_value a, mrb_value b) mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* output :a < b => -1, a = b => 0, a > b => +1 */ - if (mrb_type(r) == MRB_TT_FIXNUM) { - if (mrb_fixnum(r) == 1) return TRUE; - } - - return FALSE; + return mrb_fixnum_p(r) && mrb_fixnum(r) == 1; } static mrb_bool @@ -203,7 +199,7 @@ r_ge(mrb_state *mrb, mrb_value a, mrb_value b) mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */ /* output :a < b => -1, a = b => 0, a > b => +1 */ - if (mrb_type(r) == MRB_TT_FIXNUM) { + if (mrb_fixnum_p(r)) { mrb_int c = mrb_fixnum(r); if (c == 0 || c == 1) return TRUE; } diff --git a/src/string.c b/src/string.c index 1c577188e..e8c37180d 100644 --- a/src/string.c +++ b/src/string.c @@ -5,12 +5,7 @@ */ #include <ctype.h> -#ifndef SIZE_MAX - /* Some versions of VC++ - * has SIZE_MAX in stdint.h - */ -# include <limits.h> -#endif +#include <limits.h> #include <stddef.h> #include <stdlib.h> #include <string.h> @@ -1248,7 +1243,7 @@ mrb_str_include(mrb_state *mrb, mrb_value self) mrb_bool include_p; mrb_get_args(mrb, "o", &str2); - if (mrb_type(str2) == MRB_TT_FIXNUM) { + if (mrb_fixnum_p(str2)) { include_p = (memchr(RSTRING_PTR(self), mrb_fixnum(str2), RSTRING_LEN(self)) != NULL); } else { @@ -231,6 +231,7 @@ cipush(mrb_state *mrb) ci->ridx = ridx; ci->env = 0; ci->pc = 0; + ci->err = 0; return ci; } @@ -263,6 +264,8 @@ ecall(mrb_state *mrb, int i) p = mrb->c->ensure[i]; if (!p) return; + if (mrb->c->ci->eidx > i) + mrb->c->ci->eidx = i; ci = cipush(mrb); ci->stackidx = mrb->c->stack - mrb->c->stbase; ci->mid = ci[-1].mid; @@ -601,7 +604,6 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_init(mrb); } stack_extend(mrb, irep->nregs, stack_keep); - mrb->c->ci->err = pc; mrb->c->ci->proc = proc; mrb->c->ci->nregs = irep->nregs + 1; regs = mrb->c->stack; @@ -852,13 +854,14 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int CASE(OP_EPOP) { /* A A.times{ensure_pop().call} */ - int n; int a = GETARG_A(i); + mrb_callinfo *ci = mrb->c->ci; + int n, eidx = ci->eidx; - for (n=0; n<a; n++) { - ecall(mrb, --mrb->c->ci->eidx); + for (n=0; n<a && eidx > ci[-1].eidx; n++) { + ecall(mrb, --eidx); + ARENA_RESTORE(mrb, ai); } - ARENA_RESTORE(mrb, ai); NEXT; } @@ -2130,7 +2133,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int ecall(mrb, n); } } - mrb->c->ci->err = 0; + ERR_PC_CLR(mrb); mrb->jmp = prev_jmp; if (mrb->exc) { return mrb_obj_value(mrb->exc); diff --git a/tasks/mrbgems_test.rake b/tasks/mrbgems_test.rake index 2b5683166..cf907a510 100644 --- a/tasks/mrbgems_test.rake +++ b/tasks/mrbgems_test.rake @@ -8,7 +8,7 @@ MRuby.each_target do g.print_gem_init_header(f) test_preload = [g.dir, MRUBY_ROOT].map {|dir| File.expand_path(g.test_preload, dir) - }.find {|file| File.exists?(file) } + }.find {|file| File.exist?(file) } g.build.mrbc.run f, test_preload, "gem_test_irep_#{g.funcname}_preload" g.test_rbfiles.flatten.each_with_index do |rbfile, i| diff --git a/tasks/mruby_build_commands.rake b/tasks/mruby_build_commands.rake index 349b8717d..d7b87514e 100644 --- a/tasks/mruby_build_commands.rake +++ b/tasks/mruby_build_commands.rake @@ -106,7 +106,7 @@ module MRuby private def get_dependencies(file) file = file.ext('d') unless File.extname(file) == '.d' - if File.exists?(file) + if File.exist?(file) File.read(file).gsub("\\\n ", "").scan(/^\S+:\s+(.+)$/).flatten.map {|s| s.split(' ') }.flatten else [] @@ -265,7 +265,7 @@ module MRuby # if mrbc execution fail, drop the file unless $?.exitstatus File.delete(out.path) - exit -1 + exit(-1) end end end diff --git a/tasks/mruby_build_gem.rake b/tasks/mruby_build_gem.rake index ea1307132..0f920dbc8 100644 --- a/tasks/mruby_build_gem.rake +++ b/tasks/mruby_build_gem.rake @@ -2,7 +2,7 @@ module MRuby module LoadGems def gembox(gemboxfile) gembox = File.expand_path("#{gemboxfile}.gembox", "#{MRUBY_ROOT}/mrbgems") - fail "Can't find gembox '#{gembox}'" unless File.exists?(gembox) + fail "Can't find gembox '#{gembox}'" unless File.exist?(gembox) GemBox.config = self GemBox.path = gembox @@ -25,7 +25,7 @@ module MRuby gemrake = File.join(gemdir, "mrbgem.rake") - fail "Can't find #{gemrake}" unless File.exists?(gemrake) + fail "Can't find #{gemrake}" unless File.exist?(gemrake) Gem.current = nil load gemrake return nil unless Gem.current @@ -50,7 +50,7 @@ module MRuby url = params[:git] gemdir = "#{gem_clone_dir}/#{url.match(/([-\w]+)(\.[-\w]+|)$/).to_a[1]}" - if File.exists?(gemdir) + if File.exist?(gemdir) if $pull_gems git.run_pull gemdir, url else diff --git a/test/t/array.rb b/test/t/array.rb index 643a975b0..8ef1eee3f 100644 --- a/test/t/array.rb +++ b/test/t/array.rb @@ -46,6 +46,9 @@ 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)) end assert('Array#[]=', '15.2.12.5.5') do @@ -81,8 +84,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 +138,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 +235,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 diff --git a/test/t/class.rb b/test/t/class.rb index d6c0f1c9a..a6ba336e3 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,87 @@ 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 diff --git a/test/t/kernel.rb b/test/t/kernel.rb index 3e802b219..0f8fdebe4 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -325,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 @@ -334,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' @@ -427,3 +489,15 @@ assert('Kernel#respond_to_missing?') do assert_true Test4RespondToMissing.new.respond_to?(:a_method) assert_false Test4RespondToMissing.new.respond_to?(:no_method) end + +assert('stack extend') do + def recurse(count, stop) + return count if count > stop + recurse(count+1, stop) + end + + assert_equal 6, recurse(0, 5) + assert_raise RuntimeError do + recurse(0, 100000) + end +end diff --git a/test/t/syntax.rb b/test/t/syntax.rb index 4ddbefea3..f1fc80216 100644 --- a/test/t/syntax.rb +++ b/test/t/syntax.rb @@ -102,3 +102,26 @@ assert('Return values of case statements') do 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 |
