InlineParserMatchTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Unit\Parser\Inline;
  12. use League\CommonMark\Exception\InvalidArgumentException;
  13. use League\CommonMark\Parser\Inline\InlineParserMatch;
  14. use PHPUnit\Framework\TestCase;
  15. final class InlineParserMatchTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider provideDataForTesting
  19. */
  20. public function testGetRegex(InlineParserMatch $definition, string $expectedRegex): void
  21. {
  22. $this->assertSame($expectedRegex, $definition->getRegex());
  23. }
  24. /**
  25. * @return iterable<array{0: InlineParserMatch, 1: string}>
  26. */
  27. public function provideDataForTesting(): iterable
  28. {
  29. yield [InlineParserMatch::string('.'), '/\./i'];
  30. yield [InlineParserMatch::string('...'), '/\.\.\./i'];
  31. yield [InlineParserMatch::string('foo'), '/foo/i'];
  32. yield [InlineParserMatch::string('foo')->caseSensitive(), '/foo/'];
  33. yield [InlineParserMatch::string('🎉'), '/🎉/i'];
  34. yield [InlineParserMatch::string('/r/'), '/\/r\//i'];
  35. yield [InlineParserMatch::oneOf('foo', 'bar'), '/foo|bar/i'];
  36. yield [InlineParserMatch::oneOf('foo', 'bar')->caseSensitive(), '/foo|bar/'];
  37. yield [InlineParserMatch::oneOf('foo', '.', '[x]'), '/foo|\.|\[x\]/i'];
  38. yield [InlineParserMatch::regex('[\w-_]{3,}'), '/[\w-_]{3,}/i'];
  39. yield [InlineParserMatch::regex('[\w-_]{3,}')->caseSensitive(), '/[\w-_]{3,}/'];
  40. $complexExample = InlineParserMatch::join(
  41. InlineParserMatch::string('foo'),
  42. InlineParserMatch::oneOf('bar', 'baz'),
  43. InlineParserMatch::regex('\d+')
  44. );
  45. yield [$complexExample, '/(foo)(bar|baz)(\d+)/i'];
  46. $complexExampleCaseSensitive = InlineParserMatch::join(
  47. InlineParserMatch::string('foo'),
  48. InlineParserMatch::oneOf('bar', 'baz'),
  49. InlineParserMatch::regex('\d+')
  50. )->caseSensitive();
  51. yield [$complexExampleCaseSensitive->caseSensitive(), '/(foo)(bar|baz)(\d+)/'];
  52. $complexExampleCaseSensitiveIndividually = InlineParserMatch::join(
  53. InlineParserMatch::string('foo')->caseSensitive(),
  54. InlineParserMatch::oneOf('bar', 'baz')->caseSensitive(),
  55. InlineParserMatch::regex('\d+')->caseSensitive()
  56. );
  57. yield [$complexExampleCaseSensitiveIndividually, '/(foo)(bar|baz)(\d+)/'];
  58. }
  59. public function testCannotMixCaseSensitivity(): void
  60. {
  61. $this->expectException(InvalidArgumentException::class);
  62. InlineParserMatch::join(
  63. InlineParserMatch::string('foo')->caseSensitive(),
  64. InlineParserMatch::oneOf('bar', 'baz'),
  65. InlineParserMatch::regex('\d+')
  66. );
  67. }
  68. }