File: writer_test.go

package info (click to toggle)
golang-github-charmbracelet-colorprofile 0.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: makefile: 3
file content (185 lines) | stat: -rw-r--r-- 6,089 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
package colorprofile

import (
	"bytes"
	"io"
	"os"
	"testing"

	"github.com/charmbracelet/x/ansi"
)

var writers = map[Profile]func(io.Writer) *Writer{
	TrueColor: func(w io.Writer) *Writer { return &Writer{w, TrueColor} },
	ANSI256:   func(w io.Writer) *Writer { return &Writer{w, ANSI256} },
	ANSI:      func(w io.Writer) *Writer { return &Writer{w, ANSI} },
	Ascii:     func(w io.Writer) *Writer { return &Writer{w, Ascii} },
	NoTTY:     func(w io.Writer) *Writer { return &Writer{w, NoTTY} },
}

var writer_cases = []struct {
	name              string
	input             string
	expectedTrueColor string
	expectedANSI256   string
	expectedANSI      string
	expectedAscii     string
}{
	{
		name: "empty",
	},
	{
		name:              "no styles",
		input:             "hello world",
		expectedTrueColor: "hello world",
		expectedANSI256:   "hello world",
		expectedANSI:      "hello world",
		expectedAscii:     "hello world",
	},
	{
		name:              "simple style attributes",
		input:             "hello \x1b[1mworld\x1b[m",
		expectedTrueColor: "hello \x1b[1mworld\x1b[m",
		expectedANSI256:   "hello \x1b[1mworld\x1b[m",
		expectedANSI:      "hello \x1b[1mworld\x1b[m",
		expectedAscii:     "hello \x1b[1mworld\x1b[m",
	},
	{
		name:              "simple ansi color fg",
		input:             "hello \x1b[31mworld\x1b[m",
		expectedTrueColor: "hello \x1b[31mworld\x1b[m",
		expectedANSI256:   "hello \x1b[31mworld\x1b[m",
		expectedANSI:      "hello \x1b[31mworld\x1b[m",
		expectedAscii:     "hello \x1b[mworld\x1b[m",
	},
	{
		name:              "default fg color after ansi color",
		input:             "\x1b[31mhello \x1b[39mworld\x1b[m",
		expectedTrueColor: "\x1b[31mhello \x1b[39mworld\x1b[m",
		expectedANSI256:   "\x1b[31mhello \x1b[39mworld\x1b[m",
		expectedANSI:      "\x1b[31mhello \x1b[39mworld\x1b[m",
		expectedAscii:     "\x1b[mhello \x1b[mworld\x1b[m",
	},
	{
		name:              "ansi color fg and bg",
		input:             "\x1b[31;42mhello world\x1b[m",
		expectedTrueColor: "\x1b[31;42mhello world\x1b[m",
		expectedANSI256:   "\x1b[31;42mhello world\x1b[m",
		expectedANSI:      "\x1b[31;42mhello world\x1b[m",
		expectedAscii:     "\x1b[mhello world\x1b[m",
	},
	{
		name:              "bright ansi color fg and bg",
		input:             "\x1b[91;102mhello world\x1b[m",
		expectedTrueColor: "\x1b[91;102mhello world\x1b[m",
		expectedANSI256:   "\x1b[91;102mhello world\x1b[m",
		expectedANSI:      "\x1b[91;102mhello world\x1b[m",
		expectedAscii:     "\x1b[mhello world\x1b[m",
	},
	{
		name:              "simple 256 color fg",
		input:             "hello \x1b[38;5;196mworld\x1b[m",
		expectedTrueColor: "hello \x1b[38;5;196mworld\x1b[m",
		expectedANSI256:   "hello \x1b[38;5;196mworld\x1b[m",
		expectedANSI:      "hello \x1b[91mworld\x1b[m",
		expectedAscii:     "hello \x1b[mworld\x1b[m",
	},
	{
		name:              "256 color bg",
		input:             "\x1b[48;5;196mhello world\x1b[m",
		expectedTrueColor: "\x1b[48;5;196mhello world\x1b[m",
		expectedANSI256:   "\x1b[48;5;196mhello world\x1b[m",
		expectedANSI:      "\x1b[101mhello world\x1b[m",
		expectedAscii:     "\x1b[mhello world\x1b[m",
	},
	{
		name:              "simple true color bg",
		input:             "hello \x1b[38;2;255;133;55mworld\x1b[m", // #ff8537
		expectedTrueColor: "hello \x1b[38;2;255;133;55mworld\x1b[m",
		expectedANSI256:   "hello \x1b[38;5;209mworld\x1b[m",
		expectedANSI:      "hello \x1b[91mworld\x1b[m",
		expectedAscii:     "hello \x1b[mworld\x1b[m",
	},
	{
		name:              "itu true color bg",
		input:             "hello \x1b[38:2::255:133:55mworld\x1b[m", // #ff8537
		expectedTrueColor: "hello \x1b[38:2::255:133:55mworld\x1b[m",
		expectedANSI256:   "hello \x1b[38;5;209mworld\x1b[m",
		expectedANSI:      "hello \x1b[91mworld\x1b[m",
		expectedAscii:     "hello \x1b[mworld\x1b[m",
	},
	{
		name:              "simple ansi 256 color bg",
		input:             "hello \x1b[48:5:196mworld\x1b[m",
		expectedTrueColor: "hello \x1b[48:5:196mworld\x1b[m",
		expectedANSI256:   "hello \x1b[48;5;196mworld\x1b[m",
		expectedANSI:      "hello \x1b[101mworld\x1b[m",
		expectedAscii:     "hello \x1b[mworld\x1b[m",
	},
	{
		name:              "simple missing param",
		input:             "\x1b[31mhello \x1b[;1mworld",
		expectedTrueColor: "\x1b[31mhello \x1b[;1mworld",
		expectedANSI256:   "\x1b[31mhello \x1b[;1mworld",
		expectedANSI:      "\x1b[31mhello \x1b[;1mworld",
		expectedAscii:     "\x1b[mhello \x1b[;1mworld",
	},
	{
		name:              "color with other attributes",
		input:             "\x1b[1;38;5;204mhello \x1b[38;5;204mworld\x1b[m",
		expectedTrueColor: "\x1b[1;38;5;204mhello \x1b[38;5;204mworld\x1b[m",
		expectedANSI256:   "\x1b[1;38;5;204mhello \x1b[38;5;204mworld\x1b[m",
		expectedANSI:      "\x1b[1;91mhello \x1b[91mworld\x1b[m",
		expectedAscii:     "\x1b[1mhello \x1b[mworld\x1b[m",
	},
}

