-
Notifications
You must be signed in to change notification settings - Fork 4
/
geo_test.go
181 lines (156 loc) · 4.33 KB
/
geo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package geo
import (
"database/sql/driver"
"encoding/json"
"errors"
"testing"
)
type cases struct {
G Geometry
// Equal tests.
Same []Geometry
Different []Geometry
// Contains tests.
Inside []Point
Outside []Point
}
// pass runs the test cases that should pass.
func (c cases) test(t *testing.T) {
for i, same := range c.Same {
if ok := c.G.Equal(same); !ok {
t.Fatalf("(case %d) expected %s and %s to be the same", i, c.G.String(), same.String())
}
}
for i, diff := range c.Different {
if ok := c.G.Equal(diff); ok {
t.Fatalf("(case %d) expected %s to not equal %s", i, c.G.String(), diff.String())
}
}
for i, p := range c.Inside {
if ok := c.G.Contains(p); !ok {
t.Fatalf("(case %d) expected %s to contain %s", i, c.G.String(), p.String())
}
}
for i, p := range c.Outside {
if ok := c.G.Contains(p); ok {
t.Fatalf("(case %d) expected %s to not contain %s", i, c.G.String(), p.String())
}
}
}
// marshalTestcases is a helper type for MarshalJSON tests.
type marshalTestcases []struct {
Input Geometry
Expected string
}
// pass runs the test cases that should pass.
func (tests marshalTestcases) pass(t *testing.T) {
for i, testcase := range tests {
got, err := testcase.Input.MarshalJSON()
if err != nil {
t.Fatal(err)
}
if string(got) != testcase.Expected {
t.Fatalf("(case %d) expected %s, got %s", i, testcase.Expected, string(got))
}
}
}
// scanTestcases is a helper type for Scan tests.
type scanTestcases []struct {
Input interface{}
Instance Geometry
Expected Geometry
}
// fail runs test cases that should fail.
func (tests scanTestcases) fail(t *testing.T) {
for i, c := range tests {
if err := c.Instance.Scan(c.Input); err == nil {
t.Fatalf("(case %d) expected error, got nil", i)
}
}
}
// pass runs test cases that should pass.
func (tests scanTestcases) pass(t *testing.T) {
for i, c := range tests {
if err := c.Instance.Scan(c.Input); err != nil {
t.Fatalf("(case %d) failed to scan: %s", i, err)
}
}
}
// unmarshalTestcases is a helper type for UnmarshalJSON tests.
type unmarshalTestcases []struct {
Input []byte
Expected Geometry
Instance Geometry
}
// pass runs the test cases that should pass.
func (tests unmarshalTestcases) pass(t *testing.T) {
for i, c := range tests {
if err := json.Unmarshal(c.Input, c.Instance); err != nil {
t.Fatal(err)
}
if !c.Instance.Equal(c.Expected) {
t.Fatalf("(unmarshal %T pass case %d) expected %s to equal %s", c.Instance, i, c.Instance.String(), c.Expected.String())
}
}
}
// fail runs the test cases that should fail.
func (tests unmarshalTestcases) fail(t *testing.T) {
for i, c := range tests {
if err := json.Unmarshal(c.Input, c.Instance); err == nil {
t.Fatalf("(unmarshal %T fail case %d) expected error, got nil", c.Instance, i)
}
}
}
// valueTestcases is a helper type for MarshalJSON tests.
type valueTestcases []struct {
Input Geometry
Expected interface{}
}
// pass runs the test cases that should pass.
func (tests valueTestcases) pass(t *testing.T) {
for i, testcase := range tests {
got, err := testcase.Input.Value()
if err != nil {
t.Fatal(err)
}
if got != testcase.Expected {
t.Fatalf("(case %d) expected %v, got %v", i, testcase.Expected, got)
}
}
}
// badGeom is a type that always returns an error from MarshalJSON and UnmarshalJSON.
type badGeom struct{}
// Equal always returns false
func (badgeom badGeom) Equal(g Geometry) bool {
return false
}
// Contains always returns false.
func (badgeom badGeom) Contains(p Point) bool {
return false
}
// MarshalJSON always returns an error.
func (badgeom badGeom) MarshalJSON() ([]byte, error) {
return nil, errors.New("bad geom")
}
// Scan always returns an error.
func (badgeom badGeom) Scan(src interface{}) error {
return errors.New("bad geom")
}
// String always returns "badgeom"
func (badgeom badGeom) String() string {
return "badgeom"
}
// UnmarshalJSON always returns an error.
func (badgeom badGeom) UnmarshalJSON(data []byte) error {
return errors.New("bad geom")
}
// Value always returns an error.
func (badgeom badGeom) Value() (driver.Value, error) {
return nil, errors.New("bad geom")
}
// Transform transforms the geometry point by point.
func (badgeom badGeom) Transform(t Transformer) {
}
// VisitCoordinates visits each point in the geometry.
func (badgeom badGeom) VisitCoordinates(v Visitor) {
}