forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paragraph_printer_test.go
123 lines (100 loc) · 2.87 KB
/
paragraph_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
package pterm_test
import (
"errors"
"io"
"os"
"testing"
"github.com/MarvinJWendt/testza"
"github.com/overmindtech/pterm"
)
func TestParagraphPrinterNilPrint(t *testing.T) {
p := pterm.ParagraphPrinter{}
p.Println("Hello, World!")
}
func TestParagraphPrinterPrintMethods(t *testing.T) {
p := pterm.DefaultParagraph
t.Run("Print", func(t *testing.T) {
testPrintContains(t, func(w io.Writer, a interface{}) {
p.Print(a)
})
})
t.Run("PrintWithLongText", func(t *testing.T) {
proxyToDevNull()
testza.AssertNotZero(t, p.Print("This is a longer text to test the paragraph printer. I don't know when this text will be long enough so I will just write until I get the feeling that it's enough. Maybe about now."))
})
t.Run("PrintWithoutText", func(t *testing.T) {
proxyToDevNull()
testza.AssertNotZero(t, p.Print(""))
})
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 TestParagraphPrinter_WithMaxWidth(t *testing.T) {
p := pterm.ParagraphPrinter{}
p2 := p.WithMaxWidth(1337)
testza.AssertEqual(t, 1337, p2.MaxWidth)
}
func TestParagraphPrinter_WithWriter(t *testing.T) {
p := pterm.ParagraphPrinter{}
s := os.Stderr
p2 := p.WithWriter(s)
testza.AssertEqual(t, s, p2.Writer)
testza.AssertZero(t, p.Writer)
}