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

View File

@@ -25,7 +25,7 @@ context('semver', function()
end) end)
it('parses prereleases, if they exist', function() 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)
end) end)
@@ -82,7 +82,7 @@ context('semver', function()
end) end)
it("works with a prerelease", function() 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)
end) end)
@@ -117,6 +117,9 @@ context('semver', function()
test("false if major =, minor =, but patch >", function() test("false if major =, minor =, but patch >", function()
assert_greater_than(v'0.0.2', v'0.0.1') assert_greater_than(v'0.0.2', v'0.0.1')
end) end)
describe("prereleases", function()
end)
end) end)
describe("nextPatch", function() describe("nextPatch", function()