1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61504 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2017-12-27 16:12:47 +00:00
parent 0f989b87a0
commit a34db218ad
162 changed files with 1267 additions and 621 deletions

View file

@ -131,4 +131,47 @@ describe "Struct.new" do
lambda { StructClasses::Ruby.new('2.0', 'i686', true) }.should raise_error(ArgumentError)
end
end
ruby_version_is "2.5" do
context "keyword_init: true option" do
before :all do
@struct_with_kwa = Struct.new(:name, :legs, keyword_init: true)
@struct_without_kwa = Struct.new(:name, :legs, keyword_init: false)
end
it "creates a class that accepts keyword arguments to initialize" do
obj = @struct_with_kwa.new(name: "elefant", legs: 4)
obj.name.should == "elefant"
obj.legs.should == 4
end
describe "new class instantiation" do
it "accepts arguments as hash as well" do
obj = @struct_with_kwa.new({name: "elefant", legs: 4})
obj.name.should == "elefant"
obj.legs.should == 4
end
it "raises ArgumentError when passed not declared keyword argument" do
-> {
@struct_with_kwa.new(name: "elefant", legs: 4, foo: "bar")
}.should raise_error(ArgumentError, /unknown keywords: foo/)
end
it "raises ArgumentError when passed a list of arguments" do
-> {
@struct_with_kwa.new("elefant", 4)
}.should raise_error(ArgumentError, /wrong number of arguments/)
end
end
end
context "keyword_init: false option" do
it "behaves like it does without :keyword_init option" do
obj = @struct_without_kwa.new("elefant", 4)
obj.name.should == "elefant"
obj.legs.should == 4
end
end
end
end

View file

@ -16,6 +16,13 @@ describe :struct_equal_value, shared: true do
car.send(@method, different_car).should == false
end
it "returns false if other is of a different class" do
car = StructClasses::Car.new("Honda", "Accord", "1998")
klass = Struct.new(:make, :model, :year)
clone = klass.new("Honda", "Accord", "1998")
car.send(@method, clone).should == false
end
it "handles recursive structures by returning false if a difference can be found" do
x = StructClasses::Car.new("Honda", "Accord", "1998")
x[:make] = x