Skip to content

Commit

Permalink
Merge pull request #289 from uuf6429/feature/improve-tags-handling
Browse files Browse the repository at this point in the history
Improve tag handling; update test
  • Loading branch information
acoulton authored Feb 26, 2025
2 parents 47ee3db + 684860c commit d899102
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 179 deletions.
29 changes: 2 additions & 27 deletions src/Node/ExampleNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class ExampleNode implements ScenarioInterface, NamedScenarioInterface
{
use TaggedNodeTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -105,33 +107,6 @@ public function getTitle()
return $this->text;
}

/**
* Checks if outline is tagged with tag.
*
* @param string $tag
*
* @return bool
*/
public function hasTag($tag)
{
return in_array($tag, $this->getTags());
}

/**
* Checks if outline has tags (both inherited from feature and own).
*
* @return bool
*/
public function hasTags()
{
return count($this->getTags()) > 0;
}

/**
* Returns outline tags (including inherited from feature).
*
* @return string[]
*/
public function getTags()
{
return $this->tags;
Expand Down
9 changes: 3 additions & 6 deletions src/Node/ExampleTableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class ExampleTableNode extends TableNode
class ExampleTableNode extends TableNode implements TaggedNodeInterface
{
use TaggedNodeTrait;

/**
* @var string[]
*/
Expand Down Expand Up @@ -52,11 +54,6 @@ public function getNodeType()
return 'ExampleTable';
}

/**
* Returns attached tags.
*
* @return string[]
*/
public function getTags()
{
return $this->tags;
Expand Down
29 changes: 2 additions & 27 deletions src/Node/FeatureNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class FeatureNode implements KeywordNodeInterface, TaggedNodeInterface
{
use TaggedNodeTrait;

/**
* @var string|null
*/
Expand Down Expand Up @@ -136,33 +138,6 @@ public function getDescription()
return $this->description;
}

/**
* Checks if feature is tagged with tag.
*
* @param string $tag
*
* @return bool
*/
public function hasTag($tag)
{
return in_array($tag, $this->tags);
}

/**
* Checks if feature has tags.
*
* @return bool
*/
public function hasTags()
{
return count($this->tags) > 0;
}

/**
* Returns feature tags.
*
* @return string[]
*/
public function getTags()
{
return $this->tags;
Expand Down
29 changes: 2 additions & 27 deletions src/Node/OutlineNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class OutlineNode implements ScenarioInterface
{
use TaggedNodeTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -92,33 +94,6 @@ public function getTitle()
return $this->title;
}

/**
* Checks if outline is tagged with tag.
*
* @param string $tag
*
* @return bool
*/
public function hasTag($tag)
{
return in_array($tag, $this->getTags());
}

/**
* Checks if outline has tags (both inherited from feature and own).
*
* @return bool
*/
public function hasTags()
{
return count($this->getTags()) > 0;
}

/**
* Returns outline tags (including inherited from feature).
*
* @return string[]
*/
public function getTags()
{
return $this->tags;
Expand Down
29 changes: 2 additions & 27 deletions src/Node/ScenarioNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class ScenarioNode implements ScenarioInterface, NamedScenarioInterface
{
use TaggedNodeTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -83,33 +85,6 @@ public function getName(): ?string
return $this->title;
}

/**
* Checks if scenario is tagged with tag.
*
* @param string $tag
*
* @return bool
*/
public function hasTag($tag)
{
return in_array($tag, $this->getTags());
}

/**
* Checks if scenario has tags (both inherited from feature and own).
*
* @return bool
*/
public function hasTags()
{
return count($this->getTags()) > 0;
}

/**
* Returns scenario tags (including inherited from feature).
*
* @return array
*/
public function getTags()
{
return $this->tags;
Expand Down
4 changes: 2 additions & 2 deletions src/Node/TaggedNodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ interface TaggedNodeInterface extends NodeInterface
public function hasTag($tag);

/**
* Checks if node has tags (both inherited from feature and own).
* Checks if node has tags (including any inherited tags e.g. from feature).
*
* @return bool
*/
public function hasTags();

/**
* Returns node tags (including inherited from feature).
* Returns node tags (including any inherited tags e.g. from feature).
*
* @return list<string>
*/
Expand Down
42 changes: 42 additions & 0 deletions src/Node/TaggedNodeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Behat Gherkin Parser.
* (c) 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.
*/

namespace Behat\Gherkin\Node;

/**
* This trait partially implements {@see TaggedNodeInterface}.
*
* @internal
*/
trait TaggedNodeTrait
{
/**
* @return list<string>
*/
abstract public function getTags();

/**
* @param string $tag
*
* @return bool
*/
public function hasTag($tag)
{
return in_array($tag, $this->getTags(), true);
}

/**
* @return bool
*/
public function hasTags()
{
return $this->getTags() !== [];
}
}
40 changes: 20 additions & 20 deletions tests/Loader/ArrayLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ public function testLoadFeatures(): void

$this->assertCount(2, $features);

$this->assertEquals(3, $features[0]->getLine());
$this->assertEquals('First feature', $features[0]->getTitle());
$this->assertSame(3, $features[0]->getLine());
$this->assertSame('First feature', $features[0]->getTitle());
$this->assertNull($features[0]->getDescription());
$this->assertNull($features[0]->getFile());
$this->assertEquals('en', $features[0]->getLanguage());
$this->assertFalse($features[0]->hasTags());
$this->assertSame('en', $features[0]->getLanguage());
$this->assertSame([], $features[0]->getTags());

$this->assertEquals(1, $features[1]->getLine());
$this->assertSame(1, $features[1]->getLine());
$this->assertNull($features[1]->getTitle());
$this->assertEquals('Second feature description', $features[1]->getDescription());
$this->assertSame('Second feature description', $features[1]->getDescription());
$this->assertNull($features[1]->getFile());
$this->assertEquals('ru', $features[1]->getLanguage());
$this->assertEquals(['some', 'tags'], $features[1]->getTags());
$this->assertSame('ru', $features[1]->getLanguage());
$this->assertSame(['some', 'tags'], $features[1]->getTags());
}

public function testLoadScenarios(): void
Expand Down Expand Up @@ -105,19 +105,19 @@ public function testLoadScenarios(): void
$this->assertCount(3, $scenarios);

$this->assertInstanceOf(ScenarioNode::class, $scenarios[0]);
$this->assertEquals('First scenario', $scenarios[0]->getTitle());
$this->assertFalse($scenarios[0]->hasTags());
$this->assertEquals(2, $scenarios[0]->getLine());
$this->assertSame('First scenario', $scenarios[0]->getTitle());
$this->assertSame([], $scenarios[0]->getTags());
$this->assertSame(2, $scenarios[0]->getLine());

$this->assertInstanceOf(ScenarioNode::class, $scenarios[1]);
$this->assertNull($scenarios[1]->getTitle());
$this->assertEquals(['second', 'scenario', 'tags'], $scenarios[1]->getTags());
$this->assertEquals(1, $scenarios[1]->getLine());
$this->assertSame(['second', 'scenario', 'tags'], $scenarios[1]->getTags());
$this->assertSame(1, $scenarios[1]->getLine());

$this->assertInstanceOf(ScenarioNode::class, $scenarios[2]);
$this->assertNull($scenarios[2]->getTitle());
$this->assertEquals(['third', 'scenario'], $scenarios[2]->getTags());
$this->assertEquals(3, $scenarios[2]->getLine());
$this->assertSame(['third', 'scenario'], $scenarios[2]->getTags());
$this->assertSame(3, $scenarios[2]->getLine());
}

public function testLoadOutline(): void
Expand Down Expand Up @@ -148,14 +148,14 @@ public function testLoadOutline(): void
$this->assertCount(2, $outlines);

$this->assertInstanceOf(OutlineNode::class, $outlines[0]);
$this->assertEquals('First outline', $outlines[0]->getTitle());
$this->assertFalse($outlines[0]->hasTags());
$this->assertEquals(2, $outlines[0]->getLine());
$this->assertSame('First outline', $outlines[0]->getTitle());
$this->assertSame([], $outlines[0]->getTags());
$this->assertSame(2, $outlines[0]->getLine());

$this->assertInstanceOf(OutlineNode::class, $outlines[1]);
$this->assertNull($outlines[1]->getTitle());
$this->assertEquals(['second', 'outline', 'tags'], $outlines[1]->getTags());
$this->assertEquals(1, $outlines[1]->getLine());
$this->assertSame(['second', 'outline', 'tags'], $outlines[1]->getTags());
$this->assertSame(1, $outlines[1]->getLine());
}

public function testOutlineExamples(): void
Expand Down
Loading

0 comments on commit d899102

Please sign in to comment.