Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot compare Semantic::Version to nil #42

Open
ag-TJNII opened this issue Mar 18, 2021 · 0 comments
Open

Cannot compare Semantic::Version to nil #42

ag-TJNII opened this issue Mar 18, 2021 · 0 comments

Comments

@ag-TJNII
Copy link

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 ag-TJNII changed the title Cannot compare SemVer to nil Cannot compare Semantic::Version to nil Mar 18, 2021
bdcheung added a commit to bdcheung/semantic that referenced this issue Jun 21, 2021
bdcheung added a commit to bdcheung/semantic that referenced this issue Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant