Skip to content

Commit

Permalink
Merge pull request #71 from localheinz/fix/indent
Browse files Browse the repository at this point in the history
Fix: Sniff only pure indents, no mixed spaces and tabs
  • Loading branch information
localheinz authored Aug 12, 2018
2 parents 52ccc17 + e6e7e18 commit e6a2c7b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Format/Sniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function jsonEncodeOptions(string $json): int

private function indent(string $json): string
{
if (1 === \preg_match('/^(?P<indent>[ \t]+)("|{)/m', $json, $match)) {
if (1 === \preg_match('/^(?P<indent>( +|\t+)).*/m', $json, $match)) {
return $match['indent'];
}

Expand Down
65 changes: 50 additions & 15 deletions test/Unit/Format/SnifferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ public function providerJsonWithoutIndent(): \Generator
}

/**
* @dataProvider providerIndent
* @dataProvider providerPureIndentAndSniffedIndent
* @dataProvider providerMixedIndentAndSniffedIndent
*
* @param string $indent
* @param string $sniffedIndent
*/
public function testSniffReturnsFormatWithIndentSniffedFromArray(string $indent): void
public function testSniffReturnsFormatWithIndentSniffedFromArray(string $indent, string $sniffedIndent): void
{
$json = <<<JSON
[
Expand All @@ -156,15 +158,17 @@ public function testSniffReturnsFormatWithIndentSniffedFromArray(string $indent)
$format = $sniffer->sniff($json);

$this->assertInstanceOf(FormatInterface::class, $format);
$this->assertSame($indent, $format->indent());
$this->assertSame($sniffedIndent, $format->indent());
}

/**
* @dataProvider providerIndent
* @dataProvider providerPureIndentAndSniffedIndent
* @dataProvider providerMixedIndentAndSniffedIndent
*
* @param string $indent
* @param string $sniffedIndent
*/
public function testSniffReturnsFormatWithIndentSniffedFromObject(string $indent): void
public function testSniffReturnsFormatWithIndentSniffedFromObject(string $indent, string $sniffedIndent): void
{
$json = <<<JSON
{
Expand All @@ -181,29 +185,60 @@ public function testSniffReturnsFormatWithIndentSniffedFromObject(string $indent
$format = $sniffer->sniff($json);

$this->assertInstanceOf(FormatInterface::class, $format);
$this->assertSame($indent, $format->indent());
$this->assertSame($sniffedIndent, $format->indent());
}

public function providerIndent(): \Generator
public function providerPureIndentAndSniffedIndent(): \Generator
{
$characters = [
' ',
"\t",
'space' => ' ',
'tab' => "\t",
];

$counts = [1, 3];
$sizes = [1, 3];

foreach ($characters as $character) {
foreach ($counts as $count) {
$indent = \str_repeat($character, $count);
foreach ($characters as $style => $character) {
foreach ($sizes as $size) {
$key = \sprintf(
'%s-%d',
$style,
$size
);

yield [
$indent,
$pureIndent = \str_repeat(
$character,
$size
);

yield $key => [
$pureIndent,
$pureIndent,
];
}
}
}

public function providerMixedIndentAndSniffedIndent(): \Generator
{
$mixedIndents = [
'space-and-tab' => [
" \t",
' ',
],
'tab-and-space' => [
"\t ",
"\t",
],
];

foreach ($mixedIndents as $key => [$mixedIndent, $sniffedIndent]) {
yield $key => [
$mixedIndent,
$sniffedIndent,
];
}
}

/**
* @dataProvider providerJsonWithoutIndent
*
Expand Down

0 comments on commit e6a2c7b

Please sign in to comment.