forked from cairnapp/go-geobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
68 lines (56 loc) · 1.62 KB
/
encode_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
package geobuf
import (
"github.com/mscno/go-geobuf/internal/encode"
"github.com/paulmach/orb/geojson"
"github.com/stretchr/testify/require"
"testing"
)
func TestEncodeEmpty(t *testing.T) {
_, err := Encode(nil)
require.Error(t, err)
require.ErrorIs(t, err, ErrNilInput)
}
func TestEncodeBadType(t *testing.T) {
_, err := Encode("bad type")
require.Error(t, err)
require.ErrorIs(t, err, ErrUnsupportedType)
}
func TestEncodeEmptyFeatureCollection(t *testing.T) {
fc := geojson.NewFeatureCollection()
data, err := Encode(fc)
require.NoError(t, err)
require.NotNil(t, data)
}
func TestEncodeEmptyFeature(t *testing.T) {
fc := geojson.NewFeature(nil)
data, err := Encode(fc, WithAllowEmptyGeometry(true))
require.NoError(t, err)
require.NotNil(t, data)
fc2, err := Decode(data)
require.NoError(t, err)
require.NotNil(t, fc2)
fc3, ok := fc2.(*geojson.Feature)
require.True(t, ok)
require.NotNil(t, fc3)
require.Nil(t, fc3.Geometry)
}
var emptyGeometryFeature = `{"type":"Feature","geometry":null}`
func TestEncodeEmptyGeometryFeature(t *testing.T) {
f, err := geojson.UnmarshalFeature([]byte(emptyGeometryFeature))
data, err := Encode(f, WithAllowEmptyGeometry(false))
require.Error(t, err)
require.ErrorIs(t, err, encode.ErrEmptyGeometry)
data, err = Encode(f)
require.Error(t, err)
require.ErrorIs(t, err, encode.ErrEmptyGeometry)
data, err = Encode(f, WithAllowEmptyGeometry(true))
require.NoError(t, err)
require.NotNil(t, data)
f2, err := Decode(data)
require.NoError(t, err)
require.NotNil(t, f2)
f3, ok := f2.(*geojson.Feature)
require.True(t, ok)
require.NotNil(t, f3)
require.Nil(t, f3.Geometry)
}