From: michael libby Date: 2002-10-24T10:31:13+09:00 Subject: Re: Sort::Versions (was Re: Perl multiple match RE in Ruby?) -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Wednesday 23 October 2002 04:13, Massimiliano Mirra wrote: [and included a great script for a Version class, none of which is reproduced here] This is great! Now we can add things like Version#succ, #is_beta?, or whatever. I did find some flaws, so I added test cases from the CPAN module docs and fixed the failed tests. Patch enclosed. I'm guessing since you wrote most of it, you'd to maintain it (as a separate package from rpkg)? If not, I'll volunteer to take it on. And what's up with the English version of RAA? Can't get to it at all! (not a nag, just an alert ). -michael ichimunki@greyhound:~/ruby-misc/raa-stuff/sort_versions$ diff -u version_mirra.rb version_mirra_libby.rb - --- version_mirra.rb 2002-10-23 20:16:05.000000000 -0500 +++ version_mirra_libby.rb 2002-10-23 20:15:11.000000000 -0500 @@ -22,6 +22,10 @@ @v end + def length + @v.length + end + def to_s s = '' @sep.each_with_index do |sep, i| @@ -45,13 +49,13 @@ comp = 0 @v.each_with_index do |n, i| if n and other[i] - - comp = n <=> other[i] + if n.type == other[i].type + comp = n <=> other[i] + else + comp = n.to_s <=> other[i].to_s + end elsif n.nil? - - comp = -1 - - elsif other[i].nil? - - comp = +1 - - else - - raise "This should never happen!" + comp = +1 end if comp == 0 @@ -61,6 +65,10 @@ end end + if comp == 0 + comp = @v.length <=> other.length + end + return comp end end @@ -165,5 +173,57 @@ assert_equal ["cvs"], v.to_a end + + #test from CPAN version module + def test_CPAN_both_numeric + v1 = Version['1.1'] + v2 = Version['1.2'] + assert v1 < v2 + + v1 = Version['1.1'] + v2 = Version['1.1.1'] + assert v1 < v2 + + v1 = Version['1'] + v2 = Version['2'] + assert v1 < v2 + + v1 = Version['1'] + v2 = Version['0002'] + assert v1 < v2 + + v1 = Version['1.5'] + v2 = Version['1.06'] + assert v1 < v2 + end + + def test_CPAN_string_and_numeric + v1 = Version['1.1a'] + v2 = Version['1.2'] + assert v1 < v2 + + v1 = Version['1.1'] + v2 = Version['1.1a'] + assert v1 < v2 + + v1 = Version['1.1.a'] + v2 = Version['1.1a'] + assert v1 < v2 + + v1 = Version['1'] + v2 = Version['a'] + assert v1 < v2 + end + + def test_CPAN_both_strings + v1 = Version['a'] + v2 = Version['b'] + assert v1 < v2 + end + + def test_can_count_parts_of_version + v = Version['1.2-3.4'] + assert v.length == 4 + end end end Michael C. Libby x@ichimunki.com http://www.ichimunki.com/ http://www.ichimunki.com/public_key.txt -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE9t00y4ClW9KMwqnMRAldvAKCIF8B4467rDQ5fn0JfXJqSNQATrACgkhkM zyZugSIMB2cGGFaw+/2Bj+8= =oF6M -----END PGP SIGNATURE-----