Skip to content

Commit

Permalink
Add Range node generator and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thePanz committed Apr 5, 2018
1 parent 285543d commit 6ce8597
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/Languages/Galach/Generators/Native/Range.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace QueryTranslator\Languages\Galach\Generators\Native;

use LogicException;
use QueryTranslator\Languages\Galach\Generators\Common\Visitor;
use QueryTranslator\Languages\Galach\Values\Node\Term;
use QueryTranslator\Languages\Galach\Values\Token\Range as RangeToken;
use QueryTranslator\Values\Node;

/**
* Range Node Visitor implementation.
*/
final class Range extends Visitor
{
public function accept(Node $node)
{
return $node instanceof Term && $node->token instanceof RangeToken;
}

public function visit(Node $node, Visitor $subVisitor = null, $options = null)
{
if (!$node instanceof Term) {
throw new LogicException(
'Implementation accepts instance of Term Node'
);
}

$token = $node->token;

if (!$token instanceof RangeToken) {
throw new LogicException(
'Implementation accepts instance of Range Token'
);
}

$domainPrefix = '' === $token->domain ? '' : "{$token->domain}:";

switch ($token->type) {
case RangeToken::TYPE_INCLUSIVE:
return $domainPrefix . '[' . $token->rangeFrom . ' TO ' . $token->rangeTo . ']';

case RangeToken::TYPE_EXCLUSIVE:
return $domainPrefix . '{' . $token->rangeFrom . ' TO ' . $token->rangeTo . '}';

default:
throw new LogicException(sprintf('Range type %s is not supported', $token->type));
}
}
}
69 changes: 69 additions & 0 deletions tests/Galach/Generators/Native/RangeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace QueryTranslator\Tests\Galach\Generators\Native;

use PHPUnit\Framework\TestCase;
use QueryTranslator\Languages\Galach\Generators\Common\Visitor;
use QueryTranslator\Languages\Galach\Generators\Native\Range;
use QueryTranslator\Languages\Galach\Values\Node\Term;
use QueryTranslator\Languages\Galach\Values\Token\Range as RangeToken;
use QueryTranslator\Languages\Galach\Values\Token\Word;
use QueryTranslator\Values\Node;

class RangeTest extends TestCase
{
/**
* @var Visitor
*/
public $visitor;

protected function setUp()
{
$this->visitor = new Range();
}

public function acceptDataprovider()
{
return [
[true, new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive'))],
[false, new Term(new Word('word', 0, '', 'a'))],
];
}

/**
* @param bool $expected
* @param Node $token
*
* @dataProvider acceptDataprovider
*/
public function testAccepts($expected, $node)
{
$this->assertSame($expected, $this->visitor->accept($node));
}

public function visitDataprovider()
{
return [
['[a TO b]', new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive'))],
['{a TO b}', new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'exclusive'))],
];
}

/**
* @param string $expected
* @param Node $token
*
* @dataProvider visitDataprovider
*/
public function testVisit($expected, $node)
{
$this->assertSame($expected, $this->visitor->visit($node));
}

public function testVisitUnknownTypeFails()
{
$this->expectException(\LogicException::class);
$node = new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'unknown'));
$this->visitor->visit($node);
}
}

0 comments on commit 6ce8597

Please sign in to comment.