Skip to content

Commit

Permalink
geojson: add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Jan 16, 2021
1 parent 1bf7f83 commit 3cb1eb0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
12 changes: 12 additions & 0 deletions geojson/bbox_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package geojson

import (
"reflect"
"testing"

"github.com/paulmach/orb"
)

func TestBBox(t *testing.T) {
ls := orb.LineString{{1, 3}, {0, 4}}
b := ls.Bound()

bbox := NewBBox(b)
expected := BBox{0, 3, 1, 4}
if !reflect.DeepEqual(bbox, expected) {
t.Errorf("incorrect result: %v != %v", bbox, expected)
}
}

func TestBBoxValid(t *testing.T) {
cases := []struct {
name string
Expand Down
49 changes: 49 additions & 0 deletions geojson/feature_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,55 @@ func TestUnmarshalFeatureCollection(t *testing.T) {
}
}

func TestUnmarshalFeatureCollection_errors(t *testing.T) {
t.Run("type not a string", func(t *testing.T) {
rawJSON := `
{ "type": { "foo":"bar" },
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
}
]
}`

_, err := UnmarshalFeatureCollection([]byte(rawJSON))
if _, ok := err.(*json.UnmarshalTypeError); !ok {
t.Fatalf("wrong error: %T: %v", err, err)
}
})

t.Run("bbox invalid", func(t *testing.T) {
rawJSON := `
{ "type": "FeatureCollection",
"bbox": { "foo":"bar" },
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
}
]
}`

_, err := UnmarshalFeatureCollection([]byte(rawJSON))
if _, ok := err.(*json.UnmarshalTypeError); !ok {
t.Fatalf("wrong error: %T: %v", err, err)
}
})

t.Run("features invalid", func(t *testing.T) {
rawJSON := `
{ "type": "FeatureCollection",
"features": { "foo":"bar" }
}`

_, err := UnmarshalFeatureCollection([]byte(rawJSON))
if _, ok := err.(*json.UnmarshalTypeError); !ok {
t.Fatalf("wrong error: %T: %v", err, err)
}
})
}

func TestFeatureCollectionMarshalJSON(t *testing.T) {
fc := NewFeatureCollection()
blob, err := fc.MarshalJSON()
Expand Down

0 comments on commit 3cb1eb0

Please sign in to comment.