-
Notifications
You must be signed in to change notification settings - Fork 4
/
unmarshal_test.go
129 lines (115 loc) · 3.16 KB
/
unmarshal_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
package geo
import (
"encoding/json"
"testing"
)
func TestUnmarshalFail(t *testing.T) {
for i, data := range [][]byte{
// Invalid JSON
[]byte(`($*&%*)(`),
// Invalid feature
[]byte(`{"type":"Feature","geometry":{"type":"Point","coordinates":{"foo":"bar"}}}`),
// Invalid feature collection
[]byte(`{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":{"foo":"bar"}}}]}`),
} {
if _, err := UnmarshalJSON(data); err == nil {
t.Fatalf("(case %d) expected error, got nil", i)
}
}
}
func TestUnmarshalFeature(t *testing.T) {
// Pass
for i, testcase := range []struct {
Input []byte
Expected Geometry
}{
{
Input: []byte(`{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]}}`),
Expected: &Feature{Geometry: &Point{1, 2}},
},
} {
geom, err := UnmarshalJSON(testcase.Input)
if err != nil {
t.Fatalf("(case %d) %s", i, err)
}
if expected, got := testcase.Expected, geom; !expected.Equal(got) {
t.Fatalf("(case %d) expected %#v, got %#v", i, expected, got)
}
}
// Fail
for i, input := range [][]byte{
[]byte(`{"type":"Feature","geometry":{"type":"Point","}}`),
} {
if _, err := UnmarshalJSON(input); err == nil {
t.Fatalf("(case %d) expected error, got nil", i)
}
}
// Test the private feature type used for unmarshalling.
f := &feature{
Geometry: json.RawMessage(`{"type":"Point}`),
}
if _, err := f.ToFeature(); err == nil {
t.Fatal("expected error, got nil")
}
}
func TestUnmarshalFeatureCollection(t *testing.T) {
// Pass
for i, testcase := range []struct {
Input []byte
Expected Geometry
}{
{
Input: []byte(`{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]}}]}`),
Expected: &FeatureCollection{
{Geometry: &Point{1, 2}},
},
},
} {
geom, err := UnmarshalJSON(testcase.Input)
if err != nil {
t.Fatalf("(case %d) %s", i, err)
}
if expected, got := testcase.Expected, geom; !expected.Equal(got) {
t.Fatalf("(case %d) expected %#v, got %#v", i, expected, got)
}
}
// Fail
for i, input := range [][]byte{
[]byte(`{"type":"FeatureCollection","features":3,"bbox":[]}`),
[]byte(``),
} {
if _, err := UnmarshalJSON(input); err == nil {
t.Fatalf("(case %d) expected error, got nil", i)
}
}
}
func TestUnmarshalGeometryCollection(t *testing.T) {
for i, testcase := range []struct {
Input []byte
Expected Geometry
}{
{
Input: []byte(`{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[1,2]}]}`),
Expected: &GeometryCollection{
&Point{1, 2},
},
},
} {
geom, err := UnmarshalJSON(testcase.Input)
if err != nil {
t.Fatalf("(case %d) %s", i, err)
}
if expected, got := testcase.Expected, geom; !expected.Equal(got) {
t.Fatalf("(case %d) expected %#v, got %#v", i, expected, got)
}
}
// Fail
for i, input := range [][]byte{
[]byte(`{"type":"GeometryCollection","geometries":3,"bbox":[]}`),
[]byte(`{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":42}],"bbox":[]}`),
} {
if _, err := UnmarshalJSON(input); err == nil {
t.Fatalf("(case %d) expected error, got nil", i)
}
}
}