13e6baeffSAndreas Gohr<?php 23e6baeffSAndreas Gohr 33e6baeffSAndreas Gohrnamespace dokuwiki\test\Parsing\ParserMode; 43e6baeffSAndreas Gohr 53e6baeffSAndreas Gohruse dokuwiki\Parsing\ParserMode\Eol; 63e6baeffSAndreas Gohruse dokuwiki\Parsing\ParserMode\GfmHr; 73e6baeffSAndreas Gohr 83e6baeffSAndreas Gohr/** 93e6baeffSAndreas Gohr * Tests for the unified horizontal-rule mode. 103e6baeffSAndreas Gohr * 11*13a62f81SAndreas Gohr * Covers both pattern flavors: pure `dw` (4-or-more dashes only) 12*13a62f81SAndreas Gohr * and the wider GFM flavor that loads in `md`, `dw+md`, `md+dw` 133e6baeffSAndreas Gohr * (3-or-more of `-` / `*` / `_`). No leading, trailing, or internal 143e6baeffSAndreas Gohr * whitespace tolerance in either flavor. 153e6baeffSAndreas Gohr */ 163e6baeffSAndreas Gohrclass GfmHrTest extends ParserTestBase 173e6baeffSAndreas Gohr{ 183e6baeffSAndreas Gohr /** 193e6baeffSAndreas Gohr * Whether at least one `hr` call was emitted for the given input 203e6baeffSAndreas Gohr * under the given syntax. Returns the call count for richer 213e6baeffSAndreas Gohr * assertion messages. 223e6baeffSAndreas Gohr */ 233e6baeffSAndreas Gohr protected function countHrCalls(string $syntax, string $input): int 243e6baeffSAndreas Gohr { 253e6baeffSAndreas Gohr $this->setUp(); 263e6baeffSAndreas Gohr $this->setSyntax($syntax); 273e6baeffSAndreas Gohr $this->P->addMode('gfm_hr', new GfmHr()); 283e6baeffSAndreas Gohr $this->P->parse($input); 293e6baeffSAndreas Gohr return count(array_filter($this->H->calls, static fn($c) => $c[0] === 'hr')); 303e6baeffSAndreas Gohr } 313e6baeffSAndreas Gohr 323e6baeffSAndreas Gohr // ------------------------------------------------------------------ 33*13a62f81SAndreas Gohr // DW flavor (`$conf['syntax'] = 'dw'`) 343e6baeffSAndreas Gohr // ------------------------------------------------------------------ 353e6baeffSAndreas Gohr 363e6baeffSAndreas Gohr public function testDwFourDashes() 373e6baeffSAndreas Gohr { 38*13a62f81SAndreas Gohr $this->assertSame(1, $this->countHrCalls('dw', "\n----\n")); 393e6baeffSAndreas Gohr } 403e6baeffSAndreas Gohr 413e6baeffSAndreas Gohr public function testDwManyDashes() 423e6baeffSAndreas Gohr { 43*13a62f81SAndreas Gohr $this->assertSame(1, $this->countHrCalls('dw', "\n--------\n")); 443e6baeffSAndreas Gohr } 453e6baeffSAndreas Gohr 463e6baeffSAndreas Gohr public function testDwThreeDashesNotHr() 473e6baeffSAndreas Gohr { 48*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('dw', "\n---\n")); 493e6baeffSAndreas Gohr } 503e6baeffSAndreas Gohr 513e6baeffSAndreas Gohr public function testDwAsterisksNotHr() 523e6baeffSAndreas Gohr { 53*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('dw', "\n***\n")); 54*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('dw', "\n********\n")); 553e6baeffSAndreas Gohr } 563e6baeffSAndreas Gohr 573e6baeffSAndreas Gohr public function testDwUnderscoresNotHr() 583e6baeffSAndreas Gohr { 59*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('dw', "\n___\n")); 60*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('dw', "\n_____\n")); 613e6baeffSAndreas Gohr } 623e6baeffSAndreas Gohr 633e6baeffSAndreas Gohr public function testDwLeadingSpaceNotHr() 643e6baeffSAndreas Gohr { 65*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('dw', "\n ----\n")); 663e6baeffSAndreas Gohr } 673e6baeffSAndreas Gohr 683e6baeffSAndreas Gohr public function testDwTrailingSpaceNotHr() 693e6baeffSAndreas Gohr { 70*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('dw', "\n---- \n")); 713e6baeffSAndreas Gohr } 723e6baeffSAndreas Gohr 733e6baeffSAndreas Gohr public function testDwInterruptsParagraph() 743e6baeffSAndreas Gohr { 75*13a62f81SAndreas Gohr $this->setSyntax('dw'); 763e6baeffSAndreas Gohr $this->P->addMode('gfm_hr', new GfmHr()); 773e6baeffSAndreas Gohr $this->P->parse("Foo\n----\nBar"); 783e6baeffSAndreas Gohr $calls = [ 793e6baeffSAndreas Gohr ['document_start', []], 803e6baeffSAndreas Gohr ['p_open', []], 813e6baeffSAndreas Gohr ['cdata', ["\nFoo"]], 823e6baeffSAndreas Gohr ['p_close', []], 833e6baeffSAndreas Gohr ['hr', []], 843e6baeffSAndreas Gohr ['p_open', []], 853e6baeffSAndreas Gohr ['cdata', ["\nBar"]], 863e6baeffSAndreas Gohr ['p_close', []], 873e6baeffSAndreas Gohr ['document_end', []], 883e6baeffSAndreas Gohr ]; 893e6baeffSAndreas Gohr $this->assertCalls($calls, $this->H->calls); 903e6baeffSAndreas Gohr } 913e6baeffSAndreas Gohr 923e6baeffSAndreas Gohr // ------------------------------------------------------------------ 93*13a62f81SAndreas Gohr // GFM flavor (any non-`dw` syntax setting) 943e6baeffSAndreas Gohr // ------------------------------------------------------------------ 953e6baeffSAndreas Gohr 963e6baeffSAndreas Gohr public function testMdDashes() 973e6baeffSAndreas Gohr { 98*13a62f81SAndreas Gohr foreach (['md', 'dw+md', 'md+dw'] as $syntax) { 993e6baeffSAndreas Gohr $this->assertSame(1, $this->countHrCalls($syntax, "\n---\n"), 1003e6baeffSAndreas Gohr "syntax=$syntax: bare `---` must produce hr"); 1013e6baeffSAndreas Gohr } 1023e6baeffSAndreas Gohr } 1033e6baeffSAndreas Gohr 1043e6baeffSAndreas Gohr public function testMdAsterisks() 1053e6baeffSAndreas Gohr { 106*13a62f81SAndreas Gohr foreach (['md', 'dw+md', 'md+dw'] as $syntax) { 1073e6baeffSAndreas Gohr $this->assertSame(1, $this->countHrCalls($syntax, "\n***\n"), 1083e6baeffSAndreas Gohr "syntax=$syntax: bare `***` must produce hr"); 1093e6baeffSAndreas Gohr } 1103e6baeffSAndreas Gohr } 1113e6baeffSAndreas Gohr 1123e6baeffSAndreas Gohr public function testMdUnderscores() 1133e6baeffSAndreas Gohr { 114*13a62f81SAndreas Gohr foreach (['md', 'dw+md', 'md+dw'] as $syntax) { 1153e6baeffSAndreas Gohr $this->assertSame(1, $this->countHrCalls($syntax, "\n___\n"), 1163e6baeffSAndreas Gohr "syntax=$syntax: bare `___` must produce hr"); 1173e6baeffSAndreas Gohr } 1183e6baeffSAndreas Gohr } 1193e6baeffSAndreas Gohr 1203e6baeffSAndreas Gohr public function testMdManyChars() 1213e6baeffSAndreas Gohr { 122*13a62f81SAndreas Gohr $this->assertSame(1, $this->countHrCalls('md', "\n--------\n")); 123*13a62f81SAndreas Gohr $this->assertSame(1, $this->countHrCalls('md', "\n********\n")); 124*13a62f81SAndreas Gohr $this->assertSame(1, $this->countHrCalls('md', "\n________\n")); 1253e6baeffSAndreas Gohr } 1263e6baeffSAndreas Gohr 1273e6baeffSAndreas Gohr public function testMdTooFew() 1283e6baeffSAndreas Gohr { 129*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n--\n")); 130*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n**\n")); 131*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n__\n")); 1323e6baeffSAndreas Gohr } 1333e6baeffSAndreas Gohr 1343e6baeffSAndreas Gohr public function testMdInternalSpacesNotSupported() 1353e6baeffSAndreas Gohr { 136*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n- - -\n")); 137*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n* * *\n")); 138*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n_ _ _\n")); 1393e6baeffSAndreas Gohr } 1403e6baeffSAndreas Gohr 1413e6baeffSAndreas Gohr public function testMdLeadingSpaceNotSupported() 1423e6baeffSAndreas Gohr { 143*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n ***\n")); 144*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n ---\n")); 1453e6baeffSAndreas Gohr } 1463e6baeffSAndreas Gohr 1473e6baeffSAndreas Gohr public function testMdTrailingSpaceNotSupported() 1483e6baeffSAndreas Gohr { 149*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n--- \n")); 1503e6baeffSAndreas Gohr } 1513e6baeffSAndreas Gohr 1523e6baeffSAndreas Gohr public function testMdMixedChars() 1533e6baeffSAndreas Gohr { 154*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n-*-\n")); 155*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n***---\n")); 1563e6baeffSAndreas Gohr } 1573e6baeffSAndreas Gohr 1583e6baeffSAndreas Gohr public function testMdLetterMixed() 1593e6baeffSAndreas Gohr { 160*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n---a\n")); 161*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\na---\n")); 162*13a62f81SAndreas Gohr $this->assertSame(0, $this->countHrCalls('md', "\n---a---\n")); 1633e6baeffSAndreas Gohr } 1643e6baeffSAndreas Gohr 1653e6baeffSAndreas Gohr public function testMdInterruptsParagraph() 1663e6baeffSAndreas Gohr { 167*13a62f81SAndreas Gohr $this->setSyntax('md'); 1683e6baeffSAndreas Gohr $this->P->addMode('gfm_hr', new GfmHr()); 1693e6baeffSAndreas Gohr $this->P->addMode('eol', new Eol()); 1703e6baeffSAndreas Gohr $this->P->parse("Foo\n***\nbar"); 1713e6baeffSAndreas Gohr $modes = array_column($this->H->calls, 0); 1723e6baeffSAndreas Gohr $this->assertContains('hr', $modes, 1733e6baeffSAndreas Gohr 'thematic break interrupts paragraph without blank line (spec 28)'); 1743e6baeffSAndreas Gohr } 1753e6baeffSAndreas Gohr 1763e6baeffSAndreas Gohr // ------------------------------------------------------------------ 1773e6baeffSAndreas Gohr // Common 1783e6baeffSAndreas Gohr // ------------------------------------------------------------------ 1793e6baeffSAndreas Gohr 1803e6baeffSAndreas Gohr public function testSortValue() 1813e6baeffSAndreas Gohr { 1823e6baeffSAndreas Gohr $mode = new GfmHr(); 1833e6baeffSAndreas Gohr $this->assertSame(160, $mode->getSort()); 1843e6baeffSAndreas Gohr } 1853e6baeffSAndreas Gohr} 186