From 4fb6ac31a5d0e697e9b6dbd83c98fd04e9078681 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Mon, 11 Feb 2019 20:36:55 +0000 Subject: [PATCH] Improve tests --- .travis.yml | 2 +- psalm.xml | 10 --- src/Configurables/BlockTypes.php | 10 ++- src/Configurables/InlineTypes.php | 10 ++- tests/src/Configurables/BlockTypesTest.php | 45 +++++++++++ tests/src/Configurables/InlineTypesTest.php | 32 ++++++++ tests/src/Html/Sanitisation/EscaperTest.php | 22 ++++++ tests/src/Parsing/LinesTest.php | 82 +++++++++++++++++++++ 8 files changed, 198 insertions(+), 15 deletions(-) create mode 100644 tests/src/Configurables/BlockTypesTest.php create mode 100644 tests/src/Configurables/InlineTypesTest.php create mode 100644 tests/src/Html/Sanitisation/EscaperTest.php create mode 100644 tests/src/Parsing/LinesTest.php diff --git a/.travis.yml b/.travis.yml index a002f75..683f387 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,7 +48,7 @@ jobs: - composer install --prefer-dist --no-interaction --no-progress script: - composer test-units - - vendor/bin/infection --show-mutations --threads=4 --min-msi=85 --min-covered-msi=90 + - vendor/bin/infection --show-mutations --threads=4 --min-msi=90 --min-covered-msi=90 - <<: *MUTATION_AND_UNIT_TEST php: 7.2 - <<: *MUTATION_AND_UNIT_TEST diff --git a/psalm.xml b/psalm.xml index 0ad9b82..f8bf547 100644 --- a/psalm.xml +++ b/psalm.xml @@ -13,16 +13,6 @@ - - - - - - - - - - diff --git a/src/Configurables/BlockTypes.php b/src/Configurables/BlockTypes.php index 580f6f5..8ceff27 100644 --- a/src/Configurables/BlockTypes.php +++ b/src/Configurables/BlockTypes.php @@ -99,7 +99,10 @@ final class BlockTypes implements Configurable { return $this->settingMarked( $marker, - \array_merge($newBlockTypes, $this->blockTypes[$marker]) + \array_merge( + $newBlockTypes, + isset($this->blockTypes[$marker]) ? $this->blockTypes[$marker] : [] + ) ); } @@ -112,7 +115,10 @@ final class BlockTypes implements Configurable { return $this->settingMarked( $marker, - \array_merge($this->blockTypes[$marker], $newBlockTypes) + \array_merge( + isset($this->blockTypes[$marker]) ? $this->blockTypes[$marker] : [], + $newBlockTypes + ) ); } diff --git a/src/Configurables/InlineTypes.php b/src/Configurables/InlineTypes.php index 4b058cc..d464f00 100644 --- a/src/Configurables/InlineTypes.php +++ b/src/Configurables/InlineTypes.php @@ -81,7 +81,10 @@ final class InlineTypes implements Configurable { return $this->setting( $marker, - \array_merge($newInlineTypes, $this->inlineTypes[$marker]) + \array_merge( + $newInlineTypes, + isset($this->inlineTypes[$marker]) ? $this->inlineTypes[$marker] : [] + ) ); } @@ -94,7 +97,10 @@ final class InlineTypes implements Configurable { return $this->setting( $marker, - \array_merge($this->inlineTypes[$marker], $newInlineTypes) + \array_merge( + isset($this->inlineTypes[$marker]) ? $this->inlineTypes[$marker] : [], + $newInlineTypes + ) ); } diff --git a/tests/src/Configurables/BlockTypesTest.php b/tests/src/Configurables/BlockTypesTest.php new file mode 100644 index 0000000..261b82c --- /dev/null +++ b/tests/src/Configurables/BlockTypesTest.php @@ -0,0 +1,45 @@ +assertSame([], $BlockTypes->markedBy('@')); + $this->assertSame([], $BlockTypes->unmarked()); + + $BlockTypes = $BlockTypes->addingMarkedHighPrecedence('@', [IndentedCode::class]); + $this->assertSame([IndentedCode::class], $BlockTypes->markedBy('@')); + + $BlockTypes = $BlockTypes->addingUnmarkedHighPrecedence([Markup::class]); + $this->assertSame([IndentedCode::class], $BlockTypes->markedBy('@')); + $this->assertSame([Markup::class], $BlockTypes->unmarked()); + + $BlockTypes = $BlockTypes->addingMarkedHighPrecedence('@', [Markup::class]); + $this->assertSame([Markup::class, IndentedCode::class], $BlockTypes->markedBy('@')); + + $BlockTypes = $BlockTypes->addingUnmarkedHighPrecedence([Rule::class]); + $this->assertSame([Markup::class, IndentedCode::class], $BlockTypes->markedBy('@')); + $this->assertSame([Rule::class, Markup::class], $BlockTypes->unmarked()); + + $BlockTypes = $BlockTypes->addingMarkedLowPrecedence('@', [Rule::class]); + $this->assertSame([Markup::class, IndentedCode::class, Rule::class], $BlockTypes->markedBy('@')); + + $BlockTypes = $BlockTypes->addingUnmarkedLowPrecedence([IndentedCode::class]); + $this->assertSame([Markup::class, IndentedCode::class, Rule::class], $BlockTypes->markedBy('@')); + $this->assertSame([Rule::class, Markup::class, IndentedCode::class], $BlockTypes->unmarked()); + } +} diff --git a/tests/src/Configurables/InlineTypesTest.php b/tests/src/Configurables/InlineTypesTest.php new file mode 100644 index 0000000..604fd4d --- /dev/null +++ b/tests/src/Configurables/InlineTypesTest.php @@ -0,0 +1,32 @@ +assertSame([], $InlineTypes->markedBy('@')); + + $InlineTypes = $InlineTypes->addingHighPrecedence('@', [Emphasis::class]); + $this->assertSame([Emphasis::class], $InlineTypes->markedBy('@')); + + $InlineTypes = $InlineTypes->addingHighPrecedence('@', [Code::class]); + $this->assertSame([Code::class, Emphasis::class], $InlineTypes->markedBy('@')); + + $InlineTypes = $InlineTypes->addingLowPrecedence('@', [Link::class]); + $this->assertSame([Code::class, Emphasis::class, Link::class], $InlineTypes->markedBy('@')); + } +} diff --git a/tests/src/Html/Sanitisation/EscaperTest.php b/tests/src/Html/Sanitisation/EscaperTest.php new file mode 100644 index 0000000..e0c756e --- /dev/null +++ b/tests/src/Html/Sanitisation/EscaperTest.php @@ -0,0 +1,22 @@ +assertSame( + Escaper::htmlElementValue('&'), + '<foo bar="baz" boo=\'bim\'>&' + ); + } +} diff --git a/tests/src/Parsing/LinesTest.php b/tests/src/Parsing/LinesTest.php new file mode 100644 index 0000000..c2d6d08 --- /dev/null +++ b/tests/src/Parsing/LinesTest.php @@ -0,0 +1,82 @@ +assertFalse($Lines->containsBlankLines()); + + $Lines = $Lines->appendingTextLines('bar', 0); + + $this->assertFalse($Lines->containsBlankLines()); + + $Lines = $Lines->appendingTextLines("boo\nbaz", 0); + + $this->assertFalse($Lines->containsBlankLines()); + + $Lines = $Lines->appendingTextLines("zoo\n\nzoom", 0); + + $this->assertTrue($Lines->containsBlankLines()); + } + + /** + * @return void + * @throws \PHPUnit\Framework\ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + */ + public function testAppendContext() + { + $Lines = Lines::fromTextLines('foo', 0); + + $this->assertFalse($Lines->containsBlankLines()); + + $Lines = $Lines->appendingContext(new Context(new Line('bar'), "\n \n")); + + $this->assertTrue($Lines->containsBlankLines()); + $this->assertSame($Lines->trailingBlankLines(), 0); + + $Lines = $Lines->appendingContext(new Context(new Line('boo'), '')); + $Lines = $Lines->appendingContext(new Context(new Line('baz'), '')); + + $this->assertTrue($Lines->containsBlankLines()); + $this->assertSame($Lines->trailingBlankLines(), 0); + } + + /** + * @return void + * @throws \PHPUnit\Framework\ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + */ + public function testTrailingBlankLines() + { + $Lines = Lines::fromTextLines('foo', 0); + + $this->assertSame($Lines->trailingBlankLines(), 0); + + $Lines = $Lines->appendingTextLines("bar\n", 0); + + $this->assertSame($Lines->trailingBlankLines(), 1); + + $Lines = $Lines->appendingTextLines("boo\nbaz\n\n", 0); + + $this->assertSame($Lines->trailingBlankLines(), 2); + + $Lines = $Lines->appendingTextLines("zoo\n\nzoom", 0); + + $this->assertSame($Lines->trailingBlankLines(), 0); + } +}