ascii_test.gno
4.44 Kb ยท 182 lines
1package ascii
2
3import (
4 "strings"
5 "testing"
6)
7
8// padLine
9
10func TestpadLine_Left(t *testing.T) {
11 got := padLine("hi", 6, AlignLeft, " ")
12 want := "hi "
13 if got != want {
14 t.Fatalf("padLine left failed: got '%s', want '%s'", got, want)
15 }
16}
17
18func TestpadLine_Right(t *testing.T) {
19 got := padLine("hi", 6, AlignRight, " ")
20 want := " hi"
21 if got != want {
22 t.Fatalf("padLine right failed: got '%s', want '%s'", got, want)
23 }
24}
25
26func TestpadLine_Center(t *testing.T) {
27 got := padLine("hi", 6, AlignWCenter, " ")
28 want := " hi "
29 if got != want {
30 t.Fatalf("padLine center failed: got '%s', want '%s'", got, want)
31 }
32}
33
34func TestpadLineNegativeWidth(t *testing.T) {
35 got := padLine("hi", -1, AlignLeft, " ")
36 want := "hi"
37 if got != want {
38 t.Fatalf("padLine negative width failed: got '%s', want '%s'", got, want)
39 }
40}
41
42// padHeight
43
44func TestpadHeight_Top(t *testing.T) {
45 lines := []string{"one", "two"}
46 out := padHeight(lines, 4, AlignTop)
47 if len(out) != 4 || out[0] != "one" || out[3] != "" {
48 t.Fatalf("padHeight top failed: got %#v", out)
49 }
50}
51
52func TestpadHeight_Center(t *testing.T) {
53 lines := []string{"one"}
54 out := padHeight(lines, 3, AlignHCenter)
55 if len(out) != 3 || out[0] != "" || out[1] != "one" || out[2] != "" {
56 t.Fatalf("padHeight center failed: got %#v", out)
57 }
58}
59
60func TestpadHeight_Bottom(t *testing.T) {
61 lines := []string{"one"}
62 out := padHeight(lines, 2, AlignBottom)
63 if len(out) != 2 || out[0] != "" || out[1] != "one" {
64 t.Fatalf("padHeight bottom failed: got %#v", out)
65 }
66}
67
68func TestpadHeightNegativeHeight(t *testing.T) {
69 lines := []string{"one", "two"}
70 out := padHeight(lines, -1, AlignTop)
71 if len(out) != 2 || out[0] != "one" || out[1] != "two" {
72 t.Fatalf("padHeight negative height failed: got %#v", out)
73 }
74}
75
76// Box
77
78func TestBox_SingleLine(t *testing.T) {
79 got := Box("Hi")
80 if !strings.Contains(got, "| Hi |") {
81 t.Fatalf("Box did not render expected line: got\n%s", got)
82 }
83}
84
85func TestBox_MultiLineFallback(t *testing.T) {
86 got := Box("One\nTwo")
87 if !strings.Contains(got, "| One |") || !strings.Contains(got, "| Two |") {
88 t.Fatalf("Box (multi-line fallback) failed: got\n%s", got)
89 }
90}
91
92// FlexFrame
93
94func TestFlexFrame(t *testing.T) {
95 lines := []string{"Short", "A bit longer"}
96 got := FlexFrame(lines, AlignRight)
97 if !strings.Contains(got, "| Short |") {
98 t.Fatalf("FlexFrame right alignment failed:\n%s", got)
99 }
100}
101
102// Frame
103
104func TestFrame_SizeAlign(t *testing.T) {
105 lines := []string{"line"}
106 got := Frame(lines, AlignWCenter, 10, 3, AlignHCenter)
107 if res := strings.Count(got, "\n"); res != 7 {
108 t.Fatalf("Frame height failed (got %d):\n%s", res, got)
109 }
110 if !strings.Contains(got, "| line |") {
111 t.Fatalf("Frame width or alignment failed:\n%s", got)
112 }
113}
114
115func TestFrameNegativeSize(t *testing.T) {
116 lines := []string{"line"}
117 got := Frame(lines, AlignWCenter, -1, -1, AlignHCenter)
118 if res := strings.Count(got, "\n"); res != 1+4 {
119 t.Fatalf("Frame negative size failed (got %d):\n%s", res, got)
120 }
121 if !strings.Contains(got, "| line |") {
122 t.Fatalf("Frame negative size failed:\n%s", got)
123 }
124}
125
126// FixedFrame
127
128func TestFixedFrame(t *testing.T) {
129 lines := []string{"line"}
130 got := FixedFrame(lines, AlignWCenter, 10, 3, AlignTop)
131 if res := strings.Count(got, "\n"); res != 7 {
132 t.Fatalf("FixedFrame height failed (got %d):\n%s", res, got)
133 }
134 if !strings.Contains(got, "| line |") {
135 t.Fatalf("FixedFrame width failed:\n%s", got)
136 }
137}
138
139func TestFixedFrameNegativeSize(t *testing.T) {
140 lines := []string{"line"}
141 got := FixedFrame(lines, AlignLeft, -1, -1, AlignTop)
142 if res := strings.Count(got, "\n"); res != 4 {
143 t.Fatalf("FixedFrame negative size failed (got %d):\n%s", res, got)
144 }
145 if strings.Contains(got, "| line |") {
146 t.Fatalf("FixedFrame negative size failed:\n%s", got)
147 }
148}
149
150// ProgressBar
151
152func TestProgressBar(t *testing.T) {
153 got := ProgressBar(30, 100, 10, true)
154 if !strings.Contains(got, "30%") || !strings.Contains(got, "[###-------]") {
155 t.Fatalf("ProgressBar output incorrect: %s", got)
156 }
157}
158
159func TestProgressBar_ZeroTotal(t *testing.T) {
160 got := ProgressBar(0, 0, 10, true)
161 if !strings.Contains(got, "0%") {
162 t.Fatalf("ProgressBar zero-total failed: %s", got)
163 }
164}
165
166// Grid
167
168func TestGrid(t *testing.T) {
169 got := Grid(2, 3, "*")
170 want := "***\n***\n"
171 if got != want {
172 t.Fatalf("Grid failed: got\n%s\nwant\n%s", got, want)
173 }
174}
175
176func TestGrid_NegativeSize(t *testing.T) {
177 got := Grid(-1, -1, "*")
178 want := ""
179 if got != want {
180 t.Fatalf("Grid negative size failed: got\n%s\nwant\n%s", got, want)
181 }
182}