Skip to content

Commit

Permalink
Merge pull request #1669 from weather-gov/handle-quirky-afd-pops-table
Browse files Browse the repository at this point in the history
Handle weird extraneous newlines in AFD temp/pops table
  • Loading branch information
greg-does-weather authored Aug 29, 2024
2 parents 4940644 + 9795595 commit 5d7f0fc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions web/modules/weather_data/src/Service/AFDParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ public function parseTempsTableContent(string $str, array &$result)
return;
}

// There could be whitespace between the TEMP/POPS header and the actual
// data, so clean that up first. There *shouldn't* be, but we've seen it
// happen, so guard against it.
$str = trim($str);

$lines = explode("\n", $str);
$rx = "/^[^\d]*(.+)/";
$rows = [];
Expand Down
39 changes: 39 additions & 0 deletions web/modules/weather_data/src/Service/Test/AFDParser.php.test
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,45 @@ final class AFDParserTest extends TestCase
$this->assertEquals($expected, $actual);
}

/**
* @group unit
* @group afd-parser
* @group unit2
*/
public function testPOPsTableWithLeadingNewline(): void
{
$raw = "Place A 1 2 3 4 / 5 6 7 8
Place B 0 9 8 7 / 6 5 4 3";
// Remove leading spaces on test data.
$raw = preg_replace("/^\s*/m", "", $raw);
// Put in a leading newline.
$raw = "\n$raw";

$expected = [
[
"type" => "temps-table",
"rows" => [
[
"type" => "temps-table-row",
"numbers" => ["1", "2", "3", "4", "5", "6", "7", "8"],
"name" => "Place A",
],
[
"type" => "temps-table-row",
"numbers" => ["0", "9", "8", "7", "6", "5", "4", "3"],
"name" => "Place B",
],
],
],
];

$parser = new AFDParser($raw);
$actual = [];
$parser->parseTempsTableContent($raw, $actual);

$this->assertEquals($expected, $actual);
}

/**
* @group unit
* @group afd-parser
Expand Down

0 comments on commit 5d7f0fc

Please sign in to comment.