Browse files

Added unit tests for max line length rule

  • Loading branch information...
1 parent 22b8cd8 commit 6034a50ab68b8dee9d1e131290a2894aa2d0a849 @DASPRiD committed Dec 2, 2011
Showing with 113 additions and 3 deletions.
  1. +4 −3 src/Flitch/Rule/File/MaxLineLength.php
  2. +109 −0 tests/Flitch/Rule/File/MaxLineLengthTest.php
View
7 src/Flitch/Rule/File/MaxLineLength.php
@@ -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.
@@ -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),
View
109 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.