Skip to content

Commit

Permalink
Refactoring after foreign branch merging
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskonnertz committed Apr 5, 2018
1 parent 9a7f221 commit 1951313
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
9 changes: 4 additions & 5 deletions src/ChrisKonnertz/StringCalc/StringCalc.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class StringCalc
*
* @const string
*/
const VERSION = '1.0.9';
const VERSION = '1.0.10';

/**
* Closure that is called at the end of the grammar checking
Expand Down Expand Up @@ -57,8 +57,8 @@ class StringCalc
*
* @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
* @throws Exceptions\ContainerException If the passed container parameter is not an object or
* if the passed container parameter does not implement ContainerInterface
*/
public function __construct($container = null)
{
Expand Down Expand Up @@ -88,7 +88,6 @@ 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
Expand Down Expand Up @@ -116,7 +115,6 @@ public function calculate($term)
* Tokenize the term. Returns an array with the tokens.
*
* @param string $term
*
* @return array
* @throws Exceptions\ContainerException
* @throws Exceptions\NotFoundException
Expand Down Expand Up @@ -177,6 +175,7 @@ public function addSymbol(AbstractSymbol $symbol, $replaceSymbol = null)

/**
* Setter for the custom grammar checker property.
*
* @see Parser::setCustomGrammarChecker()
*
* @param \Closure $customGrammarChecker
Expand Down
44 changes: 17 additions & 27 deletions tests/StringCalcTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

// 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;

// Ensure backward compatibility
// @see http://stackoverflow.com/questions/42811164/class-phpunit-framework-testcase-not-found#answer-42828632
if (!class_exists('\PHPUnit\Framework\TestCase')) {
class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
}
Expand All @@ -16,12 +16,20 @@ class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
*/
class TestFunction extends AbstractFunction
{

/**
* @inheritdoc
*/
protected $identifiers = ['test'];

/**
* @inheritdoc
*/
public function execute(array $arguments)
{
return 2;
}

}

/**
Expand Down Expand Up @@ -75,7 +83,7 @@ public function testParseAndTraverse()

$node->traverse(
function ($node, $level) {
// do nothing
// Do nothing
}
);
}
Expand All @@ -101,12 +109,18 @@ public function testGetContainer()
public function testAddSymbol()
{
$stringCalc = $this->getInstance();

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

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

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

$stringCalc->addSymbol($symbol, $replaceSymbol);
}

public function testCalculations()
Expand Down Expand Up @@ -269,28 +283,4 @@ 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 1951313

Please sign in to comment.