Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
Added unit tests for max line length rule
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Dec 2, 2011
1 parent 22b8cd8 commit 6034a50
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Flitch/Rule/File/MaxLineLength.php
Expand Up @@ -20,7 +20,8 @@
namespace Flitch\Rule\File;

use Flitch\Rule\Rule,
Flitch\File\File;
Flitch\File\File,
Flitch\File\Error;

/**
* Max line length rule.
Expand All @@ -47,13 +48,13 @@ public function check(File $file, array $options = array())
$hardLimit = (isset($options['hard-limit']) ? (int) $options['hard-limit'] : 120);

foreach ($file->getLines() as $lineNo => $line) {
if (iconv_strlen($line, 'utf-8') > $hardLimit) {
if (iconv_strlen($line['content'], 'utf-8') > $hardLimit) {
$file->addError(new Error(
$lineNo, 0, Error::SEVERITY_ERROR,
sprintf('Line may not be longer than %d characters', $hardLimit),
$this
));
} elseif (iconv_strlen($line, 'utf-8') > $softLimit) {
} elseif (iconv_strlen($line['content'], 'utf-8') > $softLimit) {
$file->addError(new Error(
$lineNo, 0, Error::SEVERITY_WARNING,
sprintf('Line should not be longer than %d characters', $softLimit),
Expand Down
109 changes: 109 additions & 0 deletions tests/Flitch/Rule/File/MaxLineLengthTest.php
@@ -0,0 +1,109 @@
<?php
/**
* Flitch
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to mail@dasprids.de so I can send you a copy immediately.
*
* @category Flitch
* @package Flitch_Rule
* @subpackage UnitTests
* @copyright Copyright (c) 2011 Ben Scholzen <mail@dasprids.de>
* @license New BSD License
*/

namespace FlitchTest\Rule\File;

use Flitch\Test\RuleTestCase,
Flitch\File\File,
Flitch\Rule\File\MaxLineLength;

/**
* @category Flitch
* @package Flitch_Rule
* @subpackage UnitTests
* @copyright Copyright (c) 2011 Ben Scholzen <mail@dasprids.de>
* @license New BSD License
*/
class MaxLineLengthTest extends RuleTestCase
{
/**
* @var File
*/
protected $file;

public function setUp()
{
$this->file = new File(
'foo.php',
"<?php" . str_repeat(' ', 120) . "\n" . str_repeat(' ', 81)
);
}

public function testDefaultLimits()
{
$rule = new MaxLineLength();
$rule->check($this->file);

$this->assertRuleErrors($this->file, array(
array(
'line' => 1,
'column' => 0,
'severity' => 'error',
'message' => 'Line may not be longer than 120 characters',
'source' => 'Flitch\File\MaxLineLength'
),
array(
'line' => 2,
'column' => 0,
'severity' => 'warning',
'message' => 'Line should not be longer than 80 characters',
'source' => 'Flitch\File\MaxLineLength'
),
));
}

public function testCustomLimits()
{
$rule = new MaxLineLength();
$rule->check($this->file, array(
'hard-limit' => 80,
'soft-limit' => 40
));

$this->assertRuleErrors($this->file, array(
array(
'line' => 1,
'column' => 0,
'severity' => 'error',
'message' => 'Line may not be longer than 80 characters',
'source' => 'Flitch\File\MaxLineLength'
),
array(
'line' => 2,
'column' => 0,
'severity' => 'error',
'message' => 'Line may not be longer than 80 characters',
'source' => 'Flitch\File\MaxLineLength'
),
));
}

public function testProperHandlingWithUtf8Characters()
{
$file = new File(
'foo.php',
"<?php\n//" . str_repeat('ü', 75)
);

$rule = new MaxLineLength();
$rule->check($file);

$this->assertRuleErrors($file, array());
}
}

0 comments on commit 6034a50

Please sign in to comment.