1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-27 13:48:49 +08:00

Merge pull request #255 from mum4k/test-coverage

Improving test coverage
This commit is contained in:
Jakub Sobon 2020-11-14 00:49:03 -05:00 committed by GitHub
commit 13246b8df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 101 additions and 10 deletions

View File

@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- coveralls again triggers and reports on PRs. - coveralls again triggers and reports on PRs.
- improving test coverage in some modules.
## [0.12.2] - 31-Aug-2020 ## [0.12.2] - 31-Aug-2020

View File

@ -27,6 +27,7 @@ func TestNewOptions(t *testing.T) {
want *Options want *Options
}{ }{
{ {
desc: "no provided options", desc: "no provided options",
want: &Options{}, want: &Options{},
}, },
@ -72,6 +73,21 @@ func TestNewOptions(t *testing.T) {
BgColor: ColorMagenta, 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 { for _, tc := range tests {

View File

@ -0,0 +1,69 @@
package faketerm
import (
"image"
"testing"
"github.com/mum4k/termdash/cell"
)
func TestDiff(t *testing.T) {
tests := []struct {
desc string
term1 *Terminal
term2 *Terminal
wantDiff bool
}{
{
desc: "no diff on equal terminals",
term1: func() *Terminal {
t := MustNew(image.Point{2, 2})
t.SetCell(image.Point{0, 0}, 'a')
return t
}(),
term2: func() *Terminal {
t := MustNew(image.Point{2, 2})
t.SetCell(image.Point{0, 0}, 'a')
return t
}(),
wantDiff: false,
},
{
desc: "reports diff on when cell runes differ",
term1: func() *Terminal {
t := MustNew(image.Point{2, 2})
t.SetCell(image.Point{0, 0}, 'a')
return t
}(),
term2: func() *Terminal {
t := MustNew(image.Point{2, 2})
t.SetCell(image.Point{1, 1}, 'a')
return t
}(),
wantDiff: true,
},
{
desc: "reports diff on when cell options differ",
term1: func() *Terminal {
t := MustNew(image.Point{2, 2})
t.SetCell(image.Point{0, 0}, 'a', cell.Bold())
return t
}(),
term2: func() *Terminal {
t := MustNew(image.Point{2, 2})
t.SetCell(image.Point{0, 0}, 'a')
return t
}(),
wantDiff: true,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
gotDiff := Diff(tc.term1, tc.term2)
if (gotDiff != "") != tc.wantDiff {
t.Errorf("Diff -> unexpected diff while wantDiff:%v, the diff:\n%s", tc.wantDiff, gotDiff)
}
})
}
}

View File

@ -57,7 +57,7 @@ func cellOptsToStyle(opts *cell.Options, colorMode terminalapi.ColorMode) tcell.
fg = fixColor(fg, colorMode) fg = fixColor(fg, colorMode)
bg = fixColor(bg, 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) st = st.Foreground(fg).Background(bg).Bold(opts.Bold).Italic(opts.Italic).Underline(opts.Underline)
return st 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. // cellOptsToFg converts the cell options to the termbox foreground attribute.
func cellOptsToFg(opts *cell.Options) (tbx.Attribute, error) { func cellOptsToFg(opts *cell.Options) (tbx.Attribute, error) {
a := cellColor(opts.FgColor) a := cellColor(opts.FgColor)
var err error
if opts.Bold { if opts.Bold {
a |= tbx.AttrBold a |= tbx.AttrBold
} }
// FIXME: Termbox doesn't have an italics attribute // FIXME: Termbox doesn't have an italics attribute
if opts.Italic { if opts.Italic {
err = errors.New("Termbox: Unsupported attribute: Italic") return 0, errors.New("Termbox: Unsupported attribute: Italic")
} }
if opts.Underline { if opts.Underline {
a |= tbx.AttrUnderline a |= tbx.AttrUnderline
} }
// FIXME: Termbox doesn't have a strikethrough attribute // FIXME: Termbox doesn't have a strikethrough attribute
if opts.Strikethrough { 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. // cellOptsToBg converts the cell options to the termbox background attribute.

View File

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