From 1388d8ef80c32a63180bf025ceadf11606500b6c Mon Sep 17 00:00:00 2001 From: shawnlaffan Date: Sat, 18 Nov 2023 12:07:21 +1100 Subject: [PATCH 1/2] vsistdout.t: write method needs to return 1 Also generate two features to ensure the write works properly. Otherwise a single feature was being written but followed by a failure. --- t/vsistdout.t | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/t/vsistdout.t b/t/vsistdout.t index 02be848..61ff542 100644 --- a/t/vsistdout.t +++ b/t/vsistdout.t @@ -19,9 +19,10 @@ use JSON; sub write { my $line = shift; push @output, $line; + return 1; } sub close { - push @output, "end"; + return 1; } sub output { my $output = join '', @output; @@ -33,14 +34,17 @@ use JSON; # test vsistdout redirection if(1){ # create a small layer and copy it to vsistdout with redirection - my $layer = GetDriver('Memory')->Create->CreateLayer({GeometryType => 'None'}); + my $ds = GetDriver('Memory')->Create; + my $layer = $ds->CreateLayer({GeometryType => 'None'}); $layer->CreateField(value => 'Integer'); $layer->CreateGeomField(geom => 'Point'); - my $feature = Geo::GDAL::FFI::Feature->new($layer->GetDefn); - $feature->SetField(value => 12); - $feature->SetGeomField(geom => [WKT => 'POINT(1 1)']); - $layer->CreateFeature($feature); - + for my $i (1..2) { + my $feature = Geo::GDAL::FFI::Feature->new($layer->GetDefn); + $feature->SetField(value => 12); + $feature->SetGeomField(geom => [WKT => "POINT(1 $i)"]); + $layer->CreateFeature($feature); + } + $ds->FlushCache; my $output = Output->new; my $gdal = Geo::GDAL::FFI->get_instance; $gdal->SetWriter($output); @@ -48,16 +52,18 @@ if(1){ $gdal->CloseWriter; my $ret = $output->output; - ok($ret eq - '{"type": "FeatureCollection",'. - '"features": '. - '[{ "type": "Feature", "id": 0, "properties": '. - '{ "value": 12 }, "geometry": { "type": "Point", '. - '"coordinates": [ 1.0, 1.0 ] } }]}end', + my $exp = <<'EODATA' +{"type": "FeatureCollection","features": [{ "type": "Feature", "id": 0, "properties": { "value": 12 }, "geometry": { "type": "Point", "coordinates": [ 1.0, 1.0 ] } },{ "type": "Feature", "id": 1, "properties": { "value": 12 }, "geometry": { "type": "Point", "coordinates": [ 1.0, 2.0 ] } }]} +EODATA + ; + $exp =~ s/\n$//; + + is($ret, $exp, "Redirect vsistdout to write/close methods of a class."); } + # test Translate if(1){ my $ds = GetDriver('GTiff')->Create('/vsimem/test.tiff', 10); From 037f80dd59d070126098c7e1761f570ac76a63d9 Mon Sep 17 00:00:00 2001 From: shawnlaffan Date: Sat, 18 Nov 2023 12:22:03 +1100 Subject: [PATCH 2/2] t/vsistdout.t: decode JSON and use is_deeply for test Refactor the expected structure in the process. --- t/vsistdout.t | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/t/vsistdout.t b/t/vsistdout.t index 61ff542..8159f92 100644 --- a/t/vsistdout.t +++ b/t/vsistdout.t @@ -52,13 +52,11 @@ if(1){ $gdal->CloseWriter; my $ret = $output->output; - my $exp = <<'EODATA' -{"type": "FeatureCollection","features": [{ "type": "Feature", "id": 0, "properties": { "value": 12 }, "geometry": { "type": "Point", "coordinates": [ 1.0, 1.0 ] } },{ "type": "Feature", "id": 1, "properties": { "value": 12 }, "geometry": { "type": "Point", "coordinates": [ 1.0, 2.0 ] } }]} -EODATA - ; - $exp =~ s/\n$//; + $ret = decode_json $ret; - is($ret, $exp, + my $exp = decode_json (get_expected_json_data()); + + is_deeply ($ret, $exp, "Redirect vsistdout to write/close methods of a class."); } @@ -72,3 +70,38 @@ if(1){ } done_testing(); + + +sub get_expected_json_data { + my $json = <<'EOJSON' +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "id": 0, + "properties": { + "value": 12 + }, + "geometry": { + "type": "Point", + "coordinates": [1.0, 1.0] + } + }, { + "type": "Feature", + "id": 1, + "properties": { + "value": 12 + }, + "geometry": { + "type": "Point", + "coordinates": [1.0, 2.0] + } + } + ] +} +EOJSON + ; + return $json; +} + +