Merge pull request #1613 from xhh/ruby-eql-hash

[Ruby] Add "==", "eql?" and "hash" methods to Ruby client
This commit is contained in:
wing328 2015-11-26 10:29:59 +08:00
commit 1fbdb92625
12 changed files with 177 additions and 15 deletions

View File

@ -43,6 +43,19 @@ module {{moduleName}}
@{{{name}}} = {{{name}}}
end
{{/isEnum}}{{/vars}}
def ==(o)
return true if self.equal?(o)
self.class == o.class{{#vars}} &&
{{name}} == o.{{name}}{{/vars}}
end
def eql?(o)
self == o
end
def hash
[{{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}].hash
end
end
{{/model}}
{{/models}}

View File

@ -179,19 +179,25 @@ public class PetApiTest {
Pet pet1 = new Pet();
Pet pet2 = new Pet();
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
pet2.setName("really-happy");
pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertFalse(pet1.equals(pet2));
assertFalse(pet1.equals(pet2));
assertFalse(pet2.equals(pet1));
assertFalse(pet1.hashCode() == (pet2.hashCode()));
assertTrue(pet2.equals(pet2));
assertTrue(pet2.hashCode() == pet2.hashCode());
pet1.setName("really-happy");
pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
}

View File

@ -179,19 +179,25 @@ public class PetApiTest {
Pet pet1 = new Pet();
Pet pet2 = new Pet();
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
pet2.setName("really-happy");
pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertFalse(pet1.equals(pet2));
assertFalse(pet1.equals(pet2));
assertFalse(pet2.equals(pet1));
assertFalse(pet1.hashCode() == (pet2.hashCode()));
assertTrue(pet2.equals(pet2));
assertTrue(pet2.hashCode() == pet2.hashCode());
pet1.setName("really-happy");
pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
}

View File

@ -277,19 +277,25 @@ public class PetApiTest {
Pet pet1 = new Pet();
Pet pet2 = new Pet();
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
pet2.setName("really-happy");
pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertFalse(pet1.equals(pet2));
assertFalse(pet1.equals(pet2));
assertFalse(pet2.equals(pet1));
assertFalse(pet1.hashCode() == (pet2.hashCode()));
assertTrue(pet2.equals(pet2));
assertTrue(pet2.hashCode() == pet2.hashCode());
pet1.setName("really-happy");
pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
}

View File

@ -148,19 +148,25 @@ public class PetApiTest {
Pet pet1 = new Pet();
Pet pet2 = new Pet();
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
pet2.setName("really-happy");
pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertFalse(pet1.equals(pet2));
assertFalse(pet1.equals(pet2));
assertFalse(pet2.equals(pet1));
assertFalse(pet1.hashCode() == (pet2.hashCode()));
assertTrue(pet2.equals(pet2));
assertTrue(pet2.hashCode() == pet2.hashCode());
pet1.setName("really-happy");
pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
}

View File

@ -147,19 +147,25 @@ public class PetApiTest {
Pet pet1 = new Pet();
Pet pet2 = new Pet();
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
pet2.setName("really-happy");
pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertFalse(pet1.equals(pet2));
assertFalse(pet1.equals(pet2));
assertFalse(pet2.equals(pet1));
assertFalse(pet1.hashCode() == (pet2.hashCode()));
assertTrue(pet2.equals(pet2));
assertTrue(pet2.hashCode() == pet2.hashCode());
pet1.setName("really-happy");
pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}));
assertTrue(pet1.equals(pet2));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
}

View File

@ -41,5 +41,19 @@ module Petstore
end
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
id == o.id &&
name == o.name
end
def eql?(o)
self == o
end
def hash
[id, name].hash
end
end
end

View File

@ -81,5 +81,23 @@ module Petstore
@status = status
end
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
id == o.id &&
pet_id == o.pet_id &&
quantity == o.quantity &&
ship_date == o.ship_date &&
status == o.status &&
complete == o.complete
end
def eql?(o)
self == o
end
def hash
[id, pet_id, quantity, ship_date, status, complete].hash
end
end
end

View File

@ -85,5 +85,23 @@ module Petstore
@status = status
end
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
id == o.id &&
category == o.category &&
name == o.name &&
photo_urls == o.photo_urls &&
tags == o.tags &&
status == o.status
end
def eql?(o)
self == o
end
def hash
[id, category, name, photo_urls, tags, status].hash
end
end
end

View File

@ -41,5 +41,19 @@ module Petstore
end
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
id == o.id &&
name == o.name
end
def eql?(o)
self == o
end
def hash
[id, name].hash
end
end
end

View File

@ -89,5 +89,25 @@ module Petstore
end
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
id == o.id &&
username == o.username &&
first_name == o.first_name &&
last_name == o.last_name &&
email == o.email &&
password == o.password &&
phone == o.phone &&
user_status == o.user_status
end
def eql?(o)
self == o
end
def hash
[id, username, first_name, last_name, email, password, phone, user_status].hash
end
end
end

View File

@ -125,5 +125,40 @@ describe "Pet" do
result = @pet_api.upload_file(10002, file: File.new('hello.txt'), additional_metadata: 'metadata')
result.should be_nil
end
it "should implement eql? and hash" do
pet1 = Petstore::Pet.new
pet2 = Petstore::Pet.new
pet1.should == pet2
pet2.should == pet1
pet1.eql?(pet2).should == true
pet2.eql?(pet1).should == true
pet1.hash.should == pet2.hash
pet1.should == pet1
pet1.eql?(pet1).should == true
pet1.hash.should == pet1.hash
pet1.name = 'really-happy'
pet1.photo_urls = ['http://foo.bar.com/1', 'http://foo.bar.com/2']
pet1.should_not == pet2
pet2.should_not == pet1
pet1.eql?(pet2).should == false
pet2.eql?(pet1).should == false
pet1.hash.should_not == pet2.hash
pet1.should == pet1
pet1.eql?(pet1).should == true
pet1.hash.should == pet1.hash
pet2.name = 'really-happy'
pet2.photo_urls = ['http://foo.bar.com/1', 'http://foo.bar.com/2']
pet1.should == pet2
pet2.should == pet1
pet1.eql?(pet2).should == true
pet2.eql?(pet1).should == true
pet1.hash.should == pet2.hash
pet2.should == pet2
pet2.eql?(pet2).should == true
pet2.hash.should == pet2.hash
end
end
end