-
Notifications
You must be signed in to change notification settings - Fork 58
/
feature_collection_test.go
107 lines (91 loc) · 2.56 KB
/
feature_collection_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
package geojson
import (
"bytes"
"encoding/json"
"testing"
)
func TestNewFeatureCollection(t *testing.T) {
fc := NewFeatureCollection()
if fc.Type != "FeatureCollection" {
t.Errorf("should have type of FeatureCollection, got %v", fc.Type)
}
}
func TestUnmarshalFeatureCollection(t *testing.T) {
rawJSON := `
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
}`
fc, err := UnmarshalFeatureCollection([]byte(rawJSON))
if err != nil {
t.Fatalf("should unmarshal feature collection without issue, err %v", err)
}
if fc.Type != "FeatureCollection" {
t.Errorf("should have type of FeatureCollection, got %v", fc.Type)
}
if len(fc.Features) != 3 {
t.Errorf("should have 3 features but got %d", len(fc.Features))
}
}
func TestFeatureCollectionMarshalJSON(t *testing.T) {
fc := NewFeatureCollection()
fc.Features = nil
blob, err := fc.MarshalJSON()
if err != nil {
t.Fatalf("should marshal to json just fine but got %v", err)
}
if !bytes.Contains(blob, []byte(`"features":[]`)) {
t.Errorf("json should set features object to at least empty array")
}
}
func TestFeatureCollectionMarshal(t *testing.T) {
fc := NewFeatureCollection()
blob, err := json.Marshal(fc)
fc.Features = nil
if err != nil {
t.Fatalf("should marshal to json just fine but got %v", err)
}
if !bytes.Contains(blob, []byte(`"features":[]`)) {
t.Errorf("json should set features object to at least empty array")
}
}
func TestFeatureCollectionMarshalValue(t *testing.T) {
fc := NewFeatureCollection()
fc.Features = nil
blob, err := json.Marshal(*fc)
if err != nil {
t.Fatalf("should marshal to json just fine but got %v", err)
}
if !bytes.Contains(blob, []byte(`"features":[]`)) {
t.Errorf("json should set features object to at least empty array")
}
}