small fixes in the way the prereleases are specified and parsed

This commit is contained in:
Enrique García Cota 2012-01-15 01:37:20 +01:00
parent 84fc954877
commit 043c181c89
2 changed files with 16 additions and 11 deletions

View File

@ -16,10 +16,12 @@ local function present(value)
return value and value ~= ''
end
local function filterPrerelease(prerelease)
local identifiers = prerelease:match("-([%w-][%.%w-]+)")
assert(identifiers, ("The prerelease %q must start with a dash and be followed by dashes, alphanumerics or dots."):format(prerelease))
return identifiers
local function parsePrerelease(extra)
if not present(extra) then return end
local prerelease = extra:match("-([%w-][%.%w-]+)")
assert(prerelease, ("The prerelease %q must start with a dash and be followed by dashes, alphanumerics or dots."):format(extra))
return prerelease
end
local methods = {}
@ -58,16 +60,14 @@ end
-- defined as local at the begining of the file
version = function(major, minor, patch, prerelease)
version = function(major, minor, patch, extra)
assert(major, "At least one parameter is needed")
if type(major) == 'string' then
local sMajor, sMinor, sPatch, sPrerelease = major:match("^(%d+)%.?(%d*)%.?(%d*)(.-)$")
local sMajor, sMinor, sPatch
sMajor, sMinor, sPatch, extra = major:match("^(%d+)%.?(%d*)%.?(%d*)(.-)$")
assert(type(sMajor) == 'string', ("Could not extract version number(s) from %q"):format(major))
major, minor, patch = tonumber(sMajor), tonumber(sMinor), tonumber(sPatch)
if present(sPrerelease) then
prerelease = filterPrerelease(sPrerelease)
end
end
patch = patch or 0
@ -77,6 +77,8 @@ version = function(major, minor, patch, prerelease)
checkPositiveInteger(minor, "minor")
checkPositiveInteger(patch, "patch")
local prerelease = parsePrerelease(extra)
local result = {major=major, minor=minor, patch=patch, prerelease=prerelease}
return setmetatable(result, mt)
end

View File

@ -25,7 +25,7 @@ context('semver', function()
end)
it('parses prereleases, if they exist', function()
checkVersion(v(1,2,3,"alpha"), 1,2,3,"alpha")
checkVersion(v(1,2,3,"-alpha"), 1,2,3,"alpha")
end)
end)
@ -82,7 +82,7 @@ context('semver', function()
end)
it("works with a prerelease", function()
assert_equal("1.2.3-beta", tostring(v(1,2,3,'beta')))
assert_equal("1.2.3-beta", tostring(v(1,2,3,'-beta')))
end)
end)
@ -117,6 +117,9 @@ context('semver', function()
test("false if major =, minor =, but patch >", function()
assert_greater_than(v'0.0.2', v'0.0.1')
end)
describe("prereleases", function()
end)
end)
describe("nextPatch", function()