1package ui
2
3import (
4 "testing"
5
6 "github.com/bnema/vev/internal/domain"
7)
8
9func TestPlaceAnchors(t *testing.T) {
10 base := domain.Size{Cols: 31, Rows: 19}
11 content := domain.Size{Cols: 8, Rows: 6}
12 margins := Margins{Top: 2, Right: 3, Bottom: 4, Left: 5}
13
14 tests := []struct {
15 anchor domain.Anchor
16 want domain.Rect
17 }{
18 {domain.AnchorTopLeft, domain.Rect{X: 5, Y: 2, Width: 8, Height: 6}},
19 {domain.AnchorTop, domain.Rect{X: 11, Y: 2, Width: 8, Height: 6}},
20 {domain.AnchorTopRight, domain.Rect{X: 20, Y: 2, Width: 8, Height: 6}},
21 {domain.AnchorLeft, domain.Rect{X: 5, Y: 6, Width: 8, Height: 6}},
22 {domain.AnchorCenter, domain.Rect{X: 11, Y: 6, Width: 8, Height: 6}},
23 {domain.AnchorRight, domain.Rect{X: 20, Y: 6, Width: 8, Height: 6}},
24 {domain.AnchorBottomLeft, domain.Rect{X: 5, Y: 9, Width: 8, Height: 6}},
25 {domain.AnchorBottom, domain.Rect{X: 11, Y: 9, Width: 8, Height: 6}},
26 {domain.AnchorBottomRight, domain.Rect{X: 20, Y: 9, Width: 8, Height: 6}},
27 }
28 for _, tt := range tests {
29 t.Run(tt.anchor.String(), func(t *testing.T) {
30 if got := Place(base, content, tt.anchor, margins); got != tt.want {
31 t.Fatalf("Place() = %+v, want %+v", got, tt.want)
32 }
33 })
34 }
35}
36
37func TestPlaceNormalizesAndClamps(t *testing.T) {
38 tests := []struct {
39 name string
40 base domain.Size
41 content domain.Size
42 anchor domain.Anchor
43 margins Margins
44 want domain.Rect
45 }{
46 {
47 name: "center ignores margins and rounds down",
48 base: domain.Size{Cols: 10, Rows: 8},
49 content: domain.Size{Cols: 5, Rows: 3},
50 anchor: domain.AnchorCenter,
51 margins: Margins{Top: 100, Right: 100, Bottom: 100, Left: 100},
52 want: domain.Rect{X: 2, Y: 2, Width: 5, Height: 3},
53 },
54 {
55 name: "invalid anchor falls back to center",
56 base: domain.Size{Cols: 10, Rows: 8},
57 content: domain.Size{Cols: 5, Rows: 3},
58 anchor: domain.Anchor(99),
59 margins: Margins{Top: 100, Right: 100, Bottom: 100, Left: 100},
60 want: domain.Rect{X: 2, Y: 2, Width: 5, Height: 3},
61 },
62 {
63 name: "nonpositive base is empty",
64 base: domain.Size{Cols: 0, Rows: 8},
65 content: domain.Size{Cols: 5, Rows: 3},
66 anchor: domain.AnchorTopLeft,
67 want: domain.Rect{},
68 },
69 {
70 name: "negative content clamps to zero",
71 base: domain.Size{Cols: 10, Rows: 8},
72 content: domain.Size{Cols: -5, Rows: -3},
73 anchor: domain.AnchorBottomRight,
74 want: domain.Rect{X: 10, Y: 8},
75 },
76 {
77 name: "oversized content clamps to base",
78 base: domain.Size{Cols: 10, Rows: 8},
79 content: domain.Size{Cols: 50, Rows: 30},
80 anchor: domain.AnchorTopLeft,
81 margins: Margins{Top: 2, Left: 3},
82 want: domain.Rect{Width: 10, Height: 8},
83 },
84 {
85 name: "negative margins normalize to zero",
86 base: domain.Size{Cols: 10, Rows: 8},
87 content: domain.Size{Cols: 5, Rows: 3},
88 anchor: domain.AnchorTopLeft,
89 margins: Margins{Top: -2, Left: -3},
90 want: domain.Rect{Width: 5, Height: 3},
91 },
92 {
93 name: "excessive relevant margins clamp coordinates",
94 base: domain.Size{Cols: 10, Rows: 8},
95 content: domain.Size{Cols: 5, Rows: 3},
96 anchor: domain.AnchorBottomRight,
97 margins: Margins{Bottom: 20, Right: 20},
98 want: domain.Rect{Width: 5, Height: 3},
99 },
100 }
101 for _, tt := range tests {
102 t.Run(tt.name, func(t *testing.T) {
103 if got := Place(tt.base, tt.content, tt.anchor, tt.margins); got != tt.want {
104 t.Fatalf("Place() = %+v, want %+v", got, tt.want)
105 }
106 })
107 }
108}