From a5acf8a4278c3135afc2068bcfe4bddb98138a06 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Sun, 2 Sep 2018 19:24:57 +0200 Subject: [PATCH] Fixed issue #2 and likely #1 --- spec/sales_backend_spec.cr | 38 +++++++++++++++++++++++++++++++++++- src/sales_backend/address.cr | 28 +++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/spec/sales_backend_spec.cr b/spec/sales_backend_spec.cr index ccebaf3..a5a6b20 100644 --- a/spec/sales_backend_spec.cr +++ b/spec/sales_backend_spec.cr @@ -6,6 +6,42 @@ describe SalesBackend do String.from_json(Global.response.not_nil!.body).should eq "OK" end + it "can compare addresses" do + address_str1 = Address.from_json %( + { + "name": "String", + "address1": "String", + "address2": "{type: String, nilable: true}", + "postcode": "00001", + "city": "String", + "country": 2 + } + ) + address_str2 = Address.from_json %( + { + "name": "String", + "address1": "String", + "address2": "{type: String, nilable: true}", + "postcode": "00002", + "city": "String", + "country": 2 + } + ) + address_str3 = Address.from_json %( + { + "name": "String", + "address1": "String", + "address2": "{type: String, nilable: true}", + "postcode": "00003", + "city": "String", + "country": 2 + } + ) + (address_str1<=>address_str1).should eq( 0) + (address_str3<=>address_str2).should eq(1) + (address_str1<=>address_str2).should eq(-1) + end + it "can add a user able to log in and out" do Dir.mkdir_p Statics.data_path + "user" usr = User.new("dummy@domain.com") @@ -46,7 +82,7 @@ describe SalesBackend do Global.response.not_nil!.status_code.should eq(200) address_state = Array(Address).from_json(Global.response.not_nil!.body) expected_address_state = Array(Address).from_json(new_address_str) - (address_state.to_json==expected_address_state.to_json).should be_true + (address_state.to_json == expected_address_state.to_json).should be_true get "/user/tokens", headers Global.response.not_nil!.status_code.should eq(200) diff --git a/src/sales_backend/address.cr b/src/sales_backend/address.cr index 5520bfc..7b9c3ba 100644 --- a/src/sales_backend/address.cr +++ b/src/sales_backend/address.cr @@ -20,6 +20,32 @@ class Address is_default: {type: Bool, default: false}, ) def <=> (other : Address) - self.to_json<=>other.to_json + cmp = @name<=>other.name + if cmp==0 + cmp = @address1<=>other.address1 + if cmp==0 + if @address2.nil? + if(other.address2.nil?) + cmp=0 + elsif + cmp=-1 + end + elsif other.address2.nil? + cmp=1 + else + cmp = @address2.not_nil!<=>other.address2.not_nil! + end + if cmp==0 + cmp = @postcode<=>other.postcode + if cmp==0 + cmp = @city<=>other.city + if cmp==0 + cmp = @country<=>other.country + end + end + end + end + end + return cmp end end \ No newline at end of file