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

     1  package ui
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestStyleSGR(t *testing.T) {
     8  	// Test the SGR sequences of style attributes indirectly via VTString of
     9  	// Text, since that is how they are used.
    10  	testTextVTString(t, []textVTStringTest{
    11  		{T("foo", Bold), "\033[1mfoo\033[m"},
    12  		{T("foo", Dim), "\033[2mfoo\033[m"},
    13  		{T("foo", Italic), "\033[3mfoo\033[m"},
    14  		{T("foo", Underlined), "\033[4mfoo\033[m"},
    15  		{T("foo", Blink), "\033[5mfoo\033[m"},
    16  		{T("foo", Inverse), "\033[7mfoo\033[m"},
    17  		{T("foo", FgRed), "\033[31mfoo\033[m"},
    18  		{T("foo", BgRed), "\033[41mfoo\033[m"},
    19  		{T("foo", Bold, FgRed, BgBlue), "\033[1;31;44mfoo\033[m"},
    20  	})
    21  }
    22  
    23  type mergeFromOptionsTest struct {
    24  	style     Style
    25  	options   map[string]interface{}
    26  	wantStyle Style
    27  	wantErr   string
    28  }
    29  
    30  var mergeFromOptionsTests = []mergeFromOptionsTest{
    31  	// Parsing of each possible key.
    32  	kv("fg-color", "red", Style{Foreground: Red}),
    33  	kv("bg-color", "red", Style{Background: Red}),
    34  	kv("bold", true, Style{Bold: true}),
    35  	kv("dim", true, Style{Dim: true}),
    36  	kv("italic", true, Style{Italic: true}),
    37  	kv("underlined", true, Style{Underlined: true}),
    38  	kv("blink", true, Style{Blink: true}),
    39  	kv("inverse", true, Style{Inverse: true}),
    40  	// Merging with existing options.
    41  	{
    42  		style: Style{Bold: true, Dim: true},
    43  		options: map[string]interface{}{
    44  			"bold": false, "fg-color": "red",
    45  		},
    46  		wantStyle: Style{Dim: true, Foreground: Red},
    47  	},
    48  	// Bad key.
    49  	{
    50  		options: map[string]interface{}{"bad": true},
    51  		wantErr: "unrecognized option 'bad'",
    52  	},
    53  	// Bad type for color field.
    54  	{
    55  		options: map[string]interface{}{"fg-color": true},
    56  		wantErr: "value for option 'fg-color' must be a valid color string",
    57  	},
    58  	// Bad type for bool field.
    59  	{
    60  		options: map[string]interface{}{"bold": ""},
    61  		wantErr: "value for option 'bold' must be a bool value",
    62  	},
    63  }
    64  
    65  // A helper for constructing a test case whose input is a single key-value pair.
    66  func kv(k string, v interface{}, s Style) mergeFromOptionsTest {
    67  	return mergeFromOptionsTest{
    68  		options: map[string]interface{}{k: v}, wantStyle: s,
    69  	}
    70  }
    71  
    72  func TestMergeFromOptions(t *testing.T) {
    73  	for _, test := range mergeFromOptionsTests {
    74  		style := test.style
    75  		err := style.MergeFromOptions(test.options)
    76  		if style != test.wantStyle {
    77  			t.Errorf("(%v).MergeFromOptions(%v) -> %v, want %v",
    78  				test.style, test.options, style, test.wantStyle)
    79  		}
    80  		if err == nil {
    81  			if test.wantErr != "" {
    82  				t.Errorf("got error nil, want %v", test.wantErr)
    83  			}
    84  		} else {
    85  			if err.Error() != test.wantErr {
    86  				t.Errorf("got error %v, want error with message %s", err, test.wantErr)
    87  			}
    88  		}
    89  	}
    90  }