2018-10-11 00:25:40 +03:00
|
|
|
package geojson
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tidwall/geojson/geometry"
|
|
|
|
"github.com/tidwall/pretty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
seed := time.Now().UnixNano()
|
|
|
|
println(seed)
|
|
|
|
rand.Seed(seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func R(minX, minY, maxX, maxY float64) geometry.Rect {
|
|
|
|
return geometry.Rect{
|
|
|
|
Min: geometry.Point{X: minX, Y: minY},
|
|
|
|
Max: geometry.Point{X: maxX, Y: maxY},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func P(x, y float64) geometry.Point {
|
|
|
|
return geometry.Point{X: x, Y: y}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PO(x, y float64) *Point {
|
|
|
|
return NewPoint(P(x, y))
|
|
|
|
}
|
|
|
|
|
2018-10-27 19:23:29 +03:00
|
|
|
func MPO(points []geometry.Point) *MultiPoint {
|
|
|
|
return NewMultiPoint(points)
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
func RO(minX, minY, maxX, maxY float64) *Rect {
|
|
|
|
return NewRect(R(minX, minY, maxX, maxY))
|
|
|
|
}
|
|
|
|
|
|
|
|
func LO(points []geometry.Point) *LineString {
|
2018-10-22 05:08:56 +03:00
|
|
|
return NewLineString(geometry.NewLine(points, nil))
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:23:29 +03:00
|
|
|
func L(points []geometry.Point) *geometry.Line {
|
|
|
|
return geometry.NewLine(points, geometry.DefaultIndexOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MLO(lines []*geometry.Line) *MultiLineString {
|
|
|
|
return NewMultiLineString(lines)
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
func PPO(exterior []geometry.Point, holes [][]geometry.Point) *Polygon {
|
2018-10-22 05:08:56 +03:00
|
|
|
return NewPolygon(geometry.NewPoly(exterior, holes, nil))
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func expectJSON(t testing.TB, data string, expect interface{}) Object {
|
|
|
|
if t != nil {
|
|
|
|
t.Helper()
|
|
|
|
}
|
|
|
|
return expectJSONOpts(t, data, expect, nil)
|
|
|
|
}
|
|
|
|
func expectJSONOpts(t testing.TB, data string, expect interface{}, opts *ParseOptions) Object {
|
|
|
|
if t != nil {
|
|
|
|
t.Helper()
|
|
|
|
}
|
|
|
|
var exerr error
|
|
|
|
var exstr string
|
|
|
|
switch expect := expect.(type) {
|
|
|
|
case string:
|
|
|
|
exstr = expect
|
|
|
|
case error:
|
|
|
|
exerr = expect
|
|
|
|
case nil:
|
|
|
|
exstr = data
|
|
|
|
}
|
|
|
|
obj, err := Parse(data, opts)
|
|
|
|
if err != exerr {
|
|
|
|
if t == nil {
|
|
|
|
panic(fmt.Sprintf("expected '%v', got '%v'", exerr, err))
|
|
|
|
} else {
|
|
|
|
t.Fatalf("expected '%v', got '%v'", exerr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if exstr != "" {
|
|
|
|
if cleanJSON(exstr) != cleanJSON(string(obj.AppendJSON(nil))) {
|
|
|
|
if t == nil {
|
|
|
|
panic("json mismatch")
|
|
|
|
} else {
|
|
|
|
t.Fatal("json mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func expect(t testing.TB, what bool) {
|
|
|
|
if t != nil {
|
|
|
|
t.Helper()
|
|
|
|
}
|
|
|
|
if !what {
|
|
|
|
if t == nil {
|
|
|
|
panic("exception failure")
|
|
|
|
} else {
|
|
|
|
t.Fatal("expection failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanJSON(data string) string {
|
|
|
|
var v interface{}
|
|
|
|
if err := json.Unmarshal([]byte(data), &v); err != nil {
|
|
|
|
println(string(data))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
dst, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
opts := *pretty.DefaultOptions
|
|
|
|
opts.Width = 99999999
|
|
|
|
return string(pretty.PrettyOptions(dst, &opts))
|
|
|
|
}
|