forked from briansorahan/geo
/
line_test.go
116 lines (109 loc) · 2.41 KB
/
line_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
package geo
import (
"database/sql/driver"
"testing"
)
func TestLineCompare(t *testing.T) {
// Pass
cases{
G: &Line{{1.2, 3.4}, {5.6, 7.8}, {1.4, 9.3}, {-1.7, 7.3}},
Same: []Geometry{
&Line{{1.2, 3.4}, {5.6, 7.8}, {1.4, 9.3}, {-1.7, 7.3}},
},
Different: []Geometry{
&Line{{1.2, 3.4}, {5.6, 7.8}, {1.4, 9.3}},
&Line{{1.2, 3.4}, {5.6, 7.8}, {1.4, 9.3}, {-1.4, 7.3}},
&Line{{1.2, 3.4}, {5.6, 7.8}, {1.4, 9.3}, {-1.7, 7.5}},
&Polygon{{{1.2, 3.4}, {5.6, 7.8}, {1.4, 9.3}, {-1.7, 7.5}}},
&Point{1.2, 3.4},
},
}.test(t)
}
func TestLineMarshal(t *testing.T) {
// Pass
marshalTestcases{
{
Input: &Line{{1.2, 3.4}, {5.6, 7.8}},
Expected: `{"type":"LineString","coordinates":[[1.2,3.4],[5.6,7.8]]}`,
},
}.pass(t)
}
func TestLineScan(t *testing.T) {
// Pass
for _, c := range []struct {
Input interface{}
Expected Geometry
}{
{
Input: []byte(`LINESTRING(0 0, 1 1)`),
Expected: &Line{{0, 0}, {1, 1}},
},
{
Input: `LINESTRING(0 2, 0 3, -1.12654 5.985)`,
Expected: &Line{{0, 2}, {0, 3}, {-1.12654, 5.985}},
},
} {
l := &Line{}
if err := l.Scan(c.Input); err != nil {
t.Fatalf("could not scan %v: %s", c.Input, err)
}
if !l.Compare(c.Expected) {
t.Fatalf("expected %v, got %v", c.Expected, l)
}
}
// Fail
for _, c := range []interface{}{
4, // wrong type
`LINE()`, // wrong prefix
[]byte(`LINESTRING((3 3, 2 2))`), // too many parentheses
`LINESTRING(0, 0, 1, 1)`, // should be spaces in between coordinates
} {
l := &Line{}
if err := l.Scan(c); err == nil {
t.Fatalf("expected error, got nil for %v", c)
}
}
}
func TestLineString(t *testing.T) {
// Pass
for _, testcase := range []struct {
Input Line
Expected string
}{
{
Input: Line{},
Expected: `LINESTRING EMPTY`,
},
{
Input: Line{
{1.2, 3.4},
{5.6, 7.8},
{5.8, 1.6},
},
Expected: `LINESTRING(1.2 3.4, 5.6 7.8, 5.8 1.6)`,
},
} {
if expected, got := testcase.Expected, testcase.Input.String(); expected != got {
t.Fatalf("expected %s, got %s", expected, got)
}
}
}
func TestLineValue(t *testing.T) {
for _, c := range []struct {
Input Line
Expected driver.Value
}{
{
Input: Line{{0, 0}, {1, 1}},
Expected: driver.Value(`LINESTRING(0 0, 1 1)`),
},
} {
val, err := c.Input.Value()
if err != nil {
t.Fatal(err)
}
if val != c.Expected {
t.Fatalf("expected %v, got %v", c.Expected, val)
}
}
}