Skip to content

Commit 3bf924c

Browse files
committed
Added regression tests for emoji-test-regex-pattern
1 parent 870f6ee commit 3bf924c

File tree

5 files changed

+126
-8
lines changed

5 files changed

+126
-8
lines changed

.gitattributes

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
/.* export-ignore
2-
/README.md export-ignore
3-
/composer.lock export-ignore
4-
/docs export-ignore
5-
/phpunit.xml export-ignore
6-
/scripts export-ignore
7-
/tests export-ignore
8-
/vendor export-ignore
1+
/.* export-ignore
2+
/README.md export-ignore
3+
/composer.lock export-ignore
4+
/docs export-ignore
5+
/package.json export-ignore
6+
/package-lock.json export-ignore
7+
/phpunit.xml export-ignore
8+
/scripts export-ignore
9+
/tests export-ignore
10+
/vendor export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.*
2+
/node_modules
23
/vendor

package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"emoji-test-regex-pattern": "^2.0.2"
4+
}
5+
}

tests/EmojiTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace s9e\RegexpBuilder\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use s9e\RegexpBuilder\Builder;
7+
use s9e\RegexpBuilder\Factory\PHP;
8+
use s9e\RegexpBuilder\Factory\Java;
9+
use s9e\RegexpBuilder\Factory\JavaScript;
10+
use s9e\RegexpBuilder\Factory\RE2;
11+
12+
/**
13+
* @coversNothing
14+
*/
15+
class EmojiTest extends TestCase
16+
{
17+
protected string $emojiDir = __DIR__ . '/../node_modules/emoji-test-regex-pattern/dist/latest';
18+
19+
protected function getEmoji(): array
20+
{
21+
if (!file_exists($this->emojiDir))
22+
{
23+
$this->markTestSkipped('Missing NPM module');
24+
}
25+
26+
return file($this->emojiDir . '/index-strings.txt', FILE_IGNORE_NEW_LINES);
27+
}
28+
29+
public function testEmojiBytes()
30+
{
31+
$this->runEmojiTest('D');
32+
}
33+
34+
public function testEmojiUnicode()
35+
{
36+
$this->runEmojiTest('Du');
37+
}
38+
39+
protected function runEmojiTest($modifiers)
40+
{
41+
$emoji = $this->getEmoji();
42+
$builder = PHP::getBuilder(delimiter: '/', modifiers: $modifiers);
43+
$regexp = '/^(?:' . $builder->build($emoji) . ')$/' . $modifiers;
44+
45+
// Ensure that each emoji is matched fully
46+
foreach ($emoji as $string)
47+
{
48+
$errorMsg = "'$regexp' does not match '$string'";
49+
$this->assertSame(1, preg_match($regexp, $string, $m), $errorMsg);
50+
$this->assertSame($string, $m[0], $errorMsg);
51+
}
52+
}
53+
54+
/**
55+
* @dataProvider getEmojiTestRegexPatternTests
56+
*/
57+
public function testEmojiTestRegexPattern(Builder $builder, string $filename)
58+
{
59+
$regexp = $builder->build($this->getEmoji());
60+
$reference = file_get_contents($this->emojiDir . '/' . $filename);
61+
62+
$this->assertLessThanOrEqual(strlen($reference), strlen($regexp));
63+
}
64+
65+
public function getEmojiTestRegexPatternTests(): array
66+
{
67+
return [
68+
[
69+
RE2::getBuilder(),
70+
'cpp-re2.txt'
71+
],
72+
[
73+
Java::getBuilder(),
74+
'java.txt'
75+
],
76+
[
77+
JavaScript::getBuilder(),
78+
'javascript.txt'
79+
],
80+
[
81+
JavaScript::getBuilder(flags: 'u'),
82+
'javascript-u.txt'
83+
],
84+
];
85+
}
86+
}

0 commit comments

Comments
 (0)