Skip to content

Commit

Permalink
Universal/DisallowInlineTabs: add extra tests + fix test tab handling
Browse files Browse the repository at this point in the history
* Add a space-based test case file which shouldn't yield any errors or warnings at all.
* Make sure that the "tabwidth" set for test files is correctly respected.
* Fix the `DisallowInlineTabsUnitTest.5.inc` fixed file.
  • Loading branch information
jrfnl committed Mar 26, 2023
1 parent 5226c51 commit 19bc003
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
6 changes: 5 additions & 1 deletion Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public function process(File $phpcsFile, $stackPtr)
$this->tabWidth = Helper::getTabWidth($phpcsFile);
}

if (defined('PHP_CODESNIFFER_IN_TESTS')) {
$this->tabWidth = Helper::getCommandLineData($phpcsFile, 'tabWidth');
}

$tokens = $phpcsFile->getTokens();
$dummy = new DummyTokenizer('', $phpcsFile->config);

Expand All @@ -115,7 +119,7 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}

$dummy->replaceTabsInToken($token, ' ', ' ', $this->tabWidth);
$dummy->replaceTabsInToken($token);
}

$origContent = $token['orig_content'];
Expand Down
28 changes: 14 additions & 14 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<?php
/* *** TESTING WITHOUT A TAB WIDTH SET *** */
/**
* @param int $var Description - Bad: alignment using tabs.
* @param int $var Description - Bad: alignment using tabs.
* @param string $string Another description.
*/

$expected = ( $column - 1 );
$found = ( $this->tokens[ $closer ]['column'] - 1 ); // Bad.
$error = 'Array closer not aligned correctly; expected %s space(s) but found %s'; // Bad.
$data = array( // Bad.
$found = ( $this->tokens[ $closer ]['column'] - 1 ); // Bad.
$error = 'Array closer not aligned correctly; expected %s space(s) but found %s'; // Bad.
$data = array( // Bad.
$expected_value => 'data',
$found => 'more_data', // Bad.
$found => 'more_data', // Bad.
);

/*
* Test that the tab replacements do not negatively influence existing mid-line alignments.
*/
$a = true;
$aa = true;
$aaa = true;
$aaaa = true;
$aaaaa = true;
$a = true;
$aa = true;
$aaa = true;
$aaaa = true;
$aaaaa = true;
$aaaaaa = true;
$aaaaaaa = true;
$aaaaaaaa = true;

// Test tab replacement in inline comments.
// Test tab replacement in inline comments.

/*
* Tab indented and inline tabs.
* Tab indented and inline tabs.
*/

// Tab indented and inline tabs.
// Tab indented and inline tabs.
// Tab indented and inline tabs.
// Tab indented and inline tabs.
43 changes: 43 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/* *** SPACE BASED FILE - NO ERRORS OR WARNINGS EXPECTED AT ALL *** */

/**
* @param int $var Description.
* @param string $string Another description.
*/

$expected = ( $column - 1 );
$found = ( $this->tokens[ $closer ]['column'] - 1 );
$error = 'Array closer not aligned correctly; expected %s space(s) but found %s';
$data = array(
$expected_value => 'data',
$found => 'more_data',
);

/**
* @param int $var Description
* @param string $string Another description.
*/

$expected = ( $column - 1 );
$found = ( $this->tokens[ $closer ]['column'] - 1 );
$error = 'Array closer not aligned correctly; expected %s space(s) but found %s';
$data = array(
$expected_value => 'data',
$found => 'more_data',
);

$a = true;
$aa = true;
$aaa = true;
$aaaa = true;
$aaaaa = true;
$aaaaaa = true;
$aaaaaaa = true;
$aaaaaaaa = true;

/**
* @param int $var Description.
* @param string $string Another description.
*/
4 changes: 2 additions & 2 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function setCliValues($testFile, $config)
return;
}

if ($testFile === 'DisallowInlineTabsUnitTest.5.inc') {
// Set to the default.
if ($testFile === 'DisallowInlineTabsUnitTest.5.inc' || $testFile === 'DisallowInlineTabsUnitTest.6.inc') {
// Set to the default (results in tab width 1).
$config->tabWidth = 0;
return;
}
Expand Down

0 comments on commit 19bc003

Please sign in to comment.