diff options
author | nagachika <nagachika@ruby-lang.org> | 2020-07-26 17:45:27 +0900 |
---|---|---|
committer | nagachika <nagachika@ruby-lang.org> | 2020-07-26 17:45:31 +0900 |
commit | 61c6d433060881e952140d2154c06f8c9803dc8a (patch) | |
tree | 27192727f11eb76d4c7fcb653650953499b8acaf /test/ruby | |
parent | 43cc6997c59d2fbe3b91e91ee52faaa8749e9349 (diff) |
partially merge revision adf709a78534c1483ba851ccb0490464ca31503c: [Backport #16801]
Classes made from Struct should have default new singleton method.
Co-authored-by: Yusuke Endoh mame@ruby-lang.org
Co-authored-by: John Hawthorn john@hawthorn.email
Co-authored-by: Adam Hess HParker@github.com
Co-authored-by: Jose Cortinas jacortinas@gmail.com
Co-authored-by: Jean Boussier jean.boussier@gmail.com
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_struct.rb | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index f13afbbdd4..85de0f23a9 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -114,10 +114,9 @@ module TestStruct assert_equal "#{@Struct}::KeywordInitFalse", @Struct::KeywordInitFalse.inspect assert_equal "#{@Struct}::KeywordInitTrue(keyword_init: true)", @Struct::KeywordInitTrue.inspect # eval is needed to prevent the warning duplication filter - k = eval("Class.new(@Struct::KeywordInitFalse) {def initialize(**) end}") - assert_warn(/Using the last argument as keyword parameters is deprecated/) {k.new(a: 1, b: 2)} - k = Class.new(@Struct::KeywordInitTrue) {def initialize(**) end} - assert_warn('') {k.new(a: 1, b: 2)} + 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]) @Struct.instance_eval do remove_const(:KeywordInitTrue) @@ -152,6 +151,17 @@ module TestStruct assert_equal([1, 2], o.each.to_a) end + 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 + def test_each_pair klass = @Struct.new(:a, :b) o = klass.new(1, 2) |