Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cordoval committed May 6, 2012
1 parent 19c873a commit 2c4ca30
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 13 deletions.
18 changes: 14 additions & 4 deletions src/ComputerBuild/Vhdl/Assign.php
Expand Up @@ -2,19 +2,29 @@

namespace ComputerBuild\Vhdl;

use ComputerBuild\Vhdl\Symbol;

/**
* Assigns
*
* @author Luis Cordova <cordoval@gmail.com>
*/
class Assign extends InlineStatement
{
protected $args;

public function __construct($args = null)
/**
* @param array $args | size should be 2 or 4
*/
public function __construct(array $args)
{
if (strlen($args) == 2) {
if (sizeof($args) == 2) {
$this->target = $args[0];
$this->expression = $args[1];
} else {
$this->target = $args[0]."(".$args[1].")";
$this->expression = $args[2]."(".$args[3].")";
$this->expression .= $this->expression->toSymbol();
$this->expression = $args[2]."(\"".$args[3]."\")";
$this->expression = new Symbol($this->expression);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ComputerBuild/Vhdl/SingleLineStatement.php
Expand Up @@ -7,7 +7,7 @@
*
* @author Luis Cordova <cordoval@gmail.com>
*/
class SingleLineStatement extends Statement
abstract class SingleLineStatement extends Statement
{
public function generate($out, $indent)
{
Expand Down
23 changes: 23 additions & 0 deletions src/ComputerBuild/Vhdl/Symbol.php
@@ -0,0 +1,23 @@
<?php

namespace ComputerBuild\Vhdl;

/**
* Symbol
*
* @author Luis Cordova <cordoval@gmail.com>
*/
class Symbol
{
protected $expression;

public function __construct($expression)
{
$this->expression = $expression;
}

public function __toString()
{
return $this->expression;
}
}
16 changes: 8 additions & 8 deletions src/ComputerBuild/Vhdl/Type.php
Expand Up @@ -2,25 +2,25 @@

namespace ComputerBuild\Vhdl;

/**
* Class to render single line statement
*
* @author Luis Cordova <cordoval@gmail.com>
* @author Raul Rodriguez <raulrodriguez782@gmail.com>
*/
class Type extends SingleLineStatement
{
protected $name;
protected $values;

public function __construct($name, $values)
public function __construct($name, array $values)
{
$this->name = $name;
$this->values = $values;
}

public function line()
{
$valuesSeparatedWithComas = "";
foreach ($this->values as $key => $value) {
$separator = $last ? ", " : "";
$valuesSeparatedWithComas .= $this->values[$key].$separator;
}

return "TYPE ".$this->name." IS ( ".$valuesSeparatedWithComas." );";
return "TYPE ".$this->name." IS (".implode(', ', $this->values).");";
}
}
25 changes: 25 additions & 0 deletions src/Tests/ComputerBuild/Vhdl/AssignTest.php
@@ -0,0 +1,25 @@
<?php

namespace Tests\ComputerBuild\Vhdl;

use ComputerBuild\Vhdl\Assign;

class AssignTest extends \PHPUnit_Framework_TestCase
{
public function testAssignTwoArgumentsShouldAssignFirstTargetSecondExpression()
{
// Assign(arg0, arg1)
// arg0 <= std_logic_vector("arg1")
$assignStatement = new Assign(array('output', '0101001'));
$this->assertEquals("output <= \"0101001\"", $assignStatement->generate());
}

public function testAssignFourArgumentsShouldSplitTwoAssignments()
{
// Assign(arg0, arg1, arg2, arg3)
// output(7 downto 0) <= std_logic_vector("00010001")
$assignStatement = new Assign(array('output', '7 downto 0', 'std_logic_vector', '00010001'));
$this->assertEquals("output(7 downto 0) <= std_logic_vector(\"00010001\")", $assignStatement->generate());
}

}
18 changes: 18 additions & 0 deletions src/Tests/ComputerBuild/Vhdl/TypeTest.php
@@ -0,0 +1,18 @@
<?php

namespace Tests\ComputerBuild\Vhdl;

use ComputerBuild\Vhdl\Type;

/**
* @author Luis Cordova <cordoval@gmail.com>
* @author Raul Rodriguez <raulrodriguez782@gmail.com>
*/
class TypeTest extends \PHPUnit_Framework_TestCase
{
public function testTypeStatement()
{
$typeStatement = new Type('type_enum', array('a', 'b', 'c'));
$this->assertEquals('TYPE type_enum IS (a, b, c);', $typeStatement->line());
}
}

0 comments on commit 2c4ca30

Please sign in to comment.