-- Test relative positioning functionality package.path = package.path .. ";?.lua" require("testing/loveStub") local FlexLove = require("FlexLove") local luaunit = require("testing/luaunit") local Gui, enums = FlexLove.GUI, FlexLove.enums local Color = FlexLove.Color local Positioning = enums.Positioning TestRelativePositioning = {} -- Test 1: Basic relative positioning with pixel values function TestRelativePositioning.testBasicRelativePositioning() local parent = Gui.new({ x = 100, y = 50, w = 200, h = 150, positioning = "relative", background = Color.new(0.2, 0.2, 0.2, 1.0), }) local child = Gui.new({ parent = parent, x = 20, y = 30, w = 50, h = 40, positioning = "relative", background = Color.new(0.8, 0.2, 0.2, 1.0) }) -- Child should be positioned relative to parent luaunit.assertEquals(child.positioning, Positioning.RELATIVE) luaunit.assertEquals(child.x, 120) -- parent.x (100) + offset (20) luaunit.assertEquals(child.y, 80) -- parent.y (50) + offset (30) end -- Test 2: Relative positioning with percentage values function TestRelativePositioning.testRelativePositioningPercentages() local parent = Gui.new({ x = 50, y = 100, w = 200, h = 100, positioning = "relative", background = Color.new(0.2, 0.2, 0.2, 1.0), }) local child = Gui.new({ parent = parent, x = "10%", -- 10% of parent width = 20px y = "20%", -- 20% of parent height = 20px w = 30, h = 20, positioning = "relative", background = Color.new(0.8, 0.2, 0.2, 1.0) }) -- Child should be positioned relative to parent with percentage offsets luaunit.assertEquals(child.positioning, Positioning.RELATIVE) luaunit.assertEquals(child.x, 70.0) -- parent.x (50) + 10% of width (20) luaunit.assertEquals(child.y, 120.0) -- parent.y (100) + 20% of height (20) end -- Test 3: Relative positioning with no offset (default to parent position) function TestRelativePositioning.testRelativePositioningNoOffset() local parent = Gui.new({ x = 75, y = 125, w = 150, h = 200, positioning = "relative", background = Color.new(0.2, 0.2, 0.2, 1.0), }) local child = Gui.new({ parent = parent, w = 40, h = 30, positioning = "relative", background = Color.new(0.2, 0.8, 0.2, 1.0) }) -- Child should be positioned at parent's position with no offset luaunit.assertEquals(child.positioning, Positioning.RELATIVE) luaunit.assertEquals(child.x, 75) -- same as parent.x luaunit.assertEquals(child.y, 125) -- same as parent.y end -- Test 4: Multiple relative positioned children function TestRelativePositioning.testMultipleRelativeChildren() local parent = Gui.new({ x = 200, y = 300, w = 100, h = 100, positioning = "relative", background = Color.new(0.2, 0.2, 0.2, 1.0), }) local child1 = Gui.new({ parent = parent, x = 10, y = 15, w = 20, h = 20, positioning = "relative", background = Color.new(0.8, 0.2, 0.2, 1.0) }) local child2 = Gui.new({ parent = parent, x = 30, y = 45, w = 25, h = 25, positioning = "relative", background = Color.new(0.2, 0.8, 0.2, 1.0) }) -- Both children should be positioned relative to parent luaunit.assertEquals(child1.x, 210) -- parent.x (200) + offset (10) luaunit.assertEquals(child1.y, 315) -- parent.y (300) + offset (15) luaunit.assertEquals(child2.x, 230) -- parent.x (200) + offset (30) luaunit.assertEquals(child2.y, 345) -- parent.y (300) + offset (45) end -- Test 5: Nested relative positioning function TestRelativePositioning.testNestedRelativePositioning() local grandparent = Gui.new({ x = 50, y = 60, w = 300, h = 250, positioning = "relative", background = Color.new(0.1, 0.1, 0.1, 1.0), }) local parent = Gui.new({ parent = grandparent, x = 25, y = 35, w = 200, h = 150, positioning = "relative", background = Color.new(0.3, 0.3, 0.3, 1.0), }) local child = Gui.new({ parent = parent, x = 15, y = 20, w = 50, h = 40, positioning = "relative", background = Color.new(0.8, 0.8, 0.8, 1.0) }) -- Each level should be positioned relative to its parent luaunit.assertEquals(parent.x, 75) -- grandparent.x (50) + offset (25) luaunit.assertEquals(parent.y, 95) -- grandparent.y (60) + offset (35) luaunit.assertEquals(child.x, 90) -- parent.x (75) + offset (15) luaunit.assertEquals(child.y, 115) -- parent.y (95) + offset (20) end -- Test 6: Mixed positioning types (relative child in absolute parent) function TestRelativePositioning.testMixedPositioning() local parent = Gui.new({ x = 100, y = 200, w = 180, h = 120, positioning = "absolute", background = Color.new(0.2, 0.2, 0.2, 1.0), }) local child = Gui.new({ parent = parent, x = 40, y = 25, w = 60, h = 35, positioning = "relative", background = Color.new(0.8, 0.8, 0.2, 1.0) }) -- Relative child should still be positioned relative to absolute parent luaunit.assertEquals(parent.positioning, Positioning.ABSOLUTE) luaunit.assertEquals(child.positioning, Positioning.RELATIVE) luaunit.assertEquals(child.x, 140) -- parent.x (100) + offset (40) luaunit.assertEquals(child.y, 225) -- parent.y (200) + offset (25) end -- Run all tests os.exit(luaunit.LuaUnit.run())