func TestWriter(t *testing.T) {
	for i, c := range writer_cases {
		for profile, writer := range writers {
			t.Run(c.name+"-"+profile.String(), func(t *testing.T) {
				var buf bytes.Buffer
				w := writer(&buf)
				_, err := w.Write([]byte(c.input))
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
				var expected string
				switch profile {
				case TrueColor:
					expected = c.expectedTrueColor
				case ANSI256:
					expected = c.expectedANSI256
				case ANSI:
					expected = c.expectedANSI
				case Ascii:
					expected = c.expectedAscii
				case NoTTY:
					expected = ansi.Strip(c.input)
				}
				if got := buf.String(); got != expected {
					t.Errorf("case: %d, got: %q, expected: %q", i+1, got, expected)
				}
			})
		}
	}
}

func TestNewWriterPanic(t *testing.T) {
	_ = NewWriter(io.Discard, []string{"TERM=dumb"})
}

func TestNewWriterOsEnviron(t *testing.T) {
	w := NewWriter(io.Discard, os.Environ())
	if w.Profile != NoTTY {
		t.Errorf("expected NoTTY, got %v", w.Profile)
	}
}

func BenchmarkWriter(b *testing.B) {
	w := &Writer{&bytes.Buffer{}, ANSI}
	input := []byte("\x1b[1;3;59mhello\x1b[m \x1b[38;2;255;133;55mworld\x1b[m")
	for i := 0; i < b.N; i++ {
		_, _ = w.Write(input)
	}
}