Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Aspell output parsing when the original words contains a colon #25

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what to do if it doesn't match the pattern 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not matching, previously we wouldn't have anything in $parts. So that would be an empty array.
I would say the line is simply ignored.

But you could write a test for that where the Ispell output doesn't match. And see if it breaks :)

$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
*
*
*