Permalink
Browse files

Add conversion through standard; add more tests

  • Loading branch information...
1 parent 3cc9950 commit 2d2320f25b61867848be3575b7156c4a42cf8feb @dazuma committed May 25, 2010
View
@@ -1,7 +1,8 @@
=== 0.4.0 / 2010-05-24
-* Included Semantic Version format (http://semver.org/)
+* Included Semantic Version (http://semver.org/) format called "semver".
* Parsing conversions can now prescreen or premodify the original format value.
+* Conversion algorithm tries to convert through the standard format if a direct conversion isn't found. For example, there is currently no direct conversion between semver and rubygems formats, but the converter can nevertheless convert the two because both can convert to and from standard.
* Expanded the module version detection to include VERSION submodules.
* Some Rakefile fixes to match RDoc and Ruby 1.9 changes.
View
@@ -143,7 +143,7 @@ Versionomy is written by Daniel Azuma (http://www.daniel-azuma.com/).
== LICENSE:
-Copyright 2008-2009 Daniel Azuma.
+Copyright 2008-2010 Daniel Azuma.
All rights reserved.
@@ -78,10 +78,11 @@ def initialize(&block_)
def convert_value(value_, format_, convert_params_=nil)
begin
+ convert_params_ ||= {}
if @original_value_modifier
value_ = @original_value_modifier.call(value_, convert_params_)
end
- unparse_params_ = value_.unparse_params
+ unparse_params_ = value_.unparse_params || {}
if @unparse_params_modifier
unparse_params_ = @unparse_params_modifier.call(unparse_params_, convert_params_)
end
@@ -201,7 +201,7 @@ def self.create_standard_to_semver
end
# We're going to modify how the standard format version is
- # unparsed, so the rubygems format will have a better chance
+ # unparsed, so the semver format will have a better chance
# of parsing it.
to_modify_unparse_params do |params_, convert_params_|
@@ -131,6 +131,31 @@ def parse(str_, format_=nil, parse_params_=nil)
end
+ # Convenience method for creating a version number using the Semantic
+ # Versioning format (see http://semver.org/).
+ #
+ # You may pass a string to parse, or a hash with the following keys, all
+ # of which are optional:
+ # <tt>:major</tt>::
+ # Major version number
+ # <tt>:minor</tt>::
+ # Minor version number
+ # <tt>:patch</tt>::
+ # Patch version number
+ # <tt>:prerelease_suffix</tt>::
+ # A prerelease suffix (e.g. "b2")
+ #
+ # May raise Versionomy::Errors::ParseError if parsing failed.
+
+ def semver(input_)
+ if input_.kind_of?(::Hash)
+ create(input_, :semver)
+ else
+ parse(input_.to_s, :semver)
+ end
+ end
+
+
# Get the version of the given module as a Versionomy::Value.
# Tries a number of common approaches to embedding version numbers into
# modules, such as string or array constants, submodules containing
View
@@ -374,8 +374,20 @@ def convert(format_, convert_params_=nil)
if from_schema_ == to_schema_
return Value.new(@_values, format_, convert_params_)
end
- conversion_ = Conversion.get(from_schema_, to_schema_, true)
- conversion_.convert_value(self, format_, convert_params_)
+ conversion_ = Conversion.get(from_schema_, to_schema_)
+ if conversion_
+ conversion_.convert_value(self, format_, convert_params_)
+ else
+ standard_format_ = Format.get(:standard)
+ conversion1_ = Conversion.get(from_schema_, standard_format_)
+ conversion2_ = Conversion.get(standard_format_, to_schema_)
+ if conversion1_ && conversion2_
+ value_ = conversion1_.convert_value(self, standard_format_, convert_params_)
+ conversion2_.convert_value(value_, format_, convert_params_)
+ else
+ raise Errors::UnknownConversionError
+ end
+ end
end
@@ -161,7 +161,7 @@ def test_rubygems_to_standard_equality_comparison
# Test inequality comparisons between rubygems and standard
- def test_rubygems_to_standard_equality_comparison
+ def test_rubygems_to_standard_inequality_comparison
assert_operator(::Versionomy.parse('1.2.3', :rubygems), :<, ::Versionomy.parse('1.2.4'))
assert_operator(::Versionomy.parse('1.2.b.3', :rubygems), :>, ::Versionomy.parse('1.2b2'))
assert_operator(::Versionomy.parse('1.2', :rubygems), :>, ::Versionomy.parse('1.2b1'))
View
@@ -144,6 +144,22 @@ def test_parsing_require_all_fields
end
+ # Test convenience parsing
+
+ def test_convenience_parse
+ value_ = ::Versionomy.semver('2.0.1')
+ assert_equal([2, 0, 1, ''], value_.values_array)
+ end
+
+
+ # Test convenience creation from hash
+
+ def test_convenience_create
+ value_ = ::Versionomy.semver(:major => 2, :patch => 1, :prerelease_suffix => 'b2')
+ assert_equal([2, 0, 1, 'b2'], value_.values_array)
+ end
+
+
# Test bumping a numeric field.
def test_bump_numeric
@@ -90,6 +90,16 @@ def test_standard_to_semver_with_v
end
+ # Test conversion from standard to semver with an expectation of failure
+
+ def test_standard_to_semver_fail
+ value_ = ::Versionomy.parse('1.2.3.4', :standard)
+ assert_raise(::Versionomy::Errors::ConversionError) do
+ value2_ = value_.convert(:semver)
+ end
+ end
+
+
# Test simple conversion from semver to standard.
def test_semver_to_standard_simple
@@ -116,6 +126,71 @@ def test_semver_to_standard_beta
end
+ # Test conversion from rubygems to standard with an expectation of failure
+
+ def test_semver_to_standard_fail
+ value_ = ::Versionomy.parse('1.2.3c4', :semver)
+ assert_raise(::Versionomy::Errors::ConversionError) do
+ value2_ = value_.convert(:standard)
+ end
+ end
+
+
+ # Test conversion when there aren't unparse_params
+
+ def test_standard_to_semver_without_unparse_params
+ value_ = ::Versionomy.create([1,2,3], :standard)
+ value2_ = value_.convert(:semver)
+ assert_equal([1, 2, 3, ''], value2_.values_array)
+ end
+
+
+ # Test conversion between semver and rubygems
+
+ def test_semver_and_rubygems
+ value_ = ::Versionomy.create([1,2,3], :semver)
+ value2_ = value_.convert(:rubygems)
+ assert_equal([1, 2, 3, 0, 0, 0, 0, 0], value2_.values_array)
+ value_ = ::Versionomy.create([1,2,3], :rubygems)
+ value2_ = value_.convert(:semver)
+ assert_equal([1, 2, 3, ''], value2_.values_array)
+ end
+
+
+ # Test equality comparisons between semver and standard
+
+ def test_semver_to_standard_equality_comparison
+ assert_operator(::Versionomy.parse('1.2.0', :semver), :==, ::Versionomy.parse('1.2'))
+ assert_operator(::Versionomy.parse('1.2.0b3', :semver), :==, ::Versionomy.parse('1.2b3'))
+ end
+
+
+ # Test inequality comparisons between semver and standard
+
+ def test_semver_to_standard_inequality_comparison
+ assert_operator(::Versionomy.parse('1.2.3', :semver), :<, ::Versionomy.parse('1.2.4'))
+ assert_operator(::Versionomy.parse('1.2.0b3', :semver), :>, ::Versionomy.parse('1.2b2'))
+ assert_operator(::Versionomy.parse('1.2.0', :semver), :>, ::Versionomy.parse('1.2b1'))
+ end
+
+
+ # Test equality comparisons between standard and rubygems
+
+ def test_standard_to_semver_equality_comparison
+ assert_operator(::Versionomy.parse('1.2.0'), :==, ::Versionomy.parse('1.2.0', :semver))
+ assert_operator(::Versionomy.parse('1.2b3'), :==, ::Versionomy.parse('1.2.0beta3', :semver))
+ end
+
+
+ # Test inequality comparisons between standard and rubygems
+
+ def test_standard_to_semver_inequality_comparison
+ assert_operator(::Versionomy.parse('1.2.4'), :>, ::Versionomy.parse('1.2.3', :semver))
+ assert_operator(::Versionomy.parse('1.2b2'), :<, ::Versionomy.parse('1.2.0beta3', :semver))
+ assert_operator(::Versionomy.parse('1.2b2'), :<, ::Versionomy.parse('1.2.0', :semver))
+ end
+
+
end
end

0 comments on commit 2d2320f

Please sign in to comment.