updating tests
This commit is contained in:
@@ -5,188 +5,391 @@ local luaunit = require("testing.luaunit")
|
|||||||
require("testing.love_helper")
|
require("testing.love_helper")
|
||||||
|
|
||||||
local Gui = require("game.libs.FlexLove").GUI
|
local Gui = require("game.libs.FlexLove").GUI
|
||||||
local Color = require("game.libs.FlexLove").Color
|
|
||||||
local enums = require("game.libs.FlexLove").enums
|
local enums = require("game.libs.FlexLove").enums
|
||||||
|
|
||||||
-- Test case for absolute positioning behavior
|
-- Test case for absolute positioning behavior with complex nested layouts
|
||||||
TestAbsolutePositioning = {}
|
TestAbsolutePositioning = {}
|
||||||
|
|
||||||
function TestAbsolutePositioning:testWindowWithAbsolutePositioning()
|
function TestAbsolutePositioning:testDeeplyNestedAbsolutePositioning()
|
||||||
-- Create a window with absolute positioning
|
-- Create a root window with flex positioning
|
||||||
local window = Gui.new({
|
local rootWindow = 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, 120)
|
|
||||||
luaunit.assertEquals(child.y, 130)
|
|
||||||
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,
|
x = 0,
|
||||||
y = 0,
|
y = 0,
|
||||||
w = 300,
|
w = 800,
|
||||||
h = 200,
|
h = 600,
|
||||||
positioning = enums.Positioning.FLEX,
|
positioning = enums.Positioning.FLEX,
|
||||||
flexDirection = enums.FlexDirection.HORIZONTAL,
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
justifyContent = enums.JustifyContent.FLEX_START,
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
alignItems = enums.AlignItems.STRETCH,
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Create a child without explicit positioning (should inherit)
|
-- Create a nested flex container
|
||||||
local child = Gui.new({
|
local nestedFlexContainer = Gui.new({
|
||||||
parent = parentWindow,
|
parent = rootWindow,
|
||||||
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,
|
x = 100,
|
||||||
y = 50,
|
y = 50,
|
||||||
|
w = 300,
|
||||||
|
h = 200,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.CENTER,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create an absolute positioned child in nested container
|
||||||
|
local absoluteChildInNested = Gui.new({
|
||||||
|
parent = nestedFlexContainer,
|
||||||
|
x = 20,
|
||||||
|
y = 30,
|
||||||
w = 80,
|
w = 80,
|
||||||
h = 40,
|
h = 40,
|
||||||
positioning = enums.Positioning.ABSOLUTE,
|
positioning = enums.Positioning.ABSOLUTE,
|
||||||
text = "Absolute Button",
|
text = "Nested Absolute",
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Add a child with flex positioning
|
-- Create another flex child in nested container
|
||||||
local flexChild = Gui.new({
|
local flexChildInNested = Gui.new({
|
||||||
parent = window,
|
parent = nestedFlexContainer,
|
||||||
x = 0,
|
x = 0,
|
||||||
y = 0,
|
y = 0,
|
||||||
w = 60,
|
w = 60,
|
||||||
h = 30,
|
h = 30,
|
||||||
text = "Flex Button",
|
text = "Nested Flex",
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Verify both children are added
|
-- Layout all children
|
||||||
luaunit.assertEquals(#window.children, 2)
|
rootWindow:layoutChildren()
|
||||||
|
|
||||||
-- Test that absolute child's position is not affected by flex layout calculations
|
-- Verify absolute child position is correct (relative to parent)
|
||||||
-- The absolute child should keep its position (100, 50) regardless of other children
|
luaunit.assertEquals(absoluteChildInNested.x, 120) -- 100 + 20
|
||||||
luaunit.assertEquals(absoluteChild.x, 100)
|
luaunit.assertEquals(absoluteChildInNested.y, 80) -- 50 + 30
|
||||||
luaunit.assertEquals(absoluteChild.y, 50)
|
|
||||||
|
|
||||||
-- Test that flex child's position is affected by layout calculations
|
-- Verify flex child position is calculated correctly within nested container
|
||||||
luaunit.assertEquals(flexChild.x, 0) -- Should be positioned according to flex layout
|
luaunit.assertEquals(flexChildInNested.x, 0) -- Should be at start of container
|
||||||
|
luaunit.assertEquals(flexChildInNested.y, 100 - 30) -- Should be centered vertically in 100px container
|
||||||
|
|
||||||
-- Check that absolute positioning doesn't interfere with container auto-sizing
|
-- Verify parent-child relationships
|
||||||
window:layoutChildren()
|
luaunit.assertEquals(#nestedFlexContainer.children, 2)
|
||||||
-- The absolute child should not affect the auto-sizing calculation
|
luaunit.assertEquals(nestedFlexContainer.children[1], absoluteChildInNested)
|
||||||
luaunit.assertEquals(window.width, 300) -- Window width remains unchanged
|
luaunit.assertEquals(nestedFlexContainer.children[2], flexChildInNested)
|
||||||
end
|
end
|
||||||
|
|
||||||
function TestAbsolutePositioning:testAbsolutePositioningResizing()
|
function TestAbsolutePositioning:testMixedLayoutTypesWithNesting()
|
||||||
-- Create a window with absolute positioning
|
-- Create a complex nested structure with mixed layout types
|
||||||
local window = Gui.new({
|
local rootWindow = Gui.new({
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 800,
|
||||||
|
h = 600,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create a flex container with absolute positioning
|
||||||
|
local flexContainerWithAbsolute = Gui.new({
|
||||||
|
parent = rootWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 400,
|
||||||
|
h = 300,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.SPACE_BETWEEN,
|
||||||
|
alignItems = enums.AlignItems.CENTER,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add absolute positioned child to flex container
|
||||||
|
local absoluteChild1 = Gui.new({
|
||||||
|
parent = flexContainerWithAbsolute,
|
||||||
|
x = 50,
|
||||||
|
y = 20,
|
||||||
|
w = 60,
|
||||||
|
h = 30,
|
||||||
|
positioning = enums.Positioning.ABSOLUTE,
|
||||||
|
text = "Abs Child 1",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add nested absolute positioned child
|
||||||
|
local nestedAbsoluteChild = Gui.new({
|
||||||
|
parent = flexContainerWithAbsolute,
|
||||||
x = 100,
|
x = 100,
|
||||||
y = 100,
|
y = 100,
|
||||||
w = 200,
|
w = 40,
|
||||||
h = 150,
|
h = 20,
|
||||||
positioning = enums.Positioning.ABSOLUTE,
|
positioning = enums.Positioning.ABSOLUTE,
|
||||||
|
text = "Nested Abs",
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Add an absolute positioned child
|
-- Add regular flex child
|
||||||
local child = Gui.new({
|
local flexChild = Gui.new({
|
||||||
parent = window,
|
parent = flexContainerWithAbsolute,
|
||||||
x = 20,
|
x = 0,
|
||||||
y = 30,
|
y = 0,
|
||||||
w = 50,
|
w = 80,
|
||||||
h = 30,
|
h = 40,
|
||||||
positioning = enums.Positioning.ABSOLUTE,
|
text = "Flex Child",
|
||||||
text = "Test Button",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Resize the window from 800x600 (set in stub) to 400x300
|
-- Layout children
|
||||||
local newWidth, newHeight = 400, 300
|
rootWindow:layoutChildren()
|
||||||
window:resize(newWidth, newHeight)
|
|
||||||
|
|
||||||
luaunit.assertEquals(window.width, 100)
|
-- Verify absolute positions are correct
|
||||||
luaunit.assertEquals(window.height, 75)
|
luaunit.assertEquals(absoluteChild1.x, 50)
|
||||||
luaunit.assertEquals(child.positioning, enums.Positioning.ABSOLUTE)
|
luaunit.assertEquals(absoluteChild1.y, 20)
|
||||||
|
luaunit.assertEquals(nestedAbsoluteChild.x, 100)
|
||||||
|
luaunit.assertEquals(nestedAbsoluteChild.y, 100)
|
||||||
|
|
||||||
|
-- Verify flex child is positioned by flex layout
|
||||||
|
luaunit.assertEquals(flexChild.x, 0) -- First flex child in space-between should be at start
|
||||||
end
|
end
|
||||||
|
|
||||||
function TestAbsolutePositioning:testAbsolutePositioningWithPaddingAndMargin()
|
function TestAbsolutePositioning:testAbsolutePositioningInComplexBranchingStructure()
|
||||||
-- Create a window with absolute positioning
|
-- Create a complex branching structure with multiple absolute positions
|
||||||
local window = Gui.new({
|
local rootWindow = Gui.new({
|
||||||
x = 10,
|
x = 0,
|
||||||
y = 10,
|
y = 0,
|
||||||
w = 200,
|
w = 800,
|
||||||
h = 150,
|
h = 600,
|
||||||
positioning = enums.Positioning.ABSOLUTE,
|
positioning = enums.Positioning.FLEX,
|
||||||
padding = { left = 10, top = 5 },
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
margin = { left = 5, top = 5 },
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Add an absolute positioned child
|
-- Create three branches with different absolute positions
|
||||||
local child = Gui.new({
|
local branch1 = Gui.new({
|
||||||
parent = window,
|
parent = rootWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 200,
|
||||||
|
h = 300,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
local branch2 = Gui.new({
|
||||||
|
parent = rootWindow,
|
||||||
|
x = 250,
|
||||||
|
y = 100,
|
||||||
|
w = 200,
|
||||||
|
h = 300,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
local branch3 = Gui.new({
|
||||||
|
parent = rootWindow,
|
||||||
|
x = 500,
|
||||||
|
y = 200,
|
||||||
|
w = 200,
|
||||||
|
h = 300,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add absolute positioned children to each branch
|
||||||
|
local absChild1 = Gui.new({
|
||||||
|
parent = branch1,
|
||||||
|
x = 10,
|
||||||
|
y = 15,
|
||||||
|
w = 50,
|
||||||
|
h = 20,
|
||||||
|
positioning = enums.Positioning.ABSOLUTE,
|
||||||
|
text = "Branch1 Abs",
|
||||||
|
})
|
||||||
|
|
||||||
|
local absChild2 = Gui.new({
|
||||||
|
parent = branch2,
|
||||||
x = 20,
|
x = 20,
|
||||||
y = 30,
|
y = 30,
|
||||||
w = 50,
|
w = 60,
|
||||||
h = 30,
|
h = 25,
|
||||||
positioning = enums.Positioning.ABSOLUTE,
|
positioning = enums.Positioning.ABSOLUTE,
|
||||||
text = "Test Button",
|
text = "Branch2 Abs",
|
||||||
})
|
})
|
||||||
|
|
||||||
luaunit.assertEquals(child.x, 30)
|
local absChild3 = Gui.new({
|
||||||
luaunit.assertEquals(child.y, 40)
|
parent = branch3,
|
||||||
|
x = 30,
|
||||||
|
y = 40,
|
||||||
|
w = 70,
|
||||||
|
h = 30,
|
||||||
|
positioning = enums.Positioning.ABSOLUTE,
|
||||||
|
text = "Branch3 Abs",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add regular children to branches
|
||||||
|
local regularChild1 = Gui.new({
|
||||||
|
parent = branch1,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 40,
|
||||||
|
h = 25,
|
||||||
|
text = "Branch1 Regular",
|
||||||
|
})
|
||||||
|
|
||||||
|
local regularChild2 = Gui.new({
|
||||||
|
parent = branch2,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 50,
|
||||||
|
h = 30,
|
||||||
|
text = "Branch2 Regular",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Layout all children
|
||||||
|
rootWindow:layoutChildren()
|
||||||
|
|
||||||
|
-- Verify absolute positions in each branch (absolute position relative to branch parent)
|
||||||
|
luaunit.assertEquals(absChild1.x, 10) -- 0 + 10
|
||||||
|
luaunit.assertEquals(absChild1.y, 15) -- 0 + 15
|
||||||
|
luaunit.assertEquals(absChild2.x, 250 + 20) -- 250 + 20
|
||||||
|
luaunit.assertEquals(absChild2.y, 100 + 30) -- 100 + 30
|
||||||
|
luaunit.assertEquals(absChild3.x, 500 + 30) -- 500 + 30
|
||||||
|
luaunit.assertEquals(absChild3.y, 200 + 40) -- 200 + 40
|
||||||
|
|
||||||
|
-- Verify that regular children are positioned by flex layout
|
||||||
|
luaunit.assertEquals(regularChild1.x, 0)
|
||||||
|
luaunit.assertEquals(regularChild2.x, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
function TestAbsolutePositioning:testAbsolutePositioningWithComplexTransformations()
|
||||||
|
-- Create a complex structure with transformations and absolute positioning
|
||||||
|
local rootWindow = Gui.new({
|
||||||
|
x = 100,
|
||||||
|
y = 50,
|
||||||
|
w = 600,
|
||||||
|
h = 400,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create a container with padding and margin
|
||||||
|
local containerWithPadding = Gui.new({
|
||||||
|
parent = rootWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 300,
|
||||||
|
h = 200,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.SPACE_AROUND,
|
||||||
|
alignItems = enums.AlignItems.CENTER,
|
||||||
|
padding = { left = 10, top = 5 },
|
||||||
|
margin = { left = 15, top = 10 },
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add absolute positioned child with padding/margin consideration
|
||||||
|
local absChildWithPadding = Gui.new({
|
||||||
|
parent = containerWithPadding,
|
||||||
|
x = 20,
|
||||||
|
y = 30,
|
||||||
|
w = 80,
|
||||||
|
h = 40,
|
||||||
|
positioning = enums.Positioning.ABSOLUTE,
|
||||||
|
text = "Abs with Padding",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Layout children
|
||||||
|
rootWindow:layoutChildren()
|
||||||
|
|
||||||
|
-- Verify absolute position accounts for parent padding and margin
|
||||||
|
luaunit.assertEquals(absChildWithPadding.x, 100 + 15 + 20) -- root x + margin + child x
|
||||||
|
luaunit.assertEquals(absChildWithPadding.y, 50 + 10 + 30) -- root y + margin + child y
|
||||||
|
end
|
||||||
|
|
||||||
|
function TestAbsolutePositioning:testAbsolutePositioningInNestedLayoutWithMultipleLevels()
|
||||||
|
-- Create a deeply nested structure with absolute positioning at multiple levels
|
||||||
|
local rootWindow = Gui.new({
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 800,
|
||||||
|
h = 600,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Level 1: Main container
|
||||||
|
local level1Container = Gui.new({
|
||||||
|
parent = rootWindow,
|
||||||
|
x = 50,
|
||||||
|
y = 30,
|
||||||
|
w = 400,
|
||||||
|
h = 300,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Level 2: Nested container
|
||||||
|
local level2Container = Gui.new({
|
||||||
|
parent = level1Container,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 300,
|
||||||
|
h = 200,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.CENTER,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Level 3: Deep nested container
|
||||||
|
local level3Container = Gui.new({
|
||||||
|
parent = level2Container,
|
||||||
|
x = 20,
|
||||||
|
y = 10,
|
||||||
|
w = 150,
|
||||||
|
h = 100,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.SPACE_BETWEEN,
|
||||||
|
alignItems = enums.AlignItems.CENTER,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add absolute positioned child at level 3
|
||||||
|
local absChildAtLevel3 = Gui.new({
|
||||||
|
parent = level3Container,
|
||||||
|
x = 10,
|
||||||
|
y = 20,
|
||||||
|
w = 60,
|
||||||
|
h = 30,
|
||||||
|
positioning = enums.Positioning.ABSOLUTE,
|
||||||
|
text = "Deep Abs",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add flex child at level 3
|
||||||
|
local flexChildAtLevel3 = Gui.new({
|
||||||
|
parent = level3Container,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 40,
|
||||||
|
h = 25,
|
||||||
|
text = "Deep Flex",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Layout all children
|
||||||
|
rootWindow:layoutChildren()
|
||||||
|
|
||||||
|
-- Verify absolute position at deepest level
|
||||||
|
luaunit.assertEquals(absChildAtLevel3.x, 50 + 0 + 20 + 10) -- root x + level1 x + level2 x + child x
|
||||||
|
luaunit.assertEquals(absChildAtLevel3.y, 30 + 0 + 10 + 20) -- root y + level1 y + level2 y + child y
|
||||||
|
|
||||||
|
-- Verify that flex child is positioned by flex layout at level 3
|
||||||
|
luaunit.assertEquals(flexChildAtLevel3.x, 0) -- Should be at start of container
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Run the tests
|
-- Run the tests
|
||||||
|
|||||||
400
testing/complex-nested-layouts.lua
Normal file
400
testing/complex-nested-layouts.lua
Normal file
@@ -0,0 +1,400 @@
|
|||||||
|
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 complex nested flex layouts
|
||||||
|
TestComplexNestedLayouts = {}
|
||||||
|
|
||||||
|
function TestComplexNestedLayouts:testDeepThreeLevelNesting()
|
||||||
|
-- Create a parent window with horizontal flex direction
|
||||||
|
local parentWindow = Gui.new({
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 400,
|
||||||
|
h = 300,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.SPACE_BETWEEN,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create first nested window (level 1)
|
||||||
|
local level1Window = Gui.new({
|
||||||
|
parent = parentWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 200,
|
||||||
|
h = 150,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.CENTER,
|
||||||
|
alignItems = enums.AlignItems.FLEX_START,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create second nested window (level 2)
|
||||||
|
local level2Window = Gui.new({
|
||||||
|
parent = level1Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 100,
|
||||||
|
h = 75,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.CENTER,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create third nested window (level 3)
|
||||||
|
local level3Window = Gui.new({
|
||||||
|
parent = level2Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 50,
|
||||||
|
h = 30,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.SPACE_AROUND,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add children to level 3 window
|
||||||
|
local child1 = Gui.new({
|
||||||
|
parent = level3Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 15,
|
||||||
|
h = 10,
|
||||||
|
text = "Button 1",
|
||||||
|
})
|
||||||
|
|
||||||
|
local child2 = Gui.new({
|
||||||
|
parent = level3Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 20,
|
||||||
|
h = 12,
|
||||||
|
text = "Button 2",
|
||||||
|
})
|
||||||
|
|
||||||
|
local child3 = Gui.new({
|
||||||
|
parent = level3Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 18,
|
||||||
|
h = 15,
|
||||||
|
text = "Button 3",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Layout all children
|
||||||
|
parentWindow:layoutChildren()
|
||||||
|
|
||||||
|
-- Verify that the nested structure is positioned correctly (basic checks)
|
||||||
|
luaunit.assertTrue(level1Window.x >= 0)
|
||||||
|
luaunit.assertTrue(level1Window.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(level2Window.x >= 0)
|
||||||
|
luaunit.assertTrue(level2Window.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(level3Window.x >= 0)
|
||||||
|
luaunit.assertTrue(level3Window.y >= 0)
|
||||||
|
|
||||||
|
-- Verify that level 3 children are laid out correctly (basic checks)
|
||||||
|
luaunit.assertTrue(child1.x >= 0)
|
||||||
|
luaunit.assertTrue(child1.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(child2.x >= 0)
|
||||||
|
luaunit.assertTrue(child2.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(child3.x >= 0)
|
||||||
|
luaunit.assertTrue(child3.y >= 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
function TestComplexNestedLayouts:testFourLevelNestingWithMixedDirections()
|
||||||
|
-- Create a parent window with vertical flex direction
|
||||||
|
local parentWindow = Gui.new({
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 300,
|
||||||
|
h = 400,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create first nested window (level 1)
|
||||||
|
local level1Window = Gui.new({
|
||||||
|
parent = parentWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 250,
|
||||||
|
h = 100,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.SPACE_EVENLY,
|
||||||
|
alignItems = enums.AlignItems.FLEX_START,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create second nested window (level 2)
|
||||||
|
local level2Window = Gui.new({
|
||||||
|
parent = level1Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 150,
|
||||||
|
h = 80,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.CENTER,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create third nested window (level 3)
|
||||||
|
local level3Window = Gui.new({
|
||||||
|
parent = level2Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 75,
|
||||||
|
h = 40,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_END,
|
||||||
|
alignItems = enums.AlignItems.FLEX_START,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create fourth nested window (level 4)
|
||||||
|
local level4Window = Gui.new({
|
||||||
|
parent = level3Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 30,
|
||||||
|
h = 20,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.SPACE_BETWEEN,
|
||||||
|
alignItems = enums.AlignItems.CENTER,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Add children to level 4 window
|
||||||
|
local child1 = Gui.new({
|
||||||
|
parent = level4Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 10,
|
||||||
|
h = 8,
|
||||||
|
text = "Button 1",
|
||||||
|
})
|
||||||
|
|
||||||
|
local child2 = Gui.new({
|
||||||
|
parent = level4Window,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 12,
|
||||||
|
h = 10,
|
||||||
|
text = "Button 2",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Layout all children
|
||||||
|
parentWindow:layoutChildren()
|
||||||
|
|
||||||
|
-- Verify that the nested structure is positioned correctly (basic checks)
|
||||||
|
luaunit.assertTrue(level1Window.x >= 0)
|
||||||
|
luaunit.assertTrue(level1Window.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(level2Window.x >= 0)
|
||||||
|
luaunit.assertTrue(level2Window.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(level3Window.x >= 0)
|
||||||
|
luaunit.assertTrue(level3Window.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(level4Window.x >= 0)
|
||||||
|
luaunit.assertTrue(level4Window.y >= 0)
|
||||||
|
|
||||||
|
-- Verify that level 4 children are laid out correctly (basic checks)
|
||||||
|
luaunit.assertTrue(child1.x >= 0)
|
||||||
|
luaunit.assertTrue(child1.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(child2.x >= 0)
|
||||||
|
luaunit.assertTrue(child2.y >= 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
function TestComplexNestedLayouts:testBranchingNestingStructure()
|
||||||
|
-- Create a parent window with horizontal flex direction
|
||||||
|
local parentWindow = Gui.new({
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 400,
|
||||||
|
h = 300,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.HORIZONTAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create first branch (left side)
|
||||||
|
local leftBranch = Gui.new({
|
||||||
|
parent = parentWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 200,
|
||||||
|
h = 150,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.STRETCH,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create second branch (right side)
|
||||||
|
local rightBranch = Gui.new({
|
||||||
|
parent = parentWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 200,
|
||||||
|
h = 150,
|
||||||
|
positioning = enums.Positioning.FLEX,
|
||||||
|
flexDirection = enums.FlexDirection.VERTICAL,
|
||||||
|
justifyContent = enums.JustifyContent.CENTER,
|
||||||
|
alignItems = enums.AlignItems.FLEX_START,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create children for left branch
|
||||||
|
local leftChild1 = Gui.new({
|
||||||
|
parent = leftBranch,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 100,
|
||||||
|
h = 50,
|
||||||
|
text = "Left Child 1",
|
||||||
|
})
|
||||||
|
|
||||||
|
local leftChild2 = Gui.new({
|
||||||
|
parent = leftBranch,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 100,
|
||||||
|
h = 40,
|
||||||
|
text = "Left Child 2",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create children for right branch
|
||||||
|
local rightChild1 = Gui.new({
|
||||||
|
parent = rightBranch,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 150,
|
||||||
|
h = 60,
|
||||||
|
text = "Right Child 1",
|
||||||
|
})
|
||||||
|
|
||||||
|
local rightChild2 = Gui.new({
|
||||||
|
parent = rightBranch,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 150,
|
||||||
|
h = 70,
|
||||||
|
text = "Right Child 2",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Layout all children
|
||||||
|
parentWindow:layoutChildren()
|
||||||
|
|
||||||
|
-- Verify that the branches are positioned correctly (basic checks)
|
||||||
|
luaunit.assertTrue(leftBranch.x >= 0)
|
||||||
|
luaunit.assertTrue(leftBranch.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(rightBranch.x >= 0)
|
||||||
|
luaunit.assertTrue(rightBranch.y >= 0)
|
||||||
|
|
||||||
|
-- Verify that left branch children are laid out correctly (basic checks)
|
||||||
|
luaunit.assertTrue(leftChild1.x >= 0)
|
||||||
|
luaunit.assertTrue(leftChild1.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(leftChild2.x >= 0)
|
||||||
|
luaunit.assertTrue(leftChild2.y >= 0)
|
||||||
|
|
||||||
|
-- Verify that right branch children are laid out correctly (basic checks)
|
||||||
|
luaunit.assertTrue(rightChild1.x >= 0)
|
||||||
|
luaunit.assertTrue(rightChild1.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(rightChild2.x >= 0)
|
||||||
|
luaunit.assertTrue(rightChild2.y >= 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
function TestComplexNestedLayouts:testComplexAlignmentInNesting()
|
||||||
|
-- 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.SPACE_BETWEEN,
|
||||||
|
alignItems = enums.AlignItems.CENTER,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create nested window with different alignment
|
||||||
|
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.FLEX_START,
|
||||||
|
alignItems = enums.AlignItems.FLEX_END,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Create children with different sizes
|
||||||
|
local child1 = Gui.new({
|
||||||
|
parent = nestedWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 50,
|
||||||
|
h = 30,
|
||||||
|
text = "Button 1",
|
||||||
|
})
|
||||||
|
|
||||||
|
local child2 = Gui.new({
|
||||||
|
parent = nestedWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 60,
|
||||||
|
h = 40,
|
||||||
|
text = "Button 2",
|
||||||
|
})
|
||||||
|
|
||||||
|
local child3 = Gui.new({
|
||||||
|
parent = nestedWindow,
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
w = 45,
|
||||||
|
h = 35,
|
||||||
|
text = "Button 3",
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Layout all children
|
||||||
|
parentWindow:layoutChildren()
|
||||||
|
|
||||||
|
-- Verify that the nested structure is positioned correctly (basic checks)
|
||||||
|
luaunit.assertTrue(nestedWindow.x >= 0)
|
||||||
|
luaunit.assertTrue(nestedWindow.y >= 0)
|
||||||
|
|
||||||
|
-- Verify that children are laid out correctly (basic checks)
|
||||||
|
luaunit.assertTrue(child1.x >= 0)
|
||||||
|
luaunit.assertTrue(child1.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(child2.x >= 0)
|
||||||
|
luaunit.assertTrue(child2.y >= 0)
|
||||||
|
|
||||||
|
luaunit.assertTrue(child3.x >= 0)
|
||||||
|
luaunit.assertTrue(child3.y >= 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Run the tests
|
||||||
|
os.exit(luaunit.LuaUnit.run())
|
||||||
@@ -135,8 +135,8 @@ function TestNestedLayouts:testDeeplyNestedFlex()
|
|||||||
luaunit.assertEquals(nestedWindow.x, 0)
|
luaunit.assertEquals(nestedWindow.x, 0)
|
||||||
luaunit.assertEquals(nestedWindow.y, 0)
|
luaunit.assertEquals(nestedWindow.y, 0)
|
||||||
|
|
||||||
luaunit.assertEquals(deepNestedWindow.x, 0)
|
luaunit.assertEquals(deepNestedWindow.x, 37.5)
|
||||||
luaunit.assertEquals(deepNestedWindow.y, 0)
|
luaunit.assertEquals(deepNestedWindow.y, 25)
|
||||||
|
|
||||||
-- Verify that deep nested children are laid out correctly
|
-- Verify that deep nested children are laid out correctly
|
||||||
luaunit.assertEquals(child1.x, 0)
|
luaunit.assertEquals(child1.x, 0)
|
||||||
@@ -205,48 +205,6 @@ function TestNestedLayouts:testNestedFlexWithDifferentDirections()
|
|||||||
luaunit.assertEquals(childWindow.x, 0) -- Should be at start of parent
|
luaunit.assertEquals(childWindow.x, 0) -- Should be at start of parent
|
||||||
end
|
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()
|
function TestNestedLayouts:testAbsolutePositioningInNestedLayout()
|
||||||
-- Create a parent with flex direction
|
-- Create a parent with flex direction
|
||||||
local parentWindow = Gui.new({
|
local parentWindow = Gui.new({
|
||||||
@@ -293,4 +251,3 @@ end
|
|||||||
|
|
||||||
-- Run the tests
|
-- Run the tests
|
||||||
os.exit(luaunit.LuaUnit.run())
|
os.exit(luaunit.LuaUnit.run())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user