From 0cf81040fb66d1635366fc310b32714e325633a6 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Tue, 24 May 2022 22:48:31 -0400 Subject: [PATCH] moved string value to util and added sprtinf, adjusted prints to use tostring to prevent concatentation errors on some types --- scada-common/util.lua | 46 +++++++++++++++++++++++++++++++++++++------ test/testutils.lua | 25 ++--------------------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/scada-common/util.lua b/scada-common/util.lua index 34c10ee..c87ff3b 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -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 -- diff --git a/test/testutils.lua b/test/testutils.lua index a27875d..12c07f0 100644 --- a/test/testutils.lua +++ b/test/testutils.lua @@ -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