forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header_printer_test.go
157 lines (125 loc) · 3.75 KB
/
header_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
package pterm_test
import (
"errors"
"io"
"os"
"testing"
"github.com/MarvinJWendt/testza"
"github.com/pterm/pterm"
)
func TestHeaderPrinterNilPrint(t *testing.T) {
p := pterm.HeaderPrinter{}
p.Println("Hello, World!")
}
func TestHeaderPrinterPrintMethods(t *testing.T) {
p := pterm.DefaultHeader
t.Run("Print", func(t *testing.T) {
testPrintContains(t, func(w io.Writer, a interface{}) {
p.Print(a)
})
})
t.Run("PrintWithFullWidth", func(t *testing.T) {
testPrintContains(t, func(w io.Writer, a interface{}) {
p2 := p.WithFullWidth()
p2.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 TestHeaderPrinter_WithBackgroundStyle(t *testing.T) {
s := pterm.NewStyle(pterm.FgRed, pterm.BgGray, pterm.Bold)
p := pterm.HeaderPrinter{}
p2 := p.WithBackgroundStyle(s)
testza.AssertEqual(t, s, p2.BackgroundStyle)
}
func TestHeaderPrinter_WithFullWidth(t *testing.T) {
p := pterm.HeaderPrinter{}
p2 := p.WithFullWidth()
testza.AssertEqual(t, true, p2.FullWidth)
}
func TestHeaderPrinter_WithFullWidthToLongForTerminal(t *testing.T) {
p := pterm.HeaderPrinter{}
p2 := p.WithFullWidth().Sprint("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
testza.AssertContains(t, p2, "a")
}
func TestHeaderPrinter_ToLongForTerminal(t *testing.T) {
p := pterm.HeaderPrinter{}
p2 := p.Sprint("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
testza.AssertContains(t, p2, "a")
}
func TestHeaderPrinter_WithMargin(t *testing.T) {
p := pterm.HeaderPrinter{}
p2 := p.WithMargin(1337)
testza.AssertEqual(t, 1337, p2.Margin)
}
func TestHeaderPrinter_WithTextStyle(t *testing.T) {
s := pterm.NewStyle(pterm.FgRed, pterm.BgGray, pterm.Bold)
p := pterm.HeaderPrinter{}
p2 := p.WithTextStyle(s)
testza.AssertEqual(t, s, p2.TextStyle)
}
func TestHeaderPrinter_WithWriter(t *testing.T) {
p := pterm.HeaderPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)
testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}