zntg/ui/spacing_test.go
Sander Schobers 3ea0d76efb Created unified spacing control.
Build control methods now accept nil as visitor method.
2019-03-12 21:11:43 +01:00

63 lines
1.4 KiB
Go

package ui
import (
"testing"
"github.com/stretchr/testify/assert"
"opslag.de/schobers/geom"
)
var _ Control = &Mock{}
type Mock struct {
ControlBase
Size *geom.PointF32
}
func (m *Mock) DesiredSize(ctx Context) geom.PointF32 {
if m.Size != nil {
return *m.Size
}
return m.ControlBase.DesiredSize(ctx)
}
func TestNoStretchFillsAvailableSpace(t *testing.T) {
m := &Mock{}
s := BuildSpacing(m, func(s *Spacing) {
s.SetSize(0, 0)
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32)
assert.Equal(t, geom.RectF32(31, 37, 73, 79), m.bounds)
}
func TestStretch(t *testing.T) {
m := &Mock{}
s := BuildSpacing(m, func(s *Spacing) {
s.SetSize(geom.NaN32(), geom.NaN32())
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32)
assert.Equal(t, geom.RectF32(31, 37, 73, 79), m.bounds)
}
func TestCenter(t *testing.T) {
m := &Mock{Size: &geom.PointF32{X: 2, Y: 3}}
s := BuildSpacing(m, func(s *Spacing) {
s.Center()
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32)
assert.Equal(t, geom.RectF32(51, 56.5, 53, 59.5), m.bounds)
}
func TestFixedMargin(t *testing.T) {
m := &Mock{Size: &geom.PointF32{X: geom.NaN32(), Y: geom.NaN32()}}
s := BuildSpacing(m, func(s *Spacing) {
s.Margin.Left = Fixed(7)
s.Margin.Top = Fixed(2)
s.Margin.Right = Fixed(3)
s.Margin.Bottom = Fixed(5)
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32)
assert.Equal(t, geom.RectF32(38, 39, 70, 74), m.bounds)
}