-
Notifications
You must be signed in to change notification settings - Fork 144
[TASK] Add some tests for OutputFormatter
(part 1)
#943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
26bc66d
[TASK] Add some tests for `OutputFormatter` (part 1)
oliverklee 96092a5
Update tests/Unit/OutputFormatterTest.php
oliverklee 7459cd3
Update tests/Unit/OutputFormatterTest.php
oliverklee b86b879
Update tests/Unit/OutputFormatterTest.php
oliverklee 9870e76
Update tests/Unit/OutputFormatterTest.php
oliverklee 3e70e79
Update tests/Unit/OutputFormatterTest.php
oliverklee 2d617c6
Have a coffee.
oliverklee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,300 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sabberworm\CSS\Tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sabberworm\CSS\OutputFormat; | ||
use Sabberworm\CSS\OutputFormatter; | ||
use Sabberworm\CSS\Renderable; | ||
|
||
/** | ||
* @covers \Sabberworm\CSS\OutputFormatter | ||
*/ | ||
final class OutputFormatterTest extends TestCase | ||
{ | ||
/** | ||
* @var OutputFormatter | ||
*/ | ||
private $subject; | ||
|
||
/** | ||
* @var OutputFormat | ||
*/ | ||
private $outputFormat; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->outputFormat = new OutputFormat(); | ||
$this->subject = new OutputFormatter($this->outputFormat); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceAfterRuleNameReturnsSpaceAfterRuleNameFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceAfterRuleName($space); | ||
|
||
self::assertSame($space, $this->subject->spaceAfterRuleName()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceBeforeRulesReturnsSpaceBeforeRulesFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceBeforeRules($space); | ||
|
||
self::assertSame($space, $this->subject->spaceBeforeRules()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceAfterRulesReturnsSpaceAfterRulesFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceAfterRules($space); | ||
|
||
self::assertSame($space, $this->subject->spaceAfterRules()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceBetweenRulesReturnsSpaceBetweenRulesFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceBetweenRules($space); | ||
|
||
self::assertSame($space, $this->subject->spaceBetweenRules()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceBeforeBlocksReturnsSpaceBeforeBlocksFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceBeforeBlocks($space); | ||
|
||
self::assertSame($space, $this->subject->spaceBeforeBlocks()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceAfterBlocksReturnsSpaceAfterBlocksFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceAfterBlocks($space); | ||
|
||
self::assertSame($space, $this->subject->spaceAfterBlocks()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceBetweenBlocksReturnsSpaceBetweenBlocksFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceBetweenBlocks($space); | ||
|
||
self::assertSame($space, $this->subject->spaceBetweenBlocks()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceBeforeSelectorSeparatorReturnsSpaceBeforeSelectorSeparatorFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceBeforeSelectorSeparator($space); | ||
|
||
self::assertSame($space, $this->subject->spaceBeforeSelectorSeparator()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceAfterSelectorSeparatorReturnsSpaceAfterSelectorSeparatorFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceAfterSelectorSeparator($space); | ||
|
||
self::assertSame($space, $this->subject->spaceAfterSelectorSeparator()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceBeforeListArgumentSeparatorReturnsSpaceSetForSpecificSeparator(): void | ||
{ | ||
$separator = ','; | ||
$space = ' '; | ||
$this->outputFormat->setSpaceBeforeListArgumentSeparators([$separator => $space]); | ||
$defaultSpace = "\t\t\t\t"; | ||
$this->outputFormat->setSpaceBeforeListArgumentSeparator($defaultSpace); | ||
|
||
self::assertSame($space, $this->subject->spaceBeforeListArgumentSeparator($separator)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceBeforeListArgumentSeparatorWithoutSpecificSettingReturnsDefaultSpace( | ||
): void { | ||
$space = ' '; | ||
$this->outputFormat->setSpaceBeforeListArgumentSeparators([',' => $space]); | ||
$defaultSpace = "\t\t\t\t"; | ||
$this->outputFormat->setSpaceBeforeListArgumentSeparator($defaultSpace); | ||
|
||
self::assertSame($defaultSpace, $this->subject->spaceBeforeListArgumentSeparator(';')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceAfterListArgumentSeparatorReturnsSpaceSetForSpecificSeparator(): void | ||
{ | ||
$separator = ','; | ||
$space = ' '; | ||
$this->outputFormat->setSpaceAfterListArgumentSeparators([$separator => $space]); | ||
$defaultSpace = "\t\t\t\t"; | ||
$this->outputFormat->setSpaceAfterListArgumentSeparator($defaultSpace); | ||
|
||
self::assertSame($space, $this->subject->spaceAfterListArgumentSeparator($separator)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceAfterListArgumentSeparatorWithoutSpecificSettingReturnsDefaultSpace( | ||
): void { | ||
$space = ' '; | ||
$this->outputFormat->setSpaceAfterListArgumentSeparators([',' => $space]); | ||
$defaultSpace = "\t\t\t\t"; | ||
$this->outputFormat->setSpaceAfterListArgumentSeparator($defaultSpace); | ||
|
||
self::assertSame($defaultSpace, $this->subject->spaceAfterListArgumentSeparator(';')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function spaceBeforeOpeningBraceReturnsSpaceBeforeOpeningBraceFromOutputFormat(): void | ||
{ | ||
$space = ' '; | ||
$this->outputFormat->setSpaceBeforeOpeningBrace($space); | ||
|
||
self::assertSame($space, $this->subject->spaceBeforeOpeningBrace()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function implodeForEmptyValuesReturnsEmptyString(): void | ||
{ | ||
$values = []; | ||
|
||
$result = $this->subject->implode(', ', $values); | ||
|
||
self::assertSame('', $result); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function implodeWithOneStringValueReturnsStringValue(): void | ||
{ | ||
$value = 'tea'; | ||
$values = [$value]; | ||
|
||
$result = $this->subject->implode(', ', $values); | ||
|
||
self::assertSame($value, $result); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function implodeWithMultipleStringValuesReturnsValuesSeparatedBySeparator(): void | ||
{ | ||
$value1 = 'tea'; | ||
$value2 = 'coffee'; | ||
$values = [$value1, $value2]; | ||
$separator = ', '; | ||
|
||
$result = $this->subject->implode($separator, $values); | ||
|
||
self::assertSame($value1 . $separator . $value2, $result); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function implodeWithOneRenderableReturnsRenderedRenderable(): void | ||
{ | ||
$renderable = $this->createMock(Renderable::class); | ||
$renderedRenderable = 'tea'; | ||
$renderable->method('render')->with($this->outputFormat)->willReturn($renderedRenderable); | ||
$values = [$renderable]; | ||
|
||
$result = $this->subject->implode(', ', $values); | ||
|
||
self::assertSame($renderedRenderable, $result); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function implodeWithMultipleRenderablesReturnsRenderedRenderablesSeparatedBySeparator(): void | ||
{ | ||
$renderable1 = $this->createMock(Renderable::class); | ||
$renderedRenderable1 = 'tea'; | ||
$renderable1->method('render')->with($this->outputFormat)->willReturn($renderedRenderable1); | ||
$renderable2 = $this->createMock(Renderable::class); | ||
$renderedRenderable2 = 'coffee'; | ||
$renderable2->method('render')->with($this->outputFormat)->willReturn($renderedRenderable2); | ||
$values = [$renderable1, $renderable2]; | ||
$separator = ', '; | ||
|
||
$result = $this->subject->implode($separator, $values); | ||
|
||
self::assertSame($renderedRenderable1 . $separator . $renderedRenderable2, $result); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function implodeWithIncreaseLevelFalseUsesDefaultIndentationLevelForRendering(): void | ||
{ | ||
$renderable = $this->createMock(Renderable::class); | ||
$renderedRenderable = 'tea'; | ||
$renderable->method('render')->with($this->outputFormat)->willReturn($renderedRenderable); | ||
$values = [$renderable]; | ||
|
||
$result = $this->subject->implode(', ', $values, false); | ||
|
||
self::assertSame($renderedRenderable, $result); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function implodeWithIncreaseLevelTrueIncreasesIndentationLevelForRendering(): void | ||
{ | ||
$renderable = $this->createMock(Renderable::class); | ||
$renderedRenderable = 'tea'; | ||
$renderable->method('render')->with($this->outputFormat->nextLevel())->willReturn($renderedRenderable); | ||
$values = [$renderable]; | ||
|
||
$result = $this->subject->implode(', ', $values, true); | ||
|
||
self::assertSame($renderedRenderable, $result); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the level increment being asserted here. Shouldn't we be getting tea after some space?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's up to the
Rederable
instance how it uses the indentation level. (For example,Comment
s do not care about the indentation level at all.)What we're testing here in this unit tests is that
render()
will get the next level output format passed in: Thewith()
call asserts that the expected call has the corresponding argument (compared using==
, not===
, or likeassertEquals()
instead ofassertSame()
), and will fail the test otherwise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed the change to the argument passed to
with()
compared with the previous test. I think I see and understand what's going on now. Reviewing this has been an educative process 👍