From b485aab7e71c1625cfc27e0f92c9509f42378458 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 5 May 2024 13:19:16 +0200 Subject: Adding upstream version 1.45.3+dfsg. Signed-off-by: Daniel Baumann --- .../deps/mruby/mrbgems/mruby-struct/mrbgem.rake | 5 - .../mruby/mrbgems/mruby-struct/mrblib/struct.rb | 103 --- .../deps/mruby/mrbgems/mruby-struct/src/struct.c | 714 --------------------- .../deps/mruby/mrbgems/mruby-struct/test/struct.rb | 212 ------ 4 files changed, 1034 deletions(-) delete mode 100644 web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/mrbgem.rake delete mode 100644 web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/mrblib/struct.rb delete mode 100644 web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/src/struct.c delete mode 100644 web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/test/struct.rb (limited to 'web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct') diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/mrbgem.rake b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/mrbgem.rake deleted file mode 100644 index 2826ad2ad..000000000 --- a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/mrbgem.rake +++ /dev/null @@ -1,5 +0,0 @@ -MRuby::Gem::Specification.new('mruby-struct') do |spec| - spec.license = 'MIT' - spec.author = 'mruby developers' - spec.summary = 'standard Struct class' -end diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/mrblib/struct.rb b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/mrblib/struct.rb deleted file mode 100644 index 7cf3dd3ab..000000000 --- a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/mrblib/struct.rb +++ /dev/null @@ -1,103 +0,0 @@ -## -# Struct -# -# ISO 15.2.18 - -if Object.const_defined?(:Struct) - class Struct - - ## - # Calls the given block for each element of +self+ - # and pass the respective element. - # - # ISO 15.2.18.4.4 - def each(&block) - self.class.members.each{|field| - block.call(self[field]) - } - self - end - - ## - # Calls the given block for each element of +self+ - # and pass the name and value of the respectiev - # element. - # - # ISO 15.2.18.4.5 - def each_pair(&block) - self.class.members.each{|field| - block.call(field.to_sym, self[field]) - } - self - end - - ## - # Calls the given block for each element of +self+ - # and returns an array with all elements of which - # block is not false. - # - # ISO 15.2.18.4.7 - def select(&block) - ary = [] - self.class.members.each{|field| - val = self[field] - ary.push(val) if block.call(val) - } - ary - end - - def _inspect - name = self.class.to_s - if name[0] == "#" - str = "#" - end - - ## - # call-seq: - # struct.to_s -> string - # struct.inspect -> string - # - # Describe the contents of this struct in a string. - # - # 15.2.18.4.10(x) - # - def inspect - begin - self._inspect - rescue SystemStackError - "#" - end - end - - ## - # 15.2.18.4.11(x) - # - alias to_s inspect - end - - ## - # call-seq: - # hsh.dig(key,...) -> object - # - # Extracts the nested value specified by the sequence of key - # objects by calling +dig+ at each step, returning +nil+ if any - # intermediate step is +nil+. - # - def dig(idx,*args) - n = self[idx] - if args.size > 0 - n&.dig(*args) - else - n - end - end -end - diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/src/struct.c b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/src/struct.c deleted file mode 100644 index 67762a948..000000000 --- a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/src/struct.c +++ /dev/null @@ -1,714 +0,0 @@ -/* -** struct.c - Struct class -** -** See Copyright Notice in mruby.h -*/ - -#include -#include -#include -#include -#include -#include -#include -#include - -#define RSTRUCT_LEN(st) RARRAY_LEN(st) -#define RSTRUCT_PTR(st) RARRAY_PTR(st) - -static struct RClass * -struct_class(mrb_state *mrb) -{ - return mrb_class_get(mrb, "Struct"); -} - -static inline mrb_value -struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id) -{ - struct RClass* kclass; - struct RClass* sclass = struct_class(mrb); - mrb_value ans; - - for (;;) { - ans = mrb_iv_get(mrb, c, id); - if (!mrb_nil_p(ans)) return ans; - kclass = RCLASS_SUPER(c); - if (kclass == 0 || kclass == sclass) - return mrb_nil_value(); - c = mrb_obj_value(kclass); - } -} - -static mrb_value -struct_s_members(mrb_state *mrb, struct RClass *klass) -{ - mrb_value members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern_lit(mrb, "__members__")); - - if (mrb_nil_p(members)) { - mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct"); - } - if (!mrb_array_p(members)) { - mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct"); - } - return members; -} - -static mrb_value -struct_members(mrb_state *mrb, mrb_value s) -{ - mrb_value members = struct_s_members(mrb, mrb_obj_class(mrb, s)); - if (!mrb_array_p(s)) { - mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct"); - } - if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { - if (RSTRUCT_LEN(s) == 0) { /* probably uninitialized */ - mrb_ary_resize(mrb, s, RARRAY_LEN(members)); - } - else { - mrb_raisef(mrb, E_TYPE_ERROR, - "struct size differs (%S required %S given)", - mrb_fixnum_value(RARRAY_LEN(members)), mrb_fixnum_value(RSTRUCT_LEN(s))); - } - } - return members; -} - -static mrb_value -mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) -{ - mrb_value members, ary; - - members = struct_s_members(mrb, mrb_class_ptr(klass)); - ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members)); - mrb_ary_replace(mrb, ary, members); - return ary; -} - -static void -mrb_struct_modify(mrb_state *mrb, mrb_value strct) -{ - if (MRB_FROZEN_P(mrb_basic_ptr(strct))) { - mrb_raise(mrb, E_RUNTIME_ERROR, "can't modify frozen struct"); - } - - mrb_write_barrier(mrb, mrb_basic_ptr(strct)); -} - -/* 15.2.18.4.6 */ -/* - * call-seq: - * struct.members -> array - * - * Returns an array of strings representing the names of the instance - * variables. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * joe.members #=> [:name, :address, :zip] - */ - -static mrb_value -mrb_struct_members(mrb_state *mrb, mrb_value obj) -{ - return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj))); -} - -static mrb_value struct_aref_sym(mrb_state *mrb, mrb_value obj, mrb_sym id); - -static mrb_value -mrb_struct_ref(mrb_state *mrb, mrb_value obj) -{ - return struct_aref_sym(mrb, obj, mrb->c->ci->mid); -} - -static mrb_sym -mrb_id_attrset(mrb_state *mrb, mrb_sym id) -{ - const char *name; - char *buf; - mrb_int len; - mrb_sym mid; - - name = mrb_sym2name_len(mrb, id, &len); - buf = (char *)mrb_malloc(mrb, (size_t)len+2); - memcpy(buf, name, (size_t)len); - buf[len] = '='; - buf[len+1] = '\0'; - - mid = mrb_intern(mrb, buf, len+1); - mrb_free(mrb, buf); - return mid; -} - -static mrb_value mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val); - -static mrb_value -mrb_struct_set_m(mrb_state *mrb, mrb_value obj) -{ - mrb_value val; - - const char *name; - mrb_int slen; - mrb_sym mid; - - mrb_get_args(mrb, "o", &val); - - /* get base id */ - name = mrb_sym2name_len(mrb, mrb->c->ci->mid, &slen); - mid = mrb_intern(mrb, name, slen-1); /* omit last "=" */ - - return mrb_struct_aset_sym(mrb, obj, mid, val); -} - -static mrb_bool -is_local_id(mrb_state *mrb, const char *name) -{ - if (!name) return FALSE; - return !ISUPPER(name[0]); -} - -static mrb_bool -is_const_id(mrb_state *mrb, const char *name) -{ - if (!name) return FALSE; - return ISUPPER(name[0]); -} - -static void -make_struct_define_accessors(mrb_state *mrb, mrb_value members, struct RClass *c) -{ - const mrb_value *ptr_members = RARRAY_PTR(members); - mrb_int i; - mrb_int len = RARRAY_LEN(members); - int ai = mrb_gc_arena_save(mrb); - - for (i=0; ibasic.c->super = c->c; */ - make_struct_define_accessors(mrb, members, c); - return nstr; -} - -/* 15.2.18.3.1 */ -/* - * call-seq: - * Struct.new( [aString] [, aSym]+> ) -> StructClass - * StructClass.new(arg, ...) -> obj - * StructClass[arg, ...] -> obj - * - * Creates a new class, named by aString, containing accessor - * methods for the given symbols. If the name aString is - * omitted, an anonymous structure class will be created. Otherwise, - * the name of this struct will appear as a constant in class - * Struct, so it must be unique for all - * Structs in the system and should start with a capital - * letter. Assigning a structure class to a constant effectively gives - * the class the name of the constant. - * - * Struct::new returns a new Class object, - * which can then be used to create specific instances of the new - * structure. The number of actual parameters must be - * less than or equal to the number of attributes defined for this - * class; unset parameters default to nil. Passing too many - * parameters will raise an ArgumentError. - * - * The remaining methods listed in this section (class and instance) - * are defined for this generated class. - * - * # Create a structure with a name in Struct - * Struct.new("Customer", :name, :address) #=> Struct::Customer - * Struct::Customer.new("Dave", "123 Main") #=> # - * - * # Create a structure named by its constant - * Customer = Struct.new(:name, :address) #=> Customer - * Customer.new("Dave", "123 Main") #=> # - */ -static mrb_value -mrb_struct_s_def(mrb_state *mrb, mrb_value klass) -{ - mrb_value name, rest; - mrb_value *pargv; - mrb_int argcnt; - mrb_int i; - mrb_value b, st; - mrb_sym id; - mrb_value *argv; - mrb_int argc; - - name = mrb_nil_value(); - mrb_get_args(mrb, "*&", &argv, &argc, &b); - if (argc == 0) { /* special case to avoid crash */ - rest = mrb_ary_new(mrb); - } - else { - if (argc > 0) name = argv[0]; - pargv = &argv[1]; - argcnt = argc-1; - if (!mrb_nil_p(name) && mrb_symbol_p(name)) { - /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ - name = mrb_nil_value(); - pargv = &argv[0]; - argcnt++; - } - rest = mrb_ary_new_from_values(mrb, argcnt, pargv); - for (i=0; i anObject - * struct[fixnum] -> anObject - * - * Attribute Reference---Returns the value of the instance variable - * named by symbol, or indexed (0..length-1) by - * fixnum. Will raise NameError if the named - * variable does not exist, or IndexError if the index is - * out of range. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * - * joe["name"] #=> "Joe Smith" - * joe[:name] #=> "Joe Smith" - * joe[0] #=> "Joe Smith" - */ -static mrb_value -mrb_struct_aref(mrb_state *mrb, mrb_value s) -{ - mrb_value idx; - - mrb_get_args(mrb, "o", &idx); - if (mrb_string_p(idx)) { - mrb_value sym = mrb_check_intern_str(mrb, idx); - - if (mrb_nil_p(sym)) { - mrb_name_error(mrb, mrb_intern_str(mrb, idx), "no member '%S' in struct", idx); - } - idx = sym; - } - if (mrb_symbol_p(idx)) { - return struct_aref_sym(mrb, s, mrb_symbol(idx)); - } - return struct_aref_int(mrb, s, mrb_int(mrb, idx)); -} - -static mrb_value -mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) -{ - mrb_value members, *ptr; - const mrb_value *ptr_members; - mrb_int i, len; - - members = struct_members(mrb, s); - len = RARRAY_LEN(members); - ptr = RSTRUCT_PTR(s); - ptr_members = RARRAY_PTR(members); - for (i=0; i obj - * struct[fixnum] = obj -> obj - * - * Attribute Assignment---Assigns to the instance variable named by - * symbol or fixnum the value obj and - * returns it. Will raise a NameError if the named - * variable does not exist, or an IndexError if the index - * is out of range. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * - * joe["name"] = "Luke" - * joe[:zip] = "90210" - * - * joe.name #=> "Luke" - * joe.zip #=> "90210" - */ - -static mrb_value -mrb_struct_aset(mrb_state *mrb, mrb_value s) -{ - mrb_int i; - mrb_value idx; - mrb_value val; - - mrb_get_args(mrb, "oo", &idx, &val); - - if (mrb_string_p(idx)) { - mrb_value sym = mrb_check_intern_str(mrb, idx); - - if (mrb_nil_p(sym)) { - mrb_name_error(mrb, mrb_intern_str(mrb, idx), "no member '%S' in struct", idx); - } - idx = sym; - } - if (mrb_symbol_p(idx)) { - return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val); - } - - i = mrb_int(mrb, idx); - if (i < 0) i = RSTRUCT_LEN(s) + i; - if (i < 0) { - mrb_raisef(mrb, E_INDEX_ERROR, - "offset %S too small for struct(size:%S)", - mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); - } - if (RSTRUCT_LEN(s) <= i) { - mrb_raisef(mrb, E_INDEX_ERROR, - "offset %S too large for struct(size:%S)", - mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); - } - mrb_struct_modify(mrb, s); - return RSTRUCT_PTR(s)[i] = val; -} - -/* 15.2.18.4.1 */ -/* - * call-seq: - * struct == other_struct -> true or false - * - * Equality---Returns true if other_struct is - * equal to this one: they must be of the same class as generated by - * Struct::new, and the values of all instance variables - * must be equal (according to Object#==). - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345) - * joe == joejr #=> true - * joe == jane #=> false - */ - -static mrb_value -mrb_struct_equal(mrb_state *mrb, mrb_value s) -{ - mrb_value s2; - mrb_value *ptr, *ptr2; - mrb_int i, len; - - mrb_get_args(mrb, "o", &s2); - if (mrb_obj_equal(mrb, s, s2)) { - return mrb_true_value(); - } - if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) { - return mrb_false_value(); - } - if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { - mrb_bug(mrb, "inconsistent struct"); /* should never happen */ - } - ptr = RSTRUCT_PTR(s); - ptr2 = RSTRUCT_PTR(s2); - len = RSTRUCT_LEN(s); - for (i=0; i true or false - * - * Two structures are equal if they are the same object, or if all their - * fields are equal (using eql?). - */ -static mrb_value -mrb_struct_eql(mrb_state *mrb, mrb_value s) -{ - mrb_value s2; - mrb_value *ptr, *ptr2; - mrb_int i, len; - - mrb_get_args(mrb, "o", &s2); - if (mrb_obj_equal(mrb, s, s2)) { - return mrb_true_value(); - } - if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) { - return mrb_false_value(); - } - if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { - mrb_bug(mrb, "inconsistent struct"); /* should never happen */ - } - ptr = RSTRUCT_PTR(s); - ptr2 = RSTRUCT_PTR(s2); - len = RSTRUCT_LEN(s); - for (i=0; i Fixnum - * struct.size -> Fixnum - * - * Returns number of struct members. - */ -static mrb_value -mrb_struct_len(mrb_state *mrb, mrb_value self) -{ - return mrb_fixnum_value(RSTRUCT_LEN(self)); -} - -/* - * call-seq: - * struct.to_a -> array - * struct.values -> array - * - * Create an array from struct values. - */ -static mrb_value -mrb_struct_to_a(mrb_state *mrb, mrb_value self) -{ - return mrb_ary_new_from_values(mrb, RSTRUCT_LEN(self), RSTRUCT_PTR(self)); -} - -/* - * call-seq: - * struct.to_h -> hash - * - * Create a hash from member names and struct values. - */ -static mrb_value -mrb_struct_to_h(mrb_state *mrb, mrb_value self) -{ - mrb_value members, ret; - mrb_int i; - - members = struct_members(mrb, self); - ret = mrb_hash_new_capa(mrb, RARRAY_LEN(members)); - - for (i = 0; i < RARRAY_LEN(members); ++i) { - mrb_hash_set(mrb, ret, RARRAY_PTR(members)[i], RSTRUCT_PTR(self)[i]); - } - - return ret; -} - -static mrb_value -mrb_struct_values_at(mrb_state *mrb, mrb_value self) -{ - mrb_int argc; - mrb_value *argv; - - mrb_get_args(mrb, "*", &argv, &argc); - - return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_aref_int); -} - -/* - * A Struct is a convenient way to bundle a number of - * attributes together, using accessor methods, without having to write - * an explicit class. - * - * The Struct class is a generator of specific classes, - * each one of which is defined to hold a set of variables and their - * accessors. In these examples, we'll call the generated class - * "CustomerClass," and we'll show an example instance of that - * class as "CustomerInst." - * - * In the descriptions that follow, the parameter symbol refers - * to a symbol, which is either a quoted string or a - * Symbol (such as :name). - */ -void -mrb_mruby_struct_gem_init(mrb_state* mrb) -{ - struct RClass *st; - st = mrb_define_class(mrb, "Struct", mrb->object_class); - MRB_SET_INSTANCE_TT(st, MRB_TT_ARRAY); - - mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, MRB_ARGS_ANY()); /* 15.2.18.3.1 */ - - mrb_define_method(mrb, st, "==", mrb_struct_equal, MRB_ARGS_REQ(1)); /* 15.2.18.4.1 */ - mrb_define_method(mrb, st, "[]", mrb_struct_aref, MRB_ARGS_REQ(1)); /* 15.2.18.4.2 */ - mrb_define_method(mrb, st, "[]=", mrb_struct_aset, MRB_ARGS_REQ(2)); /* 15.2.18.4.3 */ - mrb_define_method(mrb, st, "members", mrb_struct_members, MRB_ARGS_NONE()); /* 15.2.18.4.6 */ - mrb_define_method(mrb, st, "initialize", mrb_struct_initialize, MRB_ARGS_ANY()); /* 15.2.18.4.8 */ - mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, MRB_ARGS_REQ(1)); /* 15.2.18.4.9 */ - mrb_define_method(mrb, st, "eql?", mrb_struct_eql, MRB_ARGS_REQ(1)); /* 15.2.18.4.12(x) */ - - mrb_define_method(mrb, st, "size", mrb_struct_len, MRB_ARGS_NONE()); - mrb_define_method(mrb, st, "length", mrb_struct_len, MRB_ARGS_NONE()); - mrb_define_method(mrb, st, "to_a", mrb_struct_to_a, MRB_ARGS_NONE()); - mrb_define_method(mrb, st, "values", mrb_struct_to_a, MRB_ARGS_NONE()); - mrb_define_method(mrb, st, "to_h", mrb_struct_to_h, MRB_ARGS_NONE()); - mrb_define_method(mrb, st, "values_at", mrb_struct_values_at, MRB_ARGS_ANY()); -} - -void -mrb_mruby_struct_gem_final(mrb_state* mrb) -{ -} diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/test/struct.rb b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/test/struct.rb deleted file mode 100644 index 421fe4b5d..000000000 --- a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-struct/test/struct.rb +++ /dev/null @@ -1,212 +0,0 @@ -## -# Struct ISO Test - -assert('Struct', '15.2.18') do - assert_equal Class, Struct.class -end - -assert('Struct.new', '15.2.18.3.1') do - c = Struct.new(:m1, :m2) - assert_equal Struct, c.superclass - assert_equal [:m1, :m2], c.members -end - -# Check crash bug with Struc.new and no params. -assert('Struct.new', '15.2.18.3.1') do - c = Struct.new() - assert_equal Struct, c.superclass - assert_equal [], c.members -end - -assert('Struct#==', '15.2.18.4.1') do - c = Struct.new(:m1, :m2) - cc1 = c.new(1,2) - cc2 = c.new(1,2) - assert_true cc1 == cc2 - - Struct.new(:m1, :m2) { def foo; end } - assert_raise(NoMethodError) { Struct.new(:m1).new.foo } -end - -assert('Struct#[]', '15.2.18.4.2') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - assert_equal 1, cc[:m1] - assert_equal 2, cc["m2"] - assert_equal 1, cc[0] - assert_equal 2, cc[-1] - assert_raise(TypeError) { cc[[]] } - assert_raise(IndexError) { cc[2] } - assert_raise(NameError) { cc['tama'] } -end - -assert('Struct#[]=', '15.2.18.4.3') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc[:m1] = 3 - assert_equal 3, cc[:m1] - cc["m2"] = 3 - assert_equal 3, cc["m2"] - cc[0] = 4 - assert_equal 4, cc[0] - cc[-1] = 5 - assert_equal 5, cc[-1] - assert_raise(TypeError) { cc[[]] = 3 } - assert_raise(IndexError) { cc[2] = 7 } - assert_raise(NameError) { cc['pochi'] = 8 } -end - -assert('Struct#each', '15.2.18.4.4') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - a = [] - cc.each{|x| - a << x - } - assert_equal [1, 2], a -end - -assert('Struct#each_pair', '15.2.18.4.5') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - a = [] - cc.each_pair{|k,v| - a << [k,v] - } - assert_equal [[:m1, 1], [:m2, 2]], a -end - -assert('Struct#members', '15.2.18.4.6') do - c = Struct.new(:m1, :m2) - assert_equal [:m1, :m2], c.new(1,2).members -end - -assert('Struct#select', '15.2.18.4.7') do - c = Struct.new(:m1, :m2) - assert_equal([2]) { c.new(1,2).select{|v| v % 2 == 0} } -end - -assert('large struct') do - c = Struct.new(:m1, :m2, :m3, :m4, :m5, :m6, :m7, :m8, :m9, :m10, :m11, :m12, :m13) - cc = c.new(1,2,3,4,5,6,7,8,9,10,11,12,13) - assert_equal 1, cc.m1 - assert_equal 2, cc.m2 - assert_equal 3, cc.m3 - assert_equal 4, cc.m4 - assert_equal 5, cc.m5 - assert_equal 6, cc.m6 - assert_equal 7, cc.m7 - assert_equal 8, cc.m8 - assert_equal 9, cc.m9 - assert_equal 10, cc.m10 - assert_equal 13, cc.m13 - - cc.m13 = 'test' - assert_equal 'test', cc.m13 - - assert_raise(NoMethodError) { cc.m14 } -end - -assert('wrong struct arg count') do - c = Struct.new(:m1) - assert_raise ArgumentError do - cc = c.new(1,2,3) - end -end - -assert('struct dup') do - c = Struct.new(:m1, :m2, :m3, :m4, :m5) - cc = c.new(1,2,3,4,5) - assert_nothing_raised { - assert_equal(cc, cc.dup) - } -end - -assert('struct inspect') do - c = Struct.new(:m1, :m2, :m3, :m4, :m5) - cc = c.new(1,2,3,4,5) - assert_equal "#", cc.inspect -end - -assert('Struct#length, Struct#size') do - s = Struct.new(:f1, :f2).new(0, 1) - assert_equal 2, s.size - assert_equal 2, s.length -end - -assert('Struct#to_a, Struct#values') do - s = Struct.new(:mem1, :mem2).new('a', 'b') - assert_equal ['a', 'b'], s.to_a - assert_equal ['a', 'b'], s.values -end - -assert('Struct#to_h') do - s = Struct.new(:white, :red, :green).new('ruuko', 'yuzuki', 'hitoe') - assert_equal(:white => 'ruuko', :red => 'yuzuki', :green => 'hitoe') { s.to_h } -end - -assert('Struct#values_at') do - a = Struct.new(:blue, :purple).new('aki', 'io') - assert_equal ['aki'], a.values_at(0) - assert_equal ['io', 'aki'], a.values_at(1, 0) - assert_raise(IndexError) { a.values_at 2 } -end - -assert("Struct#dig") do - a = Struct.new(:blue, :purple).new('aki', Struct.new(:red).new(1)) - assert_equal 'aki', a.dig(:blue) - assert_equal 1, a.dig(:purple, :red) - assert_equal 1, a.dig(1, 0) -end - -assert("Struct.new removes existing constant") do - skip "redefining Struct with same name cause warnings" - begin - assert_not_equal Struct.new("Test", :a), Struct.new("Test", :a, :b) - ensure - Struct.remove_const :Test - end -end - -assert("Struct#initialize_copy requires struct to be the same type") do - begin - Struct.new("Test", :a) - a = Struct::Test.new("a") - Struct.remove_const :Test - Struct.new("Test", :a, :b) - assert_raise(TypeError) do - a.initialize_copy(Struct::Test.new("a", "b")) - end - ensure - Struct.remove_const :Test - end -end - -assert("Struct.new does not allow array") do - assert_raise(TypeError) do - Struct.new("Test", [:a]) - end -end - -assert("Struct.new generates subclass of Struct") do - begin - original_struct = Struct - Struct = String - assert_equal original_struct, original_struct.new.superclass - ensure - Struct = original_struct - end -end - -assert 'Struct#freeze' do - c = Struct.new :m - - o = c.new - o.m = :test - assert_equal :test, o.m - - o.freeze - assert_raise(RuntimeError) { o.m = :modify } - assert_raise(RuntimeError) { o[:m] = :modify } - assert_equal :test, o.m -end -- cgit v1.2.3