assertSame([ '[foo]', 'foo' ], MDToken::tokenizeLabel('[foo] bar')); $this->assertSame([ '[foo\\[]', 'foo\\[' ], MDToken::tokenizeLabel('[foo\\[] bar')); $this->assertSame([ '[foo\\]]', 'foo\\]' ], MDToken::tokenizeLabel('[foo\\]] bar')); $this->assertSame([ '[foo[]]', 'foo[]' ], MDToken::tokenizeLabel('[foo[]] bar')); $this->assertSame([ '[foo\\(]', 'foo\\(' ], MDToken::tokenizeLabel('[foo\\(] bar')); $this->assertSame([ '[foo\\)]', 'foo\\)' ], MDToken::tokenizeLabel('[foo\\)] bar')); $this->assertSame([ '[foo()]', 'foo()' ], MDToken::tokenizeLabel('[foo()] bar')); $this->assertSame(null, MDToken::tokenizeLabel('foo bar')); $this->assertSame(null, MDToken::tokenizeLabel('[foo\\] bar')); $this->assertSame(null, MDToken::tokenizeLabel('[foo bar')); $this->assertSame(null, MDToken::tokenizeLabel('[foo[] bar')); } public function test_tokenizeURL() { $this->assertSame([ '(page.html)', 'page.html', null ], MDToken::tokenizeURL('(page.html) foo')); $this->assertSame([ '(page.html "link title")', 'page.html', 'link title' ], MDToken::tokenizeURL('(page.html "link title") foo')); $this->assertSame([ '(https://example.com/path/page.html?query=foo&bar=baz#fragment)', 'https://example.com/path/page.html?query=foo&bar=baz#fragment', null ], MDToken::tokenizeURL('(https://example.com/path/page.html?query=foo&bar=baz#fragment) foo')); $this->assertSame(null, MDToken::tokenizeURL('page.html foo')); $this->assertSame(null, MDToken::tokenizeURL('(page.html foo')); $this->assertSame(null, MDToken::tokenizeURL('page.html) foo')); $this->assertSame(null, MDToken::tokenizeURL('(page.html "title) foo')); $this->assertSame(null, MDToken::tokenizeURL('(page .html) foo')); $this->assertSame(null, MDToken::tokenizeURL('(user@example.com) foo')); $this->assertSame(null, MDToken::tokenizeURL('(user@example.com "title") foo')); } public function test_tokenizeEmail() { $this->assertSame([ '(user@example.com)', 'user@example.com', null ], MDToken::tokenizeEmail('(user@example.com)')); $this->assertSame([ '(user@example.com "link title")', 'user@example.com', 'link title' ], MDToken::tokenizeEmail('(user@example.com "link title")')); $this->assertSame(null, MDToken::tokenizeEmail('(https://example.com) foo')); $this->assertSame(null, MDToken::tokenizeEmail('(https://example.com "link title") foo')); $this->assertSame(null, MDToken::tokenizeEmail('(user@example.com "link title) foo')); $this->assertSame(null, MDToken::tokenizeEmail('(user@example.com foo')); $this->assertSame(null, MDToken::tokenizeEmail('user@example.com) foo')); } public function test_findFirstTokens() { $tokens = [ new MDToken('Lorem', MDTokenType::Text), new MDToken(' ', MDTokenType::Whitespace), new MDToken('_', MDTokenType::Underscore), new MDToken('ipsum', MDTokenType::Text), new MDToken('_', MDTokenType::Underscore), new MDToken(' ', MDTokenType::Whitespace), new MDToken('dolor', MDTokenType::Text), new MDToken('_', MDTokenType::Underscore), new MDToken('sit', MDTokenType::Text), new MDToken('_', MDTokenType::Underscore), ]; $pattern = [ MDTokenType::Underscore, ]; $actual = MDToken::findFirstTokens($tokens, $pattern); $expected = new MDTokenMatch([ $tokens[2] ], 2); $this->assertEquals($expected, $actual); } public function test_findFirstTokens_optionalWhitespace1() { $tokens = [ new MDToken('Lorem', MDTokenType::Text), new MDToken(' ', MDTokenType::Whitespace), new MDToken('[ipsum]', MDTokenType::Label, 'ipsum'), new MDToken('(link.html)', MDTokenType::URL, 'link.html'), new MDToken(' ', MDTokenType::Whitespace), new MDToken('dolor', MDTokenType::Text), ]; $pattern = [ MDTokenType::Label, MDTokenType::META_OptionalWhitespace, MDTokenType::URL, ]; $actual = MDToken::findFirstTokens($tokens, $pattern); $expected = new MDTokenMatch([ $tokens[2], $tokens[3] ], 2); $this->assertEquals($expected, $actual); } public function test_findFirstTokens_optionalWhitespace2() { $tokens = [ new MDToken('Lorem', MDTokenType::Text), new MDToken(' ', MDTokenType::Whitespace), new MDToken('[ipsum]', MDTokenType::Label, 'ipsum'), new MDToken(' ', MDTokenType::Whitespace), new MDToken('(link.html)', MDTokenType::URL, 'link.html'), new MDToken(' ', MDTokenType::Whitespace), new MDToken('dolor', MDTokenType::Text), ]; $pattern = [ MDTokenType::Label, MDTokenType::META_OptionalWhitespace, MDTokenType::URL, ]; $actual = MDToken::findFirstTokens($tokens, $pattern); $expected = new MDTokenmatch([ $tokens[2], $tokens[3], $tokens[4] ], 2); $this->assertEquals($expected, $actual); } public function test_findPairedTokens() { $tokens = [ new MDToken('Lorem', MDTokenType::Text), new MDToken(' ', MDTokenType::Whitespace), new MDToken('_', MDTokenType::Underscore), new MDToken('ipsum', MDTokenType::Text), new MDToken('_', MDTokenType::Underscore), new MDToken(' ', MDTokenType::Whitespace), new MDToken('dolor', MDTokenType::Text), new MDToken('_', MDTokenType::Underscore), new MDToken('sit', MDTokenType::Text), new MDToken('_', MDTokenType::Underscore), ]; $pattern = [ MDTokenType::Underscore, ]; $actual = MDToken::findPairedTokens($tokens, $pattern, $pattern); $expected = new MDPairedTokenMatch( [ $tokens[2] ], // startTokens [ $tokens[3] ], // contentTokens [ $tokens[4] ], // endTokens 2, // startIndex 3, // contentIndex 4, // endIndex 3 // totalLength ); $this->assertEquals($expected, $actual); } } ?>