diff --git a/examples/OnClickAnimations.lua b/examples/OnClickAnimations.lua index 883f885..e9e6a4c 100644 --- a/examples/OnClickAnimations.lua +++ b/examples/OnClickAnimations.lua @@ -3,10 +3,10 @@ local Gui = FlexLove.GUI local Color = FlexLove.Color ---@class AnimDemo ----@field window Window ----@field button Button ----@field fadeButton Button ----@field scaleButton Button +---@field window Element +---@field button Element +---@field fadeButton Element +---@field scaleButton Element local OnClickAnimDemo = {} OnClickAnimDemo.__index = OnClickAnimDemo @@ -14,7 +14,7 @@ function OnClickAnimDemo.init() local self = setmetatable({}, OnClickAnimDemo) -- Create a demo window - self.window = Gui.Window.new({ + self.window = Gui.new({ x = 100, y = 100, z = 10, @@ -26,7 +26,7 @@ function OnClickAnimDemo.init() }) -- Create a fade button - self.fadeButton = Gui.Button.new({ + self.fadeButton = Gui.new({ parent = self.window, x = 20, y = 80, @@ -44,7 +44,7 @@ function OnClickAnimDemo.init() }) -- Create a scale button - self.scaleButton = Gui.Button.new({ + self.scaleButton = Gui.new({ parent = self.window, x = 20, y = 140, diff --git a/testing/absolute-positioning.lua b/testing/absolute-positioning.lua new file mode 100644 index 0000000..4273115 --- /dev/null +++ b/testing/absolute-positioning.lua @@ -0,0 +1,199 @@ +package.path = package.path + .. ";./?.lua;./game/?.lua;./game/utils/?.lua;./game/components/?.lua;./game/systems/?.lua;./testing/?.lua" + +local luaunit = require("testing.luaunit") +require("testing.love_helper") + +local Gui = require("game.libs.FlexLove").GUI +local Color = require("game.libs.FlexLove").Color +local enums = require("game.libs.FlexLove").enums + +-- Test case for absolute positioning behavior +TestAbsolutePositioning = {} + +function TestAbsolutePositioning:testWindowWithAbsolutePositioning() + -- Create a window with absolute positioning + local window = Gui.new({ + x = 100, + y = 100, + w = 200, + h = 150, + positioning = enums.Positioning.ABSOLUTE, + }) + + -- Verify window properties + luaunit.assertEquals(window.x, 100) + luaunit.assertEquals(window.y, 100) + luaunit.assertEquals(window.width, 200) + luaunit.assertEquals(window.height, 150) + luaunit.assertEquals(window.positioning, enums.Positioning.ABSOLUTE) + + -- Create a child with absolute positioning + local child = Gui.new({ + parent = window, + x = 20, + y = 30, + w = 50, + h = 30, + positioning = enums.Positioning.ABSOLUTE, + text = "Test Button", + }) + + -- Verify child properties + luaunit.assertEquals(child.x, 20) + luaunit.assertEquals(child.y, 30) + luaunit.assertEquals(child.width, 50) + luaunit.assertEquals(child.height, 30) + luaunit.assertEquals(child.positioning, enums.Positioning.ABSOLUTE) + + -- Verify child is properly added to parent + luaunit.assertEquals(#window.children, 1) + luaunit.assertEquals(window.children[1], child) + + -- Verify parent-child relationship + luaunit.assertEquals(child.parent, window) +end + +function TestAbsolutePositioning:testChildInheritsAbsolutePositioning() + -- Create a window with flex positioning + local parentWindow = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Create a child without explicit positioning (should inherit) + local child = Gui.new({ + parent = parentWindow, + x = 10, + y = 10, + w = 50, + h = 30, + text = "Test Button", + }) + + -- Verify child inherits positioning from parent + luaunit.assertEquals(child.positioning, enums.Positioning.FLEX) +end + +function TestAbsolutePositioning:testAbsolutePositioningDoesNotAffectLayout() + -- Create a window with flex positioning + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add a child with absolute positioning + local absoluteChild = Gui.new({ + parent = window, + x = 100, + y = 50, + w = 80, + h = 40, + positioning = enums.Positioning.ABSOLUTE, + text = "Absolute Button", + }) + + -- Add a child with flex positioning + local flexChild = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 30, + text = "Flex Button", + }) + + -- Verify both children are added + luaunit.assertEquals(#window.children, 2) + + -- Test that absolute child's position is not affected by flex layout calculations + -- The absolute child should keep its position (100, 50) regardless of other children + luaunit.assertEquals(absoluteChild.x, 100) + luaunit.assertEquals(absoluteChild.y, 50) + + -- Test that flex child's position is affected by layout calculations + luaunit.assertEquals(flexChild.x, 0) -- Should be positioned according to flex layout + + -- Check that absolute positioning doesn't interfere with container auto-sizing + window:layoutChildren() + -- The absolute child should not affect the auto-sizing calculation + luaunit.assertEquals(window.width, 300) -- Window width remains unchanged +end + +function TestAbsolutePositioning:testAbsolutePositioningResizing() + -- Create a window with absolute positioning + local window = Gui.new({ + x = 100, + y = 100, + w = 200, + h = 150, + positioning = enums.Positioning.ABSOLUTE, + }) + + -- Add an absolute positioned child + local child = Gui.new({ + parent = window, + x = 20, + y = 30, + w = 50, + h = 30, + positioning = enums.Positioning.ABSOLUTE, + text = "Test Button", + }) + + -- Resize the window (from 200x150 to 400x300) + local newWidth, newHeight = 400, 300 + window:resize(newWidth, newHeight) + + -- The key test is that absolute positioning should work regardless of how we resize + -- The child's coordinates should be maintained as they are, and the parent should resize properly + luaunit.assertEquals(window.width, 400) + luaunit.assertEquals(window.height, 300) + luaunit.assertEquals(child.positioning, enums.Positioning.ABSOLUTE) -- Child should still be absolute + + -- We can't easily test exact coordinate values because the resize behavior is complex, + -- but we can verify that the child still exists and maintains its properties +end + +function TestAbsolutePositioning:testAbsolutePositioningWithPaddingAndMargin() + -- Create a window with absolute positioning + local window = Gui.new({ + x = 10, + y = 10, + w = 200, + h = 150, + positioning = enums.Positioning.ABSOLUTE, + padding = { left = 10, top = 5 }, + margin = { left = 5, top = 5 }, + }) + + -- Add an absolute positioned child + local child = Gui.new({ + parent = window, + x = 20, + y = 30, + w = 50, + h = 30, + positioning = enums.Positioning.ABSOLUTE, + text = "Test Button", + }) + + -- Verify absolute child position is independent of padding/margin + luaunit.assertEquals(child.x, 20) + luaunit.assertEquals(child.y, 30) +end + +-- Run the tests +luaunit.LuaUnit.run() diff --git a/testing/align-items-tests.lua b/testing/align-items-tests.lua new file mode 100644 index 0000000..3787cdc --- /dev/null +++ b/testing/align-items-tests.lua @@ -0,0 +1,241 @@ +package.path = package.path + .. ";./?.lua;./game/?.lua;./game/utils/?.lua;./game/components/?.lua;./game/systems/?.lua;./testing/?.lua" + +local luaunit = require("testing.luaunit") +require("testing.love_helper") + +local Gui = require("game.libs.FlexLove").GUI +local enums = require("game.libs.FlexLove").enums + +-- Test case for align items alignment properties +TestAlignItems = {} + +function TestAlignItems:testStretchAlignItems() + -- Create a horizontal flex container with stretch align items + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children with different heights + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With stretch, children should be stretched to fill the container height + luaunit.assertEquals(child1.height, 200) -- Should stretch to full container height + luaunit.assertEquals(child2.height, 200) -- Should stretch to full container height +end + +function TestAlignItems:testFlexStartAlignItems() + -- Create a horizontal flex container with flex-start align items + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.FLEX_START, + }) + + -- Add multiple children with different heights + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With flex-start, children should be aligned to the start of the cross axis (top) + luaunit.assertEquals(child1.y, 0) -- Should be at top position + luaunit.assertEquals(child2.y, 0) -- Should be at top position +end + +function TestAlignItems:testFlexEndAlignItems() + -- Create a horizontal flex container with flex-end align items + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.FLEX_END, + }) + + -- Add multiple children with different heights + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With flex-end, children should be aligned to the end of the cross axis (bottom) + luaunit.assertEquals(child1.y, 200 - 30) -- Should be at bottom position + luaunit.assertEquals(child2.y, 200 - 40) -- Should be at bottom position +end + +function TestAlignItems:testCenterAlignItems() + -- Create a horizontal flex container with center align items + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.CENTER, + }) + + -- Add multiple children with different heights + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With center, children should be centered along the cross axis + luaunit.assertEquals(child1.y, (200 - 30) / 2) -- Should be centered vertically + luaunit.assertEquals(child2.y, (200 - 40) / 2) -- Should be centered vertically +end + +function TestAlignItems:testVerticalAlignItems() + -- Create a vertical flex container with align items properties + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.VERTICAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.CENTER, + }) + + -- Add multiple children with different widths + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With vertical container, align items affects the X axis + luaunit.assertEquals(child1.x, (300 - 50) / 2) -- Should be centered horizontally + luaunit.assertEquals(child2.x, (300 - 60) / 2) -- Should be centered horizontally +end + +function TestAlignItems:testAlignItemsInheritance() + -- Create a parent with stretch alignment + local parentWindow = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Create a child without explicit alignment (should inherit) + local child = Gui.new({ + parent = parentWindow, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + }) + + -- Verify child inherits align items from parent + luaunit.assertEquals(child.alignItems, enums.AlignItems.STRETCH) +end + +-- Run the tests +luaunit.LuaUnit.run() diff --git a/testing/align-self-tests.lua b/testing/align-self-tests.lua new file mode 100644 index 0000000..3fdb872 --- /dev/null +++ b/testing/align-self-tests.lua @@ -0,0 +1,200 @@ +package.path = package.path + .. ";./?.lua;./game/?.lua;./game/utils/?.lua;./game/components/?.lua;./game/systems/?.lua;./testing/?.lua" + +local luaunit = require("testing.luaunit") +require("testing.love_helper") + +local Gui = require("game.libs.FlexLove").GUI +local enums = require("game.libs.FlexLove").enums + +-- Test case for align self properties +TestAlignSelf = {} + +function TestAlignSelf:testAutoAlignSelf() + -- Create a flex container with default alignment + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add a child with auto align self + local child = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + alignSelf = enums.AlignSelf.AUTO, + }) + + -- Layout children + window:layoutChildren() + + -- With auto, child should inherit alignment from parent's alignItems + luaunit.assertEquals(child.alignSelf, enums.AlignSelf.AUTO) +end + +function TestAlignSelf:testStretchAlignSelf() + -- Create a flex container with stretch alignment + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add a child with stretch align self + local child = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + alignSelf = enums.AlignSelf.STRETCH, + }) + + -- Layout children + window:layoutChildren() + + -- With stretch, child should be stretched to fill container height + luaunit.assertEquals(child.height, 200) -- Should stretch to full container height +end + +function TestAlignSelf:testFlexStartAlignSelf() + -- Create a flex container with center alignment + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.CENTER, + }) + + -- Add a child with flex-start align self + local child = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + alignSelf = enums.AlignSelf.FLEX_START, + }) + + -- Layout children + window:layoutChildren() + + -- With flex-start, child should be aligned to the start of cross axis (top) + luaunit.assertEquals(child.y, 0) -- Should be at top position +end + +function TestAlignSelf:testFlexEndAlignSelf() + -- Create a flex container with center alignment + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.CENTER, + }) + + -- Add a child with flex-end align self + local child = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + alignSelf = enums.AlignSelf.FLEX_END, + }) + + -- Layout children + window:layoutChildren() + + -- With flex-end, child should be aligned to the end of cross axis (bottom) + luaunit.assertEquals(child.y, 200 - 30) -- Should be at bottom position +end + +function TestAlignSelf:testCenterAlignSelf() + -- Create a flex container with stretch alignment + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add a child with center align self + local child = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + alignSelf = enums.AlignSelf.CENTER, + }) + + -- Layout children + window:layoutChildren() + + -- With center, child should be centered along cross axis + luaunit.assertEquals(child.y, (200 - 30) / 2) -- Should be centered vertically +end + +function TestAlignSelf:testVerticalAlignSelf() + -- Create a vertical flex container with center alignment + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.VERTICAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.CENTER, + }) + + -- Add a child with center align self + local child = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + alignSelf = enums.AlignSelf.CENTER, + }) + + -- Layout children + window:layoutChildren() + + -- With vertical container, align self affects the X axis + luaunit.assertEquals(child.x, (300 - 50) / 2) -- Should be centered horizontally +end + +-- Run the tests +luaunit.LuaUnit.run() diff --git a/testing/flex-container-tests.lua b/testing/flex-container-tests.lua new file mode 100644 index 0000000..48b9eaf --- /dev/null +++ b/testing/flex-container-tests.lua @@ -0,0 +1,243 @@ +package.path = package.path + .. ";./?.lua;./game/?.lua;./game/utils/?.lua;./game/components/?.lua;./game/systems/?.lua;./testing/?.lua" + +local luaunit = require("testing.luaunit") +require("testing.love_helper") + +-- Mock Logger to avoid dependency issues +local Logger = { + debug = function() end, + info = function() end, + warn = function() end, + error = function() end, +} + +-- Make sure the logger is available in the global scope +_G.Logger = Logger + +local Gui = require("game.libs.FlexLove").GUI +local Color = require("game.libs.FlexLove").Color +local enums = require("game.libs.FlexLove").enums + +-- Test case for flex container layout behavior +TestFlexContainer = {} + +function TestFlexContainer:testWindowWithFlexPositioning() + -- Create a window with flex positioning + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Verify window properties + luaunit.assertEquals(window.x, 0) + luaunit.assertEquals(window.y, 0) + luaunit.assertEquals(window.width, 300) + luaunit.assertEquals(window.height, 200) + luaunit.assertEquals(window.positioning, enums.Positioning.FLEX) + luaunit.assertEquals(window.flexDirection, enums.FlexDirection.HORIZONTAL) + luaunit.assertEquals(window.justifyContent, enums.JustifyContent.FLEX_START) + luaunit.assertEquals(window.alignItems, enums.AlignItems.STRETCH) +end + +function TestFlexContainer:testWindowAutoSizing() + -- Create a window with flex positioning and auto-sizing + local window = Gui.new({ + x = 0, + y = 0, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add a child with explicit dimensions + local child = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + }) + + -- Verify that the window auto-sizes to fit children (this should be calculated by layoutChildren) + window:layoutChildren() + + -- The window should have auto-sized based on children + luaunit.assertEquals(#window.children, 1) + luaunit.assertEquals(window.children[1], child) +end + +function TestFlexContainer:testWindowWithMultipleChildren() + -- Create a flex container window + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + local child3 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 70, + h = 50, + text = "Button 3", + }) + + -- Verify all children are added + luaunit.assertEquals(#window.children, 3) + luaunit.assertEquals(window.children[1], child1) + luaunit.assertEquals(window.children[2], child2) + luaunit.assertEquals(window.children[3], child3) + + -- Test layout calculation + window:layoutChildren() + + -- Verify children positions are calculated (basic checks) + luaunit.assertEquals(child1.x, 0) -- First child should be at start position +end + +function TestFlexContainer:testWindowDimensionsUpdateWhenChildrenAdded() + -- Create a flex container window with explicit size + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add a child + local child = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + }) + + -- Test that layoutChildren properly calculates positions + window:layoutChildren() + + -- Window dimensions should remain unchanged since we explicitly set them + luaunit.assertEquals(window.width, 300) + luaunit.assertEquals(window.height, 200) +end + +function TestFlexContainer:testWindowAutoSizingWhenNotExplicitlySet() + -- Create a flex container window without explicit size (should auto-size) + local window = Gui.new({ + x = 0, + y = 0, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Test auto-sizing calculation + window:layoutChildren() + + -- The window should have auto-sized based on children (this is a basic check) + luaunit.assertEquals(#window.children, 2) +end + +function TestFlexContainer:testContainerLayoutChildrenFunction() + -- Create a flex container window + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Test that layoutChildren function exists and works + luaunit.assertNotNil(window.layoutChildren) + + -- Run the layout function + window:layoutChildren() + + -- Verify child positions (this is a basic test of functionality) + luaunit.assertEquals(child1.x, 0) -- Should be at position 0 initially +end + +-- Run the tests +luaunit.LuaUnit.run() diff --git a/testing/flex-direction-tests.lua b/testing/flex-direction-tests.lua new file mode 100644 index 0000000..8f9315d --- /dev/null +++ b/testing/flex-direction-tests.lua @@ -0,0 +1,163 @@ +package.path = package.path + .. ";./?.lua;./game/?.lua;./game/utils/?.lua;./game/components/?.lua;./game/systems/?.lua;./testing/?.lua" + +local luaunit = require("testing.luaunit") +require("testing.love_helper") + +local Gui = require("game.libs.FlexLove").GUI +local enums = require("game.libs.FlexLove").enums + +-- Test case for flex direction properties +TestFlexDirection = {} + +function TestFlexDirection:testHorizontalFlexDirection() + -- Create a window with horizontal flex direction + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Verify window properties + luaunit.assertEquals(window.flexDirection, enums.FlexDirection.HORIZONTAL) +end + +function TestFlexDirection:testVerticalFlexDirection() + -- Create a window with vertical flex direction + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.VERTICAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Verify window properties + luaunit.assertEquals(window.flexDirection, enums.FlexDirection.VERTICAL) +end + +function TestFlexDirection:testHorizontalLayoutChildren() + -- Create a horizontal flex container + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- Verify positions for horizontal layout (children should be placed side by side) + luaunit.assertEquals(child1.x, 0) -- First child at start position + luaunit.assertEquals(child1.y, 0) -- First child at top position + + -- Second child should be positioned after first child + gap + luaunit.assertEquals(child2.x, 50 + 10) -- child1 width + gap + luaunit.assertEquals(child2.y, 0) -- Same y position as first child +end + +function TestFlexDirection:testVerticalLayoutChildren() + -- Create a vertical flex container + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.VERTICAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- Verify positions for vertical layout (children should be placed one below another) + luaunit.assertEquals(child1.x, 0) -- First child at left position + luaunit.assertEquals(child1.y, 0) -- First child at start position + + -- Second child should be positioned after first child + gap + luaunit.assertEquals(child2.x, 0) -- Same x position as first child + luaunit.assertEquals(child2.y, 30 + 10) -- child1 height + gap +end + +function TestFlexDirection:testFlexDirectionInheritance() + -- Create a parent with horizontal direction + local parentWindow = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Create a child without explicit direction (should inherit) + local child = Gui.new({ + parent = parentWindow, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Test Button", + }) + + -- Verify child inherits flex direction from parent + luaunit.assertEquals(child.flexDirection, enums.FlexDirection.HORIZONTAL) +end + +-- Run the tests +luaunit.LuaUnit.run() diff --git a/testing/justify-content-tests.lua b/testing/justify-content-tests.lua new file mode 100644 index 0000000..1f6f585 --- /dev/null +++ b/testing/justify-content-tests.lua @@ -0,0 +1,300 @@ +package.path = package.path + .. ";./?.lua;./game/?.lua;./game/utils/?.lua;./game/components/?.lua;./game/systems/?.lua;./testing/?.lua" + +local luaunit = require("testing.luaunit") +require("testing.love_helper") + +local Gui = require("game.libs.FlexLove").GUI +local enums = require("game.libs.FlexLove").enums + +-- Test case for justify content alignment properties +TestJustifyContent = {} + +function TestJustifyContent:testFlexStartJustifyContent() + -- Create a horizontal flex container with flex-start justify content + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With flex-start, children should start at the beginning of the container + luaunit.assertEquals(child1.x, 0) -- First child at start position +end + +function TestJustifyContent:testCenterJustifyContent() + -- Create a horizontal flex container with center justify content + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.CENTER, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With center, children should be centered in the container + -- Calculate expected position based on container width and child sizes + local totalWidth = 50 + 60 + 10 -- child1.width + child2.width + gap + local containerWidth = 300 + local expectedPosition = (containerWidth - totalWidth) / 2 + + luaunit.assertEquals(child1.x, expectedPosition) +end + +function TestJustifyContent:testFlexEndJustifyContent() + -- Create a horizontal flex container with flex-end justify content + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_END, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With flex-end, children should be positioned at the end of the container + local totalWidth = 50 + 60 + 10 -- child1.width + child2.width + gap + local containerWidth = 300 + local expectedPosition = containerWidth - totalWidth + + luaunit.assertEquals(child1.x, expectedPosition) +end + +function TestJustifyContent:testSpaceAroundJustifyContent() + -- Create a horizontal flex container with space-around justify content + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.SPACE_AROUND, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With space-around, there should be equal spacing around each child + -- This is a basic test to ensure the function doesn't crash and children are positioned + luaunit.assertNotNil(child1.x) +end + +function TestJustifyContent:testSpaceEvenlyJustifyContent() + -- Create a horizontal flex container with space-evenly justify content + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.SPACE_EVENLY, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With space-evenly, there should be equal spacing between each child + -- This is a basic test to ensure the function doesn't crash and children are positioned + luaunit.assertNotNil(child1.x) +end + +function TestJustifyContent:testSpaceBetweenJustifyContent() + -- Create a horizontal flex container with space-between justify content + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.SPACE_BETWEEN, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With space-between, there should be equal spacing between each child + -- This is a basic test to ensure the function doesn't crash and children are positioned + luaunit.assertNotNil(child1.x) +end + +function TestJustifyContent:testVerticalJustifyContent() + -- Create a vertical flex container with justify content properties + local window = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.VERTICAL, + justifyContent = enums.JustifyContent.CENTER, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add multiple children + local child1 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = window, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout children + window:layoutChildren() + + -- With vertical container, justify content affects the Y axis + luaunit.assertNotNil(child1.y) +end + +-- Run the tests +os.exit(luaunit.LuaUnit.run()) + diff --git a/testing/nested-layout-tests.lua b/testing/nested-layout-tests.lua new file mode 100644 index 0000000..a51b49a --- /dev/null +++ b/testing/nested-layout-tests.lua @@ -0,0 +1,296 @@ +package.path = package.path + .. ";./?.lua;./game/?.lua;./game/utils/?.lua;./game/components/?.lua;./game/systems/?.lua;./testing/?.lua" + +local luaunit = require("testing.luaunit") +require("testing.love_helper") + +local Gui = require("game.libs.FlexLove").GUI +local enums = require("game.libs.FlexLove").enums + +-- Test case for nested flex layouts +TestNestedLayouts = {} + +function TestNestedLayouts:testSimpleNestedFlex() + -- Create a parent window with horizontal flex direction + local parentWindow = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Create a child window (nested flex container) + local childWindow = Gui.new({ + parent = parentWindow, + x = 0, + y = 0, + w = 150, + h = 100, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.VERTICAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add children to nested window + local child1 = Gui.new({ + parent = childWindow, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = childWindow, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout all children + parentWindow:layoutChildren() + + -- Verify that the nested window is positioned correctly within parent + luaunit.assertEquals(childWindow.x, 0) -- Should be positioned at start of parent + luaunit.assertEquals(childWindow.y, 0) -- Should be positioned at start of parent + + -- Verify that nested children are laid out correctly + luaunit.assertEquals(child1.x, 0) -- Nested child should be at left position + luaunit.assertEquals(child1.y, 0) -- Nested child should be at top position + + luaunit.assertEquals(child2.x, 0) -- Nested child should be at left position + luaunit.assertEquals(child2.y, 30 + 10) -- Should be positioned after first child + gap +end + +function TestNestedLayouts:testDeeplyNestedFlex() + -- Create a parent window with horizontal flex direction + local parentWindow = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Create a nested window + local nestedWindow = Gui.new({ + parent = parentWindow, + x = 0, + y = 0, + w = 150, + h = 100, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.VERTICAL, + justifyContent = enums.JustifyContent.CENTER, + alignItems = enums.AlignItems.CENTER, + }) + + -- Create a deeply nested window + local deepNestedWindow = Gui.new({ + parent = nestedWindow, + x = 0, + y = 0, + w = 75, + h = 50, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add children to deep nested window + local child1 = Gui.new({ + parent = deepNestedWindow, + x = 0, + y = 0, + w = 20, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = deepNestedWindow, + x = 0, + y = 0, + w = 30, + h = 40, + text = "Button 2", + }) + + -- Layout all children + parentWindow:layoutChildren() + + -- Verify nested structure and positioning + luaunit.assertEquals(nestedWindow.x, 0) + luaunit.assertEquals(nestedWindow.y, 0) + + luaunit.assertEquals(deepNestedWindow.x, 0) + luaunit.assertEquals(deepNestedWindow.y, 0) + + -- Verify that deep nested children are laid out correctly + luaunit.assertEquals(child1.x, 0) + luaunit.assertEquals(child1.y, (50 - 30) / 2) -- Should be centered vertically + + luaunit.assertEquals(child2.x, 20 + 10) -- Should be positioned after first child + gap + luaunit.assertEquals(child2.y, (50 - 40) / 2) -- Should be centered vertically +end + +function TestNestedLayouts:testNestedFlexWithDifferentDirections() + -- Create a parent with horizontal direction + local parentWindow = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Create a nested window with vertical direction + local childWindow = Gui.new({ + parent = parentWindow, + x = 0, + y = 0, + w = 150, + h = 100, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.VERTICAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Add children to nested window with different sizes + local child1 = Gui.new({ + parent = childWindow, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + local child2 = Gui.new({ + parent = childWindow, + x = 0, + y = 0, + w = 60, + h = 40, + text = "Button 2", + }) + + -- Layout all children + parentWindow:layoutChildren() + + -- Verify that the nested container properly layouts its children vertically + luaunit.assertEquals(child1.x, 0) -- Should be at left position + luaunit.assertEquals(child1.y, 0) -- Should be at top position + + luaunit.assertEquals(child2.x, 0) -- Should be at left position + luaunit.assertEquals(child2.y, 30 + 10) -- Should be positioned after first child + gap + + -- Verify that the parent container positions its children horizontally + luaunit.assertEquals(childWindow.x, 0) -- Should be at start of parent +end + +function TestNestedLayouts:testInheritanceOfPropertiesInNestedLayouts() + -- Create a parent with specific flex properties + local parentWindow = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.CENTER, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Create a nested window without explicit properties (should inherit) + local childWindow = Gui.new({ + parent = parentWindow, + x = 0, + y = 0, + w = 150, + h = 100, + positioning = enums.Positioning.FLEX, + }) + + -- Add children to nested window + local child1 = Gui.new({ + parent = childWindow, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + -- Layout all children + parentWindow:layoutChildren() + + -- Verify that nested window inherited properties from parent + luaunit.assertEquals(childWindow.flexDirection, enums.FlexDirection.HORIZONTAL) + luaunit.assertEquals(childWindow.justifyContent, enums.JustifyContent.CENTER) + luaunit.assertEquals(childWindow.alignItems, enums.AlignItems.STRETCH) +end + +function TestNestedLayouts:testAbsolutePositioningInNestedLayout() + -- Create a parent with flex direction + local parentWindow = Gui.new({ + x = 0, + y = 0, + w = 300, + h = 200, + positioning = enums.Positioning.FLEX, + flexDirection = enums.FlexDirection.HORIZONTAL, + justifyContent = enums.JustifyContent.FLEX_START, + alignItems = enums.AlignItems.STRETCH, + }) + + -- Create a nested window with absolute positioning + local childWindow = Gui.new({ + parent = parentWindow, + x = 50, + y = 50, + w = 150, + h = 100, + positioning = enums.Positioning.ABSOLUTE, + }) + + -- Add children to nested window + local child1 = Gui.new({ + parent = childWindow, + x = 0, + y = 0, + w = 50, + h = 30, + text = "Button 1", + }) + + -- Layout all children + parentWindow:layoutChildren() + + -- Verify that absolute positioned nested window maintains its position + luaunit.assertEquals(childWindow.x, 50) + luaunit.assertEquals(childWindow.y, 50) + + -- Verify that absolute positioning doesn't interfere with parent layout + luaunit.assertEquals(parentWindow.children[1], childWindow) +end + +-- Run the tests +os.exit(luaunit.LuaUnit.run()) +