Skip to content

Commit

Permalink
Fix Aspell output parsing when the original words contains a colon (#25)
Browse files Browse the repository at this point in the history
* Fix Aspell output parsing when the original words contains a colon (#24)
  • Loading branch information
mmetayer authored Jun 18, 2020
1 parent dc54570 commit a358650
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/Ispell/Ispell.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,16 @@ protected function parseOutput(string $output): array
$issues [] = $issue;
break;
case '&':
$parts = explode(':', $line);
$parts[0] = explode(' ', $parts[0]);
$parts[1] = explode(', ', trim($parts[1]));
$word = $parts[0][1];
$issue = new Issue($word);
$issue->line = $lineNo;
$issue->offset = trim($parts[0][3]);
$issue->suggestions = $parts[1];
$issues [] = $issue;
$matches = [];
$pattern = '/^& (?<original>[^\s]+) \d+ (?<offset>\d+): (?<suggestions>.*)$/';
if (1 === preg_match($pattern, $line, $matches)) {
$word = $matches['original'];
$issue = new Issue($word);
$issue->line = $lineNo;
$issue->offset = $matches['offset'];
$issue->suggestions = explode(', ', $matches['suggestions']);
$issues[] = $issue;
}
break;
}
}
Expand Down
53 changes: 52 additions & 1 deletion tests/Unit/Aspell/AspellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Mekras\Speller\Aspell\Aspell;
use Mekras\Speller\Source\EncodingAwareSource;
use Mekras\Speller\Source\StringSource;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -69,7 +70,7 @@ public function testGetSupportedLanguages(): void
'ru',
'ru-ye',
'ru-yeyo',
'ru-yo'
'ru-yo',
],
$aspell->getSupportedLanguages()
);
Expand Down Expand Up @@ -115,4 +116,54 @@ public function testCheckText(): void
static::assertEquals('CCould', $issues[4]->word);
static::assertEquals(4, $issues[4]->line);
}

/**
* Test spell checking when a word contains a colon
*
* @see https://github.com/mekras/php-speller/issues/24
*/
public function testCheckTextWithColon(): void
{
$source = new StringSource('S:t Petersburg är i Ryssland', 'UTF-8');

$process = $this->prophesize(Process::class);
$process->setTimeout(600)->shouldBeCalled();
$process->setEnv([])->shouldBeCalled();
$process->setInput('S:t Petersburg är i Ryssland')->shouldBeCalled();
$process->run()->shouldBeCalled();
$process->getExitCode()->shouldBeCalled()->willReturn(0);
$process->getOutput()->shouldBeCalled()->willReturn(file_get_contents(__DIR__ . '/fixtures/check_sv.txt'));

$aspell = new Aspell();
$aspell->setProcess($process->reveal());
$issues = $aspell->checkText($source, ['sv']);

static::assertCount(1, $issues);
static::assertEquals('S:t', $issues[0]->word);
static::assertEquals(1, $issues[0]->line);
static::assertEquals(0, $issues[0]->offset);
static::assertEquals(['St', 'Set', 'Sot', 'Söt', 'Stl', 'Stå'], $issues[0]->suggestions);
}

/**
* Test spell checking when aspell binary output is unexpected
*/
public function testUnexpectedOutputParsing(): void
{
$source = new StringSource('The quick brown fox jumps over the lazy dog', 'UTF-8');

$process = $this->prophesize(Process::class);
$process->setTimeout(600)->shouldBeCalled();
$process->setEnv([])->shouldBeCalled();
$process->setInput('The quick brown fox jumps over the lazy dog')->shouldBeCalled();
$process->run()->shouldBeCalled();
$process->getExitCode()->shouldBeCalled()->willReturn(0);
$process->getOutput()->shouldBeCalled()->willReturn('& unexpected output: foo, bar, baz');

$aspell = new Aspell();
$aspell->setProcess($process->reveal());
$issues = $aspell->checkText($source, ['en']);

static::assertEmpty($issues);
}
}
6 changes: 6 additions & 0 deletions tests/Unit/Aspell/fixtures/check_sv.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.6.1)
& S:t 23 0: St, Set, Sot, Söt, Stl, Stå
? Petersburg 0 4: Peters
*
*
*

0 comments on commit a358650

Please sign in to comment.