moved string value to util and added sprtinf, adjusted prints to use tostring to prevent concatentation errors on some types

This commit is contained in:
Mikayla Fischler 2022-05-24 22:48:31 -04:00
parent 9fb6b7a880
commit 0cf81040fb
2 changed files with 42 additions and 29 deletions

View File

@ -8,25 +8,59 @@ local util = {}
-- PRINT -- -- PRINT --
-- print -- print
---@param message any
util.print = function (message) util.print = function (message)
term.write(message) term.write(tostring(message))
end end
-- print line -- print line
---@param message any
util.println = function (message) util.println = function (message)
print(message) print(tostring(message))
end end
-- timestamped print -- timestamped print
---@param message any
util.print_ts = function (message) util.print_ts = function (message)
if message == nil then return end term.write(os.date("[%H:%M:%S] ") .. tostring(message))
term.write(os.date("[%H:%M:%S] ") .. message)
end end
-- timestamped print line -- timestamped print line
---@param message any
util.println_ts = function (message) util.println_ts = function (message)
if message == nil then return end print(os.date("[%H:%M:%S] ") .. tostring(message))
print(os.date("[%H:%M:%S] ") .. message) end
-- STRING TOOLS --
-- get a value as a string
---@param val any
---@return string
util.strval = function (val)
local t = type(val)
if t == "table" or t == "function" then
return "[" .. tostring(val) .. "]"
else
return tostring(val)
end
end
-- concatenation with built-in to string
---@vararg any
---@return string
util.concat = function (...)
local str = ""
for _, v in ipairs(arg) do
str = str .. util.strval(v)
end
return str
end
-- sprintf implementation
---@param format string
---@vararg any
util.sprintf = function (format, ...)
return string.format(format, table.unpack(arg))
end end
-- TIME -- -- TIME --

View File

@ -5,27 +5,6 @@ local println = util.println
local testutils = {} local testutils = {}
-- get a value as a string
---@param val any
---@return string value value as string or "%VALSTR_UNKNOWN%"
local function valstr(val)
local t = type(val)
if t == "nil" then
return "nil"
elseif t == "number" then
return "" .. val
elseif t == "boolean" then
if val then return "true" else return "false" end
elseif t == "string" then
return val
elseif t == "table" or t == "function" then
return val
else
return "%VALSTR_UNKNOWN%"
end
end
-- test a function -- test a function
---@param name string function name ---@param name string function name
---@param f function function ---@param f function function
@ -46,7 +25,7 @@ function testutils.test_func(name, f, values, results)
for i = 1, #values do for i = 1, #values do
local check = values[i] local check = values[i]
local expect = results[i] local expect = results[i]
print(name .. "(" .. valstr(check) .. ") => ") print(name .. "(" .. util.strval(check) .. ") => ")
assert(f(check) == expect, "FAIL") assert(f(check) == expect, "FAIL")
println("PASS") println("PASS")
end end
@ -57,7 +36,7 @@ end
---@param f function function ---@param f function function
---@param result any expected result ---@param result any expected result
function testutils.test_func_nil(name, f, result) function testutils.test_func_nil(name, f, result)
print(name .. "(" .. valstr(nil) .. ") => ") print(name .. "(nil) => ")
assert(f(nil) == result, "FAIL") assert(f(nil) == result, "FAIL")
println("PASS") println("PASS")
end end