Permalink
Browse files

refactored filtering system (closes Behat#154)

  • Loading branch information...
1 parent 3fe2fb5 commit 30ea22dbeb1044a63f5911417a738dfcddb90b8e @everzet everzet committed Jul 1, 2012
@@ -37,4 +37,11 @@ public function isFeatureMatch(FeatureNode $feature);
* @return Boolean
*/
public function isScenarioMatch(ScenarioNode $scenario);
+
+ /**
+ * Filters feature according to the filter.
+ *
+ * @param FeatureNode $feature
+ */
+ public function filterFeature(FeatureNode $feature);
}
@@ -3,7 +3,8 @@
namespace Behat\Gherkin\Filter;
use Behat\Gherkin\Node\FeatureNode,
- Behat\Gherkin\Node\ScenarioNode;
+ Behat\Gherkin\Node\ScenarioNode,
+ Behat\Gherkin\Node\OutlineNode;
/*
* This file is part of the Behat Gherkin.
@@ -53,6 +54,47 @@ public function isFeatureMatch(FeatureNode $feature)
*/
public function isScenarioMatch(ScenarioNode $scenario)
{
- return $this->filterLine === $scenario->getLine();
+ if ($this->filterLine === $scenario->getLine()) {
+ return true;
+ }
+
+ if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
+ return $this->filterLine === $scenario->getLine()
@cogitatio

cogitatio Jul 1, 2012

This is redunant. Has been done in line 57

+ || in_array($this->filterLine, $scenario->getExamples()->getRowLines());
+ }
+
+ return false;
+ }
+
+ /**
+ * Filters feature according to the filter.
+ *
+ * @param FeatureNode $feature
+ */
+ public function filterFeature(FeatureNode $feature)
+ {
+ $scenarios = $feature->getScenarios();
+ foreach ($scenarios as $i => $scenario) {
+ if (!$this->isScenarioMatch($scenario)) {
+ unset($scenarios[$i]);
+ continue;
+ }
+
+ if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
+ $lines = $scenario->getExamples()->getRowLines();
+ $rows = $scenario->getExamples()->getNumeratedRows();
+
+ if (current($lines) <= $this->filterLine && end($lines) >= $this->filterLine) {
+ $scenario->getExamples()->setRows(array());
+ $scenario->getExamples()->addRow($rows[$lines[0]], $lines[0]);
+
+ if ($lines[0] !== $this->filterLine) {
+ $scenario->getExamples()->addRow($rows[$this->filterLine], $this->filterLine);
+ }
+ }
+ }
+ }
+
+ $feature->setScenarios($scenarios);
}
}
@@ -3,7 +3,8 @@
namespace Behat\Gherkin\Filter;
use Behat\Gherkin\Node\FeatureNode,
- Behat\Gherkin\Node\ScenarioNode;
+ Behat\Gherkin\Node\ScenarioNode,
+ Behat\Gherkin\Node\OutlineNode;
/*
* This file is part of the Behat Gherkin.
@@ -60,7 +61,52 @@ public function isFeatureMatch(FeatureNode $feature)
*/
public function isScenarioMatch(ScenarioNode $scenario)
{
- return $this->filterMinLine <= $scenario->getLine()
- && $this->filterMaxLine >= $scenario->getLine();
+ if ($this->filterMinLine <= $scenario->getLine()
+ && $this->filterMaxLine >= $scenario->getLine()) {
+ return true;
+ }
+
+ if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
+ foreach ($scenario->getExamples()->getRowLines() as $line) {
+ if ($line >= $this->filterMinLine && $line <= $this->filterMaxLine) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Filters feature according to the filter.
+ *
+ * @param FeatureNode $feature
+ */
+ public function filterFeature(FeatureNode $feature)
+ {
+ $scenarios = $feature->getScenarios();
+ foreach ($scenarios as $i => $scenario) {
+ if (!$this->isScenarioMatch($scenario)) {
+ unset($scenarios[$i]);
+ continue;
+ }
+
+ if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
+ $lines = $scenario->getExamples()->getRowLines();
+ $rows = $scenario->getExamples()->getNumeratedRows();
+
+ $scenario->getExamples()->setRows(array());
+ $scenario->getExamples()->addRow($rows[$lines[0]], $lines[0]);
+ unset($rows[$lines[0]]);
+
+ foreach ($rows as $line => $row) {
+ if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
+ $scenario->getExamples()->addRow($row, $line);
+ }
+ }
+ }
+ }
+
+ $feature->setScenarios($scenarios);
}
}
@@ -18,14 +18,14 @@
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
-class NameFilter implements FilterInterface
+class NameFilter extends SimpleFilter
{
protected $filterString;
/**
* Initializes filter.
*
- * @param string $filterStringString Name filter string
+ * @param string $filterString Name filter string
*/
public function __construct($filterString)
{
@@ -0,0 +1,38 @@
+<?php
+
+namespace Behat\Gherkin\Filter;
+
+use Behat\Gherkin\Node\FeatureNode;
+
+/*
+ * This file is part of the Behat Gherkin.
+ * (c) 2011 Konstantin Kudryashov <ever.zet@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Abstract filter class.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ */
+abstract class SimpleFilter implements FilterInterface
+{
+ /**
+ * Filters feature according to the filter.
+ *
+ * @param FeatureNode $feature
+ */
+ public function filterFeature(FeatureNode $feature)
+ {
+ $scenarios = $feature->getScenarios();
+ foreach ($scenarios as $i => $scenario) {
+ if (!$this->isScenarioMatch($scenario)) {
+ unset($scenarios[$i]);
+ }
+ }
+
+ $feature->setScenarios($scenarios);
+ }
+}
@@ -19,14 +19,14 @@
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
-class TagFilter implements FilterInterface
+class TagFilter extends SimpleFilter
{
protected $filterString;
/**
* Initializes filter.
*
- * @param string $filterStringString Name filter string
+ * @param string $filterString Name filter string
*/
public function __construct($filterString)
{
@@ -61,6 +61,8 @@ public function isScenarioMatch(ScenarioNode $scenario)
* Checks that node matches condition.
*
* @param AbstractNode $node Node to check
+ *
+ * @return Boolean
*/
protected function matchesCondition(AbstractNode $node)
{
@@ -98,20 +98,11 @@ public function load($resource, array $filters = array())
}
$features = $loader->load($resource);
-
foreach ($features as $feature) {
- $scenarios = $feature->getScenarios();
- foreach ($scenarios as $i => $scenario) {
- foreach ($filters as $filter) {
- if (!$filter->isScenarioMatch($scenario)) {
- unset($scenarios[$i]);
- break;
- }
- }
+ foreach ($filters as $filter) {
+ $filter->filterFeature($feature);
}
- $feature->setScenarios($scenarios);
-
if ($this->freeze) {
$feature->freeze();
}
@@ -55,10 +55,6 @@ public function createExampleRowStepArgument(array $tokens)
*/
public function addRow($row, $line = null)
{
- if (null !== $line) {
- $this->rowLines[] = $line;
- }
-
if (is_array($row)) {
$this->rows[] = $row;
} else {
@@ -68,6 +64,8 @@ public function addRow($row, $line = null)
return preg_replace("/^\s*|\s*$/", '', $item);
}, explode('|', $row));
}
+
+ $this->rowLines[count($this->rows) - 1] = $line;
}
/**
@@ -88,6 +86,7 @@ public function getRows()
public function setRows(array $rows)
{
$this->rows = $rows;
+ $this->rowLines = array();
}
/**
@@ -154,25 +153,6 @@ public function getRowsHash()
}
/**
- * Converts table into string
- *
- * @return string
- */
- public function __toString()
- {
- $string = '';
-
- for ($i = 0; $i < count($this->getRows()); $i++) {
- if ('' !== $string) {
- $string .= "\n";
- }
- $string .= $this->getRowAsString($i);
- }
-
- return $string;
- }
-
- /**
* Sets current node definition keyword.
*
* @param string $keyword Sets table keyword
@@ -193,6 +173,17 @@ public function getKeyword()
}
/**
+ * Returns numerated table lines.
+ * Line numbers are keys, lines are values.
+ *
+ * @return array
+ */
+ public function getNumeratedRows()
+ {
+ return array_combine($this->rowLines, $this->rows);
+ }
+
+ /**
* Returns line numbers for rows.
*
* @return array
@@ -213,6 +204,25 @@ public function getLine()
}
/**
+ * Converts table into string
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ $string = '';
+
+ for ($i = 0; $i < count($this->getRows()); $i++) {
+ if ('' !== $string) {
+ $string .= "\n";
+ }
+ $string .= $this->getRowAsString($i);
+ }
+
+ return $string;
+ }
+
+ /**
* Returns max length of specific column.
*
* @param integer $columnNum Column number
Oops, something went wrong.

0 comments on commit 30ea22d

Please sign in to comment.