Adding test coverage to cell options.

This commit is contained in:
Jakub Sobon 2020-11-14 00:39:44 -05:00
parent af3c5b9b00
commit e5e1397ce9
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
4 changed files with 31 additions and 10 deletions

View File

@ -27,6 +27,7 @@ func TestNewOptions(t *testing.T) {
want *Options
}{
{
desc: "no provided options",
want: &Options{},
},
@ -72,6 +73,21 @@ func TestNewOptions(t *testing.T) {
BgColor: ColorMagenta,
},
},
{
desc: "setting font attributes",
opts: []Option{
Bold(),
Italic(),
Underline(),
Strikethrough(),
},
want: &Options{
Bold: true,
Italic: true,
Underline: true,
Strikethrough: true,
},
},
}
for _, tc := range tests {

View File

@ -57,7 +57,7 @@ func cellOptsToStyle(opts *cell.Options, colorMode terminalapi.ColorMode) tcell.
fg = fixColor(fg, colorMode)
bg = fixColor(bg, colorMode)
// FIXME: tcell doesn't have a strikethrough style option
// FIXME: tcell doesn't have a strikethrough style option until #254 is resolved.
st = st.Foreground(fg).Background(bg).Bold(opts.Bold).Italic(opts.Italic).Underline(opts.Underline)
return st
}

View File

@ -31,22 +31,21 @@ func cellColor(c cell.Color) tbx.Attribute {
// cellOptsToFg converts the cell options to the termbox foreground attribute.
func cellOptsToFg(opts *cell.Options) (tbx.Attribute, error) {
a := cellColor(opts.FgColor)
var err error
if opts.Bold {
a |= tbx.AttrBold
}
// FIXME: Termbox doesn't have an italics attribute
if opts.Italic {
err = errors.New("Termbox: Unsupported attribute: Italic")
return 0, errors.New("Termbox: Unsupported attribute: Italic")
}
if opts.Underline {
a |= tbx.AttrUnderline
}
// FIXME: Termbox doesn't have a strikethrough attribute
if opts.Strikethrough {
err = errors.New("Termbox: Unsupported attribute: Strikethrough")
return 0, errors.New("Termbox: Unsupported attribute: Strikethrough")
}
return a, err
return a, nil
}
// cellOptsToBg converts the cell options to the termbox background attribute.

View File

@ -52,18 +52,24 @@ func TestCellColor(t *testing.T) {
func TestCellFontModifier(t *testing.T) {
tests := []struct {
opt cell.Options
want tbx.Attribute
opt cell.Options
want tbx.Attribute
wantErr bool
}{
{cell.Options{Bold: true}, tbx.AttrBold},
{cell.Options{Underline: true}, tbx.AttrUnderline},
{cell.Options{Bold: true}, tbx.AttrBold, false},
{cell.Options{Underline: true}, tbx.AttrUnderline, false},
{cell.Options{Italic: true}, 0, true},
{cell.Options{Strikethrough: true}, 0, true},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("%v", tc.opt), func(t *testing.T) {
got, err := cellOptsToFg(&tc.opt)
if (err != nil) != tc.wantErr {
t.Errorf("cellOptsToFg(%v) => unexpected error: %v, wantErr: %v", tc.opt, err, tc.wantErr)
}
if err != nil {
t.Errorf("cellOptsToFg(%v) failed: %s", tc.opt, err)
return
}
if got != tc.want {
t.Errorf("cellOptsToFg(%v) => got %v, want %v", tc.opt, got, tc.want)