mirror of https://github.com/mum4k/termdash.git
Improving test coverage of the Gauge widget.
This commit is contained in:
parent
3767faf476
commit
0b75d06a3f
|
@ -163,14 +163,10 @@ func (g *Gauge) progressText() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
switch g.pt {
|
||||
case progressTypePercent:
|
||||
if g.pt == progressTypePercent {
|
||||
return fmt.Sprintf("%d%%", g.current)
|
||||
case progressTypeAbsolute:
|
||||
return fmt.Sprintf("%d/%d", g.current, g.total)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%d/%d", g.current, g.total)
|
||||
}
|
||||
|
||||
// gaugeText returns full text to be displayed within the gauge, i.e. the
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package gauge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"testing"
|
||||
|
||||
|
@ -27,6 +28,7 @@ import (
|
|||
"github.com/mum4k/termdash/internal/draw/testdraw"
|
||||
"github.com/mum4k/termdash/internal/faketerm"
|
||||
"github.com/mum4k/termdash/linestyle"
|
||||
"github.com/mum4k/termdash/terminal/terminalapi"
|
||||
"github.com/mum4k/termdash/widgetapi"
|
||||
)
|
||||
|
||||
|
@ -67,6 +69,58 @@ func TestGauge(t *testing.T) {
|
|||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
desc: "fails on negative height",
|
||||
opts: []Option{
|
||||
Height(-1),
|
||||
},
|
||||
canvas: image.Rect(0, 0, 10, 3),
|
||||
want: func(size image.Point) *faketerm.Terminal {
|
||||
return faketerm.MustNew(size)
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
desc: "gauge without progress text",
|
||||
opts: []Option{
|
||||
Char('o'),
|
||||
HideTextProgress(),
|
||||
},
|
||||
percent: &percentCall{p: 35},
|
||||
canvas: image.Rect(0, 0, 10, 3),
|
||||
want: func(size image.Point) *faketerm.Terminal {
|
||||
ft := faketerm.MustNew(size)
|
||||
c := testcanvas.MustNew(ft.Area())
|
||||
|
||||
testdraw.MustRectangle(c, image.Rect(0, 0, 3, 3),
|
||||
draw.RectChar('o'),
|
||||
draw.RectCellOpts(cell.BgColor(cell.ColorGreen)),
|
||||
)
|
||||
testcanvas.MustApply(c, ft)
|
||||
return ft
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "sets gauge color",
|
||||
opts: []Option{
|
||||
Char('o'),
|
||||
HideTextProgress(),
|
||||
Color(cell.ColorBlue),
|
||||
},
|
||||
percent: &percentCall{p: 35},
|
||||
canvas: image.Rect(0, 0, 10, 3),
|
||||
want: func(size image.Point) *faketerm.Terminal {
|
||||
ft := faketerm.MustNew(size)
|
||||
c := testcanvas.MustNew(ft.Area())
|
||||
|
||||
testdraw.MustRectangle(c, image.Rect(0, 0, 3, 3),
|
||||
draw.RectChar('o'),
|
||||
draw.RectCellOpts(cell.BgColor(cell.ColorBlue)),
|
||||
)
|
||||
testcanvas.MustApply(c, ft)
|
||||
return ft
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "gauge showing percentage",
|
||||
opts: []Option{
|
||||
|
@ -87,6 +141,24 @@ func TestGauge(t *testing.T) {
|
|||
return ft
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "fails when Percent is less than zero",
|
||||
opts: []Option{
|
||||
Char('o'),
|
||||
},
|
||||
percent: &percentCall{p: -1},
|
||||
canvas: image.Rect(0, 0, 10, 3),
|
||||
wantUpdateErr: true,
|
||||
},
|
||||
{
|
||||
desc: "fails when Percent is more than 100",
|
||||
opts: []Option{
|
||||
Char('o'),
|
||||
},
|
||||
percent: &percentCall{p: 101},
|
||||
canvas: image.Rect(0, 0, 10, 3),
|
||||
wantUpdateErr: true,
|
||||
},
|
||||
{
|
||||
desc: "draws resize needed character when canvas is smaller than requested",
|
||||
opts: []Option{
|
||||
|
@ -315,6 +387,33 @@ func TestGauge(t *testing.T) {
|
|||
return ft
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "fails when Absolute done is negative",
|
||||
opts: []Option{
|
||||
Char('o'),
|
||||
},
|
||||
absolute: &absoluteCall{done: -1, total: 100},
|
||||
canvas: image.Rect(0, 0, 10, 3),
|
||||
wantUpdateErr: true,
|
||||
},
|
||||
{
|
||||
desc: "fails when Absolute total is zero",
|
||||
opts: []Option{
|
||||
Char('o'),
|
||||
},
|
||||
absolute: &absoluteCall{done: 0, total: 0},
|
||||
canvas: image.Rect(0, 0, 10, 3),
|
||||
wantUpdateErr: true,
|
||||
},
|
||||
{
|
||||
desc: "fails when Absolute total is less than done",
|
||||
opts: []Option{
|
||||
Char('o'),
|
||||
},
|
||||
absolute: &absoluteCall{done: 10, total: 5},
|
||||
canvas: image.Rect(0, 0, 10, 3),
|
||||
wantUpdateErr: true,
|
||||
},
|
||||
{
|
||||
desc: "gauge without text progress",
|
||||
opts: []Option{
|
||||
|
@ -804,6 +903,46 @@ func TestGauge(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestKeyboard(t *testing.T) {
|
||||
g, err := New()
|
||||
if err != nil {
|
||||
t.Fatalf("New => unexpected error: %v", err)
|
||||
}
|
||||
if err := g.Keyboard(&terminalapi.Keyboard{}); err == nil {
|
||||
t.Errorf("Keyboard => got nil err, wanted one")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMouse(t *testing.T) {
|
||||
g, err := New()
|
||||
if err != nil {
|
||||
t.Fatalf("New => unexpected error: %v", err)
|
||||
}
|
||||
if err := g.Mouse(&terminalapi.Mouse{}); err == nil {
|
||||
t.Errorf("Mouse => got nil err, wanted one")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProgressTypeString(t *testing.T) {
|
||||
tests := []struct {
|
||||
pt progressType
|
||||
want string
|
||||
}{
|
||||
{progressType(-1), "progressTypeUnknown"},
|
||||
{progressTypePercent, "progressTypePercent"},
|
||||
{progressTypeAbsolute, "progressTypeAbsolute"},
|
||||
}
|
||||
|
||||
for i, tc := range tests {
|
||||
t.Run(fmt.Sprintf("case(%d)", i), func(t *testing.T) {
|
||||
got := tc.pt.String()
|
||||
if tc.want != got {
|
||||
t.Errorf("String => %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptions(t *testing.T) {
|
||||
tests := []struct {
|
||||
desc string
|
||||
|
|
Loading…
Reference in New Issue