semantic icon indicating copy to clipboard operation
semantic copied to clipboard

Cannot compare Semantic::Version to nil

Open ag-TJNII opened this issue 3 years ago • 0 comments

Comparison operators throw NoMethod error when comparing to nil.

[76] pry(main)> v = Semantic::Version.new('1.2.3')
=> #<Semantic::Version:0x0000563cab56df68 @build=nil, @major=1, @minor=2, @patch=3, @pre=nil, @version="1.2.3">
[77] pry(main)> v == nil
NoMethodError: undefined method `major' for nil:NilClass
from /home/tom/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/semantic-1.6.1/lib/semantic/version.rb:86:in `block in <=>'
[78] pry(main)> v < nil
NoMethodError: undefined method `major' for nil:NilClass
from /home/tom/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/semantic-1.6.1/lib/semantic/version.rb:86:in `block in <=>'
[79] pry(main)> v > nil
NoMethodError: undefined method `major' for nil:NilClass
from /home/tom/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/semantic-1.6.1/lib/semantic/version.rb:86:in `block in <=>'
[80] pry(main)> v <=> nil
NoMethodError: undefined method `major' for nil:NilClass
from /home/tom/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/semantic-1.6.1/lib/semantic/version.rb:86:in `block in <=>'

This is a nuisance when testing methods that return a Semantic::Version on success and nil on error as this will cause rspec to throw obtuse NoMethodError exceptions on the test itself.

Recommend handling the nil argument case and throwing ArgumentError if passed something that doesn't behave like a Semantic::Version.

ag-TJNII avatar Mar 18 '21 14:03 ag-TJNII