doodle/draw/line_test.go
Noah Petherbridge 27fafdc96d Save and restore maps as JSON files
First pass at a level storage format to save and restore maps.

To save a map: press F12. It takes a screenshot PNG into the
screenshots/ folder and outputs a map JSON in the working directory.

To restore a map: "go run cmd/doodle/main.go map.json"
2018-06-17 10:31:44 -07:00

87 lines
1.3 KiB
Go

package draw_test
import (
"fmt"
"testing"
"git.kirsle.net/apps/doodle/draw"
"git.kirsle.net/apps/doodle/types"
)
func TestLine(t *testing.T) {
type task struct {
X1 int32
X2 int32
Y1 int32
Y2 int32
Expect []types.Point
}
toString := func(t task) string {
return fmt.Sprintf("Line<%d,%d -> %d,%d>",
t.X1, t.Y1,
t.X2, t.Y2,
)
}
var tasks = []task{
task{
X1: 0,
Y1: 0,
X2: 0,
Y2: 10,
Expect: []types.Point{
{X: 0, Y: 0},
{X: 0, Y: 1},
{X: 0, Y: 2},
{X: 0, Y: 3},
{X: 0, Y: 4},
{X: 0, Y: 5},
{X: 0, Y: 6},
{X: 0, Y: 7},
{X: 0, Y: 8},
{X: 0, Y: 9},
{X: 0, Y: 10},
},
},
task{
X1: 10,
Y1: 10,
X2: 15,
Y2: 15,
Expect: []types.Point{
{X: 10, Y: 10},
{X: 11, Y: 11},
{X: 12, Y: 12},
{X: 13, Y: 13},
{X: 14, Y: 14},
{X: 15, Y: 15},
},
},
}
for _, test := range tasks {
gen := draw.Line(test.X1, test.Y1, test.X2, test.Y2)
var i int
for point := range gen {
if i >= len(test.Expect) {
t.Errorf("%s: Got more pixels back than expected: %s",
toString(test),
point,
)
break
}
expect := test.Expect[i]
if expect != point {
t.Errorf("%s: at index %d I got %s but expected %s",
toString(test),
i,
point,
expect,
)
}
i++
}
}
}