mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
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:
parent
9fb6b7a880
commit
0cf81040fb
@ -8,25 +8,59 @@ local util = {}
|
||||
-- PRINT --
|
||||
|
||||
-- print
|
||||
---@param message any
|
||||
util.print = function (message)
|
||||
term.write(message)
|
||||
term.write(tostring(message))
|
||||
end
|
||||
|
||||
-- print line
|
||||
---@param message any
|
||||
util.println = function (message)
|
||||
print(message)
|
||||
print(tostring(message))
|
||||
end
|
||||
|
||||
-- timestamped print
|
||||
---@param message any
|
||||
util.print_ts = function (message)
|
||||
if message == nil then return end
|
||||
term.write(os.date("[%H:%M:%S] ") .. message)
|
||||
term.write(os.date("[%H:%M:%S] ") .. tostring(message))
|
||||
end
|
||||
|
||||
-- timestamped print line
|
||||
---@param message any
|
||||
util.println_ts = function (message)
|
||||
if message == nil then return end
|
||||
print(os.date("[%H:%M:%S] ") .. message)
|
||||
print(os.date("[%H:%M:%S] ") .. tostring(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
|
||||
|
||||
-- TIME --
|
||||
|
@ -5,27 +5,6 @@ local println = util.println
|
||||
|
||||
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
|
||||
---@param name string function name
|
||||
---@param f function function
|
||||
@ -46,7 +25,7 @@ function testutils.test_func(name, f, values, results)
|
||||
for i = 1, #values do
|
||||
local check = values[i]
|
||||
local expect = results[i]
|
||||
print(name .. "(" .. valstr(check) .. ") => ")
|
||||
print(name .. "(" .. util.strval(check) .. ") => ")
|
||||
assert(f(check) == expect, "FAIL")
|
||||
println("PASS")
|
||||
end
|
||||
@ -57,7 +36,7 @@ end
|
||||
---@param f function function
|
||||
---@param result any expected result
|
||||
function testutils.test_func_nil(name, f, result)
|
||||
print(name .. "(" .. valstr(nil) .. ") => ")
|
||||
print(name .. "(nil) => ")
|
||||
assert(f(nil) == result, "FAIL")
|
||||
println("PASS")
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user