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

More test coverage.

This commit is contained in:
Jakub Sobon 2019-04-30 16:04:25 -04:00
parent 6159007466
commit 6578c20aea
2 changed files with 80 additions and 16 deletions

View File

@ -29,22 +29,6 @@ import (
"github.com/mum4k/termdash/internal/segdisp/sixteen"
)
// segmentSize given an area for the display determines the size of individual
// segments, i.e. the width of a vertical or the height of a horizontal
// segment.
func segmentSize(ar image.Rectangle) int {
// widthPerc is the relative width of a segment to the width of the canvas.
const widthPerc = 9
s := int(math.Round(float64(ar.Dx()) * widthPerc / 100))
if s > 3 && s%2 == 0 {
// Segments with odd number of pixels in their width/height look
// better, since the spike at the top of their slopes has only one
// pixel.
s++
}
return s
}
// attributes contains attributes needed to draw the segment display.
// Refer to doc/segment_placement.svg for a visual aid and explanation of the
// usage of the square roots.

View File

@ -20,6 +20,7 @@ import (
"testing"
"github.com/kylelemons/godebug/pretty"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/internal/area"
"github.com/mum4k/termdash/internal/canvas"
"github.com/mum4k/termdash/internal/canvas/braille/testbraille"
@ -127,6 +128,85 @@ func TestDraw(t *testing.T) {
return ft
},
},
{
desc: "smallest valid display 6x5, all segments, sets cell options",
opts: []Option{
CellOpts(
cell.FgColor(cell.ColorRed),
cell.BgColor(cell.ColorGreen),
),
},
cellCanvas: image.Rect(0, 0, segdisp.MinCols, segdisp.MinRows),
update: func(d *Display) error {
for _, seg := range AllSegments() {
if err := d.SetSegment(seg); err != nil {
return err
}
}
return nil
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
bc := testbraille.MustNew(ft.Area())
opts := []segment.Option{
segment.CellOpts(
cell.FgColor(cell.ColorRed),
cell.BgColor(cell.ColorGreen),
),
}
testsegment.MustHV(bc, image.Rect(5, 6, 7, 8), segment.Horizontal, opts...) // D1
testsegment.MustHV(bc, image.Rect(5, 12, 7, 14), segment.Horizontal, opts...) // D2
testsegment.MustHV(bc, image.Rect(5, 15, 7, 17), segment.Horizontal, opts...) // D5
testbraille.MustApply(bc, ft)
return ft
},
},
{
desc: "smallest valid display 6x5, D1",
cellCanvas: image.Rect(0, 0, segdisp.MinCols, segdisp.MinRows),
update: func(d *Display) error {
return d.SetSegment(D1)
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
bc := testbraille.MustNew(ft.Area())
testsegment.MustHV(bc, image.Rect(5, 6, 7, 8), segment.Horizontal) // D1
testbraille.MustApply(bc, ft)
return ft
},
},
{
desc: "smallest valid display 6x5, D2",
cellCanvas: image.Rect(0, 0, segdisp.MinCols, segdisp.MinRows),
update: func(d *Display) error {
return d.SetSegment(D2)
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
bc := testbraille.MustNew(ft.Area())
testsegment.MustHV(bc, image.Rect(5, 12, 7, 14), segment.Horizontal) // D2
testbraille.MustApply(bc, ft)
return ft
},
},
{
desc: "smallest valid display 6x5, D3",
cellCanvas: image.Rect(0, 0, segdisp.MinCols, segdisp.MinRows),
update: func(d *Display) error {
return d.SetSegment(D3)
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
bc := testbraille.MustNew(ft.Area())
testsegment.MustHV(bc, image.Rect(5, 15, 7, 17), segment.Horizontal) // D3
testbraille.MustApply(bc, ft)
return ft
},
},
}
for _, tc := range tests {