gitstack

grindlemire/go-tui code browser

2.0 KB Go 93 lines 2026-06-12 · 56e8c04 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package tui

import "testing"

func TestEdgeSymmetric(t *testing.T) {
	type tc struct {
		v, h int
		want Edges
	}

	tests := map[string]tc{
		"vertical and horizontal differ": {
			v:    2,
			h:    5,
			want: Edges{Top: 2, Right: 5, Bottom: 2, Left: 5},
		},
		"equal values": {
			v:    3,
			h:    3,
			want: Edges{Top: 3, Right: 3, Bottom: 3, Left: 3},
		},
		"zero values": {
			v:    0,
			h:    0,
			want: Edges{},
		},
		"vertical only": {
			v:    4,
			h:    0,
			want: Edges{Top: 4, Bottom: 4},
		},
		"negative values": {
			v:    -1,
			h:    -2,
			want: Edges{Top: -1, Right: -2, Bottom: -1, Left: -2},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := EdgeSymmetric(tt.v, tt.h)
			if got != tt.want {
				t.Errorf("EdgeSymmetric(%d, %d) = %+v, want %+v", tt.v, tt.h, got, tt.want)
			}
		})
	}
}

func TestInsetRect(t *testing.T) {
	type tc struct {
		r                        Rect
		top, right, bottom, left int
		want                     Rect
	}

	tests := map[string]tc{
		"uniform inset": {
			r:   NewRect(0, 0, 10, 10),
			top: 1, right: 1, bottom: 1, left: 1,
			want: NewRect(1, 1, 8, 8),
		},
		"asymmetric inset": {
			r:   NewRect(5, 5, 20, 10),
			top: 1, right: 2, bottom: 3, left: 4,
			want: NewRect(9, 6, 14, 6),
		},
		"zero inset returns original": {
			r:   NewRect(2, 3, 7, 8),
			top: 0, right: 0, bottom: 0, left: 0,
			want: NewRect(2, 3, 7, 8),
		},
		"negative inset expands": {
			r:   NewRect(5, 5, 10, 10),
			top: -1, right: -2, bottom: -3, left: -4,
			want: NewRect(1, 4, 16, 14),
		},
		"inset larger than rect goes negative": {
			r:   NewRect(0, 0, 4, 4),
			top: 3, right: 3, bottom: 3, left: 3,
			want: NewRect(3, 3, -2, -2),
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := InsetRect(tt.r, tt.top, tt.right, tt.bottom, tt.left)
			if got != tt.want {
				t.Errorf("InsetRect(%+v, %d, %d, %d, %d) = %+v, want %+v",
					tt.r, tt.top, tt.right, tt.bottom, tt.left, got, tt.want)
			}
		})
	}
}