1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-28 13:48:51 +08:00
termdash/draw/border_test.go

160 lines
5.1 KiB
Go
Raw Normal View History

// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package draw
import (
"image"
"testing"
"github.com/mum4k/termdash/canvas"
2018-04-07 14:24:55 +02:00
"github.com/mum4k/termdash/canvas/testcanvas"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/terminal/faketerm"
)
func TestBorder(t *testing.T) {
tests := []struct {
desc string
canvas image.Rectangle
border image.Rectangle
opts []BorderOption
2018-04-07 14:24:55 +02:00
want func(size image.Point) *faketerm.Terminal
wantErr bool
}{
{
desc: "border is larger than canvas",
canvas: image.Rect(0, 0, 1, 1),
border: image.Rect(0, 0, 2, 2),
wantErr: true,
},
{
desc: "border is too small",
canvas: image.Rect(0, 0, 2, 2),
border: image.Rect(0, 0, 1, 1),
wantErr: true,
},
{
desc: "unsupported line style",
canvas: image.Rect(0, 0, 4, 4),
border: image.Rect(0, 0, 2, 2),
opts: []BorderOption{
BorderLineStyle(LineStyle(-1)),
},
wantErr: true,
},
{
desc: "draws border around the canvas",
canvas: image.Rect(0, 0, 4, 4),
border: image.Rect(0, 0, 4, 4),
2018-04-07 14:24:55 +02:00
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
c := testcanvas.MustNew(ft.Area())
testcanvas.MustSetCell(c, image.Point{0, 0}, lineStyleChars[LineStyleLight][topLeftCorner])
testcanvas.MustSetCell(c, image.Point{0, 1}, lineStyleChars[LineStyleLight][vLine])
testcanvas.MustSetCell(c, image.Point{0, 2}, lineStyleChars[LineStyleLight][vLine])
testcanvas.MustSetCell(c, image.Point{0, 3}, lineStyleChars[LineStyleLight][bottomLeftCorner])
testcanvas.MustSetCell(c, image.Point{1, 0}, lineStyleChars[LineStyleLight][hLine])
testcanvas.MustSetCell(c, image.Point{1, 3}, lineStyleChars[LineStyleLight][hLine])
testcanvas.MustSetCell(c, image.Point{2, 0}, lineStyleChars[LineStyleLight][hLine])
testcanvas.MustSetCell(c, image.Point{2, 3}, lineStyleChars[LineStyleLight][hLine])
testcanvas.MustSetCell(c, image.Point{3, 0}, lineStyleChars[LineStyleLight][topRightCorner])
testcanvas.MustSetCell(c, image.Point{3, 1}, lineStyleChars[LineStyleLight][vLine])
testcanvas.MustSetCell(c, image.Point{3, 2}, lineStyleChars[LineStyleLight][vLine])
testcanvas.MustSetCell(c, image.Point{3, 3}, lineStyleChars[LineStyleLight][bottomRightCorner])
testcanvas.MustApply(c, ft)
return ft
},
},
{
desc: "draws border in the canvas",
canvas: image.Rect(0, 0, 4, 4),
border: image.Rect(1, 1, 3, 3),
2018-04-07 14:24:55 +02:00
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
c := testcanvas.MustNew(ft.Area())
testcanvas.MustSetCell(c, image.Point{1, 1}, lineStyleChars[LineStyleLight][topLeftCorner])
testcanvas.MustSetCell(c, image.Point{1, 2}, lineStyleChars[LineStyleLight][bottomLeftCorner])
testcanvas.MustSetCell(c, image.Point{2, 1}, lineStyleChars[LineStyleLight][topRightCorner])
testcanvas.MustSetCell(c, image.Point{2, 2}, lineStyleChars[LineStyleLight][bottomRightCorner])
testcanvas.MustApply(c, ft)
return ft
},
},
{
desc: "draws border with cell options",
canvas: image.Rect(0, 0, 4, 4),
border: image.Rect(1, 1, 3, 3),
opts: []BorderOption{
BorderCellOpts(cell.FgColor(cell.ColorRed)),
},
2018-04-07 14:24:55 +02:00
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
c := testcanvas.MustNew(ft.Area())
testcanvas.MustSetCell(c, image.Point{1, 1},
lineStyleChars[LineStyleLight][topLeftCorner], cell.FgColor(cell.ColorRed))
testcanvas.MustSetCell(c, image.Point{1, 2},
lineStyleChars[LineStyleLight][bottomLeftCorner], cell.FgColor(cell.ColorRed))
testcanvas.MustSetCell(c, image.Point{2, 1},
lineStyleChars[LineStyleLight][topRightCorner], cell.FgColor(cell.ColorRed))
testcanvas.MustSetCell(c, image.Point{2, 2},
lineStyleChars[LineStyleLight][bottomRightCorner], cell.FgColor(cell.ColorRed))
testcanvas.MustApply(c, ft)
return ft
},
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
c, err := canvas.New(tc.canvas)
if err != nil {
t.Fatalf("canvas.New => unexpected error: %v", err)
}
err = Border(c, tc.border, tc.opts...)
if (err != nil) != tc.wantErr {
t.Errorf("Border => unexpected error: %v, wantErr: %v", err, tc.wantErr)
}
if err != nil {
return
}
2018-04-07 14:24:55 +02:00
got, err := faketerm.New(c.Size())
if err != nil {
t.Fatalf("faketerm.New => unexpected error: %v", err)
}
2018-04-07 14:24:55 +02:00
if err := c.Apply(got); err != nil {
t.Fatalf("Apply => unexpected error: %v", err)
}
2018-04-07 14:24:55 +02:00
if diff := faketerm.Diff(tc.want(c.Size()), got); diff != "" {
t.Errorf("Border => %v", diff)
}
})
}
}