tile38/pkg/geojson/geojson_test.go

90 lines
3.5 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package geojson
import (
"encoding/json"
"fmt"
"log"
"testing"
)
func P(x, y float64) Position {
return Position{x, y, 0}
}
func P3(x, y, z float64) Position {
return Position{x, y, z}
}
const testPolyHoles = `
{"type":"Polygon","coordinates":[
[[0,0],[0,6],[12,-6],[12,0],[0,0]],
[[1,1],[1,2],[2,2],[2,1],[1,1]],
[[11,-1],[11,-3],[9,-1],[11,-1]]
]}`
func tPoint(x, y float64) Point {
o, err := ObjectJSON(fmt.Sprintf(`{"type":"Point","coordinates":[%f,%f]}`, x, y))
if err != nil {
log.Fatal(err)
}
return testConvertToPoint(o)
}
func doesJSONMatch(js1, js2 string) bool {
var m1, m2 map[string]interface{}
json.Unmarshal([]byte(js1), &m1)
json.Unmarshal([]byte(js2), &m2)
b1, _ := json.Marshal(m1)
b2, _ := json.Marshal(m2)
return string(b1) == string(b2)
}
func testJSON(t *testing.T, jstr string) Object {
t.Helper()
2016-03-05 02:08:16 +03:00
o, err := ObjectJSON(jstr)
if err != nil {
t.Fatal(err)
}
if !doesJSONMatch(o.JSON(), jstr) {
t.Fatalf("expected '%v', got '%v'", o.JSON(), jstr)
2016-03-05 02:08:16 +03:00
}
return o
}
func testInvalidJSON(t *testing.T, js string, expecting error) {
t.Helper()
2016-03-05 02:08:16 +03:00
_, err := ObjectJSON(js)
if err == nil {
t.Fatalf("expecting an error for json '%s'", js)
}
if err.Error() != expecting.Error() {
t.Fatalf("\nInvalid error for json:\n'%s'\ngot '%s'\nexpected '%s'", js, err.Error(), expecting.Error())
}
}
func TestInvalidJSON(t *testing.T) {
testInvalidJSON(t, `{}`, errInvalidTypeMember)
testInvalidJSON(t, `{"type":"Poin"}`, fmt.Errorf(fmtErrTypeIsUnknown, "Poin"))
testInvalidJSON(t, `{"type":"Point"}`, errCoordinatesRequired)
testInvalidJSON(t, `{"type":"Point","coordinates":[]}`, errInvalidNumberOfPositionValues)
testInvalidJSON(t, `{"type":"Point","coordinates":[1]}`, errInvalidNumberOfPositionValues)
testInvalidJSON(t, `{"type":"Point","coordinates":[1,2,"asd"]}`, errInvalidPositionValue)
testInvalidJSON(t, `{"type":"Point","coordinates":[[1,2]]}`, errInvalidPositionValue)
testInvalidJSON(t, `{"type":"Point","coordinates":[[1,2],[1,3]]}`, errInvalidPositionValue)
testInvalidJSON(t, `{"type":"MultiPoint","coordinates":[1,2]}`, errInvalidCoordinates)
2016-03-05 02:08:16 +03:00
testInvalidJSON(t, `{"type":"MultiPoint","coordinates":[[]]}`, errInvalidNumberOfPositionValues)
testInvalidJSON(t, `{"type":"MultiPoint","coordinates":[[1]]}`, errInvalidNumberOfPositionValues)
testInvalidJSON(t, `{"type":"MultiPoint","coordinates":[[1,2,"asd"]]}`, errInvalidPositionValue)
testInvalidJSON(t, `{"type":"LineString","coordinates":[]}`, errLineStringInvalidCoordinates)
testInvalidJSON(t, `{"type":"MultiLineString","coordinates":[[]]}`, errLineStringInvalidCoordinates)
testInvalidJSON(t, `{"type":"MultiLineString","coordinates":[[[]]]}`, errInvalidNumberOfPositionValues)
testInvalidJSON(t, `{"type":"MultiLineString","coordinates":[[[]]]}`, errInvalidNumberOfPositionValues)
testInvalidJSON(t, `{"type":"Polygon","coordinates":[[1,1],[2,2],[3,3],[4,4]]}`, errInvalidCoordinates)
2016-03-05 02:08:16 +03:00
testInvalidJSON(t, `{"type":"Polygon","coordinates":[[[1,1],[2,2],[3,3],[4,4]]]}`, errMustBeALinearRing)
testInvalidJSON(t, `{"type":"Polygon","coordinates":[[[1,1],[2,2],[3,3],[1,1]],[[1,1],[2,2],[3,3],[4,4]]]}`, errMustBeALinearRing)
testInvalidJSON(t, `{"type":"Point","coordinates":[1,2,3],"bbox":123}`, errBBoxInvalidType)
testInvalidJSON(t, `{"type":"Point","coordinates":[1,2,3],"bbox":[]}`, errBBoxInvalidNumberOfValues)
testInvalidJSON(t, `{"type":"Point","coordinates":[1,2,3],"bbox":[1,2,3]}`, errBBoxInvalidNumberOfValues)
testInvalidJSON(t, `{"type":"Point","coordinates":[1,2,3],"bbox":[1,2,3,"a"]}`, errBBoxInvalidValue)
}