better unit check error
This commit is contained in:
@@ -348,19 +348,25 @@ function Element.new(props, deps)
|
||||
|
||||
------ add non-hereditary ------
|
||||
--- self drawing---
|
||||
self.border = props.border
|
||||
and {
|
||||
-- Handle border (can be number or table)
|
||||
if type(props.border) == "table" then
|
||||
self.border = {
|
||||
top = props.border.top or false,
|
||||
right = props.border.right or false,
|
||||
bottom = props.border.bottom 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,
|
||||
right = false,
|
||||
bottom = false,
|
||||
left = false,
|
||||
}
|
||||
end
|
||||
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)
|
||||
|
||||
|
||||
@@ -29,6 +29,15 @@ function Units.parse(value)
|
||||
return 0, "px"
|
||||
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)
|
||||
if value:match("%d%s+%a") then
|
||||
if ErrorHandler then
|
||||
@@ -59,7 +68,7 @@ function Units.parse(value)
|
||||
unit = "px"
|
||||
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 ErrorHandler then
|
||||
ErrorHandler.error("Units", string.format("Unknown unit '%s' in '%s'. Valid units: px, %%, vw, vh, ew, eh", unit, value))
|
||||
|
||||
@@ -89,28 +89,20 @@ function TestUnitsParse:testParseZero()
|
||||
end
|
||||
|
||||
function TestUnitsParse:testParseInvalidType()
|
||||
local value, unit = Units.parse(nil)
|
||||
luaunit.assertEquals(value, 0)
|
||||
luaunit.assertEquals(unit, "px")
|
||||
luaunit.assertErrorMsgContains("Invalid unit value type", Units.parse, nil)
|
||||
end
|
||||
|
||||
function TestUnitsParse:testParseInvalidString()
|
||||
local value, unit = Units.parse("abc")
|
||||
luaunit.assertEquals(value, 0)
|
||||
luaunit.assertEquals(unit, "px")
|
||||
luaunit.assertErrorMsgContains("Invalid unit format", Units.parse, "abc")
|
||||
end
|
||||
|
||||
function TestUnitsParse:testParseInvalidUnit()
|
||||
local value, unit = Units.parse("100xyz")
|
||||
luaunit.assertEquals(value, 100)
|
||||
luaunit.assertEquals(unit, "px") -- Falls back to px
|
||||
luaunit.assertErrorMsgContains("Unknown unit", Units.parse, "100xyz")
|
||||
end
|
||||
|
||||
function TestUnitsParse:testParseWithSpace()
|
||||
-- Spaces between number and unit should be invalid
|
||||
local value, unit = Units.parse("100 px")
|
||||
luaunit.assertEquals(value, 0)
|
||||
luaunit.assertEquals(unit, "px")
|
||||
luaunit.assertErrorMsgContains("contains space", Units.parse, "100 px")
|
||||
end
|
||||
|
||||
-- Test suite for Units.resolve()
|
||||
@@ -377,15 +369,11 @@ function TestUnitsEdgeCases:testResolveZeroParentSize()
|
||||
end
|
||||
|
||||
function TestUnitsEdgeCases:testParseEmptyString()
|
||||
local value, unit = Units.parse("")
|
||||
luaunit.assertEquals(value, 0)
|
||||
luaunit.assertEquals(unit, "px")
|
||||
luaunit.assertErrorMsgContains("Invalid unit format", Units.parse, "")
|
||||
end
|
||||
|
||||
function TestUnitsEdgeCases:testParseOnlyUnit()
|
||||
local value, unit = Units.parse("px")
|
||||
luaunit.assertEquals(value, 0)
|
||||
luaunit.assertEquals(unit, "px")
|
||||
luaunit.assertErrorMsgContains("Missing numeric value before unit", Units.parse, "px")
|
||||
end
|
||||
|
||||
function TestUnitsEdgeCases:testResolveNegativePercentage()
|
||||
|
||||
Reference in New Issue
Block a user