1package domain
2
3import "testing"
4
5func TestAnchor(t *testing.T) {
6 tests := []struct {
7 name string
8 anchor Anchor
9 valid bool
10 text string
11 }{
12 {"center", AnchorCenter, true, "center"},
13 {"top left", AnchorTopLeft, true, "top-left"},
14 {"top", AnchorTop, true, "top"},
15 {"top right", AnchorTopRight, true, "top-right"},
16 {"left", AnchorLeft, true, "left"},
17 {"right", AnchorRight, true, "right"},
18 {"bottom left", AnchorBottomLeft, true, "bottom-left"},
19 {"bottom", AnchorBottom, true, "bottom"},
20 {"bottom right", AnchorBottomRight, true, "bottom-right"},
21 {"invalid", Anchor(255), false, "unknown"},
22 }
23
24 if AnchorCenter != 0 {
25 t.Errorf("AnchorCenter = %d, want 0", AnchorCenter)
26 }
27
28 for _, tt := range tests {
29 t.Run(tt.name, func(t *testing.T) {
30 if got := tt.anchor.Valid(); got != tt.valid {
31 t.Errorf("Anchor(%d).Valid() = %v, want %v", tt.anchor, got, tt.valid)
32 }
33 if got := tt.anchor.String(); got != tt.text {
34 t.Errorf("Anchor(%d).String() = %q, want %q", tt.anchor, got, tt.text)
35 }
36 })
37 }
38}
39
40func TestParseAnchor(t *testing.T) {
41 tests := []struct {
42 input string
43 want Anchor
44 ok bool
45 }{
46 {"center", AnchorCenter, true},
47 {" TOP-LEFT ", AnchorTopLeft, true},
48 {"Top", AnchorTop, true},
49 {"top-right", AnchorTopRight, true},
50 {"LEFT", AnchorLeft, true},
51 {"right", AnchorRight, true},
52 {"bottom-left", AnchorBottomLeft, true},
53 {"Bottom", AnchorBottom, true},
54 {"bottom-right", AnchorBottomRight, true},
55 {"auto", AnchorCenter, false},
56 {"", AnchorCenter, false},
57 {"diagonal", AnchorCenter, false},
58 }
59
60 for _, tt := range tests {
61 t.Run(tt.input, func(t *testing.T) {
62 got, ok := ParseAnchor(tt.input)
63 if got != tt.want || ok != tt.ok {
64 t.Errorf("ParseAnchor(%q) = (%q, %v), want (%q, %v)", tt.input, got, ok, tt.want, tt.ok)
65 }
66 })
67 }
68}
69
70func TestSizeValid(t *testing.T) {
71 tests := []struct {
72 name string
73 sz Size
74 want bool
75 }{
76 {"positive", Size{Cols: 80, Rows: 24}, true},
77 {"zero cols", Size{Cols: 0, Rows: 24}, false},
78 {"zero rows", Size{Cols: 80, Rows: 0}, false},
79 {"zero both", Size{Cols: 0, Rows: 0}, false},
80 {"negative cols", Size{Cols: -1, Rows: 24}, false},
81 {"negative rows", Size{Cols: 80, Rows: -1}, false},
82 }
83
84 for _, tt := range tests {
85 t.Run(tt.name, func(t *testing.T) {
86 if got := tt.sz.Valid(); got != tt.want {
87 t.Errorf("Size%+v.Valid() = %v, want %v", tt.sz, got, tt.want)
88 }
89 })
90 }
91}