From b96e4ec46d65d3860e31d1f353387bd4edabef3d Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 10 May 2018 17:26:22 +0100 Subject: [PATCH] Implement comparison operator for ApiModel --- lib/requisite/api_model.rb | 4 ++++ test/requisite/api_model_test.rb | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/requisite/api_model.rb b/lib/requisite/api_model.rb index c77a9c7..4f0c483 100644 --- a/lib/requisite/api_model.rb +++ b/lib/requisite/api_model.rb @@ -46,6 +46,10 @@ def to_json(show_nil: false) to_hash(show_nil: show_nil).to_json end + def ==(other) + other.class == self.class && other.to_hash == self.to_hash + end + private def parse_typed_hash(name, hash) diff --git a/test/requisite/api_model_test.rb b/test/requisite/api_model_test.rb index 7eaf906..0026beb 100644 --- a/test/requisite/api_model_test.rb +++ b/test/requisite/api_model_test.rb @@ -187,6 +187,30 @@ def response.b; 2; end response.to_hash.must_equal(:num => 12) end + describe '#==' do + let(:response_1) do + DummyApiModel.serialized_attributes { attribute :a } + DummyApiModel.new(a: 'A') + end + + it 'is true with the same object' do + response_2 = response_1 + assert_equal response_1, response_2 + end + + it 'is true with an equal object' do + DummyApiModel.serialized_attributes { attribute :a } + response_2 = DummyApiModel.new(a: 'A') + assert_equal response_1, response_2 + end + + it 'is false with an unequal object' do + DummyApiModel.serialized_attributes { attribute :a } + response_2 = DummyApiModel.new(a: 'B') + refute_equal response_1, response_2 + end + end + describe 'with nested structures' do describe 'with typed arrays' do