Replacing errors with warns in non-critical areas

This commit is contained in:
Michael Freno
2025-11-17 01:56:02 -05:00
parent f4d514bf2e
commit e5e7b55709
25 changed files with 3596 additions and 313 deletions

View File

@@ -1223,6 +1223,771 @@ function TestElementAdditional:test_element_with_userdata()
luaunit.assertEquals(element.userdata.count, 42)
end
-- ==========================================
-- UNHAPPY PATH TESTS
-- ==========================================
TestElementUnhappyPaths = {}
function TestElementUnhappyPaths:setUp()
FlexLove.beginFrame(1920, 1080)
end
function TestElementUnhappyPaths:tearDown()
FlexLove.endFrame()
end
-- Test: Element with missing deps parameter
function TestElementUnhappyPaths:test_element_without_deps()
local Element = require("modules.Element")
local success = pcall(function()
Element.new({}, nil)
end)
luaunit.assertFalse(success) -- Should error without deps
end
-- Test: Element with negative dimensions
function TestElementUnhappyPaths:test_element_negative_dimensions()
local element = FlexLove.new({
id = "negative",
x = 0,
y = 0,
width = -100,
height = -50,
})
luaunit.assertNotNil(element)
-- Element should still be created (negative values handled)
end
-- Test: Element with zero dimensions
function TestElementUnhappyPaths:test_element_zero_dimensions()
local element = FlexLove.new({
id = "zero",
x = 0,
y = 0,
width = 0,
height = 0,
})
luaunit.assertNotNil(element)
end
-- Test: Element with extreme dimensions
function TestElementUnhappyPaths:test_element_extreme_dimensions()
local element = FlexLove.new({
id = "huge",
x = 0,
y = 0,
width = 1000000,
height = 1000000,
})
luaunit.assertNotNil(element)
end
-- Test: Element with invalid opacity values
function TestElementUnhappyPaths:test_element_invalid_opacity()
-- Opacity > 1
local success = pcall(function()
FlexLove.new({
id = "high_opacity",
width = 100,
height = 100,
opacity = 2.5,
})
end)
luaunit.assertFalse(success) -- Should error (validateRange)
-- Negative opacity
success = pcall(function()
FlexLove.new({
id = "negative_opacity",
width = 100,
height = 100,
opacity = -0.5,
})
end)
luaunit.assertFalse(success) -- Should error (validateRange)
end
-- Test: Element with invalid imageOpacity values
function TestElementUnhappyPaths:test_element_invalid_image_opacity()
-- imageOpacity > 1
local success = pcall(function()
FlexLove.new({
id = "high_img_opacity",
width = 100,
height = 100,
imageOpacity = 3.0,
})
end)
luaunit.assertFalse(success)
-- Negative imageOpacity
success = pcall(function()
FlexLove.new({
id = "negative_img_opacity",
width = 100,
height = 100,
imageOpacity = -1.0,
})
end)
luaunit.assertFalse(success)
end
-- Test: Element with invalid textSize
function TestElementUnhappyPaths:test_element_invalid_text_size()
-- Zero textSize
local success = pcall(function()
FlexLove.new({
id = "zero_text",
width = 100,
height = 100,
textSize = 0,
})
end)
luaunit.assertFalse(success)
-- Negative textSize
success = pcall(function()
FlexLove.new({
id = "negative_text",
width = 100,
height = 100,
textSize = -12,
})
end)
luaunit.assertFalse(success)
end
-- Test: Element with invalid textAlign enum
function TestElementUnhappyPaths:test_element_invalid_text_align()
local success = pcall(function()
FlexLove.new({
id = "invalid_align",
width = 100,
height = 100,
textAlign = "invalid_value",
})
end)
luaunit.assertFalse(success) -- Should error (validateEnum)
end
-- Test: Element with invalid positioning enum
function TestElementUnhappyPaths:test_element_invalid_positioning()
local success = pcall(function()
FlexLove.new({
id = "invalid_pos",
width = 100,
height = 100,
positioning = "invalid_positioning",
})
end)
luaunit.assertFalse(success) -- Should error (validateEnum)
end
-- Test: Element with invalid flexDirection enum
function TestElementUnhappyPaths:test_element_invalid_flex_direction()
local success = pcall(function()
FlexLove.new({
id = "invalid_flex",
width = 100,
height = 100,
positioning = "flex",
flexDirection = "diagonal",
})
end)
luaunit.assertFalse(success) -- Should error (validateEnum)
end
-- Test: Element with invalid objectFit enum
function TestElementUnhappyPaths:test_element_invalid_object_fit()
local success = pcall(function()
FlexLove.new({
id = "invalid_fit",
width = 100,
height = 100,
objectFit = "stretch",
})
end)
luaunit.assertFalse(success) -- Should error (validateEnum)
end
-- Test: Element with nonexistent image path
function TestElementUnhappyPaths:test_element_nonexistent_image()
local element = FlexLove.new({
id = "no_image",
width = 100,
height = 100,
imagePath = "/nonexistent/path/to/image.png",
})
luaunit.assertNotNil(element)
luaunit.assertNil(element._loadedImage) -- Image should fail to load silently
end
-- Test: Element with passwordMode and multiline (conflicting)
function TestElementUnhappyPaths:test_element_password_multiline_conflict()
local element = FlexLove.new({
id = "conflict",
width = 200,
height = 100,
editable = true,
passwordMode = true,
multiline = true, -- Should be disabled by passwordMode
})
luaunit.assertNotNil(element)
luaunit.assertFalse(element.multiline) -- multiline should be forced to false
end
-- Test: Element addChild with nil child
function TestElementUnhappyPaths:test_add_nil_child()
local parent = FlexLove.new({
id = "parent",
width = 200,
height = 200,
})
local success = pcall(function()
parent:addChild(nil)
end)
luaunit.assertFalse(success) -- Should error
end
-- Test: Element removeChild that doesn't exist
function TestElementUnhappyPaths:test_remove_nonexistent_child()
local parent = FlexLove.new({
id = "parent",
width = 200,
height = 200,
})
local notAChild = FlexLove.new({
id = "orphan",
width = 50,
height = 50,
})
parent:removeChild(notAChild) -- Should not crash
luaunit.assertEquals(#parent.children, 0)
end
-- Test: Element removeChild with nil
function TestElementUnhappyPaths:test_remove_nil_child()
local parent = FlexLove.new({
id = "parent",
width = 200,
height = 200,
})
parent:removeChild(nil) -- Should not crash
luaunit.assertTrue(true)
end
-- Test: Element clearChildren on empty parent
function TestElementUnhappyPaths:test_clear_children_empty()
local parent = FlexLove.new({
id = "parent",
width = 200,
height = 200,
})
parent:clearChildren() -- Should not crash
luaunit.assertEquals(#parent.children, 0)
end
-- Test: Element clearChildren called twice
function TestElementUnhappyPaths:test_clear_children_twice()
local parent = FlexLove.new({
id = "parent",
width = 200,
height = 200,
})
local child = FlexLove.new({
id = "child",
width = 50,
height = 50,
parent = parent,
})
parent:clearChildren()
parent:clearChildren() -- Call again
luaunit.assertEquals(#parent.children, 0)
end
-- Test: Element contains with extreme coordinates
function TestElementUnhappyPaths:test_contains_extreme_coordinates()
local element = FlexLove.new({
id = "test",
x = 10,
y = 20,
width = 100,
height = 50,
})
luaunit.assertFalse(element:contains(math.huge, math.huge))
luaunit.assertFalse(element:contains(-math.huge, -math.huge))
end
-- Test: Element contains with NaN coordinates
function TestElementUnhappyPaths:test_contains_nan_coordinates()
local element = FlexLove.new({
id = "test",
x = 10,
y = 20,
width = 100,
height = 50,
})
local nan = 0 / 0
local result = element:contains(nan, nan)
-- NaN comparisons return false, so this should be false
luaunit.assertFalse(result)
end
-- Test: Element setScrollPosition without ScrollManager
function TestElementUnhappyPaths:test_scroll_without_manager()
local element = FlexLove.new({
id = "no_scroll",
width = 100,
height = 100,
-- No overflow property, so no ScrollManager
})
element:setScrollPosition(50, 50) -- Should not crash
luaunit.assertTrue(true)
end
-- Test: Element setScrollPosition with extreme values
function TestElementUnhappyPaths:test_scroll_extreme_values()
local element = FlexLove.new({
id = "scrollable",
width = 200,
height = 200,
overflow = "scroll",
})
element:setScrollPosition(1000000, 1000000) -- Should clamp
luaunit.assertTrue(true)
element:setScrollPosition(-1000000, -1000000) -- Should clamp to 0
local scrollX, scrollY = element:getScrollPosition()
luaunit.assertEquals(scrollX, 0)
luaunit.assertEquals(scrollY, 0)
end
-- Test: Element scrollBy with nil values
function TestElementUnhappyPaths:test_scroll_by_nil()
local element = FlexLove.new({
id = "scrollable",
width = 200,
height = 200,
overflow = "scroll",
})
element:scrollBy(nil, nil) -- Should use current position
luaunit.assertTrue(true)
end
-- Test: Element destroy on already destroyed element
function TestElementUnhappyPaths:test_destroy_twice()
local element = FlexLove.new({
id = "destroyable",
width = 100,
height = 100,
})
element:destroy()
element:destroy() -- Call again - should not crash
luaunit.assertTrue(true)
end
-- Test: Element destroy with circular reference (parent-child)
function TestElementUnhappyPaths:test_destroy_with_children()
local parent = FlexLove.new({
id = "parent",
width = 200,
height = 200,
})
local child = FlexLove.new({
id = "child",
width = 50,
height = 50,
parent = parent,
})
parent:destroy() -- Should destroy all children too
luaunit.assertEquals(#parent.children, 0)
end
-- Test: Element update with nil dt
function TestElementUnhappyPaths:test_update_nil_dt()
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
})
local success = pcall(function()
element:update(nil)
end)
-- May or may not error depending on implementation
end
-- Test: Element update with negative dt
function TestElementUnhappyPaths:test_update_negative_dt()
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
})
element:update(-0.016) -- Should not crash
luaunit.assertTrue(true)
end
-- Test: Element draw with nil backdropCanvas
function TestElementUnhappyPaths:test_draw_nil_backdrop()
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
})
element:draw(nil) -- Should not crash
luaunit.assertTrue(true)
end
-- Test: Element with invalid cornerRadius types
function TestElementUnhappyPaths:test_invalid_corner_radius()
-- String cornerRadius
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
cornerRadius = "invalid",
})
luaunit.assertNotNil(element)
-- Negative cornerRadius
element = FlexLove.new({
id = "test2",
width = 100,
height = 100,
cornerRadius = -10,
})
luaunit.assertNotNil(element)
end
-- Test: Element with partial cornerRadius table
function TestElementUnhappyPaths:test_partial_corner_radius()
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
cornerRadius = {
topLeft = 10,
-- Missing other corners
},
})
luaunit.assertNotNil(element)
luaunit.assertEquals(element.cornerRadius.topLeft, 10)
luaunit.assertEquals(element.cornerRadius.topRight, 0)
end
-- Test: Element with invalid border types
function TestElementUnhappyPaths:test_invalid_border()
-- String border
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
border = "invalid",
})
luaunit.assertNotNil(element)
-- Negative border
element = FlexLove.new({
id = "test2",
width = 100,
height = 100,
border = -5,
})
luaunit.assertNotNil(element)
end
-- Test: Element with partial border table
function TestElementUnhappyPaths:test_partial_border()
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
border = {
top = 2,
left = 3,
-- Missing right and bottom
},
})
luaunit.assertNotNil(element)
luaunit.assertEquals(element.border.top, 2)
luaunit.assertEquals(element.border.left, 3)
luaunit.assertFalse(element.border.right)
luaunit.assertFalse(element.border.bottom)
end
-- Test: Element with invalid padding types
function TestElementUnhappyPaths:test_invalid_padding()
-- String padding
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
padding = "invalid",
})
luaunit.assertNotNil(element)
-- Negative padding
element = FlexLove.new({
id = "test2",
width = 100,
height = 100,
padding = { top = -10, left = -10, right = -10, bottom = -10 },
})
luaunit.assertNotNil(element)
end
-- Test: Element with invalid margin types
function TestElementUnhappyPaths:test_invalid_margin()
-- String margin
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
margin = "invalid",
})
luaunit.assertNotNil(element)
-- Huge margin
element = FlexLove.new({
id = "test2",
width = 100,
height = 100,
margin = { top = 1000000, left = 1000000, right = 1000000, bottom = 1000000 },
})
luaunit.assertNotNil(element)
end
-- Test: Element with invalid gap value
function TestElementUnhappyPaths:test_invalid_gap()
-- Negative gap
local element = FlexLove.new({
id = "test",
width = 300,
height = 200,
positioning = "flex",
gap = -10,
})
luaunit.assertNotNil(element)
-- Huge gap
element = FlexLove.new({
id = "test2",
width = 300,
height = 200,
positioning = "flex",
gap = 1000000,
})
luaunit.assertNotNil(element)
end
-- Test: Element with invalid grid properties
function TestElementUnhappyPaths:test_invalid_grid_properties()
-- Zero rows/columns
local element = FlexLove.new({
id = "test",
width = 300,
height = 200,
positioning = "grid",
gridRows = 0,
gridColumns = 0,
})
luaunit.assertNotNil(element)
-- Negative rows/columns
element = FlexLove.new({
id = "test2",
width = 300,
height = 200,
positioning = "grid",
gridRows = -5,
gridColumns = -5,
})
luaunit.assertNotNil(element)
end
-- Test: Element setText on non-text element
function TestElementUnhappyPaths:test_set_text_on_non_text()
local element = FlexLove.new({
id = "no_text",
width = 100,
height = 100,
})
element:setText("New text") -- Should not crash
luaunit.assertEquals(element.text, "New text")
end
-- Test: Element setText with nil
function TestElementUnhappyPaths:test_set_text_nil()
local element = FlexLove.new({
id = "text",
width = 100,
height = 100,
text = "Initial",
})
element:setText(nil)
luaunit.assertNil(element.text)
end
-- Test: Element setText with extreme length
function TestElementUnhappyPaths:test_set_text_extreme_length()
local element = FlexLove.new({
id = "text",
width = 100,
height = 100,
text = "Initial",
})
local longText = string.rep("a", 100000)
element:setText(longText)
luaunit.assertEquals(element.text, longText)
end
-- Test: Element with conflicting size constraints
function TestElementUnhappyPaths:test_conflicting_size_constraints()
-- Width less than padding
local element = FlexLove.new({
id = "conflict",
width = 10,
height = 10,
padding = { top = 20, left = 20, right = 20, bottom = 20 },
})
luaunit.assertNotNil(element)
-- Content width should be clamped to 0 or handled gracefully
end
-- Test: Element textinput on non-editable element
function TestElementUnhappyPaths:test_textinput_non_editable()
local element = FlexLove.new({
id = "not_editable",
width = 100,
height = 100,
editable = false,
})
local success = pcall(function()
element:textinput("a")
end)
-- Should either do nothing or handle gracefully
end
-- Test: Element keypressed on non-editable element
function TestElementUnhappyPaths:test_keypressed_non_editable()
local element = FlexLove.new({
id = "not_editable",
width = 100,
height = 100,
editable = false,
})
local success = pcall(function()
element:keypressed("return", "return", false)
end)
-- Should either do nothing or handle gracefully
end
-- Test: Element with invalid blur configuration
function TestElementUnhappyPaths:test_invalid_blur_config()
-- Negative intensity
local element = FlexLove.new({
id = "blur",
width = 100,
height = 100,
contentBlur = { intensity = -10, quality = 5 },
})
luaunit.assertNotNil(element)
-- Intensity > 100
element = FlexLove.new({
id = "blur2",
width = 100,
height = 100,
backdropBlur = { intensity = 150, quality = 5 },
})
luaunit.assertNotNil(element)
-- Invalid quality
element = FlexLove.new({
id = "blur3",
width = 100,
height = 100,
contentBlur = { intensity = 50, quality = 0 },
})
luaunit.assertNotNil(element)
end
-- Test: Element getAvailableContentWidth/Height on element with no padding
function TestElementUnhappyPaths:test_available_content_no_padding()
local element = FlexLove.new({
id = "test",
width = 100,
height = 100,
})
local availWidth = element:getAvailableContentWidth()
local availHeight = element:getAvailableContentHeight()
luaunit.assertEquals(availWidth, 100)
luaunit.assertEquals(availHeight, 100)
end
-- Test: Element with maxLines but no multiline
function TestElementUnhappyPaths:test_max_lines_without_multiline()
local element = FlexLove.new({
id = "text",
width = 200,
height = 100,
editable = true,
multiline = false,
maxLines = 5, -- Should be ignored for single-line
})
luaunit.assertNotNil(element)
end
-- Test: Element with maxLength 0
function TestElementUnhappyPaths:test_max_length_zero()
local element = FlexLove.new({
id = "text",
width = 200,
height = 40,
editable = true,
maxLength = 0,
})
luaunit.assertNotNil(element)
end
-- Test: Element with negative maxLength
function TestElementUnhappyPaths:test_max_length_negative()
local element = FlexLove.new({
id = "text",
width = 200,
height = 40,
editable = true,
maxLength = -10,
})
luaunit.assertNotNil(element)
end
if not _G.RUNNING_ALL_TESTS then
os.exit(luaunit.LuaUnit.run())
end