mirror of
https://github.com/ruby/ruby.git
synced 2025-09-16 17:14:01 +02:00
struct.c: add keyword_init option to Struct.new
to initialize struct with keyword arguments. [Feature #11925] [close GH-1771] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a679472c0e
commit
02015974a3
3 changed files with 92 additions and 11 deletions
|
@ -92,6 +92,23 @@ module TestStruct
|
|||
assert_equal([:utime, :stime, :cutime, :cstime], Process.times.members)
|
||||
end
|
||||
|
||||
def test_struct_new_with_keyword_init
|
||||
@Struct.new("KeywordArgsTrue", :a, :b, keyword_init: true)
|
||||
@Struct.new("KeywordArgsFalse", :a, :b, keyword_init: false)
|
||||
|
||||
assert_raise(ArgumentError) { @Struct::KeywordArgsTrue.new(1, 2) }
|
||||
assert_nothing_raised { @Struct::KeywordArgsFalse.new(1, 2) }
|
||||
assert_nothing_raised { @Struct::KeywordArgsTrue.new(a: 1, b: 2) }
|
||||
assert_raise(ArgumentError) { @Struct::KeywordArgsTrue.new(1, b: 2) }
|
||||
assert_raise(ArgumentError) { @Struct::KeywordArgsTrue.new(a: 1, b: 2, c: 3) }
|
||||
assert_equal @Struct::KeywordArgsTrue.new(a: 1, b: 2).values, @Struct::KeywordArgsFalse.new(1, 2).values
|
||||
|
||||
@Struct.instance_eval do
|
||||
remove_const(:KeywordArgsTrue)
|
||||
remove_const(:KeywordArgsFalse)
|
||||
end
|
||||
end
|
||||
|
||||
def test_initialize
|
||||
klass = @Struct.new(:a)
|
||||
assert_raise(ArgumentError) { klass.new(1, 2) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue