Initial version comparison implementation
Signed-off-by: Jack Nagel <jacknagel@gmail.com>
This commit is contained in:
		
							parent
							
								
									969ce2f67d
								
							
						
					
					
						commit
						956c1d653a
					
				@ -11,6 +11,10 @@ module VersionAssertions
 | 
				
			|||||||
  def assert_version_nil url
 | 
					  def assert_version_nil url
 | 
				
			||||||
    assert_nil Version.parse(url)
 | 
					    assert_nil Version.parse(url)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def assert_comparison a, comparison, b
 | 
				
			||||||
 | 
					    eval "assert Version.new(a) #{comparison} Version.new(b)"
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestBadVersion < TestBall
 | 
					class TestBadVersion < TestBall
 | 
				
			||||||
@ -21,7 +25,19 @@ class TestBadVersion < TestBall
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class VersionTests < Test::Unit::TestCase
 | 
					class VersionComparisonTests < Test::Unit::TestCase
 | 
				
			||||||
 | 
					  include VersionAssertions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def test_version_comparisons
 | 
				
			||||||
 | 
					    assert_comparison '0.1', '==', '0.1.0'
 | 
				
			||||||
 | 
					    assert_comparison '0.1', '!=', '0.2'
 | 
				
			||||||
 | 
					    assert_comparison '1.2.3', '>', '1.2.2'
 | 
				
			||||||
 | 
					    assert_comparison '1.2.3-p34', '>', '1.2.3-p33'
 | 
				
			||||||
 | 
					    assert_comparison '1.2.4', '<', '1.2.4.1'
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class VersionParsingTests < Test::Unit::TestCase
 | 
				
			||||||
  include VersionAssertions
 | 
					  include VersionAssertions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def test_pathname_version
 | 
					  def test_pathname_version
 | 
				
			||||||
 | 
				
			|||||||
@ -6,12 +6,30 @@ class Version
 | 
				
			|||||||
    @version = val.to_s.strip
 | 
					    @version = val.to_s.strip
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def head?
 | 
				
			||||||
 | 
					    @version == 'HEAD'
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def nums
 | 
					  def nums
 | 
				
			||||||
    @version.scan(/\d+/).map { |d| d.to_i }
 | 
					    @version.scan(/\d+/).map { |d| d.to_i }
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def <=>(other)
 | 
					  def <=>(other)
 | 
				
			||||||
    @version <=> other.version
 | 
					    return nil unless other.is_a? Version
 | 
				
			||||||
 | 
					    return 0 if self.head? and other.head?
 | 
				
			||||||
 | 
					    return 1 if self.head? and not other.head?
 | 
				
			||||||
 | 
					    return -1 if not self.head? and other.head?
 | 
				
			||||||
 | 
					    return 1 if other.nil?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    snums = self.nums
 | 
				
			||||||
 | 
					    onums = other.nums
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    count = [snums.length, onums.length].max
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    snums.fill(0, snums.length, count - snums.length)
 | 
				
			||||||
 | 
					    onums.fill(0, onums.length, count - onums.length)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    snums <=> onums
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def to_s
 | 
					  def to_s
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user