From 2b1dc603908b6bed9acdf0cf5ff4fb5fc9aa253a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sat, 14 Jan 2012 01:08:19 +0100 Subject: [PATCH] added nextMinor, nextMajor, nextPatch --- semver.lua | 46 +++++++++++++++++++++++++++++--------------- spec/semver_spec.lua | 23 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/semver.lua b/semver.lua index 47ba09a..6c7e4ca 100644 --- a/semver.lua +++ b/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. -- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra +local version + local function checkPositiveInteger(number, name) assert(number >= 0, name .. ' must be a valid positive number') assert(math.floor(number) == number, name .. ' must be an integer') end +local methods = {} -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 -} +function methods:nextMajor() + 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 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") if type(major) == 'string' then diff --git a/spec/semver_spec.lua b/spec/semver_spec.lua index 6f682a6..e649323 100644 --- a/spec/semver_spec.lua +++ b/spec/semver_spec.lua @@ -101,4 +101,27 @@ context('semver', function() 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)