-
Notifications
You must be signed in to change notification settings - Fork 48
/
object_test.go
207 lines (180 loc) · 3.71 KB
/
object_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
package osm
import (
"reflect"
"testing"
)
func TestParseObjectID(t *testing.T) {
cases := []struct {
name string
string string
id ObjectID
}{
{
name: "node",
id: NodeID(0).ObjectID(1),
},
{
name: "zero version node",
id: NodeID(3).ObjectID(0),
},
{
name: "way",
id: WayID(10).ObjectID(2),
},
{
name: "relation",
id: RelationID(100).ObjectID(3),
},
{
name: "changeset",
id: ChangesetID(1000).ObjectID(),
},
{
name: "note",
id: NoteID(10000).ObjectID(),
},
{
name: "user",
id: UserID(5000).ObjectID(),
},
{
name: "node feature",
string: "node/100",
id: NodeID(100).ObjectID(0),
},
{
name: "bounds",
string: "bounds/0",
id: (&Bounds{}).ObjectID(),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var (
id ObjectID
err error
)
if tc.string == "" {
id, err = ParseObjectID(tc.id.String())
if err != nil {
t.Errorf("parse error: %v", err)
return
}
} else {
id, err = ParseObjectID(tc.string)
if err != nil {
t.Errorf("parse error: %v", err)
return
}
}
if id != tc.id {
t.Errorf("incorrect id: %v != %v", id, tc.id)
}
})
}
// errors
if _, err := ParseObjectID("123"); err == nil {
t.Errorf("should return error if only one part")
}
if _, err := ParseObjectID("node/1:1:1"); err == nil {
t.Errorf("should return error if multiple :")
}
if _, err := ParseObjectID("node/abc:1"); err == nil {
t.Errorf("should return error if id not a number")
}
if _, err := ParseObjectID("node/1:abc"); err == nil {
t.Errorf("should return error if version not a number")
}
if _, err := ParseObjectID("lake/1:1"); err == nil {
t.Errorf("should return error if not a valid type")
}
}
func TestObjects_ObjectIDs(t *testing.T) {
es := Objects{
&Node{ID: 1, Version: 5},
&Way{ID: 2, Version: 6},
&Relation{ID: 3, Version: 7},
&Node{ID: 4, Version: 8},
&User{ID: 5},
&Note{ID: 6},
}
expected := ObjectIDs{
NodeID(1).ObjectID(5),
WayID(2).ObjectID(6),
RelationID(3).ObjectID(7),
NodeID(4).ObjectID(8),
UserID(5).ObjectID(),
NoteID(6).ObjectID(),
}
if ids := es.ObjectIDs(); !reflect.DeepEqual(ids, expected) {
t.Errorf("incorrect ids: %v", ids)
}
}
func TestObjectID_implementations(t *testing.T) {
type oid interface {
ObjectID() ObjectID
}
var _ oid = ElementID(0)
var _ oid = &Node{}
var _ oid = &Way{}
var _ oid = &Relation{}
var _ oid = &Changeset{}
var _ oid = &Note{}
var _ oid = &User{}
var _ oid = ChangesetID(0)
var _ oid = NoteID(0)
var _ oid = UserID(0)
type oidv interface {
ObjectID(v int) ObjectID
}
var _ oidv = FeatureID(0)
var _ oidv = NodeID(0)
var _ oidv = WayID(0)
var _ oidv = RelationID(0)
// These should not implement the ObjectID methods
noImplement := []interface{}{
WayNode{},
Member{},
}
for _, ni := range noImplement {
if _, ok := ni.(oid); ok {
t.Errorf("%T should not have ObjectID() method", ni)
}
if _, ok := ni.(oidv); ok {
t.Errorf("%T should not have ObjectID(v int) method", ni)
}
}
}
func TestObject_implementations(t *testing.T) {
var _ Object = &Node{}
var _ Object = &Way{}
var _ Object = &Relation{}
var _ Object = &Changeset{}
var _ Object = &Note{}
var _ Object = &User{}
// These should not implement the Object interface
noImplement := []interface{}{
ObjectID(0),
FeatureID(0),
ElementID(0),
WayNode{},
Member{},
NodeID(0),
WayID(0),
RelationID(0),
ChangesetID(0),
NoteID(0),
UserID(0),
Nodes{},
Ways{},
Relations{},
Changesets{},
Notes{},
Users{},
}
for _, ni := range noImplement {
if _, ok := ni.(Object); ok {
t.Errorf("%T should not be an object", ni)
}
}
}