diff options
author | Marc-Andre Lafortune <github@marc-andre.ca> | 2020-05-07 23:58:19 -0400 |
---|---|---|
committer | Marc-André Lafortune <github@marc-andre.ca> | 2020-05-08 04:18:45 -0400 |
commit | adf709a78534c1483ba851ccb0490464ca31503c (patch) | |
tree | 3bd79435a29f2320e457646839e98768c69862f7 /test | |
parent | da345adc1c9cb6182ee11c43853f67bb41aeea76 (diff) |
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 <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>
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3093
Diffstat (limited to 'test')
-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 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) |