Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/Behat/Gherkin/Filter/LineFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,26 @@ public function filterFeature(FeatureNode $feature)
}

if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
$table = $scenario->getExampleTable()->getTable();
$lines = array_keys($table);

if (in_array($this->filterLine, $lines)) {
$filteredTable = array($lines[0] => $table[$lines[0]]);

if ($lines[0] !== $this->filterLine) {
$filteredTable[$this->filterLine] = $table[$this->filterLine];
foreach ($scenario->getExampleTables() as $exampleTable) {
$table = $exampleTable->getTable();
$lines = array_keys($table);

if (in_array($this->filterLine, $lines)) {
$filteredTable = array($lines[0] => $table[$lines[0]]);

if ($lines[0] !== $this->filterLine) {
$filteredTable[$this->filterLine] = $table[$this->filterLine];
}

$scenario = new OutlineNode(
$scenario->getTitle(),
$scenario->getTags(),
$scenario->getSteps(),
array(new ExampleTableNode($filteredTable, $exampleTable->getKeyword(), $exampleTable->getTags())),
$scenario->getKeyword(),
$scenario->getLine()
);
}

$scenario = new OutlineNode(
$scenario->getTitle(),
$scenario->getTags(),
$scenario->getSteps(),
new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()),
$scenario->getKeyword(),
$scenario->getLine()
);
}
}

Expand Down
25 changes: 17 additions & 8 deletions src/Behat/Gherkin/Filter/LineRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,32 @@ public function filterFeature(FeatureNode $feature)
}

if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
$table = $scenario->getExampleTable()->getTable();
$lines = array_keys($table);
// first accumulate examples and then create scenario
$exampleTableNodes = array();

$filteredTable = array($lines[0] => $table[$lines[0]]);
unset($table[$lines[0]]);
foreach ($scenario->getExampleTables() as $exampleTable) {
$table = $exampleTable->getTable();
$lines = array_keys($table);

foreach ($table as $line => $row) {
if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
$filteredTable[$line] = $row;
$filteredTable = array($lines[0] => $table[$lines[0]]);
unset($table[$lines[0]]);

foreach ($table as $line => $row) {
if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
$filteredTable[$line] = $row;
}
}

if (count($filteredTable) > 1) {
$exampleTableNodes[] = new ExampleTableNode($filteredTable, $exampleTable->getKeyword(), $exampleTable->getTags());
}
}

$scenario = new OutlineNode(
$scenario->getTitle(),
$scenario->getTags(),
$scenario->getSteps(),
new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()),
$exampleTableNodes,
$scenario->getKeyword(),
$scenario->getLine()
);
Expand Down
64 changes: 63 additions & 1 deletion src/Behat/Gherkin/Filter/TagFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Behat\Gherkin\Filter;

use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\OutlineNode;
use Behat\Gherkin\Node\ScenarioInterface;

/**
Expand All @@ -32,6 +33,57 @@ public function __construct($filterString)
$this->filterString = trim($filterString);
}

/**
* Filters feature according to the filter.
*
* @param FeatureNode $feature
*
* @return FeatureNode
*/
public function filterFeature(FeatureNode $feature)
{
$scenarios = array();
foreach ($feature->getScenarios() as $scenario) {
if (!$this->isScenarioMatch($feature, $scenario)) {
continue;
}

if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {

$exampleTables = array();

foreach ($scenario->getExampleTables() as $exampleTable) {
if ($this->isTagsMatchCondition(array_merge($feature->getTags(), $scenario->getTags(), $exampleTable->getTags()))) {
$exampleTables[] = $exampleTable;
}
}

$scenario = new OutlineNode(
$scenario->getTitle(),
$scenario->getTags(),
$scenario->getSteps(),
$exampleTables,
$scenario->getKeyword(),
$scenario->getLine()
);
}

$scenarios[] = $scenario;
}

return new FeatureNode(
$feature->getTitle(),
$feature->getDescription(),
$feature->getTags(),
$feature->getBackground(),
$scenarios,
$feature->getKeyword(),
$feature->getLanguage(),
$feature->getFile(),
$feature->getLine()
);
}

/**
* Checks if Feature matches specified filter.
*
Expand All @@ -47,13 +99,23 @@ public function isFeatureMatch(FeatureNode $feature)
/**
* Checks if scenario or outline matches specified filter.
*
* @param FeatureNode $feature Feature node instance
* @param FeatureNode $feature Feature node instance
* @param ScenarioInterface $scenario Scenario or Outline node instance
*
* @return Boolean
*/
public function isScenarioMatch(FeatureNode $feature, ScenarioInterface $scenario)
{
if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
foreach ($scenario->getExampleTables() as $example) {
if ($this->isTagsMatchCondition(array_merge($feature->getTags(), $scenario->getTags(), $example->getTags()))) {
return true;
}
}

return false;
}

return $this->isTagsMatchCondition(array_merge($feature->getTags(), $scenario->getTags()));
}

Expand Down
66 changes: 64 additions & 2 deletions src/Behat/Gherkin/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,26 @@ protected function loadBackgroundHash(array $hash)

$steps = $this->loadStepsHash($hash['steps']);

