-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathformatter_test.go
More file actions
176 lines (149 loc) · 3.68 KB
/
formatter_test.go
File metadata and controls
176 lines (149 loc) · 3.68 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
package libghostty
import (
"bytes"
"io"
"strings"
"testing"
)
// Verify interface satisfaction at compile time.
var _ io.WriterTo = (*Formatter)(nil)
func TestFormatterPlainText(t *testing.T) {
term, err := NewTerminal(WithSize(80, 24))
if err != nil {
t.Fatal(err)
}
defer term.Close()
term.VTWrite([]byte("hello world"))
f, err := NewFormatter(term, WithFormatterFormat(FormatterFormatPlain))
if err != nil {
t.Fatal(err)
}
defer f.Close()
out, err := f.FormatString()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "hello world") {
t.Fatalf("expected output to contain 'hello world', got %q", out)
}
}
func TestFormatterVT(t *testing.T) {
term, err := NewTerminal(WithSize(80, 24))
if err != nil {
t.Fatal(err)
}
defer term.Close()
// Write red text.
term.VTWrite([]byte("\x1b[31mred text\x1b[0m"))
f, err := NewFormatter(term, WithFormatterFormat(FormatterFormatVT))
if err != nil {
t.Fatal(err)
}
defer f.Close()
out, err := f.FormatString()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "red text") {
t.Fatalf("expected output to contain 'red text', got %q", out)
}
// VT format should contain escape sequences.
if !strings.Contains(out, "\x1b[") {
t.Fatalf("expected VT output to contain escape sequences, got %q", out)
}
}
func TestFormatterHTML(t *testing.T) {
term, err := NewTerminal(WithSize(80, 24))
if err != nil {
t.Fatal(err)
}
defer term.Close()
term.VTWrite([]byte("\x1b[1mbold text\x1b[0m"))
f, err := NewFormatter(term, WithFormatterFormat(FormatterFormatHTML))
if err != nil {
t.Fatal(err)
}
defer f.Close()
out, err := f.FormatString()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "bold text") {
t.Fatalf("expected output to contain 'bold text', got %q", out)
}
}
func TestFormatterFormat(t *testing.T) {
term, err := NewTerminal(WithSize(80, 24))
if err != nil {
t.Fatal(err)
}
defer term.Close()
term.VTWrite([]byte("bytes test"))
f, err := NewFormatter(term, WithFormatterFormat(FormatterFormatPlain))
if err != nil {
t.Fatal(err)
}
defer f.Close()
out, err := f.Format()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(out), "bytes test") {
t.Fatalf("expected output to contain 'bytes test', got %q", string(out))
}
}
func TestFormatterReflectsCurrentState(t *testing.T) {
term, err := NewTerminal(WithSize(80, 24))
if err != nil {
t.Fatal(err)
}
defer term.Close()
f, err := NewFormatter(term, WithFormatterFormat(FormatterFormatPlain))
if err != nil {
t.Fatal(err)
}
defer f.Close()
// Format before writing anything.
out1, err := f.FormatString()
if err != nil {
t.Fatal(err)
}
// Write some text and format again.
term.VTWrite([]byte("after write"))
out2, err := f.FormatString()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out2, "after write") {
t.Fatalf("expected second format to contain 'after write', got %q", out2)
}
if strings.Contains(out1, "after write") {
t.Fatal("first format should not contain text written afterward")
}
}
func TestFormatterWriteTo(t *testing.T) {
term, err := NewTerminal(WithSize(80, 24))
if err != nil {
t.Fatal(err)
}
defer term.Close()
term.VTWrite([]byte("writeto test"))
f, err := NewFormatter(term, WithFormatterFormat(FormatterFormatPlain))
if err != nil {
t.Fatal(err)
}
defer f.Close()
// Formatter satisfies io.WriterTo.
var wt io.WriterTo = f
var buf bytes.Buffer
n, err := wt.WriteTo(&buf)
if err != nil {
t.Fatal(err)
}
if n != int64(buf.Len()) {
t.Fatalf("WriteTo returned %d but buffer has %d bytes", n, buf.Len())
}
if !strings.Contains(buf.String(), "writeto test") {
t.Fatalf("expected output to contain 'writeto test', got %q", buf.String())
}
}