check argument type in newFile(), fixes #4

This commit is contained in:
megagrump
2021-07-11 19:40:44 +02:00
parent c01aea7dfa
commit dbfefa28c2
2 changed files with 9 additions and 0 deletions

View File

@@ -216,6 +216,9 @@ local nativefs = {}
local loveC = ffi.os == 'Windows' and ffi.load('love') or C
function nativefs.newFile(name)
if type(name) ~= 'string' then
error("bad argument #1 to 'newFile' (string expected, got " .. type(name) .. ")")
end
return setmetatable({
_name = name,
_mode = 'c',

View File

@@ -7,6 +7,7 @@ local fs
local equals, notEquals = lu.assertEquals, lu.assertNotEquals
local contains = lu.assertStrContains
local errorContains = lu.assertErrorMsgContains
local function notFailed(ok, err)
equals(ok, true)
@@ -27,6 +28,11 @@ local testFile1, testSize1 = 'data/ümläüt.txt', 446
local testFile2, testSize2 = 'data/𠆢ßЩ.txt', 450
function test_fs_newFile()
errorContains('bad argument', fs.newFile)
for _, v in ipairs({ 1, true, false, function() end, {} }) do
errorContains(type(v), fs.newFile, v)
end
local file = fs.newFile('test.file')
notEquals(file, nil)
equals(file:type(), 'File')