Skip to content

Commit a358650

Browse files
authored
Fix Aspell output parsing when the original words contains a colon (#25)
* Fix Aspell output parsing when the original words contains a colon (#24)
1 parent dc54570 commit a358650

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

src/Ispell/Ispell.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,16 @@ protected function parseOutput(string $output): array
183183
$issues [] = $issue;
184184
break;
185185
case '&':
186-
$parts = explode(':', $line);
187-
$parts[0] = explode(' ', $parts[0]);
188-
$parts[1] = explode(', ', trim($parts[1]));
189-
$word = $parts[0][1];
190-
$issue = new Issue($word);
191-
$issue->line = $lineNo;
192-
$issue->offset = trim($parts[0][3]);
193-
$issue->suggestions = $parts[1];
194-
$issues [] = $issue;
186+
$matches = [];
187+
$pattern = '/^& (?<original>[^\s]+) \d+ (?<offset>\d+): (?<suggestions>.*)$/';
188+
if (1 === preg_match($pattern, $line, $matches)) {
189+
$word = $matches['original'];
190+
$issue = new Issue($word);
191+
$issue->line = $lineNo;
192+
$issue->offset = $matches['offset'];
193+
$issue->suggestions = explode(', ', $matches['suggestions']);
194+
$issues[] = $issue;
195+
}
195196
break;
196197
}
197198
}

tests/Unit/Aspell/AspellTest.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Mekras\Speller\Aspell\Aspell;
1616
use Mekras\Speller\Source\EncodingAwareSource;
17+
use Mekras\Speller\Source\StringSource;
1718
use PHPUnit\Framework\TestCase;
1819
use Symfony\Component\Process\Process;
1920

@@ -69,7 +70,7 @@ public function testGetSupportedLanguages(): void
6970
'ru',
7071
'ru-ye',
7172
'ru-yeyo',
72-
'ru-yo'
73+
'ru-yo',
7374
],
7475
$aspell->getSupportedLanguages()
7576
);
@@ -115,4 +116,54 @@ public function testCheckText(): void
115116
static::assertEquals('CCould', $issues[4]->word);
116117
static::assertEquals(4, $issues[4]->line);
117118
}
119+
120+
/**
121+
* Test spell checking when a word contains a colon
122+
*
123+
* @see https://github.com/mekras/php-speller/issues/24
124+
*/
125+
public function testCheckTextWithColon(): void
126+
{
127+
$source = new StringSource('S:t Petersburg är i Ryssland', 'UTF-8');
128+
129+
$process = $this->prophesize(Process::class);
130+
$process->setTimeout(600)->shouldBeCalled();
131+
$process->setEnv([])->shouldBeCalled();
132+
$process->setInput('S:t Petersburg är i Ryssland')->shouldBeCalled();
133+
$process->run()->shouldBeCalled();
134+
$process->getExitCode()->shouldBeCalled()->willReturn(0);
135+
$process->getOutput()->shouldBeCalled()->willReturn(file_get_contents(__DIR__ . '/fixtures/check_sv.txt'));
136+
137+
$aspell = new Aspell();
138+
$aspell->setProcess($process->reveal());
139+
$issues = $aspell->checkText($source, ['sv']);
140+
141+
static::assertCount(1, $issues);
142+
static::assertEquals('S:t', $issues[0]->word);
143+
static::assertEquals(1, $issues[0]->line);
144+
static::assertEquals(0, $issues[0]->offset);
145+
static::assertEquals(['St', 'Set', 'Sot', 'Söt', 'Stl', 'Stå'], $issues[0]->suggestions);
146+
}
147+
148+
/**
149+
* Test spell checking when aspell binary output is unexpected
150+
*/
151+
public function testUnexpectedOutputParsing(): void
152+
{
153+
$source = new StringSource('The quick brown fox jumps over the lazy dog', 'UTF-8');
154+
155+
$process = $this->prophesize(Process::class);
156+
$process->setTimeout(600)->shouldBeCalled();
157+
$process->setEnv([])->shouldBeCalled();
158+
$process->setInput('The quick brown fox jumps over the lazy dog')->shouldBeCalled();
159+
$process->run()->shouldBeCalled();
160+
$process->getExitCode()->shouldBeCalled()->willReturn(0);
161+
$process->getOutput()->shouldBeCalled()->willReturn('& unexpected output: foo, bar, baz');
162+
163+
$aspell = new Aspell();
164+
$aspell->setProcess($process->reveal());
165+
$issues = $aspell->checkText($source, ['en']);
166+
167+
static::assertEmpty($issues);
168+
}
118169
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.6.1)
2+
& S:t 23 0: St, Set, Sot, Söt, Stl, Stå
3+
? Petersburg 0 4: Peters
4+
*
5+
*
6+
*

0 commit comments

Comments
 (0)