Welp let’s get straight to the point, I made some functions that I thought were better than the usual assert and found them particularly useful, so I wanted to share them here.
First off, assertArgs checking if args exist at all and can be used with tuples
--[[ assertArgs func usage
function blabla(arghere, otherArg, anotherhere)
assertArgs(3, arghere, otherArg, anotherhere)
-- ^ argument amount (you can also do amts below the max if the last args are optional)
-- if one of these args isnt present itll error
end
blabla() -- errors due to arg 1 not being present
blabla(1,"two", 3) -- doesn't error due to all args being present
blabla(1, 2) -- errors due to arg 3 but if you would make the arg amt 2 it wouldn't error)
]]
-- the function itself v
function assertArgs(argnum, ...)
for i=1, argnum do
if not select(i, ...) then
assert(select(i, ...), "On Arg #"..i..": Argument must be present!")
end
end
end
Second and last off, expect doing all things you could want or need in assert, in one function (I prefer using this rather than assertArgs, though it can take up space)
--[[ expect func usage
function blabla(arghere, ignoredArg, otherarghere)
expect {
{
arg = arghere,
toBe = "Instance"
-- ^ put any roblox type here
},
0, -- important to make not wanted to be checked args 0, so the arg count is accurate
{
arg = otherarghere,
-- if toBe arg is left out, it expects that arg to exist at all
}, etc...
}
end
blabla("string!", 420) -- errors due to argument 3 not being present and argument 1 not being an instance
blabla(randomPart, nil, "hey") -- doesn't error due to arg 1 being an instance, arg 2 being ignored, arg 3 existing
]]
-- the function itself v
function expect(t)
if not t then error('unfinished expect statement',9) end
for argNum, tb in pairs(t) do
if tb == 0 then continue end
if not tb.toBe then
if not tb.arg then
error('Expected Arg #'..argNum..' to be passed and not nil',9)
end
else
if typeof(tb.arg) ~= tb.toBe then -- can still be nil
error('Expected Arg #'..argNum..' to be of type ['..tb.toBe..'], got ['..tostring(typeof(tb.arg))..']', 9)
end
end
end
end
And yeah, those are the functions I wanted to share here.