-
Notifications
You must be signed in to change notification settings - Fork 58
/
properties_test.go
187 lines (148 loc) · 3.99 KB
/
properties_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
package geojson
import (
"testing"
)
func propertiesTestFeature() *Feature {
rawJSON := `
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"bool":true,"falsebool":false,"int": 1,"float64": 1.2,"string":"text"}
}`
f, _ := UnmarshalFeature([]byte(rawJSON))
return f
}
func TestFeatureSetProperty(t *testing.T) {
f := NewPointFeature([]float64{1, 2})
f.Properties = nil
f.SetProperty("key", "value")
if f.PropertyMustString("key") != "value" {
t.Errorf("property not set correctly")
}
}
func TestFeaturePropertyBool(t *testing.T) {
f := propertiesTestFeature()
_, err := f.PropertyBool("random")
if err == nil {
t.Errorf("should return error if invalid key")
}
b, err := f.PropertyBool("bool")
if err != nil {
t.Errorf("should not return error if valid key")
}
if b != true {
t.Errorf("should return proper property")
}
}
func TestFeaturePropertyInt(t *testing.T) {
f := propertiesTestFeature()
_, err := f.PropertyInt("random")
if err == nil {
t.Errorf("should return error if invalid key")
}
i, err := f.PropertyInt("int")
if err != nil {
t.Errorf("should not return error if valid key")
}
if i != 1 {
t.Errorf("should return proper property")
}
}
func TestFeaturePropertyFloat64(t *testing.T) {
f := propertiesTestFeature()
_, err := f.PropertyFloat64("random")
if err == nil {
t.Errorf("should return error if invalid key")
}
i, err := f.PropertyFloat64("float64")
if err != nil {
t.Errorf("should not return error if valid key")
}
if i != 1.2 {
t.Errorf("should return proper property")
}
}
func TestFeaturePropertyString(t *testing.T) {
f := propertiesTestFeature()
_, err := f.PropertyString("random")
if err == nil {
t.Errorf("should return error if invalid key")
}
s, err := f.PropertyString("string")
if err != nil {
t.Errorf("should not return error if valid key")
}
if s != "text" {
t.Errorf("should return proper property")
}
}
func TestFeaturePropertyMustBool(t *testing.T) {
f := propertiesTestFeature()
b := f.PropertyMustBool("random", true)
if b != true {
t.Errorf("should return default if property doesn't exist")
}
b = f.PropertyMustBool("falsebool", true)
if b != false {
t.Errorf("should return proper property, with default")
}
b = f.PropertyMustBool("falsebool")
if b != false {
t.Errorf("should return proper property, without default")
}
}
func TestFeaturePropertyMustInt(t *testing.T) {
f := propertiesTestFeature()
i := f.PropertyMustInt("random", 10)
if i != 10 {
t.Errorf("should return default if property doesn't exist")
}
i = f.PropertyMustInt("int", 10)
if i != 1 {
t.Errorf("should return proper property, with default")
}
i = f.PropertyMustInt("int")
if i != 1 {
t.Errorf("should return proper property, without default")
}
f.SetProperty("true_int", 5)
i = f.PropertyMustInt("true_int")
if i != 5 {
// json decode makes all things float64,
// but manually setting will be a true int
t.Errorf("should work for true integer types")
}
i = f.PropertyMustInt("float64")
if i != 1 {
t.Errorf("should convert float64 to int")
}
}
func TestFeaturePropertyMustFloat64(t *testing.T) {
f := propertiesTestFeature()
i := f.PropertyMustFloat64("random", 10)
if i != 10 {
t.Errorf("should return default if property doesn't exist")
}
i = f.PropertyMustFloat64("float64", 10.0)
if i != 1.2 {
t.Errorf("should return proper property, with default")
}
i = f.PropertyMustFloat64("float64")
if i != 1.2 {
t.Errorf("should return proper property, without default")
}
}
func TestFeaturePropertyMustString(t *testing.T) {
f := propertiesTestFeature()
s := f.PropertyMustString("random", "something")
if s != "something" {
t.Errorf("should return default if property doesn't exist")
}
s = f.PropertyMustString("string", "something")
if s != "text" {
t.Errorf("should return proper property, with default")
}
s = f.PropertyMustString("string")
if s != "text" {
t.Errorf("should return proper property, without default")
}
}