2012-11-19 02:31:05 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
2009-11-26 07:17:47 -05:00
|
|
|
require 'timeout'
|
2003-09-04 12:18:59 -04:00
|
|
|
|
2013-10-27 11:52:39 -04:00
|
|
|
module TestStruct
|
2003-09-04 12:18:59 -04:00
|
|
|
def test_struct
|
2013-10-27 11:52:39 -04:00
|
|
|
struct_test = @Struct.new("Test", :foo, :bar)
|
|
|
|
assert_equal(@Struct::Test, struct_test)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
test = struct_test.new(1, 2)
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(1, test.foo)
|
|
|
|
assert_equal(2, test.bar)
|
|
|
|
assert_equal(1, test[0])
|
|
|
|
assert_equal(2, test[1])
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
a, b = test.to_a
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(1, a)
|
|
|
|
assert_equal(2, b)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
test[0] = 22
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(22, test.foo)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
test.bar = 47
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(47, test.bar)
|
2020-01-28 01:21:26 -05:00
|
|
|
|
|
|
|
@Struct.class_eval do
|
|
|
|
remove_const :Test
|
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
end
|
2005-06-01 11:03:09 -04:00
|
|
|
|
|
|
|
# [ruby-dev:26247] more than 10 struct members causes segmentation fault
|
|
|
|
def test_morethan10members
|
|
|
|
list = %w( a b c d e f g h i j k l m n o p )
|
|
|
|
until list.empty?
|
2013-10-27 11:52:39 -04:00
|
|
|
c = @Struct.new(* list.map {|ch| ch.intern }).new
|
2005-06-01 11:03:09 -04:00
|
|
|
list.each do |ch|
|
|
|
|
c.__send__(ch)
|
|
|
|
end
|
|
|
|
list.pop
|
|
|
|
end
|
|
|
|
end
|
2006-02-05 10:10:01 -05:00
|
|
|
|
|
|
|
def test_small_structs
|
|
|
|
names = [:a, :b, :c, :d]
|
|
|
|
1.upto(4) {|n|
|
|
|
|
fields = names[0, n]
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(*fields)
|
2006-02-05 10:10:01 -05:00
|
|
|
o = klass.new(*(0...n).to_a)
|
|
|
|
fields.each_with_index {|name, i|
|
|
|
|
assert_equal(i, o[name])
|
|
|
|
}
|
|
|
|
o = klass.new(*(0...n).to_a.reverse)
|
|
|
|
fields.each_with_index {|name, i|
|
|
|
|
assert_equal(n-i-1, o[name])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2008-01-23 08:49:06 -05:00
|
|
|
|
|
|
|
def test_inherit
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
klass2 = Class.new(klass)
|
|
|
|
o = klass2.new(1)
|
|
|
|
assert_equal(1, o.a)
|
|
|
|
end
|
|
|
|
|
2019-07-05 13:01:06 -04:00
|
|
|
def test_attrset_id
|
|
|
|
assert_raise(ArgumentError) { Struct.new(:x=) }
|
|
|
|
end
|
|
|
|
|
2008-01-23 08:49:06 -05:00
|
|
|
def test_members
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal([:a], klass.members)
|
|
|
|
assert_equal([:a], o.members)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ref
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal(1, o[:a])
|
|
|
|
assert_raise(NameError) { o[:b] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
|
|
|
o[:a] = 2
|
|
|
|
assert_equal(2, o[:a])
|
|
|
|
assert_raise(NameError) { o[:b] = 3 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_struct_new
|
2013-10-27 11:52:39 -04:00
|
|
|
assert_raise(NameError) { @Struct.new("foo") }
|
|
|
|
assert_nothing_raised { @Struct.new("Foo") }
|
|
|
|
@Struct.instance_eval { remove_const(:Foo) }
|
|
|
|
assert_nothing_raised { @Struct.new(:a) { } }
|
|
|
|
assert_raise(RuntimeError) { @Struct.new(:a) { raise } }
|
2008-01-23 08:49:06 -05:00
|
|
|
|
|
|
|
assert_equal([:utime, :stime, :cutime, :cstime], Process.times.members)
|
|
|
|
end
|
|
|
|
|
2022-03-17 05:54:49 -04:00
|
|
|
def test_struct_new_with_hash
|
|
|
|
assert_raise_with_message(TypeError, /not a symbol/) {Struct.new(:a, {})}
|
|
|
|
assert_raise_with_message(TypeError, /not a symbol/) {Struct.new(:a, {name: "b"})}
|
2019-09-03 17:02:24 -04:00
|
|
|
end
|
|
|
|
|
2017-12-12 03:12:43 -05:00
|
|
|
def test_struct_new_with_keyword_init
|
2017-12-12 08:47:46 -05:00
|
|
|
@Struct.new("KeywordInitTrue", :a, :b, keyword_init: true)
|
|
|
|
@Struct.new("KeywordInitFalse", :a, :b, keyword_init: false)
|
2017-12-12 03:12:43 -05:00
|
|
|
|
2017-12-12 08:47:46 -05:00
|
|
|
assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new(1, 2) }
|
2019-10-29 20:08:01 -04:00
|
|
|
assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new({a: 100}, 2) }
|
2017-12-12 08:47:46 -05:00
|
|
|
assert_nothing_raised { @Struct::KeywordInitFalse.new(1, 2) }
|
|
|
|
assert_nothing_raised { @Struct::KeywordInitTrue.new(a: 1, b: 2) }
|
|
|
|
assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new(1, b: 2) }
|
|
|
|
assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new(a: 1, b: 2, c: 3) }
|
|
|
|
assert_equal @Struct::KeywordInitTrue.new(a: 1, b: 2).values, @Struct::KeywordInitFalse.new(1, 2).values
|
2017-12-12 10:03:45 -05:00
|
|
|
assert_equal "#{@Struct}::KeywordInitFalse", @Struct::KeywordInitFalse.inspect
|
|
|
|
assert_equal "#{@Struct}::KeywordInitTrue(keyword_init: true)", @Struct::KeywordInitTrue.inspect
|
2019-12-19 19:19:39 -05:00
|
|
|
# eval is needed to prevent the warning duplication filter
|
2020-05-07 23:58:19 -04:00
|
|
|
k = Class.new(@Struct::KeywordInitTrue) {def initialize(b, options); super(a: options, b: b); end}
|
|
|
|
o = assert_warn('') { k.new(42, {foo: 1, bar: 2}) }
|
|
|
|
assert_equal(1, o.a[:foo])
|
2017-12-12 03:12:43 -05:00
|
|
|
|
|
|
|
@Struct.instance_eval do
|
2017-12-12 08:47:46 -05:00
|
|
|
remove_const(:KeywordInitTrue)
|
|
|
|
remove_const(:KeywordInitFalse)
|
2017-12-12 03:12:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-27 11:07:49 -04:00
|
|
|
def test_struct_new_with_keyword_init_and_block
|
|
|
|
struct = @Struct.new(:a, :b, keyword_init: true) do
|
|
|
|
def c
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(3, struct.new(a: 1, b: 2).c)
|
|
|
|
end
|
|
|
|
|
2021-07-15 05:21:49 -04:00
|
|
|
def test_struct_keyword_init_p
|
|
|
|
struct = @Struct.new(:a, :b, keyword_init: true)
|
|
|
|
assert_equal(true, struct.keyword_init?)
|
|
|
|
|
|
|
|
struct = @Struct.new(:a, :b, keyword_init: false)
|
|
|
|
assert_equal(false, struct.keyword_init?)
|
2021-07-15 08:33:39 -04:00
|
|
|
|
|
|
|
struct = @Struct.new(:a, :b)
|
|
|
|
assert_nil(struct.keyword_init?)
|
2021-07-15 05:21:49 -04:00
|
|
|
end
|
|
|
|
|
2008-01-23 08:49:06 -05:00
|
|
|
def test_initialize
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
assert_raise(ArgumentError) { klass.new(1, 2) }
|
2015-12-15 22:25:09 -05:00
|
|
|
klass = @Struct.new(:total) do
|
|
|
|
def initialize(a, b)
|
|
|
|
super(a+b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 3, klass.new(1,2).total
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
2020-05-07 23:58:19 -04:00
|
|
|
def test_initialize_with_kw
|
|
|
|
klass = @Struct.new(:foo, :options) do
|
|
|
|
def initialize(foo, **options)
|
|
|
|
super(foo, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal({}, klass.new(42, **Hash.new).options)
|
|
|
|
x = assert_warn('') { klass.new(1, bar: 2) }
|
|
|
|
assert_equal 2, x.options[:bar]
|
|
|
|
end
|
|
|
|
|
2008-01-23 08:49:06 -05:00
|
|
|
def test_each
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a, :b)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1, 2)
|
|
|
|
assert_equal([1, 2], o.each.to_a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_pair
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a, :b)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1, 2)
|
|
|
|
assert_equal([[:a, 1], [:b, 2]], o.each_pair.to_a)
|
2012-11-19 02:31:05 -05:00
|
|
|
bug7382 = '[ruby-dev:46533]'
|
|
|
|
a = []
|
|
|
|
o.each_pair {|x| a << x}
|
|
|
|
assert_equal([[:a, 1], [:b, 2]], a, bug7382)
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal("#<struct a=1>", o.inspect)
|
|
|
|
o.a = o
|
2009-08-12 02:15:30 -04:00
|
|
|
assert_match(/^#<struct a=#<struct #<.*?>:...>>$/, o.inspect)
|
2008-01-23 08:49:06 -05:00
|
|
|
|
2013-10-27 11:52:39 -04:00
|
|
|
@Struct.new("Foo", :a)
|
|
|
|
o = @Struct::Foo.new(1)
|
|
|
|
assert_equal("#<struct #@Struct::Foo a=1>", o.inspect)
|
|
|
|
@Struct.instance_eval { remove_const(:Foo) }
|
2008-01-23 08:49:06 -05:00
|
|
|
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a, :b)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1, 2)
|
|
|
|
assert_equal("#<struct a=1, b=2>", o.inspect)
|
|
|
|
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:@a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
2013-08-11 02:11:17 -04:00
|
|
|
assert_equal(1, o.__send__(:@a))
|
2008-01-23 08:49:06 -05:00
|
|
|
assert_equal("#<struct :@a=1>", o.inspect)
|
2013-08-11 02:11:17 -04:00
|
|
|
o.__send__(:"@a=", 2)
|
|
|
|
assert_equal(2, o.__send__(:@a))
|
|
|
|
assert_equal("#<struct :@a=2>", o.inspect)
|
|
|
|
o.__send__("@a=", 3)
|
|
|
|
assert_equal(3, o.__send__(:@a))
|
|
|
|
assert_equal("#<struct :@a=3>", o.inspect)
|
|
|
|
|
2013-08-10 00:53:31 -04:00
|
|
|
methods = klass.instance_methods(false)
|
2016-11-14 14:27:24 -05:00
|
|
|
assert_equal([:@a, :"@a="].sort.inspect, methods.sort.inspect, '[Bug #8756]')
|
2013-08-10 00:53:31 -04:00
|
|
|
assert_include(methods, :@a)
|
|
|
|
assert_include(methods, :"@a=")
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_init_copy
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal(o, o.dup)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_aref
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal(1, o[0])
|
2015-11-16 00:46:50 -05:00
|
|
|
assert_raise_with_message(IndexError, /offset -2\b/) {o[-2]}
|
|
|
|
assert_raise_with_message(IndexError, /offset 1\b/) {o[1]}
|
2014-03-28 08:36:27 -04:00
|
|
|
assert_raise_with_message(NameError, /foo/) {o["foo"]}
|
|
|
|
assert_raise_with_message(NameError, /foo/) {o[:foo]}
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_aset
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
|
|
|
o[0] = 2
|
|
|
|
assert_equal(2, o[:a])
|
2015-11-16 00:46:50 -05:00
|
|
|
assert_raise_with_message(IndexError, /offset -2\b/) {o[-2] = 3}
|
|
|
|
assert_raise_with_message(IndexError, /offset 1\b/) {o[1] = 3}
|
2014-03-28 08:36:27 -04:00
|
|
|
assert_raise_with_message(NameError, /foo/) {o["foo"] = 3}
|
|
|
|
assert_raise_with_message(NameError, /foo/) {o[:foo] = 3}
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_values_at
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a, :b, :c, :d, :e, :f)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1, 2, 3, 4, 5, 6)
|
|
|
|
assert_equal([2, 4, 6], o.values_at(1, 3, 5))
|
|
|
|
assert_equal([2, 3, 4, 3, 4, 5], o.values_at(1..3, 2...5))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_select
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a, :b, :c, :d, :e, :f)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1, 2, 3, 4, 5, 6)
|
|
|
|
assert_equal([1, 3, 5], o.select {|v| v % 2 != 0 })
|
|
|
|
assert_raise(ArgumentError) { o.select(1) }
|
|
|
|
end
|
|
|
|
|
2018-09-16 00:00:14 -04:00
|
|
|
def test_filter
|
|
|
|
klass = @Struct.new(:a, :b, :c, :d, :e, :f)
|
|
|
|
o = klass.new(1, 2, 3, 4, 5, 6)
|
|
|
|
assert_equal([1, 3, 5], o.filter {|v| v % 2 != 0 })
|
|
|
|
assert_raise(ArgumentError) { o.filter(1) }
|
|
|
|
end
|
|
|
|
|
2014-12-09 10:43:49 -05:00
|
|
|
def test_big_struct
|
|
|
|
klass1 = @Struct.new(*('a'..'z').map(&:to_sym))
|
|
|
|
o = klass1.new
|
|
|
|
assert_nil o.z
|
|
|
|
assert_equal(:foo, o.z = :foo)
|
|
|
|
assert_equal(:foo, o.z)
|
|
|
|
assert_equal(:foo, o[25])
|
|
|
|
end
|
|
|
|
|
2014-12-16 01:18:25 -05:00
|
|
|
def test_overridden_aset
|
|
|
|
bug10601 = '[ruby-core:66846] [Bug #10601]: should not be affected by []= method'
|
|
|
|
|
|
|
|
struct = Class.new(Struct.new(*(:a..:z), :result)) do
|
|
|
|
def []=(*args)
|
|
|
|
raise args.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
obj = struct.new
|
|
|
|
assert_nothing_raised(RuntimeError, bug10601) do
|
|
|
|
obj.result = 42
|
|
|
|
end
|
|
|
|
assert_equal(42, obj.result, bug10601)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_overridden_aref
|
|
|
|
bug10601 = '[ruby-core:66846] [Bug #10601]: should not be affected by [] method'
|
|
|
|
|
|
|
|
struct = Class.new(Struct.new(*(:a..:z), :result)) do
|
|
|
|
def [](*args)
|
|
|
|
raise args.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
obj = struct.new
|
|
|
|
obj.result = 42
|
|
|
|
result = assert_nothing_raised(RuntimeError, bug10601) do
|
|
|
|
break obj.result
|
|
|
|
end
|
|
|
|
assert_equal(42, result, bug10601)
|
|
|
|
end
|
|
|
|
|
2008-01-23 08:49:06 -05:00
|
|
|
def test_equal
|
2013-10-27 11:52:39 -04:00
|
|
|
klass1 = @Struct.new(:a)
|
|
|
|
klass2 = @Struct.new(:a, :b)
|
2008-01-23 08:49:06 -05:00
|
|
|
o1 = klass1.new(1)
|
|
|
|
o2 = klass1.new(1)
|
|
|
|
o3 = klass2.new(1)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal(o1, o2)
|
|
|
|
assert_not_equal(o1, o3)
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
2016-05-21 14:06:04 -04:00
|
|
|
assert_kind_of(Integer, o.hash)
|
2019-04-08 01:06:43 -04:00
|
|
|
assert_kind_of(String, o.hash.to_s)
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_eql
|
2013-10-27 11:52:39 -04:00
|
|
|
klass1 = @Struct.new(:a)
|
|
|
|
klass2 = @Struct.new(:a, :b)
|
2008-01-23 08:49:06 -05:00
|
|
|
o1 = klass1.new(1)
|
|
|
|
o2 = klass1.new(1)
|
|
|
|
o3 = klass2.new(1)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(o1, :eql?, o2)
|
|
|
|
assert_not_operator(o1, :eql?, o3)
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_size
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a)
|
2008-01-23 08:49:06 -05:00
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal(1, o.size)
|
|
|
|
end
|
2008-05-19 01:20:21 -04:00
|
|
|
|
|
|
|
def test_error
|
|
|
|
assert_raise(TypeError){
|
2013-10-27 11:52:39 -04:00
|
|
|
@Struct.new(0)
|
2008-05-19 01:20:21 -04:00
|
|
|
}
|
|
|
|
end
|
2009-08-12 02:32:21 -04:00
|
|
|
|
2013-10-27 11:52:39 -04:00
|
|
|
def test_redefinition_warning
|
2020-01-28 01:21:26 -05:00
|
|
|
@Struct.new(name = "RedefinitionWarning")
|
2013-10-27 11:52:39 -04:00
|
|
|
e = EnvUtil.verbose_warning do
|
|
|
|
@Struct.new("RedefinitionWarning")
|
|
|
|
end
|
|
|
|
assert_match(/redefining constant #@Struct::RedefinitionWarning/, e)
|
2020-01-28 01:21:26 -05:00
|
|
|
|
|
|
|
@Struct.class_eval do
|
|
|
|
remove_const name
|
|
|
|
end
|
2013-10-27 11:52:39 -04:00
|
|
|
end
|
|
|
|
|
2021-01-17 04:35:54 -05:00
|
|
|
def test_keyword_args_warning
|
2021-12-26 08:39:48 -05:00
|
|
|
assert_warn('') { assert_equal(1, @Struct.new(:a).new(a: 1).a) }
|
|
|
|
assert_warn('') { assert_equal(1, @Struct.new(:a, keyword_init: nil).new(a: 1).a) }
|
2021-01-17 04:35:54 -05:00
|
|
|
assert_warn('') { assert_equal({a: 1}, @Struct.new(:a).new({a: 1}).a) }
|
|
|
|
assert_warn('') { assert_equal({a: 1}, @Struct.new(:a, :b).new(1, a: 1).b) }
|
|
|
|
assert_warn('') { assert_equal(1, @Struct.new(:a, keyword_init: true).new(a: 1).a) }
|
|
|
|
assert_warn('') { assert_equal({a: 1}, @Struct.new(:a, keyword_init: nil).new({a: 1}).a) }
|
|
|
|
assert_warn('') { assert_equal({a: 1}, @Struct.new(:a, keyword_init: false).new(a: 1).a) }
|
|
|
|
assert_warn('') { assert_equal({a: 1}, @Struct.new(:a, keyword_init: false).new({a: 1}).a) }
|
|
|
|
end
|
|
|
|
|
2009-08-12 02:32:21 -04:00
|
|
|
def test_nonascii
|
2020-01-28 01:21:26 -05:00
|
|
|
struct_test = @Struct.new(name = "R\u{e9}sum\u{e9}", :"r\u{e9}sum\u{e9}")
|
2013-10-27 11:52:39 -04:00
|
|
|
assert_equal(@Struct.const_get("R\u{e9}sum\u{e9}"), struct_test, '[ruby-core:24849]')
|
2009-08-12 02:32:21 -04:00
|
|
|
a = struct_test.new(42)
|
2013-10-27 11:52:39 -04:00
|
|
|
assert_equal("#<struct #@Struct::R\u{e9}sum\u{e9} r\u{e9}sum\u{e9}=42>", a.inspect, '[ruby-core:24849]')
|
|
|
|
e = EnvUtil.verbose_warning do
|
|
|
|
@Struct.new("R\u{e9}sum\u{e9}", :"r\u{e9}sum\u{e9}")
|
|
|
|
end
|
|
|
|
assert_nothing_raised(Encoding::CompatibilityError) do
|
|
|
|
assert_match(/redefining constant #@Struct::R\u{e9}sum\u{e9}/, e)
|
|
|
|
end
|
2020-01-28 01:21:26 -05:00
|
|
|
|
|
|
|
@Struct.class_eval do
|
|
|
|
remove_const name
|
|
|
|
end
|
2009-08-12 02:32:21 -04:00
|
|
|
end
|
2009-11-26 07:17:47 -05:00
|
|
|
|
2012-12-21 21:52:48 -05:00
|
|
|
def test_junk
|
2013-10-27 11:52:39 -04:00
|
|
|
struct_test = @Struct.new("Foo", "a\000")
|
2012-12-21 21:52:48 -05:00
|
|
|
o = struct_test.new(1)
|
|
|
|
assert_equal(1, o.send("a\000"))
|
2013-10-27 11:52:39 -04:00
|
|
|
@Struct.instance_eval { remove_const(:Foo) }
|
2012-12-21 21:52:48 -05:00
|
|
|
end
|
|
|
|
|
2009-11-26 07:17:47 -05:00
|
|
|
def test_comparison_when_recursive
|
2013-10-27 11:52:39 -04:00
|
|
|
klass1 = @Struct.new(:a, :b, :c)
|
2009-11-26 07:17:47 -05:00
|
|
|
|
|
|
|
x = klass1.new(1, 2, nil); x.c = x
|
|
|
|
y = klass1.new(1, 2, nil); y.c = y
|
|
|
|
Timeout.timeout(1) {
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal x, y
|
|
|
|
assert_operator x, :eql?, y
|
2009-11-26 07:17:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
z = klass1.new(:something, :other, nil); z.c = z
|
|
|
|
Timeout.timeout(1) {
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_equal x, z
|
|
|
|
assert_not_operator x, :eql?, z
|
2009-11-26 07:17:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
x.c = y; y.c = x
|
|
|
|
Timeout.timeout(1) {
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal x, y
|
|
|
|
assert_operator x, :eql?, y
|
2009-11-26 07:17:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
x.c = z; z.c = x
|
|
|
|
Timeout.timeout(1) {
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_equal x, z
|
|
|
|
assert_not_operator x, :eql?, z
|
2009-11-26 07:17:47 -05:00
|
|
|
}
|
|
|
|
end
|
2011-07-17 03:26:45 -04:00
|
|
|
|
2012-04-23 23:46:55 -04:00
|
|
|
def test_to_h
|
2013-10-27 11:52:39 -04:00
|
|
|
klass = @Struct.new(:a, :b, :c, :d, :e, :f)
|
2012-04-23 23:46:55 -04:00
|
|
|
o = klass.new(1, 2, 3, 4, 5, 6)
|
|
|
|
assert_equal({a:1, b:2, c:3, d:4, e:5, f:6}, o.to_h)
|
|
|
|
end
|
2013-10-27 11:52:39 -04:00
|
|
|
|
2018-09-20 11:06:56 -04:00
|
|
|
def test_to_h_block
|
|
|
|
klass = @Struct.new(:a, :b, :c, :d, :e, :f)
|
|
|
|
o = klass.new(1, 2, 3, 4, 5, 6)
|
|
|
|
assert_equal({"a" => 1, "b" => 4, "c" => 9, "d" => 16, "e" => 25, "f" => 36},
|
|
|
|
o.to_h {|k, v| [k.to_s, v*v]})
|
|
|
|
end
|
|
|
|
|
2013-12-06 18:25:24 -05:00
|
|
|
def test_question_mark_in_member
|
|
|
|
klass = @Struct.new(:a, :b?)
|
2013-12-06 20:47:22 -05:00
|
|
|
x = Object.new
|
|
|
|
o = klass.new("test", x)
|
|
|
|
assert_same(x, o.b?)
|
2014-02-13 01:43:18 -05:00
|
|
|
o.send("b?=", 42)
|
|
|
|
assert_equal(42, o.b?)
|
2013-12-06 18:25:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bang_mark_in_member
|
|
|
|
klass = @Struct.new(:a, :b!)
|
2013-12-06 20:47:22 -05:00
|
|
|
x = Object.new
|
|
|
|
o = klass.new("test", x)
|
|
|
|
assert_same(x, o.b!)
|
2014-02-13 01:43:18 -05:00
|
|
|
o.send("b!=", 42)
|
|
|
|
assert_equal(42, o.b!)
|
2013-12-06 18:25:24 -05:00
|
|
|
end
|
|
|
|
|
2014-01-04 08:44:34 -05:00
|
|
|
def test_setter_method_returns_value
|
|
|
|
klass = @Struct.new(:a)
|
|
|
|
x = klass.new
|
|
|
|
assert_equal "[Bug #9353]", x.send(:a=, "[Bug #9353]")
|
|
|
|
end
|
|
|
|
|
2015-11-16 04:21:56 -05:00
|
|
|
def test_dig
|
|
|
|
klass = @Struct.new(:a)
|
|
|
|
o = klass.new(klass.new({b: [1, 2, 3]}))
|
|
|
|
assert_equal(1, o.dig(:a, :a, :b, 0))
|
|
|
|
assert_nil(o.dig(:b, 0))
|
|
|
|
end
|
|
|
|
|
2019-11-07 22:44:31 -05:00
|
|
|
def test_new_duplicate
|
2016-04-15 21:00:13 -04:00
|
|
|
bug12291 = '[ruby-core:74971] [Bug #12291]'
|
|
|
|
assert_raise_with_message(ArgumentError, /duplicate member/, bug12291) {
|
|
|
|
@Struct.new(:a, :a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-11-07 21:37:07 -05:00
|
|
|
def test_deconstruct_keys
|
|
|
|
klass = @Struct.new(:a, :b)
|
|
|
|
o = klass.new(1, 2)
|
|
|
|
assert_equal({a: 1, b: 2}, o.deconstruct_keys(nil))
|
|
|
|
assert_equal({a: 1, b: 2}, o.deconstruct_keys([:b, :a]))
|
|
|
|
assert_equal({a: 1}, o.deconstruct_keys([:a]))
|
2019-11-19 09:53:01 -05:00
|
|
|
assert_not_send([o.deconstruct_keys([:a, :c]), :key?, :c])
|
2019-11-07 21:37:07 -05:00
|
|
|
assert_raise(TypeError) {
|
|
|
|
o.deconstruct_keys(0)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-11-20 10:31:51 -05:00
|
|
|
def test_public_send
|
|
|
|
klass = @Struct.new(:a)
|
|
|
|
x = klass.new(1)
|
|
|
|
assert_equal(1, x.public_send("a"))
|
|
|
|
assert_equal(42, x.public_send("a=", 42))
|
|
|
|
assert_equal(42, x.public_send("a"))
|
|
|
|
end
|
|
|
|
|
2021-12-12 12:02:41 -05:00
|
|
|
def test_arity
|
|
|
|
klass = @Struct.new(:a)
|
|
|
|
assert_equal 0, klass.instance_method(:a).arity
|
|
|
|
assert_equal 1, klass.instance_method(:a=).arity
|
|
|
|
|
|
|
|
klass.module_eval do
|
2021-12-12 12:11:05 -05:00
|
|
|
define_method(:b=, instance_method(:a=))
|
2021-12-12 12:02:41 -05:00
|
|
|
alias c= a=
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 1, klass.instance_method(:b=).arity
|
|
|
|
assert_equal 1, klass.instance_method(:c=).arity
|
|
|
|
end
|
|
|
|
|
2021-12-12 11:58:21 -05:00
|
|
|
def test_parameters
|
|
|
|
klass = @Struct.new(:a)
|
|
|
|
assert_equal [], klass.instance_method(:a).parameters
|
|
|
|
# NOTE: :_ may not be a spec.
|
|
|
|
assert_equal [[:req, :_]], klass.instance_method(:a=).parameters
|
|
|
|
|
|
|
|
klass.module_eval do
|
2021-12-12 12:11:05 -05:00
|
|
|
define_method(:b=, instance_method(:a=))
|
2021-12-12 11:58:21 -05:00
|
|
|
alias c= a=
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [[:req, :_]], klass.instance_method(:b=).parameters
|
|
|
|
assert_equal [[:req, :_]], klass.instance_method(:c=).parameters
|
|
|
|
end
|
|
|
|
|
2013-10-27 11:52:39 -04:00
|
|
|
class TopStruct < Test::Unit::TestCase
|
|
|
|
include TestStruct
|
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
@Struct = Struct
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SubStruct < Test::Unit::TestCase
|
|
|
|
include TestStruct
|
|
|
|
SubStruct = Class.new(Struct)
|
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
@Struct = SubStruct
|
|
|
|
end
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|