forked from johnmikee/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
center_printer_test.go
163 lines (132 loc) · 3.95 KB
/
center_printer_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package pterm_test
import (
"errors"
"io"
"os"
"testing"
"github.com/MarvinJWendt/testza"
"github.com/gemini/pterm"
)
func TestCenterPrinter_WithCenterEachLineSeparately(t *testing.T) {
p := pterm.CenterPrinter{}
p2 := p.WithCenterEachLineSeparately()
testza.AssertTrue(t, p2.CenterEachLineSeparately)
testza.AssertFalse(t, p.CenterEachLineSeparately)
}
func TestCenterPrinterPrintMethods(t *testing.T) {
p := pterm.DefaultCenter
t.Run("Print", func(t *testing.T) {
testPrintContains(t, func(w io.Writer, a interface{}) {
p.Print(a)
})
})
t.Run("Printf", func(t *testing.T) {
testPrintfContains(t, func(w io.Writer, format string, a interface{}) {
p.Printf(format, a)
})
})
t.Run("Printfln", func(t *testing.T) {
testPrintflnContains(t, func(w io.Writer, format string, a interface{}) {
p.Printfln(format, a)
})
})
t.Run("Println", func(t *testing.T) {
testPrintlnContains(t, func(w io.Writer, a interface{}) {
p.Println(a)
})
})
t.Run("Sprint", func(t *testing.T) {
testSprintContains(t, func(a interface{}) string {
return p.Sprint(a)
})
})
t.Run("Sprintf", func(t *testing.T) {
testSprintfContains(t, func(format string, a interface{}) string {
return p.Sprintf(format, a)
})
})
t.Run("Sprintfln", func(t *testing.T) {
testSprintflnContains(t, func(format string, a interface{}) string {
return p.Sprintfln(format, a)
})
})
t.Run("Sprintln", func(t *testing.T) {
testSprintlnContains(t, func(a interface{}) string {
return p.Sprintln(a)
})
})
t.Run("PrintOnError", func(t *testing.T) {
result := captureStdout(func(w io.Writer) {
p.PrintOnError(errors.New("hello world"))
})
testza.AssertContains(t, result, "hello world")
})
t.Run("PrintIfError_WithoutError", func(t *testing.T) {
result := captureStdout(func(w io.Writer) {
p.PrintOnError(nil)
})
testza.AssertZero(t, result)
})
t.Run("PrintOnErrorf", func(t *testing.T) {
result := captureStdout(func(w io.Writer) {
p.PrintOnErrorf("wrapping error : %w", errors.New("hello world"))
})
testza.AssertContains(t, result, "hello world")
})
t.Run("PrintIfError_WithoutErrorf", func(t *testing.T) {
result := captureStdout(func(w io.Writer) {
p.PrintOnErrorf("", nil)
})
testza.AssertZero(t, result)
})
}
func TestCenterPrinterPrintMethodsCenterSeparately(t *testing.T) {
p := pterm.DefaultCenter.WithCenterEachLineSeparately()
t.Run("Print", func(t *testing.T) {
testPrintContains(t, func(w io.Writer, a interface{}) {
p.Print(a)
})
})
t.Run("Printf", func(t *testing.T) {
testPrintfContains(t, func(w io.Writer, format string, a interface{}) {
p.Printf(format, a)
})
})
t.Run("Println", func(t *testing.T) {
testPrintlnContains(t, func(w io.Writer, a interface{}) {
p.Println(a)
})
})
t.Run("Sprint", func(t *testing.T) {
testSprintContains(t, func(a interface{}) string {
return p.Sprint(a)
})
})
t.Run("Sprintf", func(t *testing.T) {
testSprintfContains(t, func(format string, a interface{}) string {
return p.Sprintf(format, a)
})
})
t.Run("Sprintln", func(t *testing.T) {
testSprintlnContains(t, func(a interface{}) string {
return p.Sprintln(a)
})
})
}
func TestCenterPrinter_SprintLineLongerThanTerminal(t *testing.T) {
p := pterm.DefaultCenter
p2 := p.Sprint("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
testza.AssertContains(t, p2, "a")
}
func TestCenterPrinter_SprintLineLongerThanTerminalWithCenterEachLineSeparately(t *testing.T) {
p := pterm.DefaultCenter
p2 := p.WithCenterEachLineSeparately().Sprint("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
testza.AssertContains(t, p2, "a")
}
func TestCenterPrinter_WithWriter(t *testing.T) {
p := pterm.CenterPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)
testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}