added nextMinor, nextMajor, nextPatch

This commit is contained in:
Enrique García Cota 2012-01-14 01:08:19 +01:00
parent 1e00a63eae
commit 2b1dc60390
2 changed files with 53 additions and 16 deletions

View File

@ -5,29 +5,43 @@
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra -- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra
local version
local function checkPositiveInteger(number, name) local function checkPositiveInteger(number, name)
assert(number >= 0, name .. ' must be a valid positive number') assert(number >= 0, name .. ' must be a valid positive number')
assert(math.floor(number) == number, name .. ' must be an integer') assert(math.floor(number) == number, name .. ' must be an integer')
end end
local methods = {}
local mt = { function methods:nextMajor()
__eq = function(self, other) return version(self.major + 1, 0, 0)
return self.major == other.major and end
self.minor == other.minor and function methods:nextMinor()
self.patch == other.patch return version(self.major, self.minor + 1, 0)
end, end
__lt = function(self, other) function methods:nextPatch()
return self.major < other.major or return version(self.major, self.minor, self.patch + 1)
self.minor < other.minor or end
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 mt = { __index = methods }
function mt:__eq(other)
return self.major == other.major and
self.minor == other.minor and
self.patch == other.patch
end
function mt:__lt(other)
return self.major < other.major or
self.minor < other.minor or
self.patch < other.patch
end
function mt:__tostring()
return ("%d.%d.%d"):format(self.major, self.minor, self.patch)
end
-- defined as local at the begining of the file
version = function(major, minor, patch)
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

View File

@ -101,4 +101,27 @@ context('semver', function()
end) end)
end) end)
describe("nextPatch", function()
it("increases the patch number by 1", function()
assert_equal(v'1.0.1', v'1.0.0':nextPatch())
end)
end)
describe("nextMinor", function()
it("increases the minor number by 1", function()
assert_equal(v'1.2.0', v'1.1.0':nextMinor())
end)
it("resets the patch number", function()
assert_equal(v'1.2.0', v'1.1.7':nextMinor())
end)
end)
describe("nextMajor", function()
it("increases the major number by 1", function()
assert_equal(v'2.0.0', v'1.0.0':nextMajor())
end)
it("resets the patch number", function()
assert_equal(v'2.0.0', v'1.2.3':nextMajor())
end)
end)
end) end)