diff --git a/pkg/point/absolute_point_test.go b/pkg/point/absolute_point_test.go new file mode 100644 index 0000000..052d631 --- /dev/null +++ b/pkg/point/absolute_point_test.go @@ -0,0 +1,71 @@ +package point_test + +import ( + "math" + "testing" + + "git.wtrh.nl/patterns/gopatterns/pkg/point" + "git.wtrh.nl/patterns/gopatterns/pkg/position" + "git.wtrh.nl/patterns/gopatterns/pkg/util" + "git.wtrh.nl/patterns/gopatterns/pkg/vector" + "github.com/stretchr/testify/require" + "github.com/tdewolff/canvas" +) + +func EqualMatrix(t *testing.T, expected, actual canvas.Matrix, delta float64) { + t.Helper() + + require.InDeltaSlice(t, expected[0][:], actual[0][:], delta) + require.InDeltaSlice(t, expected[1][:], actual[1][:], delta) +} + +func TestAbsolutePoint_Matrix(t *testing.T) { + rotation := math.Pi/2 + 2 + newPoint := point.NewAbsolutePoint(1, 2, rotation, "1") + actual := newPoint.Matrix() + + expected := canvas.Matrix{ + {math.Cos(rotation), -math.Sin(rotation), 1.0}, + {math.Sin(rotation), math.Cos(rotation), 2.0}, + } + + EqualMatrix(t, actual, expected, 1e-10) +} + +func TestAbsolutePoint_ID(t *testing.T) { + newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value") + require.Equal(t, util.ID("this is a test value"), newPoint.ID()) +} + +func TestAbsolutePoint_Name(t *testing.T) { + require.Equal(t, "this is a test value", point.NewAbsolutePoint(1, 2, 3, "this is a test value").Name()) +} + +func TestAbsolutePoint_Position(t *testing.T) { + actual := point.NewAbsolutePoint(1, 2, 3, "this is a test value").Position() + expected := position.Position{ + Vector: vector.Vector{X: 1, Y: 2}, + Rotation: 3, + } + + require.Equal(t, expected, actual) +} + +func TestAbsolutePoint_Draw(t *testing.T) { + newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value") + require.False(t, newPoint.Draw()) + + newPoint.SetDraw() + require.True(t, newPoint.Draw()) + + newPoint.UnsetDraw() + require.False(t, newPoint.Draw()) +} + +func TestAbsolutePoint_Hide(t *testing.T) { + newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value") + require.False(t, newPoint.Hide()) + + newPoint.SetHide() + require.True(t, newPoint.Hide()) +} diff --git a/pkg/point/between_point_test.go b/pkg/point/between_point_test.go new file mode 100644 index 0000000..c12292d --- /dev/null +++ b/pkg/point/between_point_test.go @@ -0,0 +1,48 @@ +package point_test + +import ( + "math" + "testing" + + "git.wtrh.nl/patterns/gopatterns/pkg/point" + "git.wtrh.nl/patterns/gopatterns/pkg/position" + "git.wtrh.nl/patterns/gopatterns/pkg/position/testutil" + "git.wtrh.nl/patterns/gopatterns/pkg/vector" + "github.com/tdewolff/canvas" +) + +func TestBetweenPoint_Position(t *testing.T) { + p1 := point.NewAbsolutePoint(0, 0, 0, "1") + p2 := point.NewAbsolutePoint(1, 1, 0, "2") + p3 := point.NewBetweenPoint(p1, p2, 0.5, "3") + testutil.EqualPosition(t, position.Position{ + Vector: vector.Vector{ + X: 0.5, + Y: 0.5, + }, + Rotation: -math.Pi / 4, + }, p3.Position(), 1e-10) + + matrix := p3.Matrix() + EqualMatrix(t, canvas.Matrix{ + {math.Cos(-math.Pi / 4), -math.Sin(-math.Pi / 4), 0.5}, + {math.Sin(-math.Pi / 4), math.Cos(-math.Pi / 4), 0.5}, + }, matrix, 1e-10) + + p4 := point.NewAbsolutePoint(0, 1, 0, "4") + p5 := point.NewBetweenPoint(p3, p4, 0.5, "5") + + testutil.EqualPosition(t, position.Position{ + Vector: vector.Vector{ + X: 0.25, + Y: 0.75, + }, + Rotation: math.Pi / 4, + }, p5.Position(), 1e-10) + + matrix2 := p5.Matrix() + EqualMatrix(t, canvas.Matrix{ + {math.Cos(math.Pi / 4), -math.Sin(math.Pi / 4), 0.25}, + {math.Sin(math.Pi / 4), math.Cos(math.Pi / 4), 0.75}, + }, matrix2, 1e-10) +}