Skip to content

Commit

Permalink
Merge pull request #11 from jkphl/master
Browse files Browse the repository at this point in the history
Fix symbol container bug / custom symbol addition (closes #10)
  • Loading branch information
chriskonnertz committed Apr 5, 2018
2 parents dec5f17 + 0987f62 commit 9a7f221
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 55 deletions.
15 changes: 11 additions & 4 deletions src/ChrisKonnertz/StringCalc/StringCalc.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class StringCalc
* StringCalc constructor.
*
* @param ContainerInterface $container
*
* @throws Exceptions\ContainerException If the passed container parameter is not an object
* @throws Exceptions\ContainerException If the passed container parameter does not implement ContainerInterface
*/
public function __construct($container = null)
{
Expand Down Expand Up @@ -85,7 +88,10 @@ public function __construct($container = null)
* Will return 0 if there is nothing to calculate.
*
* @param string $term
*
* @return float|int
* @throws Exceptions\ContainerException
* @throws Exceptions\NotFoundException
*/
public function calculate($term)
{
Expand All @@ -110,7 +116,10 @@ public function calculate($term)
* Tokenize the term. Returns an array with the tokens.
*
* @param string $term
*
* @return array
* @throws Exceptions\ContainerException
* @throws Exceptions\NotFoundException
*/
public function tokenize($term)
{
Expand All @@ -136,14 +145,12 @@ public function tokenize($term)
* Parses an array of tokens. Returns a single node that is a container node.
*
* @param Token[] $tokens
*
* @return ContainerNode
*/
public function parse(array $tokens)
{
/** @var SymbolContainerInterface $symbolContainer */
$symbolContainer = $this->container->get('stringcalc_symbolcontainer');

$parser = new Parser($symbolContainer);
$parser = new Parser($this->symbolContainer);

if ($this->customGrammarChecker !== null) {
$parser->setCustomGrammarChecker($this->customGrammarChecker);
Expand Down
117 changes: 66 additions & 51 deletions tests/StringCalcTests.php → tests/StringCalcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@

// Ensure backward compatibility
// @see http://stackoverflow.com/questions/42811164/class-phpunit-framework-testcase-not-found#answer-42828632
use ChrisKonnertz\StringCalc\Container\Container;
use ChrisKonnertz\StringCalc\Parser\Nodes\ContainerNode;
use ChrisKonnertz\StringCalc\Symbols\AbstractFunction;
use ChrisKonnertz\StringCalc\Symbols\SymbolContainer;

if (!class_exists('\PHPUnit\Framework\TestCase')) {
class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
}

/**
* Custom test function
*/
class TestFunction extends AbstractFunction
{
protected $identifiers = ['test'];

public function execute(array $arguments)
{
return 2;
}
}

/**
* Class StringCalcTest for tests with PHPUnit.
*/
Expand All @@ -22,16 +40,6 @@ class StringCalcTest extends \PHPUnit\Framework\TestCase
*/
private $stringCalc = null;

/**
* Creates and returns an instance of the main class
*
* @return \ChrisKonnertz\StringCalc\StringCalc
*/
protected function getInstance()
{
return new ChrisKonnertz\StringCalc\StringCalc();
}

public function testTokenize()
{
$stringCalc = $this->getInstance();
Expand All @@ -43,6 +51,16 @@ public function testTokenize()
$this->assertNotNull($tokens);
}

/**
* Creates and returns an instance of the main class
*
* @return \ChrisKonnertz\StringCalc\StringCalc
*/
protected function getInstance()
{
return new ChrisKonnertz\StringCalc\StringCalc();
}

public function testParseAndTraverse()
{
$stringCalc = $this->getInstance();
Expand All @@ -53,12 +71,13 @@ public function testParseAndTraverse()

$node = $stringCalc->parse($tokens);

$this->assertNotNull($node);
$this->assertInstanceOf(ContainerNode::class, $node);

$node->traverse(function($node, $level)
{
// do nothing
});
$node->traverse(
function ($node, $level) {
// do nothing
}
);
}

public function testGetSymbolContainer()
Expand All @@ -67,7 +86,7 @@ public function testGetSymbolContainer()

$symbolContainer = $stringCalc->getSymbolContainer();

$this->assertNotNull($symbolContainer);
$this->assertInstanceOf(SymbolContainer::class, $symbolContainer);
}

public function testGetContainer()
Expand All @@ -76,22 +95,18 @@ public function testGetContainer()

$container = $stringCalc->getContainer();

$this->assertNotNull($container);
$this->assertInstanceOf(Container::class, $container);
}

public function testAddSymbol()
{
$stringCalc = $this->getInstance();

$container = $stringCalc->getContainer();

$stringCalc = $this->getInstance();
$container = $stringCalc->getContainer();
$stringHelper = $container->get('stringcalc_stringhelper');
$symbol = new TestFunction($stringHelper);
$stringCalc->addSymbol($symbol);

$symbol = new ChrisKonnertz\StringCalc\Symbols\Concrete\Constants\PiConstant($stringHelper);

$replaceSymbol = get_class($symbol);

$stringCalc->addSymbol($symbol, $replaceSymbol);
$this->assertEquals(6, $stringCalc->calculate('3 * test()'));
}

public function testCalculations()
Expand Down Expand Up @@ -233,30 +248,6 @@ public function testCalculations()
$this->stringCalc->calculate('rand(1,2)');
}

public function testRandomCalculations()
{
$this->stringCalc = $this->getInstance();
$grammar = new \ChrisKonnertz\StringCalc\Grammar\StringCalcGrammar();

$numberOfCalculations = 100;
$calculations = [];

// TODO Implement the rest of this method
return;

for ($i = 0; $i < $numberOfCalculations; $i++) {
$term = '';
$result = eval($term);

$calculation = [$term, $result];
$calculations[] = $calculation;
}

// Will call the assertEquals method with the delta parameter set which means assertEquals
// will report an error if result and expected result are not within $delta of each other
$this->doCalculations($calculations, self::RESULT_DELTA);
}

/**
* Performs the calculations.
* $calculations is an array of arrays with two values:
Expand All @@ -269,7 +260,7 @@ public function testRandomCalculations()
private function doCalculations(array $calculations, $delta = 0.0)
{
foreach ($calculations as $calculation) {
$term = $calculation[0];
$term = $calculation[0];
$expectedResult = $calculation[1];

$calculatedResult = $this->stringCalc->calculate($term);
Expand All @@ -278,4 +269,28 @@ private function doCalculations(array $calculations, $delta = 0.0)
}
}

public function _testRandomCalculations()
{
$this->stringCalc = $this->getInstance();
$grammar = new \ChrisKonnertz\StringCalc\Grammar\StringCalcGrammar();

$numberOfCalculations = 100;
$calculations = [];

// TODO Implement the rest of this method (and remove the underscore from method name to re-enable this test)
return;

for ($i = 0; $i < $numberOfCalculations; $i++) {
$term = '';
$result = eval($term);

$calculation = [$term, $result];
$calculations[] = $calculation;
}

// Will call the assertEquals method with the delta parameter set which means assertEquals
// will report an error if result and expected result are not within $delta of each other
$this->doCalculations($calculations, self::RESULT_DELTA);
}

}

0 comments on commit 9a7f221

Please sign in to comment.