forked from qedus/osmpbf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
osm_types_test.go
222 lines (202 loc) · 4.39 KB
/
osm_types_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package gosmonaut
import (
"reflect"
"testing"
)
func TestOsmTypes(t *testing.T) {
// Construct some relation that contains all OSM types
relation := Relation{-10000,
NewOSMTagsFromMap(map[string]string{
"type": "multipolygon",
"abc": "xyz",
}),
[]Member{
{"outer",
Way{123,
NewOSMTagsFromMap(map[string]string{
"point": "123.345",
}),
[]Node{
{1, 2, 3,
NewOSMTagsFromMap(map[string]string{
"abc": "xyz",
"test:hello": "",
}),
},
{-100, -1.234567812345678, 0, NewOSMTags(0)},
{-99, 0, 0, NewOSMTagsFromMap(nil)},
{0, 0.5, 1.234567812345678, nil},
},
},
},
{"inner",
Node{5, 2, 3, NewOSMTagsFromMap(map[string]string{})},
},
},
}
expected := `{
"type": "relation",
"id": -10000,
"tags": {
"abc": "xyz",
"type": "multipolygon"
},
"members": [
{
"role": "outer",
"entity": {
"type": "way",
"id": 123,
"tags": {
"point": "123.345"
},
"nodes": [
{
"type": "node",
"id": 1,
"lat": 2.0000000,
"lon": 3.0000000,
"tags": {
"abc": "xyz",
"test:hello": ""
}
},
{
"type": "node",
"id": -100,
"lat": -1.2345678,
"lon": 0.0000000
},
{
"type": "node",
"id": -99,
"lat": 0.0000000,
"lon": 0.0000000
},
{
"type": "node",
"id": 0,
"lat": 0.5000000,
"lon": 1.2345678
}
]
}
},
{
"role": "inner",
"entity": {
"type": "node",
"id": 5,
"lat": 2.0000000,
"lon": 3.0000000
}
}
]
}
`
if relation.String() != expected {
t.Error("Relation doesn't match expected JSON output")
}
}
/* OSM Type Set */
func TestOsmTypeSet(t *testing.T) {
var set OSMTypeSet
testOsmTypeSet(t, set, false, false, false)
set.Set(NodeType, true)
testOsmTypeSet(t, set, true, false, false)
set.Set(WayType, true)
set.Set(RelationType, true)
testOsmTypeSet(t, set, true, true, true)
set.Set(WayType, false)
set.Set(NodeType, false)
testOsmTypeSet(t, set, false, false, true)
set = NewOSMTypeSet(true, false, true)
testOsmTypeSet(t, set, true, false, true)
}
func testOsmTypeSet(t *testing.T, set OSMTypeSet, nodeEnabled, wayEnabled, relationEnabled bool) {
if set.Get(NodeType) != nodeEnabled {
t.Error("Node enabled is wrong")
}
if set.Get(WayType) != wayEnabled {
t.Error("Way enabled is wrong")
}
if set.Get(RelationType) != relationEnabled {
t.Error("Relation enabled is wrong")
}
}
/* OSM Tags */
func TestTags(t *testing.T) {
tags := NewOSMTags(-1)
testTagsWithMap(t, tags, map[string]string{})
var tags2 OSMTags
tags2.Set("A", "5")
tags2.Set("a", "5")
testTagsWithMap(t, tags2, map[string]string{
"a": "5",
"A": "5",
})
tags3 := NewOSMTags(100)
tags3.Set("created_by", "test")
tags3.Set("addr:housenumber", "170-232")
tags3.Set("addr:interpolation", "all")
tags3.Set("addr:interpolation", "all")
tags3.Set("created_by", "Potlatch 0.5d")
testTagsWithMap(t, tags3, map[string]string{
"addr:housenumber": "170-232",
"addr:interpolation": "all",
"created_by": "Potlatch 0.5d",
})
}
func testTagsWithMap(t *testing.T, tags OSMTags, m map[string]string) {
// Check length
if tags.Len() != len(m) {
t.Error("Wrong length")
}
// Test map
if !reflect.DeepEqual(tags.Map(), m) {
t.Error("Returned wrong map")
}
for k, v := range m {
// Test get
if val, ok := tags.Get(k); ok {
if val != v {
t.Error("Got wrong value")
}
} else {
t.Error("Did not get value")
}
// Test has
if !tags.Has(k) {
t.Error("Does not contain key")
}
// Test has value
if !tags.HasValue(k, v) {
t.Error("Does not contain key/value")
}
}
// False positives
s := []string{
"",
"test",
"ADDR:housenumber",
"321",
}
for _, k := range s {
if _, ok := m[k]; ok {
continue
}
// Test get
if _, ok := tags.Get(k); ok {
t.Error("Got value for non-existing key")
}
// Test has
if tags.Has(k) {
t.Error("Contains non-existing key")
}
}
// Rebuild from map
tagsNew := NewOSMTagsFromMap(tags.Map())
if !reflect.DeepEqual(tagsNew.Map(), m) {
t.Error("Rebuild tags from map are wrong")
}
}