mirror of
https://github.com/kikito/semver.lua.git
synced 2024-10-10 04:24:18 +00:00
added nextMinor, nextMajor, nextPatch
This commit is contained in:
parent
1e00a63eae
commit
2b1dc60390
32
semver.lua
32
semver.lua
@ -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)
|
||||||
|
end
|
||||||
|
function methods:nextMinor()
|
||||||
|
return version(self.major, self.minor + 1, 0)
|
||||||
|
end
|
||||||
|
function methods:nextPatch()
|
||||||
|
return version(self.major, self.minor, self.patch + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
local mt = { __index = methods }
|
||||||
|
function mt:__eq(other)
|
||||||
return self.major == other.major and
|
return self.major == other.major and
|
||||||
self.minor == other.minor and
|
self.minor == other.minor and
|
||||||
self.patch == other.patch
|
self.patch == other.patch
|
||||||
end,
|
end
|
||||||
__lt = function(self, other)
|
function mt:__lt(other)
|
||||||
return self.major < other.major or
|
return self.major < other.major or
|
||||||
self.minor < other.minor or
|
self.minor < other.minor or
|
||||||
self.patch < other.patch
|
self.patch < other.patch
|
||||||
end,
|
end
|
||||||
__tostring = function(self)
|
function mt:__tostring()
|
||||||
return ("%d.%d.%d"):format(self.major, self.minor, self.patch)
|
return ("%d.%d.%d"):format(self.major, self.minor, self.patch)
|
||||||
end
|
end
|
||||||
}
|
|
||||||
|
|
||||||
local function version(major, minor, patch)
|
|
||||||
|
-- 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
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user