Permalink
Browse files

more detailed tests

  • Loading branch information...
1 parent ea06b24 commit e01dde895c3e177663a8ee9ca1d8f2761b32a22b reiz committed Apr 6, 2012
Showing with 56 additions and 13 deletions.
  1. +1 −1 lib/naturalsorter/version.rb
  2. +35 −12 lib/versioncmp.rb
  3. +20 −0 spec/versioncmp_spec.rb
@@ -1,3 +1,3 @@
module Naturalsorter
- VERSION = "0.2.3"
+ VERSION = "0.2.4"
end
View
@@ -37,21 +37,17 @@ def self.compare(a, b)
part1 = Versioncmp.getAPiece(offset1, a);
part2 = Versioncmp.getAPiece(offset2, b);
- if part1.length() == 8 && part1.match(/^[0-9]+$/) != nil && part2.length() < 8
+ if Versioncmp.timestamp?(part1) && part2.length() < 8
return -1
end
- if part2.length() == 8 && part2.match(/^[0-9]+$/) != nil && part1.length() < 8
+ if Versioncmp.timestamp?(part2) && part1.length() < 8
return 1
end
offset1 += part1.length() + 1;
offset2 += part2.length() + 1;
if ( part1.match(/^[0-9]+$/) != nil && part2.match(/^[0-9]+$/) != nil )
-
-
-
-
ai = part1.to_i;
bi = part2.to_i;
result = Versioncmp.compareInt(ai, bi);
@@ -90,36 +86,59 @@ def self.checkForRC(a, b)
small = String.new(a)
end
if (Versioncmp.isRc(big))
- bigwithoutRc = big.gsub(/\.RC[1-9]*$/, "").gsub(/\.rc[1-9]*$/, "")
+ bigwithoutRc = big.gsub(/\.rc.*$/i, "")
+ bigwithoutRc = bigwithoutRc.gsub(/\-rc.*$/i, "")
if (Versioncmp.compareString(bigwithoutRc, small) == 0)
return Versioncmp.getRcValue(a, b)
end
elsif (Versioncmp.isBeta(big))
- bigwithoutBeta = big.gsub(/\.BETA[1-9]*$/, "").gsub(/\.beta[1-9]*$/, "")
+ bigwithoutBeta = big.gsub(/\.beta[1-9]*$/i, "")
+ bigwithoutBeta = bigwithoutBeta.gsub(/\-beta[1-9]*$/i, "")
if (Versioncmp.compareString(bigwithoutBeta, small) == 0)
return Versioncmp.getRcValue(a, b)
end
+ elsif (Versioncmp.isAlpha(big))
+ bigwithoutAlpha = big.gsub(/\.alpha[1-9]*$/i, "")
+ bigwithoutAlpha = bigwithoutAlpha.gsub(/\-alpha[1-9]*$/i, "")
+ if (Versioncmp.compareString(bigwithoutAlpha, small) == 0)
+ return Versioncmp.getRcValue(a, b)
+ end
elsif (Versioncmp.isPre(big))
- bigwithoutPre = big.gsub(/\.PRE[1-9]*$/, "").gsub(/\.pre[1-9]*$/, "")
+ bigwithoutPre = big.gsub(/\.pre[1-9]*$/i, "")
+ bigwithoutPre = bigwithoutPre..gsub(/\-pre[1-9]*$/i, "")
if (Versioncmp.compareString(bigwithoutPre, small) == 0)
return Versioncmp.getRcValue(a, b)
end
+ elsif (Versioncmp.isJbossorg(big))
+ bigwithoutRc = big.gsub(/\.jbossorg.*$/i, "")
+ bigwithoutRc = bigwithoutRc.gsub(/\-jbossorg.*$/i, "")
+ if (Versioncmp.compareString(bigwithoutRc, small) == 0)
+ return Versioncmp.getRcValue(a, b)
+ end
end
return 1 if a.length > b.length
return -1 if a.length < b.length
return 0
end
def self.isRc(a)
- return a.match(/.*RC[1-9]*$/) != nil || a.match(/.*rc[1-9]*$/) != nil;
+ return a.match(/.*rc.*$/i) != nil;
+ end
+
+ def self.isJbossorg(a)
+ return a.match(/.*jbossorg.*$/i) != nil;
end
def self.isBeta(a)
- return a.match(/.*BETA[1-9]*$/) != nil || a.match(/.*beta[1-9]*$/) != nil;
+ return a.match(/.*beta[1-9]*$/i) != nil;
+ end
+
+ def self.isAlpha(a)
+ return a.match(/.*alpha[1-9]*$/i) != nil;
end
def self.isPre(a)
- return a.match(/.*PRE[1-9]*$/) != nil || a.match(/.*pre[1-9]*$/) != nil;
+ return a.match(/.*pre[1-9]*$/i) != nil;
end
def self.getAPiece(offset, cake)
@@ -142,5 +161,9 @@ def self.getRcValue(a, b)
return 1 if (a.length() < b.length())
return -1
end
+
+ def self.timestamp?(part)
+ return part.length() == 8 && part.match(/^[0-9]+$/) != nil
+ end
end
View
@@ -34,6 +34,26 @@
Versioncmp.compare("1.1.rc1", "1.1").should eql(-1)
end
+ it "smaller RC" do
+ Versioncmp.compare("1.1-rc1", "1.1").should eql(-1)
+ end
+
+ it "smaller alpha" do
+ Versioncmp.compare("1.1-alpha1", "1.1").should eql(-1)
+ end
+
+ it "smaller beta" do
+ Versioncmp.compare("3.1-beta1", "3.1").should eql(-1)
+ end
+
+ it "smaller 3.0-rc4-negotiate" do
+ Versioncmp.compare("3.0-rc4-negotiate", "3.0").should eql(-1)
+ end
+
+ it "smaller 3.1-jbossorg-1" do
+ Versioncmp.compare("3.1-jbossorg-1", "3.1").should eql(-1)
+ end
+
it "bigger RC" do
Versioncmp.compare("1.1.rc3", "1.1.rc2").should eql(1)
end

0 comments on commit e01dde8

Please sign in to comment.