mirror of
https://github.com/TangentFoxy/semver.lua.git
synced 2025-07-28 02:52:21 +00:00
added < support
This commit is contained in:
18
semver.lua
18
semver.lua
@@ -11,6 +11,22 @@ local function checkPositiveInteger(number, name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local mt = {
|
||||||
|
__eq = function(self, other)
|
||||||
|
return self.major == other.major and
|
||||||
|
self.minor == other.minor and
|
||||||
|
self.patch == other.patch
|
||||||
|
end,
|
||||||
|
__lt = function(self, other)
|
||||||
|
return self.major < other.major or
|
||||||
|
self.minor < other.minor or
|
||||||
|
self.patch < other.patch
|
||||||
|
end,
|
||||||
|
__tostring = function(self)
|
||||||
|
return ("%d.%d.%d"):format(self.major, self.minor, self.patch)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
local function version(major, minor, patch)
|
local function version(major, minor, patch)
|
||||||
assert(major, "At least one parameter is needed")
|
assert(major, "At least one parameter is needed")
|
||||||
|
|
||||||
@@ -27,7 +43,7 @@ local function version(major, minor, patch)
|
|||||||
checkPositiveInteger(minor, "minor")
|
checkPositiveInteger(minor, "minor")
|
||||||
checkPositiveInteger(patch, "patch")
|
checkPositiveInteger(patch, "patch")
|
||||||
|
|
||||||
return {major=major, minor=minor, patch=patch}
|
return setmetatable({major=major, minor=minor, patch=patch}, mt)
|
||||||
end
|
end
|
||||||
|
|
||||||
return version
|
return version
|
||||||
|
@@ -63,6 +63,42 @@ context('semver', function()
|
|||||||
assert_error(function() v("1.2.3foobar") end)
|
assert_error(function() v("1.2.3foobar") end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("tostring", function()
|
||||||
|
it("works with major, minor and patch", function()
|
||||||
|
assert_equal("1.2.3", tostring(v(1,2,3)))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("==", function()
|
||||||
|
it("is true when major, minor and patch are the same", function()
|
||||||
|
assert_equal(v(1,2,3), v'1.2.3')
|
||||||
|
end)
|
||||||
|
it("is false when major, minor and patch are not the same", function()
|
||||||
|
assert_not_equal(v(1,2,3), v(4,5,6))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("<", function()
|
||||||
|
test("true if major < minor", function()
|
||||||
|
assert_less_than(v'1.100.10', v'2.0.0')
|
||||||
|
end)
|
||||||
|
test("false if major > minor", function()
|
||||||
|
assert_greater_than(v'2', v'1')
|
||||||
|
end)
|
||||||
|
test("true if major = major but minor < minor", function()
|
||||||
|
assert_less_than(v'1.2.0', v'1.3.0')
|
||||||
|
end)
|
||||||
|
test("false if minor < minor", function()
|
||||||
|
assert_greater_than(v'1.1', v'1.0')
|
||||||
|
end)
|
||||||
|
test("true if major =, minor =, but patch <", function()
|
||||||
|
assert_less_than(v'0.0.1', v'0.0.10')
|
||||||
|
end)
|
||||||
|
test("false if major =, minor =, but patch >", function()
|
||||||
|
assert_greater_than(v'0.0.2', v'0.0.1')
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
end)
|
|
||||||
|
Reference in New Issue
Block a user