Skip to content

Commit

Permalink
Fixes #42: Cannot compare Semantic::Version to nil
Browse files Browse the repository at this point in the history
  • Loading branch information
bdcheung committed Jun 21, 2021
1 parent 54de37b commit 110cd61
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/semantic/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions spec/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 110cd61

Please sign in to comment.