Disallow initializing Versions with non-strings

Closes Homebrew/homebrew#23553.
This commit is contained in:
Jack Nagel 2013-10-25 17:29:36 -05:00
parent e67286369e
commit 383b321119
2 changed files with 19 additions and 1 deletions

View File

@ -1,6 +1,19 @@
require 'testing_env'
require 'version'
class VersionTests < Test::Unit::TestCase
def test_accepts_objects_responding_to_to_str
value = stub(:to_str => '0.1')
assert_equal '0.1', Version.new(value).to_s
end
def test_raises_for_non_string_objects
assert_raises(TypeError) { Version.new(1.1) }
assert_raises(TypeError) { Version.new(1) }
assert_raises(TypeError) { Version.new(:symbol) }
end
end
class VersionComparisonTests < Test::Unit::TestCase
include VersionAssertions

View File

@ -163,7 +163,12 @@ class Version
end
def initialize(val, detected=false)
@version = val.to_s
if val.respond_to?(:to_str)
@version = val.to_str
else
raise TypeError, "Version value must be a string"
end
@detected_from_url = detected
end