From bb0ee2e10563074f8d543c1644bf0a01d522b035 Mon Sep 17 00:00:00 2001 From: RDW Date: Fri, 9 Feb 2024 19:10:52 +0100 Subject: [PATCH] Runtime: Streamline the transform library's input validation This code was copy/pasted from the previous iteration of the runtime, which didn't include a validation library. --- Runtime/Libraries/transform.lua | 8 +++++--- Tests/BDD/transform-library.spec.lua | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Runtime/Libraries/transform.lua b/Runtime/Libraries/transform.lua index ec8baaea6..c3baa4fe8 100644 --- a/Runtime/Libraries/transform.lua +++ b/Runtime/Libraries/transform.lua @@ -1,3 +1,6 @@ +local validation = require("validation") +local validateString = validation.validateString + local transform = { ENABLE_TEXT_TRANSFORMATIONS = true, START_SEQUENCE = "\27[", @@ -68,9 +71,8 @@ function transform.strip(coloredConsoleText) end local function transformText(text, color) - if type(text) ~= "string" then - error("Usage: transform." .. color .. "(text : string)", 0) - end + validateString(text, "text") + validateString(color, "color") if not transform.ENABLE_TEXT_TRANSFORMATIONS then return text diff --git a/Tests/BDD/transform-library.spec.lua b/Tests/BDD/transform-library.spec.lua index 2eed30fad..c088378ce 100644 --- a/Tests/BDD/transform-library.spec.lua +++ b/Tests/BDD/transform-library.spec.lua @@ -77,9 +77,13 @@ describe("transform", function() } for _, color in ipairs(supportedTerminalColors) do for _, invalidValue in ipairs(invalidValues) do + local actualType = type(invalidValue) assertThrows(function() transform[color](invalidValue) - end, "Usage: transform." .. color .. "(text : string)") + end, format( + "Expected argument text to be a string value, but received a %s value instead", + actualType + )) end end end)