return new BackgroundNode($hash['title'], $steps, $hash['keyword'], $hash['line']);
$examples = array();
if (isset($hash['examples'])) {
if (isset($hash['examples']['keyword'])) {
$examplesKeyword = $hash['examples']['keyword'];
unset($hash['examples']['keyword']);
} else {
$examplesKeyword = 'Examples';
}

$exHash = $hash['examples'];

if ($this->examplesAreInArray($exHash)) {
$examples = $this->processExamplesArray($exHash, $examplesKeyword, $examples);
} else {
// examples as a single table - we create an array with the only one element
$examples[] = new ExampleTableNode($exHash, $examplesKeyword);;
}
}

return new BackgroundNode($hash['title'], $steps, $hash['keyword'], $hash['line'], $examples);
}

/**
Expand Down Expand Up @@ -179,7 +198,15 @@ protected function loadOutlineHash(array $hash, $line = 0)
$examplesKeyword = 'Examples';
}

$examples = new ExampleTableNode($hash['examples'], $examplesKeyword);
$exHash = $hash['examples'];
$examples = array();

if ($this->examplesAreInArray($exHash)) {
$examples = $this->processExamplesArray($exHash, $examplesKeyword, $examples);
} else {
// examples as a single table - we create an array with the only one element
$examples[] = new ExampleTableNode($exHash, $examplesKeyword);;
}

return new OutlineNode($hash['title'], $hash['tags'], $steps, $examples, $hash['keyword'], $hash['line']);
}
Expand Down Expand Up @@ -266,4 +293,39 @@ protected function loadPyStringHash(array $hash, $line = 0)

return new PyStringNode($strings, $line);
}

/**
* Checks if examples node is an array
* @param $exHash object hash
* @return bool
*/
private function examplesAreInArray($exHash)
{
return isset($exHash[0]);
}

/**
* Processes cases when examples are in the form of array of arrays
* OR in the form of array of objects
*
* @param $exHash array hash
* @param $examplesKeyword string
* @param $examples array
* @return array
*/
private function processExamplesArray($exHash, $examplesKeyword, $examples)
{
for ($i = 0; $i < count($exHash); $i++) {
if (isset($exHash[$i]['table'])) {
// we have examples as objects, hence there could be tags
$exHashTags = isset($exHash[$i]['tags']) ? $exHash[$i]['tags'] : array();
$examples[] = new ExampleTableNode($exHash[$i]['table'], $examplesKeyword, $exHashTags);
} else {
// we have examples as arrays
$examples[] = new ExampleTableNode($exHash[$i], $examplesKeyword);
}
}

return $examples;
}
}
46 changes: 45 additions & 1 deletion src/Behat/Gherkin/Node/BackgroundNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class BackgroundNode implements ScenarioLikeInterface
* @var integer
*/
private $line;
/**
* @var ExampleTableNode[]
*/
private $exampleTable;

/**
* Initializes background.
Expand All @@ -41,13 +45,23 @@ class BackgroundNode implements ScenarioLikeInterface
* @param StepNode[] $steps
* @param string $keyword
* @param integer $line
* @param null|ExampleTableNode|ExampleTableNode[] $exampleTable
*/
public function __construct($title, array $steps, $keyword, $line)
public function __construct($title, array $steps, $keyword, $line, $exampleTable=null)
{
$this->title = $title;
$this->steps = $steps;
$this->keyword = $keyword;
$this->line = $line;
if ($exampleTable === null) {
$this->exampleTable = null;
}else if (is_array($exampleTable)) {
if (!empty($exampleTable)) {
$this->exampleTable = $exampleTable;
}
} else {
$this->$exampleTable = array($exampleTable);
}
}

/**
Expand Down Expand Up @@ -109,4 +123,34 @@ public function getLine()
{
return $this->line;
}

/**
* Returns if background has ExampleTable
*
* @return boolean
*/
public function hasExamples()
{
return $this->exampleTable !== null && count($this->exampleTable) > 0;
}

/**
* Returns if background has ExampleTable
*
* @return null|ExampleTableNode[]
*/
public function getExamples()
{
return $this->exampleTable;
}

/**
* Add example Table to the background Node
*
* @param ExampleTableNode
*/
public function addExample($table)
{
$this->exampleTable[] = $table;
}
}
20 changes: 18 additions & 2 deletions src/Behat/Gherkin/Node/ExampleTableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
class ExampleTableNode extends TableNode
{
/**
* @var string[]
*/
private $tags;

/**
* @var string
*/
Expand All @@ -25,12 +30,14 @@ class ExampleTableNode extends TableNode
/**
* Initializes example table.
*
* @param array $table Table in form of [$rowLineNumber => [$val1, $val2, $val3]]
* @param array $table Table in form of [$rowLineNumber => [$val1, $val2, $val3]]
* @param string $keyword
* @param string[] $tags
*/
public function __construct(array $table, $keyword)
public function __construct(array $table, $keyword, array $tags = array())
{
$this->keyword = $keyword;
$this->tags = $tags;

parent::__construct($table);
}
Expand All @@ -45,6 +52,15 @@ public function getNodeType()
return 'ExampleTable';
}

/**
* Returns attached tags
* @return \string[]
*/
public function getTags()
{
return $this->tags;
}

/**
* Returns example table keyword.
*
Expand Down
Loading