better unit check error

This commit is contained in:
Michael Freno
2025-11-14 22:49:08 -05:00
parent f35bb11770
commit 2504ae506e
3 changed files with 30 additions and 27 deletions

View File

@@ -348,19 +348,25 @@ function Element.new(props, deps)
------ add non-hereditary ------ ------ add non-hereditary ------
--- self drawing--- --- self drawing---
self.border = props.border -- Handle border (can be number or table)
and { if type(props.border) == "table" then
self.border = {
top = props.border.top or false, top = props.border.top or false,
right = props.border.right or false, right = props.border.right or false,
bottom = props.border.bottom or false, bottom = props.border.bottom or false,
left = props.border.left or false, left = props.border.left or false,
} }
or { elseif props.border then
-- If border is a number or truthy value, keep it as-is
self.border = props.border
else
self.border = {
top = false, top = false,
right = false, right = false,
bottom = false, bottom = false,
left = false, left = false,
} }
end
self.borderColor = props.borderColor or self._deps.Color.new(0, 0, 0, 1) self.borderColor = props.borderColor or self._deps.Color.new(0, 0, 0, 1)
self.backgroundColor = props.backgroundColor or self._deps.Color.new(0, 0, 0, 0) self.backgroundColor = props.backgroundColor or self._deps.Color.new(0, 0, 0, 0)

View File

@@ -29,6 +29,15 @@ function Units.parse(value)
return 0, "px" return 0, "px"
end end
-- Check for unit-only input (e.g., "px", "%", "vw" without a number)
local validUnits = { px = true, ["%"] = true, vw = true, vh = true, ew = true, eh = true }
if validUnits[value] then
if ErrorHandler then
ErrorHandler.error("Units", string.format("Missing numeric value before unit '%s'. Use format like '50%s' (e.g., '50px', '10%%', '2vw')", value, value))
end
return 0, "px"
end
-- Check for invalid format (space between number and unit) -- Check for invalid format (space between number and unit)
if value:match("%d%s+%a") then if value:match("%d%s+%a") then
if ErrorHandler then if ErrorHandler then
@@ -59,7 +68,7 @@ function Units.parse(value)
unit = "px" unit = "px"
end end
local validUnits = { px = true, ["%"] = true, vw = true, vh = true, ew = true, eh = true } -- validUnits is already defined at the top of the function
if not validUnits[unit] then if not validUnits[unit] then
if ErrorHandler then if ErrorHandler then
ErrorHandler.error("Units", string.format("Unknown unit '%s' in '%s'. Valid units: px, %%, vw, vh, ew, eh", unit, value)) ErrorHandler.error("Units", string.format("Unknown unit '%s' in '%s'. Valid units: px, %%, vw, vh, ew, eh", unit, value))

View File

@@ -89,28 +89,20 @@ function TestUnitsParse:testParseZero()
end end
function TestUnitsParse:testParseInvalidType() function TestUnitsParse:testParseInvalidType()
local value, unit = Units.parse(nil) luaunit.assertErrorMsgContains("Invalid unit value type", Units.parse, nil)
luaunit.assertEquals(value, 0)
luaunit.assertEquals(unit, "px")
end end
function TestUnitsParse:testParseInvalidString() function TestUnitsParse:testParseInvalidString()
local value, unit = Units.parse("abc") luaunit.assertErrorMsgContains("Invalid unit format", Units.parse, "abc")
luaunit.assertEquals(value, 0)
luaunit.assertEquals(unit, "px")
end end
function TestUnitsParse:testParseInvalidUnit() function TestUnitsParse:testParseInvalidUnit()
local value, unit = Units.parse("100xyz") luaunit.assertErrorMsgContains("Unknown unit", Units.parse, "100xyz")
luaunit.assertEquals(value, 100)
luaunit.assertEquals(unit, "px") -- Falls back to px
end end
function TestUnitsParse:testParseWithSpace() function TestUnitsParse:testParseWithSpace()
-- Spaces between number and unit should be invalid -- Spaces between number and unit should be invalid
local value, unit = Units.parse("100 px") luaunit.assertErrorMsgContains("contains space", Units.parse, "100 px")
luaunit.assertEquals(value, 0)
luaunit.assertEquals(unit, "px")
end end
-- Test suite for Units.resolve() -- Test suite for Units.resolve()
@@ -377,15 +369,11 @@ function TestUnitsEdgeCases:testResolveZeroParentSize()
end end
function TestUnitsEdgeCases:testParseEmptyString() function TestUnitsEdgeCases:testParseEmptyString()
local value, unit = Units.parse("") luaunit.assertErrorMsgContains("Invalid unit format", Units.parse, "")
luaunit.assertEquals(value, 0)
luaunit.assertEquals(unit, "px")
end end
function TestUnitsEdgeCases:testParseOnlyUnit() function TestUnitsEdgeCases:testParseOnlyUnit()
local value, unit = Units.parse("px") luaunit.assertErrorMsgContains("Missing numeric value before unit", Units.parse, "px")
luaunit.assertEquals(value, 0)
luaunit.assertEquals(unit, "px")
end end
function TestUnitsEdgeCases:testResolveNegativePercentage() function TestUnitsEdgeCases:testResolveNegativePercentage()