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