From 19bc00367d0cd3e524e5f238950c15bf154278ee Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 26 Mar 2023 00:56:30 +0100 Subject: [PATCH] Universal/DisallowInlineTabs: add extra tests + fix test tab handling * 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. --- .../WhiteSpace/DisallowInlineTabsSniff.php | 6 ++- .../DisallowInlineTabsUnitTest.5.inc.fixed | 28 ++++++------ .../DisallowInlineTabsUnitTest.6.inc | 43 +++++++++++++++++++ .../WhiteSpace/DisallowInlineTabsUnitTest.php | 4 +- 4 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc diff --git a/Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php b/Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php index 284bcd9..2f17e0f 100644 --- a/Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php +++ b/Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php @@ -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); @@ -115,7 +119,7 @@ public function process(File $phpcsFile, $stackPtr) continue; } - $dummy->replaceTabsInToken($token, ' ', ' ', $this->tabWidth); + $dummy->replaceTabsInToken($token); } $origContent = $token['orig_content']; diff --git a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed index ceb402c..ce00160 100644 --- a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed +++ b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed @@ -1,35 +1,35 @@ 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. diff --git a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc new file mode 100644 index 0000000..05b5520 --- /dev/null +++ b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc @@ -0,0 +1,43 @@ +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. + */ diff --git a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php index 3079a3c..262127a 100644 --- a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php +++ b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php @@ -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; }