1package core
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7)
8
9func geometryTiles() []tile {
10 return []tile{{"A", 0, 0, 200, 100}, {"B", 200, 0, 400, 200}}
11}
12
13func TestScreenAt(t *testing.T) {
14 c := layoutCore(0, geometryTiles()...)
15 for _, tc := range []struct {
16 x, y float64
17 want int
18 }{
19 {199, 50, 0}, {200, 50, 1}, {250, 150, 1}, {100, 150, -1}, {-1, 0, -1},
20 } {
21 assert.Equal(t, tc.want, c.screenAt(tc.x, tc.y), "(%v,%v)", tc.x, tc.y)
22 }
23
24 p := layoutCore(0, tile{"", 0, 0, 200, 100})
25 assert.Equal(t, -1, p.screenAt(10, 10), "a placeholder is never returned")
26}
27
28func TestClampPointer(t *testing.T) {
29 c := layoutCore(0, geometryTiles()...)
30 x, y := c.clampPointer(0, 0, 100, 50)
31 assert.Equal(t, [2]float64{100, 50}, [2]float64{x, y})
32 x, y = c.clampPointer(100, 50, 100, 150)
33 assert.Equal(t, [2]float64{100, 99}, [2]float64{x, y})
34 x, y = c.clampPointer(-5, -5, -10, -10)
35 assert.Equal(t, [2]float64{0, 0}, [2]float64{x, y})
36
37 none := layoutCore(0, tile{"", 0, 0, 200, 100})
38 x, y = none.clampPointer(0, 0, 500, 500)
39 assert.Equal(t, [2]float64{500, 500}, [2]float64{x, y})
40}
41
42func TestScreenAtAllocations(t *testing.T) {
43 c := layoutCore(0, geometryTiles()...)
44 n := testing.AllocsPerRun(100, func() {
45 c.screenAt(250, 150)
46 c.clampPointer(100, 50, 100, 150)
47 })
48 assert.Zero(t, n)
49}