| 1 |
package ansi |
| 2 |
|
| 3 |
import "testing" |
| 4 |
|
| 5 |
func TestSelectGraphicRendition(t *testing.T) { |
| 6 |
tests := []struct { |
| 7 |
name string |
| 8 |
args []Attr |
| 9 |
want string |
| 10 |
}{ |
| 11 |
{ |
| 12 |
name: "no attributes", |
| 13 |
args: []Attr{}, |
| 14 |
want: "\x1b[m", |
| 15 |
}, |
| 16 |
{ |
| 17 |
name: "single basic attribute", |
| 18 |
args: []Attr{BoldAttr}, |
| 19 |
want: "\x1b[1m", |
| 20 |
}, |
| 21 |
{ |
| 22 |
name: "multiple basic attributes", |
| 23 |
args: []Attr{BoldAttr, ItalicAttr, UnderlineAttr}, |
| 24 |
want: "\x1b[1;3;4m", |
| 25 |
}, |
| 26 |
{ |
| 27 |
name: "foreground colors", |
| 28 |
args: []Attr{RedForegroundColorAttr, BoldAttr}, |
| 29 |
want: "\x1b[31;1m", |
| 30 |
}, |
| 31 |
{ |
| 32 |
name: "background colors", |
| 33 |
args: []Attr{BlueBackgroundColorAttr, BoldAttr}, |
| 34 |
want: "\x1b[44;1m", |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "bright colors", |
| 38 |
args: []Attr{BrightRedForegroundColorAttr, BrightBlueBackgroundColorAttr}, |
| 39 |
want: "\x1b[91;104m", |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "reset attributes", |
| 43 |
args: []Attr{ResetAttr}, |
| 44 |
want: "\x1b[0m", |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "negative attribute value", |
| 48 |
args: []Attr{-1}, |
| 49 |
want: "\x1b[0m", |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "custom attribute value", |
| 53 |
args: []Attr{99}, |
| 54 |
want: "\x1b[99m", |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "mixed known and custom attributes", |
| 58 |
args: []Attr{BoldAttr, 99, ItalicAttr}, |
| 59 |
want: "\x1b[1;99;3m", |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "all text decorations", |
| 63 |
args: []Attr{ |
| 64 |
BoldAttr, |
| 65 |
FaintAttr, |
| 66 |
ItalicAttr, |
| 67 |
UnderlineAttr, |
| 68 |
SlowBlinkAttr, |
| 69 |
ReverseAttr, |
| 70 |
ConcealAttr, |
| 71 |
StrikethroughAttr, |
| 72 |
}, |
| 73 |
want: "\x1b[1;2;3;4;5;7;8;9m", |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "all color reset attributes", |
| 77 |
args: []Attr{ |
| 78 |
DefaultForegroundColorAttr, |
| 79 |
DefaultBackgroundColorAttr, |
| 80 |
DefaultUnderlineColorAttr, |
| 81 |
}, |
| 82 |
want: "\x1b[39;49;59m", |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "extended color attributes", |
| 86 |
args: []Attr{ |
| 87 |
ExtendedForegroundColorAttr, |
| 88 |
ExtendedBackgroundColorAttr, |
| 89 |
ExtendedUnderlineColorAttr, |
| 90 |
}, |
| 91 |
want: "\x1b[38;48;58m", |
| 92 |
}, |
| 93 |
} |
| 94 |
|
| 95 |
for _, tt := range tests { |
| 96 |
t.Run(tt.name, func(t *testing.T) { |
| 97 |
if got := SelectGraphicRendition(tt.args...); got != tt.want { |
| 98 |
t.Errorf("SelectGraphicRendition() = %q, want %q", got, tt.want) |
| 99 |
} |
| 100 |
}) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestSGR(t *testing.T) { |
| 105 |
// Test that SGR is an alias for SelectGraphicRendition |
| 106 |
tests := []struct { |
| 107 |
name string |
| 108 |
args []Attr |
| 109 |
}{ |
| 110 |
{ |
| 111 |
name: "empty args", |
| 112 |
args: []Attr{}, |
| 113 |
}, |
| 114 |
{ |
| 115 |
name: "single arg", |
| 116 |
args: []Attr{BoldAttr}, |
| 117 |
}, |
| 118 |
{ |
| 119 |
name: "multiple args", |
| 120 |
args: []Attr{BoldAttr, RedForegroundColorAttr, BlueBackgroundColorAttr}, |
| 121 |
}, |
| 122 |
} |
| 123 |
|
| 124 |
for _, tt := range tests { |
| 125 |
t.Run(tt.name, func(t *testing.T) { |
| 126 |
got := SGR(tt.args...) |
| 127 |
want := SelectGraphicRendition(tt.args...) |
| 128 |
if got != want { |
| 129 |
t.Errorf("SGR() = %q, want %q", got, want) |
| 130 |
} |
| 131 |
}) |
| 132 |
} |
| 133 |
} |