-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgeopoint_test.go
102 lines (98 loc) · 3.05 KB
/
geopoint_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
package schema
import (
"testing"
"github.com/matryer/is"
)
func TestCastGeoPoint(t *testing.T) {
data := []struct {
desc string
format string
value string
want GeoPoint
}{
{"DefaultNoParentheses", defaultFieldFormat, "90,40", GeoPoint{90, 40}},
{"DefaultNoParenthesesFloats", defaultFieldFormat, "90.5,40.44", GeoPoint{90.5, 40.44}},
{"DefaultNoParenthesesNegative", defaultFieldFormat, "-90.10,-40", GeoPoint{-90.10, -40}},
{"DefaultNoParenthesesEmptyFormat", "", "90,40", GeoPoint{90, 40}},
{"DefaultWithSpace", "", "90, 40", GeoPoint{90, 40}},
{"DefaultWithSpaceNegative", "", "-90, -40", GeoPoint{-90, -40}},
{"Array", GeoPointArrayFormat, "[90,40]", GeoPoint{90, 40}},
{"ArrayFloat", GeoPointArrayFormat, "[90.5,40.44]", GeoPoint{90.5, 40.44}},
{"ArrayNegative", GeoPointArrayFormat, "[-90.5,-40]", GeoPoint{-90.5, -40}},
{"ArrayWithSpace", GeoPointArrayFormat, "[90, 40]", GeoPoint{90, 40}},
{"ArrayWithSpaceNegative", GeoPointArrayFormat, "[-90, -40]", GeoPoint{-90, -40}},
{"Object", GeoPointObjectFormat, `{"lon": 90, "lat": 45}`, GeoPoint{90, 45}},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
got, err := castGeoPoint(d.format, d.value)
is.NoErr(err)
is.Equal(got, d.want)
})
}
t.Run("Error", func(t *testing.T) {
data := []struct {
desc string
format string
value string
}{
{"BadJSON", GeoPointObjectFormat, ""},
{"BadGeoPointJSON", GeoPointObjectFormat, `{"longi": 90, "lat": 45}`},
{"BadFormat", "badformat", `{"longi": 90, "lat": 45}`},
{"InvalidDefault", defaultFieldFormat, "/10,10/"},
{"InvalidArray", defaultFieldFormat, "/[10,10]/"},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
_, err := castGeoPoint(d.format, d.value)
is.True(err != nil)
})
}
})
}
func TestUncastGeoPoint(t *testing.T) {
t.Run("Success", func(t *testing.T) {
data := []struct {
desc string
format string
value interface{}
want string
}{
{"GeoPointObject", GeoPointObjectFormat, GeoPoint{10, 10}, "{Lon:10 Lat:10}"},
{"GeoPointArray", GeoPointArrayFormat, "[10,10]", "[10,10]"},
{"GeoPointDefault", defaultFieldFormat, "10,10", "10,10"},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
got, err := uncastGeoPoint(d.format, d.value)
is.NoErr(err)
is.Equal(d.want, got)
})
}
})
t.Run("Error", func(t *testing.T) {
data := []struct {
desc string
format string
value interface{}
}{
{"InvalidObjectType_Object", GeoPointObjectFormat, int(10)},
{"InvalidObjectType_Array", GeoPointArrayFormat, int(10)},
{"InvalidArray", GeoPointArrayFormat, "10,10"},
{"InvalidObjectType_Empty", "", int(10)},
{"InvalidObjectType_Default", defaultFieldFormat, int(10)},
{"InvalidDefault", defaultFieldFormat, "/10,10/"},
{"InvalidFormat", "badFormat", int(10)},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
_, err := uncastGeoPoint(d.format, d.value)
is.True(err != nil)
})
}
})
}