-
Notifications
You must be signed in to change notification settings - Fork 26
/
drawbox_test.go
101 lines (92 loc) · 4.3 KB
/
drawbox_test.go
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
94
95
96
97
98
99
100
101
package strutil
import (
"fmt"
"testing"
)
func TestDrawBox(t *testing.T) {
tests := []struct {
input string
width int
align AlignType
expected string
err bool
}{
{"", 20, Center, "┌──────────────────┐\n│ │\n└──────────────────┘", false},
{"Hello World", 20, Center, "┌──────────────────┐\n│ Hello World │\n└──────────────────┘", false},
{"\nHello World\n", 20, Center, "┌──────────────────┐\n│ │\n│ Hello World │\n│ │\n└──────────────────┘", false},
{"résumé", 10, Left, "┌────────┐\n│résumé │\n└────────┘", false},
{"Hello World", 2, Left, "", true},
{"Hello\n\n\nWorld", 10, Left, "┌────────┐\n│Hello │\n│ │\n│ │\n│World │\n└────────┘", false},
}
for i, test := range tests {
output, err := DrawBox(test.input, test.width, test.align)
if test.err {
Assert(t, true, err != nil, "Test case %d is not successful, expecting err while we have %v\n", i, err)
} else {
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}
}
func TestDrawCustomBox(t *testing.T) {
test9Slice := SimpleBox9Slice()
test9Slice.Top = "-৹_৹"
test9Slice.Left = "Ͼ|"
test9Slice.Right = "|Ͽ"
test9Slice.Bottom = "৹-৹"
tests := []struct {
input string
width int
align AlignType
expected string
err bool
}{
{"", 20, Center, "+-৹_৹-৹_৹-৹_৹-৹_৹-৹+\nϾ| |Ͽ\n+৹-৹৹-৹৹-৹৹-৹৹-৹৹-৹+", false},
{"Hello World", 20, Center, "+-৹_৹-৹_৹-৹_৹-৹_৹-৹+\nϾ| Hello World |Ͽ\n+৹-৹৹-৹৹-৹৹-৹৹-৹৹-৹+", false},
{"\nHello World\n", 20, Center, "+-৹_৹-৹_৹-৹_৹-৹_৹-৹+\nϾ| |Ͽ\nϾ| Hello World |Ͽ\nϾ| |Ͽ\n+৹-৹৹-৹৹-৹৹-৹৹-৹৹-৹+", false},
{"résumé", 10, Left, "+-৹_৹-৹_৹+\nϾ|résumé|Ͽ\n+৹-৹৹-৹৹-+", false},
{"résumé", 9, Left, "+-৹_৹-৹_+\nϾ|résum|Ͽ\nϾ|é |Ͽ\n+৹-৹৹-৹৹+", false},
{"Hello World", 2, Left, "", true},
{"Hello\n\n\nWorld", 10, Right, "+-৹_৹-৹_৹+\nϾ| Hello|Ͽ\nϾ| |Ͽ\nϾ| |Ͽ\nϾ| World|Ͽ\n+৹-৹৹-৹৹-+", false},
}
for i, test := range tests {
output, err := DrawCustomBox(test.input, test.width, test.align, test9Slice, "\n")
if test.err {
Assert(t, true, err != nil, "Test case %d is not successful, expecting error while we have %v\n", i, err)
} else {
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}
}
func ExampleDrawBox() {
output, _ := DrawBox("Hello World", 20, Center)
fmt.Println(output)
// Output:
//┌──────────────────┐
//│ Hello World │
//└──────────────────┘
}
func ExampleDrawBox_long() {
text := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.`
output, _ := DrawBox(text, 30, Left)
fmt.Println(output)
// Output:
// ┌────────────────────────────┐
// │Lorem ipsum dolor sit amet, │
// │consectetur adipiscing elit,│
// │sed do eiusmod tempor │
// │incididunt ut labore et │
// │dolore magna aliqua. Ut enim│
// │ad minim veniam, quis │
// │nostrud exercitation ullamco│
// │laboris nisi ut aliquip ex │
// │ea commodo consequat. │
// └────────────────────────────┘
}
func ExampleDrawCustomBox() {
output, _ := DrawCustomBox("Hello World", 20, Center, DefaultBox9Slice(), "\n")
fmt.Println(output)
// Output:
//┌──────────────────┐
//│ Hello World │
//└──────────────────┘
}