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

Raise ArgumentError if Version is initialized with a nil version string #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/semantic/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ class Version
attr_reader :build

def initialize version_str
v = version_str.match(SemVerRegexp)
v = parse_version_string!(version_str)

raise ArgumentError.new("#{version_str} is not a valid SemVer Version (http://semver.org)") if v.nil?
@major = v[1].to_i
@minor = v[2].to_i
@patch = v[3].to_i
Expand Down Expand Up @@ -176,5 +175,13 @@ def semverified version_string
parts.join('.')
end

def parse_version_string! version_str
if !version_str.nil? && version = version_str.match(SemVerRegexp)
version
else
raise ArgumentError.new("#{version_str} is not a valid SemVer Version (http://semver.org)") if version.nil?
end
end

end
end
9 changes: 9 additions & 0 deletions spec/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
end
end

it 'raises an error when passed nil' do
@bad_versions.each do |v|
expect { Semantic::Version.new nil }.to raise_error(
ArgumentError,
/not a valid SemVer/
)
end
end

it 'stores parsed versions in member variables' do
v1 = Semantic::Version.new '1.5.9'
expect(v1.major).to eq(1)
Expand Down