From 6c298acdef5bca6c61626558c6af7447ab6f1fac Mon Sep 17 00:00:00 2001 From: Jakub Pavlik Date: Mon, 3 Apr 2023 00:14:26 +0200 Subject: [PATCH] Rank, AbstractDate: fix exception on comparison with instance of a different class --- lib/calendarium-romanum/abstract_date.rb | 2 ++ lib/calendarium-romanum/rank.rb | 2 ++ spec/abstract_date_spec.rb | 1 + spec/rank_spec.rb | 1 + 4 files changed, 6 insertions(+) diff --git a/lib/calendarium-romanum/abstract_date.rb b/lib/calendarium-romanum/abstract_date.rb index 009e7c66..e1495653 100644 --- a/lib/calendarium-romanum/abstract_date.rb +++ b/lib/calendarium-romanum/abstract_date.rb @@ -25,6 +25,8 @@ def self.from_date(date) attr_reader :month, :day def <=>(other) + return nil unless other.class == self.class + if month != other.month month <=> other.month else diff --git a/lib/calendarium-romanum/rank.rb b/lib/calendarium-romanum/rank.rb index 4f18542a..85c19774 100644 --- a/lib/calendarium-romanum/rank.rb +++ b/lib/calendarium-romanum/rank.rb @@ -44,6 +44,8 @@ def short_desc end def <=>(other) + return nil unless other.class == self.class + other.priority <=> priority end diff --git a/spec/abstract_date_spec.rb b/spec/abstract_date_spec.rb index 41a65fa5..de726d86 100644 --- a/spec/abstract_date_spec.rb +++ b/spec/abstract_date_spec.rb @@ -59,6 +59,7 @@ describe '#==' do it { expect(AD.new(1, 1)).to be == AD.new(1, 1) } it { expect(AD.new(1, 1)).not_to be == AD.new(1, 2) } + it { expect(AD.new(1, 1) == 'instance of another class').to be false } end describe 'as a Hash key' do diff --git a/spec/rank_spec.rb b/spec/rank_spec.rb index 32976b31..401fd111 100644 --- a/spec/rank_spec.rb +++ b/spec/rank_spec.rb @@ -26,6 +26,7 @@ describe '#==' do it { expect(CR::Ranks[1.2]).to be == CR::Ranks[1.2] } it { expect(CR::Ranks[1.2]).not_to be == CR::Ranks[1.1] } + it { expect(CR::Ranks::FERIAL == 'instance of another class').to be false } end describe 'descriptions' do