From e1f1b4898cdc6881630eca8037ffbdfab2197017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sat, 14 Jan 2012 00:08:47 +0100 Subject: [PATCH] initial version --- MIT-LICENSE.txt | 20 +++++++++++++++ README.textile | 42 ++++++++++++++++++++++++++++++++ semver.lua | 24 ++++++++++++++++++ spec/semver_spec.lua | 58 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 MIT-LICENSE.txt create mode 100644 README.textile create mode 100644 semver.lua create mode 100644 spec/semver_spec.lua diff --git a/MIT-LICENSE.txt b/MIT-LICENSE.txt new file mode 100644 index 0000000..525287a --- /dev/null +++ b/MIT-LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2011 Enrique García Cota + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of 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. diff --git a/README.textile b/README.textile new file mode 100644 index 0000000..f61adb6 --- /dev/null +++ b/README.textile @@ -0,0 +1,42 @@ +h1. semver.lua + +Semantic versioning for Lua. + +See http://semver.org/ for details about semantic versioning. + +h1. Documentation + + local v = require 'semver' + + -- accepts integers and numbers + v1 = v(1,0,0) + v2_5_1 = v('2.5.1') + + -- major, minor and patch attributes + v2_5_1.major -- 2 + v2_5_1.minor -- 5 + v2_5_1.patch -- 1 + + -- + + +h1. Installation + +Just copy the semver.lua file wherever you want it (for example on a lib/ folder). Then write this in any Lua file where you want to use it. You must assign the require to a global or local variable (I use a local @v@): + +
local v = require 'semver'
+ +Using @v@ allows for the following nice syntax: @v'1.2.3'@. + +The @package.path@ variable must be configured so that the folder in which middleclass.lua is copied is available, of course. + +Please make sure that you read the license, too (for your convenience it's now included at the beginning of the semver.lua file). + +h1. Specs + +This project uses "telescope":https://github.com/norman/telescope for its specs. If you want to run the specs, you will have to install telescope first. Then just execute the following from the root inspect folder: + +
+tsc -f spec/*
+
+ diff --git a/semver.lua b/semver.lua new file mode 100644 index 0000000..55da50c --- /dev/null +++ b/semver.lua @@ -0,0 +1,24 @@ +-- semver.lua - v2.1 (2012-01) +-- Copyright (c) 2012 Enrique García Cota +-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of 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 + + +local function version(major, minor, patch) + assert(major, "At least one parameter is needed") + + if type(major) == 'string' then + local sMajor, sMinor, sPatch = major:match("^(%d+)%.?(%d*)%.?(%d*)$") + assert(type(sMajor) == 'string', ("Could not version number(s) from %q"):format(major)) + major, minor, patch = tonumber(sMajor), tonumber(sMinor), tonumber(sPatch) + end + + patch = patch or 0 + minor = minor or 0 + + return {major=major, minor=minor, patch=patch} +end + +return version diff --git a/spec/semver_spec.lua b/spec/semver_spec.lua new file mode 100644 index 0000000..f9404d9 --- /dev/null +++ b/spec/semver_spec.lua @@ -0,0 +1,58 @@ +local v = require 'semver' + +local function checkVersion(ver, major, minor, patch) + assert_equal(major, ver.major) + assert_equal(minor, ver.minor) + assert_equal(patch, ver.patch) +end + +context('semver', function() + + context('creation', function() + + context('from numbers', function() + it('parses 3 numbers correctly', function() + checkVersion(v(1,2,3), 1,2,3) + end) + + it('parses 2 numbers correctly', function() + checkVersion(v(1,2), 1,2,0) + end) + + it('parses 1 number correctly', function() + checkVersion(v(1), 1,0,0) + end) + end) + + describe("from strings", function() + test("1.2.3", function() + checkVersion( v'1.2.3', 1,2,3) + end) + test("10.20.123", function() + checkVersion( v'10.20.123', 10,20,123) + end) + test("2.0", function() + checkVersion( v'2.0', 2,0,0) + end) + test("5", function() + checkVersion( v'5', 5,0,0) + end) + end) + + describe('errors', function() + it('throws an error if no parameters are passed', function() + assert_error(function() v() end) + end) + it('throws an error on an empty string', function() + assert_error(function() v("") end) + end) + it('throws an error with garbage at the beginning of the string', function() + assert_error(function() v("foobar1.2.3") end) + end) + it('throws an error with garbage at the end of the string', function() + assert_error(function() v("1.2.3foobar") end) + end) + end) + + end) +end)