Skip to content
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

Fix: Sniff only pure indents, no mixed spaces and tabs #71

Merged
merged 2 commits into from
Aug 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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