Skip to content

Commit 19de1d3

Browse files
authored
Add test cases for ansi (#4)
1 parent 64536cd commit 19de1d3

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

ansi/buffer_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ansi
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
func TestBuffer_PrintableRuneWidth(t *testing.T) {
9+
var bb bytes.Buffer
10+
bb.WriteString("\x1B[38;2;249;38;114mfoo")
11+
b := Buffer{bb}
12+
13+
if n := b.PrintableRuneWidth(); n != 3 {
14+
t.Fatalf("width should be 3, got %d", n)
15+
}
16+
}
17+
18+
func BenchmarkPrintableRuneWidth(b *testing.B) {
19+
s := "\x1B[38;2;249;38;114mfoo"
20+
var n int
21+
22+
b.ReportAllocs()
23+
b.ResetTimer()
24+
for i := 0; i < b.N; i++ {
25+
n = PrintableRuneWidth(s)
26+
}
27+
28+
if n != 3 {
29+
b.Fatalf("width should be 3, got %d", n)
30+
}
31+
}

ansi/writer_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package ansi
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"io/ioutil"
7+
"testing"
8+
)
9+
10+
func TestWriter_Write(t *testing.T) {
11+
buf := []byte("\x1B[38;2;249;38;114mfoo\x1B[0m")
12+
w := &Writer{Forward: ioutil.Discard}
13+
14+
n, err := w.Write(buf)
15+
16+
w.ResetAnsi()
17+
18+
w.RestoreAnsi()
19+
20+
if err != nil {
21+
t.Fatalf("err should be nil, but got %v", err)
22+
}
23+
24+
if l := len(buf); n != l {
25+
t.Fatalf("n should be %d, got %d", l, n)
26+
}
27+
28+
if ls := w.LastSequence(); ls != "" {
29+
t.Fatalf("LastSequence should be empty, got %s", ls)
30+
}
31+
}
32+
33+
var fakeErr = errors.New("fake error")
34+
35+
type fakeWriter struct{}
36+
37+
func (fakeWriter) Write(_ []byte) (int, error) {
38+
return 0, fakeErr
39+
}
40+
41+
func TestWriter_Write_Error(t *testing.T) {
42+
w := &Writer{Forward: fakeWriter{}}
43+
44+
_, err := w.Write([]byte("foo"))
45+
46+
if err != fakeErr {
47+
t.Fatalf("err should be fakeErr, but got %v", err)
48+
}
49+
}
50+
51+
func BenchmarkWriter_Write(b *testing.B) {
52+
buf := []byte("\x1B[38;2;249;38;114mfoo\x1B[0m")
53+
w := &Writer{Forward: ioutil.Discard}
54+
var (
55+
n int
56+
err error
57+
)
58+
b.ReportAllocs()
59+
b.ResetTimer()
60+
61+
for i := 0; i < b.N; i++ {
62+
n, err = w.Write(buf)
63+
}
64+
65+
if err != nil {
66+
b.Fatalf("err should be nil, but got %v", err)
67+
}
68+
69+
if l := len(buf); n != l {
70+
b.Fatalf("n should be %d, got %d", l, n)
71+
}
72+
73+
if ls := w.LastSequence(); ls != "" {
74+
b.Fatalf("LastSequence should be empty, got %s", ls)
75+
}
76+
}
77+
78+
func TestWriter_ResetAnsi(t *testing.T) {
79+
b := &bytes.Buffer{}
80+
w := &Writer{Forward: b}
81+
82+
w.ResetAnsi()
83+
84+
if b.String() != "" {
85+
t.Fatal("b should be empty")
86+
}
87+
88+
w.seqchanged = true
89+
90+
w.ResetAnsi()
91+
92+
if s := b.String(); s != "\x1b[0m" {
93+
t.Fatalf("b.String() should be \"\\x1b[0m\", got %s", s)
94+
}
95+
}
96+
97+
func TestWriter_RestoreAnsi(t *testing.T) {
98+
b := &bytes.Buffer{}
99+
w := &Writer{Forward: b, lastseq: "\x1B[38;2;249;38;114m"}
100+
101+
w.RestoreAnsi()
102+
103+
if s := b.String(); s != "\x1B[38;2;249;38;114m" {
104+
t.Fatalf("b.String() should be \"\\x1B[38;2;249;38;114m\", got %s", s)
105+
}
106+
}

0 commit comments

Comments
 (0)