From 110cd610d55bc601ee817c36a3d80718e1b9331b Mon Sep 17 00:00:00 2001 From: Brian Cheung Date: Sun, 20 Jun 2021 21:43:01 -0400 Subject: [PATCH] Fixes #42: Cannot compare Semantic::Version to nil --- lib/semantic/version.rb | 2 ++ spec/version_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/semantic/version.rb b/lib/semantic/version.rb index 7eda70c..bba4090 100644 --- a/lib/semantic/version.rb +++ b/lib/semantic/version.rb @@ -81,6 +81,8 @@ def eql? other_version end def <=> other_version + raise ArgumentError.new('Expected a type of Semantic::Version for comparison') if other_version.nil? + other_version = Version.new(other_version) if other_version.is_a? String [:major, :minor, :patch].each do |part| c = (self.send(part) <=> other_version.send(part)) diff --git a/spec/version_spec.rb b/spec/version_spec.rb index 2ac8bd0..83654e5 100644 --- a/spec/version_spec.rb +++ b/spec/version_spec.rb @@ -263,6 +263,13 @@ /should be an array of versions/ ) end + + it 'raises an ArugmentError when compared to nil' do + expect { @v1_5_9_pre_1 < nil }.to raise_error(ArgumentError) + expect { @v1_5_9_pre_1 > nil }.to raise_error(ArgumentError) + expect { @v1_5_9_pre_1 == nil }.to raise_error(ArgumentError) + expect { @v1_5_9_pre_1 <=> nil }.to raise_error(ArgumentError) + end end context 'type coercions' do