RelatedParagraphAnchorParserTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. use App\Services\RelatedParagraph\AnchorParser;
  3. /*
  4. * 锚点写法全表(documents/related-paragraphs-rebuild.md §3.1)每一行一条用例。
  5. */
  6. it('parses every para anchor form found in wbw_templates', function (string $word, int $begin, int $end, ?string $suffix, bool $warns) {
  7. $parsed = AnchorParser::parse($word);
  8. expect($parsed['kind'])->toBe(AnchorParser::KIND_PARA)
  9. ->and($parsed['begin'])->toBe($begin)
  10. ->and($parsed['end'])->toBe($end)
  11. ->and($parsed['suffix'])->toBe($suffix)
  12. ->and($parsed['warning'] !== null)->toBe($warns);
  13. })->with([
  14. '单号' => ['para5', 5, 5, null, false],
  15. '单号带后缀' => ['para5_mn1', 5, 5, 'mn1', false],
  16. '单号 0' => ['para0_an10', 0, 0, 'an10', false],
  17. '前导 0' => ['para01_mn1', 1, 1, 'mn1', true],
  18. '完整区间' => ['para23-25', 23, 25, null, false],
  19. '完整区间带后缀' => ['para156-166_an10', 156, 166, 'an10', false],
  20. '跨位数区间' => ['para308-1151_an5', 308, 1151, 'an5', false],
  21. '缩写区间' => ['para292-3', 292, 293, null, false],
  22. '缩写区间带后缀' => ['para380-1_mn1', 380, 381, 'mn1', false],
  23. '缩写区间多位' => ['para1008-9_sn5', 1008, 1009, 'sn5', false],
  24. '链式' => ['para18-19-20', 18, 20, null, false],
  25. '链式缩写' => ['para292-3-6', 292, 296, null, false],
  26. '倒序笔误' => ['para504-407_dn1', 504, 507, 'dn1', true],
  27. '倒序笔误 2' => ['para706-608', 706, 708, null, true],
  28. '截断' => ['para179-', 179, 179, null, true],
  29. ]);
  30. it('carries when an abbreviated end would not exceed the start', function () {
  31. expect(AnchorParser::completePrefix(298, '3'))->toBe(303);
  32. });
  33. it('parses chapter, preface, book and page markers', function () {
  34. expect(AnchorParser::parse('dn1_3'))->toMatchArray(['kind' => AnchorParser::KIND_CHAPTER, 'name' => 'dn1', 'preface' => false])
  35. ->and(AnchorParser::parse('an5_1_1'))->toMatchArray(['kind' => AnchorParser::KIND_CHAPTER, 'name' => 'an5', 'preface' => false])
  36. ->and(AnchorParser::parse('mn1_0'))->toMatchArray(['kind' => AnchorParser::KIND_CHAPTER, 'name' => 'mn1', 'preface' => true])
  37. ->and(AnchorParser::parse('kn14b_17'))->toMatchArray(['kind' => AnchorParser::KIND_CHAPTER, 'name' => 'kn14'])
  38. ->and(AnchorParser::parse('kn11'))->toMatchArray(['kind' => AnchorParser::KIND_BOOK, 'name' => 'kn11'])
  39. ->and(AnchorParser::parse('P1.0023'))->toMatchArray(['kind' => AnchorParser::KIND_PAGE])
  40. ->and(AnchorParser::parse('M0.0001'))->toMatchArray(['kind' => AnchorParser::KIND_PAGE]);
  41. });
  42. it('rejects forms outside the table instead of skipping them', function (string $word) {
  43. AnchorParser::parse($word);
  44. })->throws(InvalidArgumentException::class)->with(['para', 'para5,7', 'paraX', 'foo']);