github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/ui/color_test.go (about)

     1  package ui
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestColorSGR(t *testing.T) {
     9  	// Test the SGR sequences of colors indirectly via VTString of Text, since
    10  	// that is how they are used.
    11  	testTextVTString(t, []textVTStringTest{
    12  		{T("foo", FgRed), "\033[31mfoo\033[m"},
    13  		{T("foo", BgRed), "\033[41mfoo\033[m"},
    14  
    15  		{T("foo", FgBrightRed), "\033[91mfoo\033[m"},
    16  		{T("foo", BgBrightRed), "\033[101mfoo\033[m"},
    17  
    18  		{T("foo", Fg(XTerm256Color(30))), "\033[38;5;30mfoo\033[m"},
    19  		{T("foo", Bg(XTerm256Color(30))), "\033[48;5;30mfoo\033[m"},
    20  
    21  		{T("foo", Fg(TrueColor(30, 40, 50))), "\033[38;2;30;40;50mfoo\033[m"},
    22  		{T("foo", Bg(TrueColor(30, 40, 50))), "\033[48;2;30;40;50mfoo\033[m"},
    23  	})
    24  }
    25  
    26  var colorStringTests = []struct {
    27  	color Color
    28  	str   string
    29  }{
    30  	{Red, "red"},
    31  	{BrightRed, "bright-red"},
    32  	{XTerm256Color(30), "color30"},
    33  	{TrueColor(0x33, 0x44, 0x55), "#334455"},
    34  }
    35  
    36  func TestColorString(t *testing.T) {
    37  	for _, test := range colorStringTests {
    38  		s := test.color.String()
    39  		if s != test.str {
    40  			t.Errorf("%v.String() -> %q, want %q", test.color, s, test.str)
    41  		}
    42  	}
    43  }
    44  
    45  func TestParseColor(t *testing.T) {
    46  	for _, test := range colorStringTests {
    47  		c := parseColor(test.str)
    48  		if !reflect.DeepEqual(c, test.color) {
    49  			t.Errorf("parseError(%q) -> %v, want %v", test.str, c, test.color)
    50  		}
    51  	}
    52  }