forked from cet001/gogeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
97 lines (83 loc) · 2.22 KB
/
main_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
package gogeo
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestParseLat(t *testing.T) {
lat, err := ParseLat("45.75")
assert.Nil(t, err)
assert.Equal(t, float32(45.75), lat)
lat, err = ParseLat("-45.75")
assert.Nil(t, err)
assert.Equal(t, float32(-45.75), lat)
_, err = ParseLat("-90.1")
assert.NotNil(t, err)
_, err = ParseLat("90.1")
assert.NotNil(t, err)
}
func TestParseLng(t *testing.T) {
lat, err := ParseLng("120.12575")
assert.Nil(t, err)
assert.Equal(t, float32(120.12575), lat)
lat, err = ParseLng("-120.12575")
assert.Nil(t, err)
assert.Equal(t, float32(-120.12575), lat)
_, err = ParseLng("-180.1")
assert.NotNil(t, err)
_, err = ParseLng("180.1")
assert.NotNil(t, err)
}
func ExampleParseLatLng() {
lat, lng, _ := ParseLatLng("50.456, -80.2468")
fmt.Println(lat)
fmt.Println(lng)
// Output:
// 50.456
// -80.2468
}
func TestParseLatLng_badInputs(t *testing.T) {
badInputs := []string{
"",
" ",
" , ",
"aaa,12.345", // lat not a number
"12.345,bbb", // lng not a number
"aaa,bbb", // both lat and lng not numbers
"11.11, 22.22, 33.33", // too many items
"11.11", // not enough items
"-91.1, 0.0", // latitude value too small. not in range [-90, +90]
"90.1, 0.0", // latitude value too large. not in range [-90, +90]
"0.0, -181.1", // longitude value too small. not in range [-180, +180]
"0.0, +181.1", // longitude value too large. not in range [-180, +180]
}
for _, badInput := range badInputs {
_, _, err := ParseLatLng(badInput)
assert.NotNil(t, err)
}
}
func TestParseFloat32(t *testing.T) {
testCases := []struct {
s string
f float32
isErr bool
}{
{s: "0", f: 0.0},
{s: "1.2345", f: 1.2345},
{s: "-1.2345", f: -1.2345},
{s: " 1.5", f: 1.5},
{s: "ABC", isErr: true},
{s: "1x", isErr: true},
{s: "", isErr: true},
}
for i, testCase := range testCases {
testCaseLabel := fmt.Sprintf("testCases[%v]", i)
f, err := parseFloat32(testCase.s)
if testCase.isErr {
assert.NotNil(t, err, "%v should have returned an error", testCaseLabel)
} else {
assert.Nil(t, err, testCaseLabel)
assert.Equal(t, testCase.f, f, testCaseLabel)
}
}
}