Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
first public TDD in Kaunas!!!
<?php
class Test extends PHPUnit_Framework_TestCase {
/**
* @var Calc
*/
private $calc;
protected function setUp() {
$this->calc = new Calc();
}
public function test_sums_2_numbers()
{
$this->assertSame(2, $this->calc->calculate('1 + 1'));
}
public function test_makes_minus_on_2_numbers()
{
$this->assertSame(1, $this->calc->calculate('2 - 1'));
}
public function test_calc_can_multiply()
{
$this->assertSame(6, $this->calc->calculate('2 * 3'));
}
public function test_multiple_operands()
{
$this->assertSame(2, $this->calc->calculate('2 + 2 - 2'));
}
public function test_operator_position()
{
$this->assertSame(6, $this->calc->calculate('2 + 2 * 2'));
}
}
class Calc
{
public function calculate($string)
{
$elements = explode(' ', $string);
return $this->parseArray($elements);
}
protected function parseArray($elements)
{
if (count($elements) == 1) {
return array_pop($elements);
}
$left = array_shift($elements);
$operand = array_shift($elements);
switch($operand) {
case '+':
return $left + $this->parseArray($elements);
case '-':
return $left - $this->parseArray($elements);
case '*':
return $left * $this->parseArray($elements);
}
}
}
@tygas

This comment has been minimized.

Show comment Hide comment
@tygas

tygas Dec 18, 2013

Ačiū už pamoką

tygas commented Dec 18, 2013

Ačiū už pamoką

@darles

This comment has been minimized.

Show comment Hide comment
@vzailskas

This comment has been minimized.

Show comment Hide comment
@dominykas

This comment has been minimized.

Show comment Hide comment
@okaminu

This comment has been minimized.

Show comment Hide comment
@acirtautas

This comment has been minimized.

Show comment Hide comment
@acirtautas

acirtautas Dec 19, 2013

EVAL EVERYTHING !!!

EVAL EVERYTHING !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment