From adf709a78534c1483ba851ccb0490464ca31503c Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 7 May 2020 23:58:19 -0400 Subject: Classes made from Struct should have default `new` singleton method. [Bug #16465] [Bug #16801] [Fix GH-2795] [Fix GH-2944] [Fix GH-3045] [Fix GH-3093] Note: Backporting shouldn't modify object.h and instead can use struct_new_kw which is basically a duplicate implementation of rb_class_new_instance_pass_kw Co-authored-by: Yusuke Endoh Co-authored-by: John Hawthorn Co-authored-by: Adam Hess Co-authored-by: Jose Cortinas Co-authored-by: Jean Boussier --- test/ruby/test_struct.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index 5f073a3315..c313ab0dbe 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -118,10 +118,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_raise(ArgumentError) { 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) @@ -150,6 +149,17 @@ module TestStruct assert_equal 3, klass.new(1,2).total 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 klass = @Struct.new(:a, :b) o = klass.new(1, 2) -- cgit v1.2.3