summaryrefslogtreecommitdiffstats
path: root/pkg/gui/boxlayout
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2020-08-15 08:58:58 +1000
committerJesse Duffield <jessedduffield@gmail.com>2020-08-15 09:04:40 +1000
commitf02ccca0e00a2e249afaf6687d60f58e5a130433 (patch)
tree87860931a159a6834e44b0297beac626b27e3d77 /pkg/gui/boxlayout
parent1e12a60b346f373b53edf5aa3721f9b5e6ee574f (diff)
add specs to boxlayout package
Diffstat (limited to 'pkg/gui/boxlayout')
-rw-r--r--pkg/gui/boxlayout/boxlayout.go36
-rw-r--r--pkg/gui/boxlayout/boxlayout_test.go189
2 files changed, 207 insertions, 18 deletions
diff --git a/pkg/gui/boxlayout/boxlayout.go b/pkg/gui/boxlayout/boxlayout.go
index 383a12ffd..f4bd8310e 100644
--- a/pkg/gui/boxlayout/boxlayout.go
+++ b/pkg/gui/boxlayout/boxlayout.go
@@ -47,24 +47,6 @@ type Box struct {
Weight int
}
-func (b *Box) isStatic() bool {
- return b.Size > 0
-}
-
-func (b *Box) getDirection(width int, height int) int {
- if b.ConditionalDirection != nil {
- return b.ConditionalDirection(width, height)
- }
- return b.Direction
-}
-
-func (b *Box) getChildren(width int, height int) []*Box {
- if b.ConditionalChildren != nil {
- return b.ConditionalChildren(width, height)
- }
- return b.Children
-}
-
func ArrangeViews(root *Box, x0, y0, width, height int) map[string]Dimensions {
children := root.getChildren(width, height)
if len(children) == 0 {
@@ -134,6 +116,24 @@ func ArrangeViews(root *Box, x0, y0, width, height int) map[string]Dimensions {
return result
}
+func (b *Box) isStatic() bool {
+ return b.Size > 0
+}
+
+func (b *Box) getDirection(width int, height int) int {
+ if b.ConditionalDirection != nil {
+ return b.ConditionalDirection(width, height)
+ }
+ return b.Direction
+}
+
+func (b *Box) getChildren(width int, height int) []*Box {
+ if b.ConditionalChildren != nil {
+ return b.ConditionalChildren(width, height)
+ }
+ return b.Children
+}
+
func mergeDimensionMaps(a map[string]Dimensions, b map[string]Dimensions) map[string]Dimensions {
result := map[string]Dimensions{}
for _, dimensionMap := range []map[string]Dimensions{a, b} {
diff --git a/pkg/gui/boxlayout/boxlayout_test.go b/pkg/gui/boxlayout/boxlayout_test.go
new file mode 100644
index 000000000..05d97ca9d
--- /dev/null
+++ b/pkg/gui/boxlayout/boxlayout_test.go
@@ -0,0 +1,189 @@
+package boxlayout
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestArrangeViews(t *testing.T) {
+ type scenario struct {
+ testName string
+ root *Box
+ x0 int
+ y0 int
+ width int
+ height int
+ test func(result map[string]Dimensions)
+ }
+
+ scenarios := []scenario{
+ {
+ "Empty box",
+ &Box{},
+ 0,
+ 0,
+ 10,
+ 10,
+ func(result map[string]Dimensions) {
+ assert.EqualValues(t, result, map[string]Dimensions{})
+ },
+ },
+ {
+ "Box with static and dynamic panel",
+ &Box{Children: []*Box{{Size: 1, ViewName: "static"}, {Weight: 1, ViewName: "dynamic"}}},
+ 0,
+ 0,
+ 10,
+ 10,
+ func(result map[string]Dimensions) {
+ assert.EqualValues(
+ t,
+ result,
+ map[string]Dimensions{
+ "dynamic": {X0: 0, X1: 9, Y0: 1, Y1: 9},
+ "static": {X0: 0, X1: 9, Y0: 0, Y1: 0},
+ },
+ )
+ },
+ },
+ {
+ "Box with static and two dynamic panels",
+ &Box{Children: []*Box{{Size: 1, ViewName: "static"}, {Weight: 1, ViewName: "dynamic1"}, {Weight: 2, ViewName: "dynamic2"}}},
+ 0,
+ 0,
+ 10,
+ 10,
+ func(result map[string]Dimensions) {
+ assert.EqualValues(
+ t,
+ result,
+ map[string]Dimensions{
+ "static": {X0: 0, X1: 9, Y0: 0, Y1: 0},
+ "dynamic1": {X0: 0, X1: 9, Y0: 1, Y1: 3},
+ "dynamic2": {X0: 0, X1: 9, Y0: 4, Y1: 9},
+ },
+ )
+ },
+ },
+ {
+ "Box with COLUMN direction",
+ &Box{Direction: COLUMN, Children: []*Box{{Size: 1, ViewName: "static"}, {Weight: 1, ViewName: "dynamic1"}, {Weight: 2, ViewName: "dynamic2"}}},
+ 0,
+ 0,
+ 10,
+ 10,
+ func(result map[string]Dimensions) {
+ assert.EqualValues(
+ t,
+ result,
+ map[string]Dimensions{
+ "static": {X0: 0, X1: 0, Y0: 0, Y1: 9},
+ "dynamic1": {X0: 1, X1: 3, Y0: 0, Y1: 9},
+ "dynamic2": {X0: 4, X1: 9, Y0: 0, Y1: 9},
+ },
+ )
+ },
+ },
+ {
+ "Box with COLUMN direction only on wide boxes with narrow box",
+ &Box{ConditionalDirection: func(width int, height int) int {
+ if width > 4 {
+ return COLUMN
+ } else {
+ return ROW
+ }
+ }, Children: []*Box{{Weight: 1, ViewName: "dynamic1"}, {Weight: 1, ViewName: "dynamic2"}}},
+ 0,
+ 0,
+ 4,
+ 4,
+ func(result map[string]Dimensions) {
+ assert.EqualValues(
+ t,
+ result,
+ map[string]Dimensions{
+ "dynamic1": {X0: 0, X1: 3, Y0: 0, Y1: 1},
+ "dynamic2": {X0: 0, X1: 3, Y0: 2, Y1: 3},
+ },
+ )
+ },
+ },
+ {
+ "Box with COLUMN direction only on wide boxes with wide box",
+ &Box{ConditionalDirection: func(width int, height int) int {
+ if width > 4 {
+ return COLUMN
+ } else {
+ return ROW
+ }
+ }, Children: []*Box{{Weight: 1, ViewName: "dynamic1"}, {Weight: 1, ViewName: "dynamic2"}}},
+ 0,
+ 0,
+ 5,
+ 5,
+ func(result map[string]Dimensions) {
+ assert.EqualValues(
+ t,
+ result,
+ map[string]Dimensions{
+ "dynamic1": {X0: 0, X1: 2, Y0: 0, Y1: 4},
+ "dynamic2": {X0: 3, X1: 4, Y0: 0, Y1: 4},
+ },
+ )
+ },
+ },
+ {
+ "Box with conditional children where box is wide",
+ &Box{ConditionalChildren: func(width int, height int) []*Box {
+ if width > 4 {
+ return []*Box{{ViewName: "wide", Weight: 1}}
+ } else {
+ return []*Box{{ViewName: "narrow", Weight: 1}}
+ }
+ }},
+ 0,
+ 0,
+ 5,
+ 5,
+ func(result map[string]Dimensions) {
+ assert.EqualValues(
+ t,
+ result,
+ map[string]Dimensions{
+ "wide": {X0: 0, X1: 4, Y0: 0, Y1: 4},
+ },
+ )
+ },
+ },
+ {
+ "Box with conditional children where box is narrow",
+ &Box{ConditionalChildren: func(width int, height int) []*Box {
+ if width > 4 {
+ return []*Box{{ViewName: "wide", Weight: 1}}
+ } else {
+ return []*Box{{ViewName: "narrow", Weight: 1}}
+ }
+ }},
+ 0,
+ 0,
+ 4,
+ 4,
+ func(result map[string]Dimensions) {
+ assert.EqualValues(
+ t,
+ result,
+ map[string]Dimensions{
+ "narrow": {X0: 0, X1: 3, Y0: 0, Y1: 3},
+ },
+ )
+ },
+ },
+ }
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ s.test(ArrangeViews(s.root, s.x0, s.y0, s.width, s.height))
+ })
+ }
+}