| 1 | package ansi_test |
| 2 | |
| 3 | import ( |
| 4 | "image/color" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/charmbracelet/x/ansi" |
| 8 | ) |
| 9 | |
| 10 | func TestReset(t *testing.T) { |
| 11 | var s ansi.Style |
| 12 | if s.String() != "\x1b[m" { |
| 13 | t.Errorf("Unexpected reset sequence: %q", ansi.ResetStyle) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | func TestBold(t *testing.T) { |
| 18 | var s ansi.Style |
| 19 | s = s.Bold() |
| 20 | if s.String() != "\x1b[1m" { |
| 21 | t.Errorf("Unexpected bold sequence: %q", s) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestDefaultBackground(t *testing.T) { |
| 26 | var s ansi.Style |
| 27 | s = s.DefaultBackgroundColor() |
| 28 | if s.String() != "\x1b[49m" { |
| 29 | t.Errorf("Unexpected default background sequence: %q", s) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestSequence(t *testing.T) { |
| 34 | var s ansi.Style |
| 35 | s = s.Bold().Underline().ForegroundColor(ansi.ExtendedColor(255)) |
| 36 | if s.String() != "\x1b[1;4;38;5;255m" { |
| 37 | t.Errorf("Unexpected sequence: %q", s) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestColorColor(t *testing.T) { |
| 42 | s := ansi.NewStyle().Bold().Underline().ForegroundColor(color.Black) |
| 43 | if s.String() != "\x1b[1;4;38;2;0;0;0m" { |
| 44 | t.Errorf("Unexpected sequence: %q", s) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func BenchmarkStyle(b *testing.B) { |
| 49 | b.ReportAllocs() |
| 50 | for i := 0; i < b.N; i++ { |
| 51 | _ = ansi.NewStyle(). |
| 52 | Bold(). |
| 53 | DoubleUnderline(). |
| 54 | ForegroundColor(color.RGBA{255, 255, 255, 255}). |
| 55 | String() |
| 56 | } |
| 57 | } |