elvish/pkg/ui/style_test.go
Qi Xiao 42c6c3b1aa pkg/ui: Make styled text context-insensitive, and remove the "default" color.
Styled text is not supposed to "inherit" the current SGR styling context when
written to the terminal. This has always been the intention, but not correctly
implemented. This commit fixes that for both styled segments and styled texts.
Tests are amended to account for the difference in the output.

With context insensitivity correctly implemented, there is now no need for a
"default" color. It is functionally equivalent to a lack of color.

The parsing of SGR still needs to be aware of the codes 39 (default foreground)
and 49 (default background), but these codes are now translated into FgDefault
and BgDefault, which resets the foreground and background color fields.
2022-07-24 14:31:02 +01:00

91 lines
2.5 KiB
Go

package ui
import (
"testing"
)
func TestStyleSGR(t *testing.T) {
// Test the SGR sequences of style attributes indirectly via VTString of
// Text, since that is how they are used.
testTextVTString(t, []textVTStringTest{
{T("foo", Bold), "\033[;1mfoo\033[m"},
{T("foo", Dim), "\033[;2mfoo\033[m"},
{T("foo", Italic), "\033[;3mfoo\033[m"},
{T("foo", Underlined), "\033[;4mfoo\033[m"},
{T("foo", Blink), "\033[;5mfoo\033[m"},
{T("foo", Inverse), "\033[;7mfoo\033[m"},
{T("foo", FgRed), "\033[;31mfoo\033[m"},
{T("foo", BgRed), "\033[;41mfoo\033[m"},
{T("foo", Bold, FgRed, BgBlue), "\033[;1;31;44mfoo\033[m"},
})
}
type mergeFromOptionsTest struct {
style Style
options map[string]any
wantStyle Style
wantErr string
}
var mergeFromOptionsTests = []mergeFromOptionsTest{
// Parsing of each possible key.
kv("fg-color", "red", Style{Foreground: Red}),
kv("bg-color", "red", Style{Background: Red}),
kv("bold", true, Style{Bold: true}),
kv("dim", true, Style{Dim: true}),
kv("italic", true, Style{Italic: true}),
kv("underlined", true, Style{Underlined: true}),
kv("blink", true, Style{Blink: true}),
kv("inverse", true, Style{Inverse: true}),
// Merging with existing options.
{
style: Style{Bold: true, Dim: true},
options: map[string]any{
"bold": false, "fg-color": "red",
},
wantStyle: Style{Dim: true, Foreground: Red},
},
// Bad key.
{
options: map[string]any{"bad": true},
wantErr: "unrecognized option 'bad'",
},
// Bad type for color field.
{
options: map[string]any{"fg-color": true},
wantErr: "value for option 'fg-color' must be a valid color string",
},
// Bad type for bool field.
{
options: map[string]any{"bold": ""},
wantErr: "value for option 'bold' must be a bool value",
},
}
// A helper for constructing a test case whose input is a single key-value pair.
func kv(k string, v any, s Style) mergeFromOptionsTest {
return mergeFromOptionsTest{
options: map[string]any{k: v}, wantStyle: s,
}
}
func TestMergeFromOptions(t *testing.T) {
for _, test := range mergeFromOptionsTests {
style := test.style
err := style.MergeFromOptions(test.options)
if style != test.wantStyle {
t.Errorf("(%v).MergeFromOptions(%v) -> %v, want %v",
test.style, test.options, style, test.wantStyle)
}
if err == nil {
if test.wantErr != "" {
t.Errorf("got error nil, want %v", test.wantErr)
}
} else {
if err.Error() != test.wantErr {
t.Errorf("got error %v, want error with message %s", err, test.wantErr)
}
}
}
}