added pessimistic upgrade operator

This commit is contained in:
Enrique García Cota
2012-01-14 02:08:34 +01:00
parent fce189134d
commit d07756e5af
2 changed files with 31 additions and 0 deletions

View File

@@ -35,6 +35,10 @@ function mt:__lt(other)
self.minor < other.minor or
self.patch < other.patch
end
function mt:__pow(other)
return self.major == other.major and
self.minor <= other.minor
end
function mt:__tostring()
return ("%d.%d.%d"):format(self.major, self.minor, self.patch)
end

View File

@@ -127,4 +127,31 @@ context('semver', function()
assert_equal(v'2.0.0', v'1.2.3':nextMajor())
end)
end)
-- This works like the "pessimisstic operator" in Rubygems.
-- if a and b are versions, a ^ b means "b is backwards-compatible with a"
-- in other words, "it's safe to upgrade from a to b"
describe("^", function()
test("true for self", function()
assert_true(v(1,2,3) ^ v(1,2,3))
end)
test("different major versions mean it's always unsafe", function()
assert_false(v(2,0,0) ^ v(3,0,0))
assert_false(v(2,0,0) ^ v(1,0,0))
end)
test("patch versions are always compatible", function()
assert_true(v(1,2,3) ^ v(1,2,0))
assert_true(v(1,2,3) ^ v(1,2,5))
end)
test("it's safe to upgrade to a newer minor version", function()
assert_true(v(1,2,0) ^ v(1,5,0))
end)
test("it's unsafe to downgrade to an earlier minor version", function()
assert_false(v(1,5,0) ^ v(1,2,0))
end)
end)
end)