File: sgr_test.go

package info (click to toggle)
golang-github-charmbracelet-x 0.0~git20251028.0cf22f8%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,940 kB
  • sloc: sh: 124; makefile: 5
file content (133 lines) | stat: -rw-r--r-- 2,615 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
package ansi

import "testing"

func TestSelectGraphicRendition(t *testing.T) {
	tests := []struct {
		name string
		args []Attr
		want string
	}{
		{
			name: "no attributes",
			args: []Attr{},
			want: "\x1b[m",
		},
		{
			name: "single basic attribute",
			args: []Attr{BoldAttr},
			want: "\x1b[1m",
		},
		{
			name: "multiple basic attributes",
			args: []Attr{BoldAttr, ItalicAttr, UnderlineAttr},
			want: "\x1b[1;3;4m",
		},
		{
			name: "foreground colors",
			args: []Attr{RedForegroundColorAttr, BoldAttr},
			want: "\x1b[31;1m",
		},
		{
			name: "background colors",
			args: []Attr{BlueBackgroundColorAttr, BoldAttr},
			want: "\x1b[44;1m",
		},
		{
			name: "bright colors",
			args: []Attr{BrightRedForegroundColorAttr, BrightBlueBackgroundColorAttr},
			want: "\x1b[91;104m",
		},
		{
			name: "reset attributes",
			args: []Attr{ResetAttr},
			want: "\x1b[0m",
		},
		{
			name: "negative attribute value",
			args: []Attr{-1},
			want: "\x1b[0m",
		},
		{
			name: "custom attribute value",
			args: []Attr{99},
			want: "\x1b[99m",
		},
		{
			name: "mixed known and custom attributes",
			args: []Attr{BoldAttr, 99, ItalicAttr},
			want: "\x1b[1;99;3m",
		},
		{
			name: "all text decorations",
			args: []Attr{
				BoldAttr,
				FaintAttr,
				ItalicAttr,
				UnderlineAttr,
				SlowBlinkAttr,
				ReverseAttr,
				ConcealAttr,
				StrikethroughAttr,
			},
			want: "\x1b[1;2;3;4;5;7;8;9m",
		},
		{
			name: "all color reset attributes",
			args: []Attr{
				DefaultForegroundColorAttr,
				DefaultBackgroundColorAttr,
				DefaultUnderlineColorAttr,
			},
			want: "\x1b[39;49;59m",
		},
		{
			name: "extended color attributes",
			args: []Attr{
				ExtendedForegroundColorAttr,
				ExtendedBackgroundColorAttr,
				ExtendedUnderlineColorAttr,
			},
			want: "\x1b[38;48;58m",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := SelectGraphicRendition(tt.args...); got != tt.want {
				t.Errorf("SelectGraphicRendition() = %q, want %q", got, tt.want)
			}
		})
	}
}

func TestSGR(t *testing.T) {
	// Test that SGR is an alias for SelectGraphicRendition
	tests := []struct {
		name string
		args []Attr
	}{
		{
			name: "empty args",
			args: []Attr{},
		},
		{
			name: "single arg",
			args: []Attr{BoldAttr},
		},
		{
			name: "multiple args",
			args: []Attr{BoldAttr, RedForegroundColorAttr, BlueBackgroundColorAttr},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := SGR(tt.args...)
			want := SelectGraphicRendition(tt.args...)
			if got != want {
				t.Errorf("SGR() = %q, want %q", got, want)
			}
		})
	}
}