forked from johnmikee/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_text_printer_test.go
115 lines (94 loc) · 2.53 KB
/
basic_text_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
package pterm_test
import (
"errors"
"io"
"os"
"testing"
"github.com/MarvinJWendt/testza"
"github.com/gemini/pterm"
)
func TestBasicTextPrinterNilPrint(t *testing.T) {
proxyToDevNull()
p := pterm.BasicTextPrinter{}
p.Println("Hello, World!")
}
func TestBasicTextPrinterPrintMethods(t *testing.T) {
p := pterm.DefaultBasicText
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 TestBasicTextPrinter_WithStyle(t *testing.T) {
s := pterm.NewStyle(pterm.FgRed, pterm.BgBlue, pterm.Bold)
p := pterm.BasicTextPrinter{}
p2 := p.WithStyle(s)
testza.AssertEqual(t, s, p2.Style)
}
func TestBasicTextPrinter_WithWriter(t *testing.T) {
p := pterm.BasicTextPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)
testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}