@@ -10,7 +10,7 @@ import (
1010 "strings"
1111 "testing"
1212
13- "github.com/engelsjk/polygol/geojson"
13+ geojson "github.com/engelsjk/polygol/geojson"
1414)
1515
1616const (
2727 opsSkip = []string {}
2828)
2929
30- type TestCase struct {
30+ type testCase struct {
3131 Name string
3232 OperationType string
3333 ResultPath string
@@ -52,10 +52,9 @@ func TestEndToEnd(t *testing.T) {
5252 }
5353
5454 targetDir := path .Join (endToEndDir , target .Name ())
55-
5655 argsPath := path .Join (targetDir , "args.geojson" )
5756
58- args , err := loadGeoms (argsPath , false )
57+ args , err := loadGeoms (argsPath )
5958 if err != nil {
6059 t .Fatal (err )
6160 }
@@ -65,7 +64,7 @@ func TestEndToEnd(t *testing.T) {
6564 log .Fatal (err )
6665 }
6766
68- testCases := []TestCase {}
67+ testCases := []testCase {}
6968
7069 for _ , f := range files {
7170 if f .Name () == "args.geojson" {
@@ -79,13 +78,13 @@ func TestEndToEnd(t *testing.T) {
7978 opType := strings .TrimSuffix (fn , ext )
8079 fp := filepath .Join (targetDir , fn )
8180 if opType != "all" {
82- testCases = append (testCases , TestCase {
83- Name : fmt .Sprintf ("%s-all " , target .Name ()),
81+ testCases = append (testCases , testCase {
82+ Name : fmt .Sprintf ("%s-%s " , target .Name (), opType ),
8483 OperationType : opType ,
8584 ResultPath : fp ,
8685 })
8786 } else {
88- testCases = []TestCase {
87+ testCases = []testCase {
8988 {
9089 Name : fmt .Sprintf ("%s-union" , target .Name ()),
9190 OperationType : "union" ,
@@ -97,7 +96,7 @@ func TestEndToEnd(t *testing.T) {
9796 ResultPath : fp ,
9897 },
9998 {
100- Name : fmt .Sprintf ("%s-all " , target .Name ()),
99+ Name : fmt .Sprintf ("%s-xor " , target .Name ()),
101100 OperationType : "xor" ,
102101 ResultPath : fp ,
103102 },
@@ -119,7 +118,8 @@ func TestEndToEnd(t *testing.T) {
119118 if contains (opsSkip , testCase .OperationType ) {
120119 fmt .Printf ("skipping op type %s...\n " , testCase .OperationType )
121120 }
122- geoms , err := loadGeoms (testCase .ResultPath , true )
121+
122+ geoms , err := loadGeoms (testCase .ResultPath )
123123 if err != nil {
124124 t .Fatal (err )
125125 }
@@ -130,6 +130,7 @@ func TestEndToEnd(t *testing.T) {
130130 if err != nil {
131131 t .Error (err )
132132 }
133+
133134 expect (t , equalMultiPoly (expected , result ))
134135 })
135136 }
@@ -145,8 +146,9 @@ func contains(s []string, str string) bool {
145146 return false
146147}
147148
148- func loadGeoms (filepath string , singleFeature bool ) ([]Geom , error ) {
149+ func loadGeoms (filepath string ) ([]Geom , error ) {
149150
151+ fmt .Println (filepath )
150152 f , err := os .Open (filepath )
151153 if err != nil {
152154 return nil , err
@@ -158,25 +160,11 @@ func loadGeoms(filepath string, singleFeature bool) ([]Geom, error) {
158160 return nil , err
159161 }
160162
161- var features []* geojson.Feature
162-
163- if singleFeature {
164- f , err := geojson .UnmarshalFeature (b )
165- if err != nil {
166- return nil , err
167- }
168- features = append (features , f )
169- } else {
170- fc , err := geojson .UnmarshalFeatureCollection (b )
171- if err != nil {
172- return nil , err
173- }
174- features = append (features , fc .Features ... )
175- }
163+ newFeatures := unmarshalFeatureOrFeatureCollection (b )
176164
177- geoms := make ([]Geom , len (features ))
178- for i := range features {
179- fg := features [i ].Geometry
165+ geoms := make ([]Geom , len (newFeatures ))
166+ for i := range newFeatures {
167+ fg := newFeatures [i ].Geometry
180168 switch fg .Type {
181169 case "Polygon" :
182170 geoms [i ] = Geom {fg .Polygon }
@@ -189,3 +177,18 @@ func loadGeoms(filepath string, singleFeature bool) ([]Geom, error) {
189177
190178 return geoms , nil
191179}
180+
181+ func unmarshalFeatureOrFeatureCollection (b []byte ) []* geojson.Feature {
182+ feature , err := geojson .UnmarshalFeature (b )
183+ if err != nil {
184+ return nil
185+ }
186+ if feature .Type != "FeatureCollection" {
187+ return []* geojson.Feature {feature }
188+ }
189+ fc , err := geojson .UnmarshalFeatureCollection (b )
190+ if err != nil {
191+ return nil
192+ }
193+ return fc .Features
194+ }
0 commit comments