Skip to content

Commit

Permalink
Runtime: Streamline the transform library's input validation
Browse files Browse the repository at this point in the history
This code was copy/pasted from the previous iteration of the runtime, which didn't include a validation library.
  • Loading branch information
rdw-software committed Feb 9, 2024
1 parent 09368ee commit bb0ee2e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 5 additions & 3 deletions Runtime/Libraries/transform.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local validation = require("validation")
local validateString = validation.validateString

local transform = {
ENABLE_TEXT_TRANSFORMATIONS = true,
START_SEQUENCE = "\27[",
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion Tests/BDD/transform-library.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bb0ee2e

Please sign in to comment.