added more input checking

This commit is contained in:
Enrique García Cota 2012-01-14 00:22:56 +01:00
parent e1f1b4898c
commit 912dd1d8ee
2 changed files with 24 additions and 5 deletions

View File

@ -5,6 +5,11 @@
-- 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 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 function version(major, minor, patch)
assert(major, "At least one parameter is needed")
@ -18,6 +23,10 @@ local function version(major, minor, patch)
patch = patch or 0
minor = minor or 0
checkPositiveInteger(major, "major")
checkPositiveInteger(minor, "minor")
checkPositiveInteger(patch, "patch")
return {major=major, minor=minor, patch=patch}
end

View File

@ -39,17 +39,27 @@ context('semver', function()
end)
end)
describe('errors', function()
it('throws an error if no parameters are passed', function()
describe('errors are thrown in', function()
test('no parameters are passed', function()
assert_error(function() v() end)
end)
it('throws an error on an empty string', function()
test('negative numbers', function()
assert_error(function() v(-1, 0, 0) end)
assert_error(function() v( 0,-1, 0) end)
assert_error(function() v( 0, 0,-1) end)
end)
test('floats', function()
assert_error(function() v(.1, 0, 0) end)
assert_error(function() v( 0,.1, 0) end)
assert_error(function() v( 0, 0,.1) end)
end)
test('empty string', function()
assert_error(function() v("") end)
end)
it('throws an error with garbage at the beginning of the string', function()
test('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()
test('garbage at the end of the string', function()
assert_error(function() v("1.2.3foobar") end)
end)
end)