diff options
Diffstat (limited to '')
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-proc-ext/test/proc.c | 56 | ||||
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-proc-ext/test/proc.rb | 96 |
2 files changed, 152 insertions, 0 deletions
diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-proc-ext/test/proc.c b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-proc-ext/test/proc.c new file mode 100644 index 000000000..a77b68e9d --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-proc-ext/test/proc.c @@ -0,0 +1,56 @@ +#include <mruby.h> +#include <mruby/proc.h> +#include <mruby/class.h> + +static mrb_value +return_func_name(mrb_state *mrb, mrb_value self) +{ + return mrb_cfunc_env_get(mrb, 0); +} + +static mrb_value +proc_new_cfunc_with_env(mrb_state *mrb, mrb_value self) +{ + mrb_sym n; + mrb_value n_val; + mrb_get_args(mrb, "n", &n); + n_val = mrb_symbol_value(n); + mrb_define_method_raw(mrb, mrb_class_ptr(self), n, + mrb_proc_new_cfunc_with_env(mrb, return_func_name, 1, &n_val)); + return self; +} + +static mrb_value +return_env(mrb_state *mrb, mrb_value self) +{ + mrb_int idx; + mrb_get_args(mrb, "i", &idx); + return mrb_cfunc_env_get(mrb, idx); +} + +static mrb_value +cfunc_env_get(mrb_state *mrb, mrb_value self) +{ + mrb_sym n; + mrb_value *argv; mrb_int argc; + mrb_get_args(mrb, "na", &n, &argv, &argc); + mrb_define_method_raw(mrb, mrb_class_ptr(self), n, + mrb_proc_new_cfunc_with_env(mrb, return_env, argc, argv)); + return self; +} + +static mrb_value +cfunc_without_env(mrb_state *mrb, mrb_value self) +{ + return mrb_cfunc_env_get(mrb, 0); +} + +void mrb_mruby_proc_ext_gem_test(mrb_state *mrb) +{ + struct RClass *cls; + + cls = mrb_define_class(mrb, "ProcExtTest", mrb->object_class); + mrb_define_module_function(mrb, cls, "mrb_proc_new_cfunc_with_env", proc_new_cfunc_with_env, MRB_ARGS_REQ(1)); + mrb_define_module_function(mrb, cls, "mrb_cfunc_env_get", cfunc_env_get, MRB_ARGS_REQ(2)); + mrb_define_module_function(mrb, cls, "cfunc_without_env", cfunc_without_env, MRB_ARGS_NONE()); +} diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-proc-ext/test/proc.rb b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-proc-ext/test/proc.rb new file mode 100644 index 000000000..037d8d124 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-proc-ext/test/proc.rb @@ -0,0 +1,96 @@ +## +# Proc(Ext) Test + +assert('Proc#source_location') do + loc = Proc.new {}.source_location + next true if loc.nil? + assert_equal loc[0][-7, 7], 'proc.rb' + assert_equal loc[1], 5 +end + +assert('Proc#inspect') do + ins = Proc.new{}.inspect + assert_kind_of String, ins +end + +assert('Proc#lambda?') do + assert_true lambda{}.lambda? + assert_true !Proc.new{}.lambda? +end + +assert('Proc#===') do + proc = Proc.new {|a| a * 2} + assert_equal 20, (proc === 10) +end + +assert('Proc#yield') do + proc = Proc.new {|a| a * 2} + assert_equal 20, proc.yield(10) +end + +assert('Proc#curry') do + b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } + assert_equal 6, b.curry[1][2][3] + assert_equal 6, b.curry[1, 2][3, 4] + assert_equal 6, b.curry(5)[1][2][3][4][5] + assert_equal 6, b.curry(5)[1, 2][3, 4][5] + assert_equal 1, b.curry(1)[1] + + b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) } + assert_equal 6, b.curry[1][2][3] + assert_raise(ArgumentError) { b.curry[1, 2][3, 4] } + assert_raise(ArgumentError) { b.curry(5) } + assert_raise(ArgumentError) { b.curry(1) } + + assert_false(proc{}.curry.lambda?) + assert_true(lambda{}.curry.lambda?) +end + +assert('Proc#parameters') do + assert_equal([], Proc.new {}.parameters) + assert_equal([], Proc.new {||}.parameters) + assert_equal([[:opt, :a]], Proc.new {|a|}.parameters) + assert_equal([[:req, :a]], lambda {|a|}.parameters) + assert_equal([[:opt, :a]], lambda {|a=nil|}.parameters) + assert_equal([[:req, :a]], ->(a){}.parameters) + assert_equal([[:rest]], lambda { |*| }.parameters) + assert_equal([[:rest, :a]], Proc.new {|*a|}.parameters) + assert_equal([[:opt, :a], [:opt, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:opt, :f], [:opt, :g], [:block, :h]], Proc.new {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters) + assert_equal([[:req, :a], [:req, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:req, :f], [:req, :g], [:block, :h]], lambda {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters) +end + +assert('Proc#to_proc') do + proc = Proc.new {} + assert_equal proc, proc.to_proc +end + +assert('Kernel#proc') do + assert_true !proc{|a|}.lambda? + + assert_raise LocalJumpError do + proc{ break }.call + end +end + +assert('mrb_proc_new_cfunc_with_env') do + ProcExtTest.mrb_proc_new_cfunc_with_env(:test) + ProcExtTest.mrb_proc_new_cfunc_with_env(:mruby) + + t = ProcExtTest.new + + assert_equal :test, t.test + assert_equal :mruby, t.mruby +end + +assert('mrb_cfunc_env_get') do + ProcExtTest.mrb_cfunc_env_get :get_int, [0, 1, 2] + + t = ProcExtTest.new + + assert_raise(TypeError) { t.cfunc_without_env } + + assert_raise(IndexError) { t.get_int(-1) } + assert_raise(IndexError) { t.get_int(3) } + + assert_equal 1, t.get_int(1) +end |