File: style_test.go

package info (click to toggle)
golang-github-charmbracelet-x 0.0~git20240809.9ab0ca0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,004 kB
  • sloc: sh: 55; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,168 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
package ansi_test

import (
	"image/color"
	"testing"

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

func TestReset(t *testing.T) {
	var s ansi.Style
	if s.String() != "\x1b[m" {
		t.Errorf("Unexpected reset sequence: %q", ansi.ResetStyle)
	}
}

func TestBold(t *testing.T) {
	var s ansi.Style
	s = s.Bold()
	if s.String() != "\x1b[1m" {
		t.Errorf("Unexpected bold sequence: %q", s)
	}
}

func TestDefaultBackground(t *testing.T) {
	var s ansi.Style
	s = s.DefaultBackgroundColor()
	if s.String() != "\x1b[49m" {
		t.Errorf("Unexpected default background sequence: %q", s)
	}
}

func TestSequence(t *testing.T) {
	var s ansi.Style
	s = s.Bold().Underline().ForegroundColor(ansi.ExtendedColor(255))
	if s.String() != "\x1b[1;4;38;5;255m" {
		t.Errorf("Unexpected sequence: %q", s)
	}
}

func TestColorColor(t *testing.T) {
	var s ansi.Style
	s = s.Bold().Underline().ForegroundColor(color.Black)
	if s.String() != "\x1b[1;4;38;2;0;0;0m" {
		t.Errorf("Unexpected sequence: %q", s)
	}
}

func BenchmarkStyle(b *testing.B) {
	s := ansi.Style{}.Bold().Underline().ForegroundColor(color.RGBA{255, 255, 255, 255})
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		_ = s.String()
	}
}