mirror of
https://github.com/kikito/semver.lua.git
synced 2024-10-10 04:24:18 +00:00
initial version
This commit is contained in:
commit
e1f1b4898c
20
MIT-LICENSE.txt
Normal file
20
MIT-LICENSE.txt
Normal file
@ -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.
|
42
README.textile
Normal file
42
README.textile
Normal file
@ -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@):
|
||||
|
||||
<pre>local v = require 'semver'</pre>
|
||||
|
||||
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:
|
||||
|
||||
<pre>
|
||||
tsc -f spec/*
|
||||
</pre>
|
||||
|
24
semver.lua
Normal file
24
semver.lua
Normal file
@ -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
|
58
spec/semver_spec.lua
Normal file
58
spec/semver_spec.lua
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user