2018-06-25 11:50:52 +08:00
|
|
|
// 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"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
2019-02-24 14:42:34 +08:00
|
|
|
"github.com/mum4k/termdash/linestyle"
|
2020-04-11 03:26:45 +08:00
|
|
|
"github.com/mum4k/termdash/private/canvas"
|
2018-06-25 11:50:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMultiEdgeNodes(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
lines []HVLine
|
|
|
|
want []*hVLineNode
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no lines added",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "single-edge nodes only",
|
|
|
|
lines: []HVLine{
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{0, 0},
|
|
|
|
End: image.Point{0, 1},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{1, 0},
|
|
|
|
End: image.Point{1, 1},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "lines don't cross",
|
|
|
|
lines: []HVLine{
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{0, 0},
|
|
|
|
End: image.Point{0, 2},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{1, 0},
|
|
|
|
End: image.Point{1, 2},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*hVLineNode{
|
|
|
|
{
|
|
|
|
p: image.Point{0, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{0, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{0, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
p: image.Point{1, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{1, 0}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{1, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "lines cross, node has two edges",
|
|
|
|
lines: []HVLine{
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{0, 0},
|
|
|
|
End: image.Point{0, 1},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{0, 0},
|
|
|
|
End: image.Point{1, 0},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*hVLineNode{
|
|
|
|
{
|
|
|
|
p: image.Point{0, 0},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{0, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{1, 0}): true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "lines cross, node has three edges",
|
|
|
|
lines: []HVLine{
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{0, 0},
|
|
|
|
End: image.Point{0, 2},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{0, 1},
|
|
|
|
End: image.Point{1, 1},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*hVLineNode{
|
|
|
|
{
|
|
|
|
p: image.Point{0, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{0, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{0, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "lines cross, node has four edges",
|
|
|
|
lines: []HVLine{
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{1, 0},
|
|
|
|
End: image.Point{1, 2},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
2019-01-13 10:13:03 +08:00
|
|
|
Start: image.Point{0, 1},
|
|
|
|
End: image.Point{2, 1},
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*hVLineNode{
|
|
|
|
{
|
|
|
|
p: image.Point{1, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{1, 0}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{2, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{1, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
c, err := canvas.New(image.Rect(0, 0, 3, 3))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("canvas.New => unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g := newHVLineGraph()
|
|
|
|
for i, l := range tc.lines {
|
2019-01-13 10:13:03 +08:00
|
|
|
line, err := newHVLine(c, l.Start, l.End, newHVLineOptions())
|
2018-06-25 11:50:52 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("newHVLine[%d] => unexpected error: %v", i, err)
|
|
|
|
}
|
|
|
|
g.addLine(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := g.multiEdgeNodes()
|
|
|
|
|
|
|
|
lessFn := func(i, j int) bool {
|
|
|
|
return got[i].p.X < got[j].p.X || got[i].p.Y < got[j].p.Y
|
|
|
|
}
|
|
|
|
sort.Slice(got, lessFn)
|
|
|
|
sort.Slice(tc.want, lessFn)
|
|
|
|
if diff := pretty.Compare(tc.want, got); diff != "" {
|
|
|
|
t.Errorf("multiEdgeNodes => unexpected diff (-want, +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeRune(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
node *hVLineNode
|
2019-02-24 14:42:34 +08:00
|
|
|
ls linestyle.LineStyle
|
2018-06-25 11:50:52 +08:00
|
|
|
want rune
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "fails on node with no edges",
|
|
|
|
node: &hVLineNode{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "fails on unsupported two edge combination",
|
|
|
|
node: &hVLineNode{
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{2, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
2018-06-25 11:50:52 +08:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "fails on unsupported three edge combination",
|
|
|
|
node: &hVLineNode{
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{0, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{2, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
2018-06-25 11:50:52 +08:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2018-06-25 12:07:33 +08:00
|
|
|
{
|
|
|
|
desc: "fails on unsupported line style",
|
|
|
|
node: &hVLineNode{},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.LineStyle(-1),
|
2018-06-25 12:07:33 +08:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2018-06-25 11:50:52 +08:00
|
|
|
{
|
|
|
|
desc: "horizontal line",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{1, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{2, 1}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][hLine],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "vertical line",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{1, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{1, 0}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{1, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][vLine],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "top left corner",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{0, 0},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{1, 0}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{0, 1}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][topLeftCorner],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "top right corner",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{2, 0},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{1, 0}, image.Point{2, 0}): true,
|
|
|
|
newHVLineEdge(image.Point{2, 0}, image.Point{2, 1}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][topRightCorner],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "bottom left corner",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{0, 2},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{0, 2}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 2}, image.Point{1, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][bottomLeftCorner],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "bottom right corner",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{2, 2},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{1, 2}, image.Point{2, 2}): true,
|
|
|
|
newHVLineEdge(image.Point{2, 1}, image.Point{2, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][bottomRightCorner],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "T horizontal and up",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{1, 2},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{1, 2}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 2}, image.Point{1, 2}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 2}, image.Point{2, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][hAndUp],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "T horizontal and down",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{1, 0},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{1, 0}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 0}, image.Point{2, 0}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 0}, image.Point{1, 1}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][hAndDown],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "T vertical and right",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{0, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{0, 0}, image.Point{0, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{0, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][vAndRight],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "T vertical and left",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{2, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{2, 0}, image.Point{2, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{2, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{2, 1}, image.Point{2, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][vAndLeft],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "cross",
|
|
|
|
node: &hVLineNode{
|
|
|
|
p: image.Point{1, 1},
|
|
|
|
edges: map[hVLineEdge]bool{
|
|
|
|
newHVLineEdge(image.Point{1, 0}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{0, 1}, image.Point{1, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{2, 1}): true,
|
|
|
|
newHVLineEdge(image.Point{1, 1}, image.Point{1, 2}): true,
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 14:42:34 +08:00
|
|
|
ls: linestyle.Light,
|
|
|
|
want: lineStyleChars[linestyle.Light][vAndH],
|
2018-06-25 11:50:52 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
got, err := tc.node.rune(tc.ls)
|
|
|
|
if (err != nil) != tc.wantErr {
|
|
|
|
t.Errorf("rune => unexpected error: %v, wantErr: %v", err, tc.wantErr)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("rune => got %c, want %c", got, tc.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|