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 a0bd95a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 6 additions & 6 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 @@ -52,9 +55,7 @@ function transform.disable()
end

function transform.strip(coloredConsoleText)
if type(coloredConsoleText) ~= "string" then
error("Usage: transform.strip(text : string)", 0)
end
validateString(coloredConsoleText, "coloredConsoleText")

-- All credit goes to this fine gentleman: https://stackoverflow.com/users/3735873/tonypdmtr
local strippedConsoleText = coloredConsoleText
Expand All @@ -68,9 +69,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
12 changes: 11 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 Expand Up @@ -108,6 +112,12 @@ describe("transform", function()
end)

describe("strip", function()
it("should throw if a non-string value was passed", function()
assertThrows(function()
transform.strip(42)
end, "Expected argument coloredConsoleText to be a string value, but received a number value instead")
end)

it("should remove color codes from the input without otherwise modifying it", function()
for name, transformation in pairs(transform) do
if type(transformation) == "function" and transform.colorCodes[name] then
Expand Down

0 comments on commit a0bd95a

Please sign in to comment.