From 17202a5a94f6344c47f8f6281215413727d74350 Mon Sep 17 00:00:00 2001 From: Daniel Wilkowski Date: Thu, 23 Nov 2023 14:14:56 +0100 Subject: [PATCH] Clean parser tests --- .../Services/Parser/Parsers/CensoreTest.php | 40 +++++++---------- .../Services/Parser/Parsers/PurifierTest.php | 38 +++++----------- .../Parser/Parsers/SimpleMarkdownTest.php | 16 +++---- .../Services/Parser/Parsers/SmiliesTest.php | 44 +++++++++++++++---- 4 files changed, 71 insertions(+), 67 deletions(-) diff --git a/tests/Unit/Services/Parser/Parsers/CensoreTest.php b/tests/Unit/Services/Parser/Parsers/CensoreTest.php index 45ba778b2..a7d82aee6 100644 --- a/tests/Unit/Services/Parser/Parsers/CensoreTest.php +++ b/tests/Unit/Services/Parser/Parsers/CensoreTest.php @@ -1,39 +1,33 @@ parser = new \Coyote\Services\Parser\Parsers\Censore($word); + $this->parser = new Censore(new WordRepository(app())); } + /** + * @test + */ public function testHashCodeTag() { - $text = 'kurczak'; - - $result = $this->parser->parse($text); - $this->assertEquals($result, $text); - - $text = 'kurczak'; - - $result = $this->parser->parse($text); - $this->assertEquals($result, $text); - - $text = 'kurczak'; + $this->assertIdentity('kurczak'); + $this->assertIdentity('kurczak'); + $this->assertIdentity('kurczak'); + } - $result = $this->parser->parse($text); - $this->assertEquals($result, $text); + private function assertIdentity(string $content): void + { + $this->assertSame( + $this->parser->parse($content), + $content); } } diff --git a/tests/Unit/Services/Parser/Parsers/PurifierTest.php b/tests/Unit/Services/Parser/Parsers/PurifierTest.php index 9bf5e5107..466287ae3 100644 --- a/tests/Unit/Services/Parser/Parsers/PurifierTest.php +++ b/tests/Unit/Services/Parser/Parsers/PurifierTest.php @@ -1,58 +1,42 @@ http://www.wp.pl'; - $this->assertEquals($input, $parser->parse($input)); + $this->assertIdentity('http://www.wp.pl'); } public function testParseBlockquotes() { - $parser = new Purifier(); - - $input = '
lorem ipsum
lorem ipsum
'; - $this->assertEquals($input, $parser->parse($input)); + $this->assertIdentity('
lorem ipsum
lorem ipsum
'); } public function testParseUnderscore() { - $parser = new Purifier(); - - $input = 'foo'; - $this->assertEquals($input, $parser->parse($input)); + $this->assertIdentity('foo'); } public function testAllowKbd() { - $parser = new Purifier(); - - $input = 'Ctrl'; - $this->assertEquals($input, $parser->parse($input)); + $this->assertIdentity('Ctrl'); } public function testAllowMark() { - $parser = new Purifier(); - - $input = 'Ctrl'; - $this->assertEquals($input, $parser->parse($input)); + $this->assertIdentity('Ctrl'); } public function testAllowIframeInsideSpan() { - $parser = new Purifier(); - - $input = '

'; - $this->assertSame($input, $parser->parse($input)); + $this->assertIdentity('

'); + } + private function assertIdentity(string $input): void + { + $this->assertEquals($input, (new Purifier())->parse($input)); } } diff --git a/tests/Unit/Services/Parser/Parsers/SimpleMarkdownTest.php b/tests/Unit/Services/Parser/Parsers/SimpleMarkdownTest.php index f780fddde..437f9bfc2 100644 --- a/tests/Unit/Services/Parser/Parsers/SimpleMarkdownTest.php +++ b/tests/Unit/Services/Parser/Parsers/SimpleMarkdownTest.php @@ -1,27 +1,27 @@ app[UserRepository::class], $this->app[PageRepository::class], 'host'); - - $input = "one\ntwo\nthree"; - $this->assertEquals($input, trim($markdown->parse($input))); + $this->assertIdentity($markdown, "one\ntwo\nthree\n"); } public function testAutolinkExtension() { $markdown = new SimpleMarkdown($this->app[UserRepository::class], $this->app[PageRepository::class], 'host'); - $link = 'https://docs.djangoproject.com/en/2.0/#first-steps'; - $this->assertEquals("$link", trim($markdown->parse($link))); + $this->assertEquals("$link\n", $markdown->parse($link)); + } + + private function assertIdentity(SimpleMarkdown $markdown, string $input): void + { + $this->assertEquals($input, $markdown->parse($input)); } } diff --git a/tests/Unit/Services/Parser/Parsers/SmiliesTest.php b/tests/Unit/Services/Parser/Parsers/SmiliesTest.php index 50d6945a8..765d95344 100644 --- a/tests/Unit/Services/Parser/Parsers/SmiliesTest.php +++ b/tests/Unit/Services/Parser/Parsers/SmiliesTest.php @@ -1,20 +1,46 @@ assertSame(':)', $parser->parse(':)')); + } - $this->assertMatchesRegularExpression('/\:\)/', $parser->parse(':)')); - $this->assertMatchesRegularExpression('/

\:\)<\/p>/', $parser->parse('

:)

')); - $this->assertMatchesRegularExpression('/\(\:\)\)/', $parser->parse('(:))')); - $this->assertEquals('admin:)', $parser->parse('admin:)')); + /** + * @test + */ + public function smileInParenthesis() + { + $this->assertIdentity(new Smilies(), '(:))'); + } + + /** + * @test + */ + public function smileInParagraph() + { + $parser = new Smilies(); + $this->assertSame('

:)

', $parser->parse('

:)

')); + } + + /** + * @test + */ + public function smileAfterWord() + { + $this->assertIdentity(new Smilies(), 'admin:)'); + } + + private function assertIdentity(Smilies $parser, string $text): void + { + $this->assertEquals($text, $parser->parse($text)); } }