-
Notifications
You must be signed in to change notification settings - Fork 4
/
point_test.go
99 lines (90 loc) · 1.68 KB
/
point_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
package geo
import "testing"
func TestPointEqual(t *testing.T) {
// Different
cases{
G: &Point{1.2, 3.4},
Different: []Geometry{
&Point{1.2, 3.7},
&Point{9.2, 3.7},
&Line{{9.2, 3.7}},
&Polygon{{{9.2, 3.7}}},
},
}.test(t)
}
func TestPointContains(t *testing.T) {
cases{
G: &Point{1, 1},
Inside: []Point{{1, 1}},
Outside: []Point{{1, 1.2}},
}.test(t)
}
func TestPointMarshalJSON(t *testing.T) {
marshalTestcases{
{
Input: &Point{1.2, 3.4},
Expected: `{"type":"Point","coordinates":[1.2,3.4]}`,
},
}.pass(t)
}
func TestPointScan(t *testing.T) {
// Good
scanTestcases{
{
Input: `POINT(1.2 3.4)`,
Instance: &Point{},
Expected: &Point{1.2, 3.4},
},
}.pass(t)
// Bad
scanTestcases{
{
Input: "POINT(1.2, 3.4)", // bad comma
Instance: &Point{},
},
{
Input: []byte("PIONT(1.4 2.3)"), // typo
Instance: &Point{},
},
{
Input: 7, // bad type
Instance: &Point{},
},
}.fail(t)
}
func TestPointUnmarshal(t *testing.T) {
unmarshalTestcases{
{
Input: []byte(`{"type":"Point","coordinates":[1,1]}`),
Instance: &Point{},
Expected: &Point{1, 1},
},
}.pass(t)
unmarshalTestcases{
{
Input: []byte(`{"type":"Punto","coordinates":[1,1]}`),
Instance: &Point{},
},
{
Input: []byte(`{"type":"Point","coordinates":"bork"}`),
Instance: &Point{},
},
}.fail(t)
}
func TestPointValue(t *testing.T) {
var (
p = &Point{1.2, 3.4}
expected = `POINT(1.2 3.4)`
)
value, err := p.Value()
if err != nil {
t.Fatal(err)
}
got, ok := value.(string)
if !ok {
t.Fatalf("expected string, got %T", value)
}
if expected != got {
t.Fatalf("expected %s, got %s", expected, got)
}
}