mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
Update testsuite
This commit is contained in:
parent
567f7c3536
commit
2c68d1c09e
@ -2,8 +2,8 @@
|
||||
|
||||
class Unit_NodeTraverserTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
function testTraverse() {
|
||||
$node = array(
|
||||
function getTestNode() {
|
||||
return array(
|
||||
new PHPParser_Node_Stmt_Namespace(array(
|
||||
'name' => new PHPParser_Node_Name(array(
|
||||
'parts' => array('Foo', 'Bar')
|
||||
@ -24,49 +24,116 @@ class Unit_NodeTraverserTest extends PHPUnit_Framework_TestCase
|
||||
'isBinary' => false,
|
||||
'type' => PHPParser_Node_Scalar_String::SINGLE_QUOTED
|
||||
))
|
||||
))
|
||||
)),
|
||||
)
|
||||
)),
|
||||
);
|
||||
|
||||
// on enter
|
||||
$this->visitedNodes = array();
|
||||
$nodeTraverser = new PHPParser_NodeTraverser;
|
||||
$nodeTraverser->addVisitor(array($this, 'visitNode'), PHPParser_NodeTraverser::ON_ENTER);
|
||||
$nodeTraverser->traverse($node);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'Stmt_Namespace',
|
||||
'Name',
|
||||
'Stmt_Echo',
|
||||
'Scalar_String',
|
||||
'Expr_Print',
|
||||
'Scalar_String',
|
||||
),
|
||||
$this->visitedNodes
|
||||
);
|
||||
|
||||
// on leave
|
||||
$this->visitedNodes = array();
|
||||
$nodeTraverser = new PHPParser_NodeTraverser;
|
||||
$nodeTraverser->addVisitor(array($this, 'visitNode'), PHPParser_NodeTraverser::ON_LEAVE);
|
||||
$nodeTraverser->traverse($node);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'Name',
|
||||
'Scalar_String',
|
||||
'Stmt_Echo',
|
||||
'Scalar_String',
|
||||
'Expr_Print',
|
||||
'Stmt_Namespace',
|
||||
),
|
||||
$this->visitedNodes
|
||||
);
|
||||
}
|
||||
|
||||
private $visitedNodes;
|
||||
function testTraverse() {
|
||||
$node = $this->getTestNode();
|
||||
|
||||
public function visitNode(PHPParser_NodeAbstract $node) {
|
||||
$this->visitedNodes[] = $node->getType();
|
||||
$visitor = new Unit_NodeVisitor;
|
||||
$traverser = new PHPParser_NodeTraverser;
|
||||
|
||||
$traverser->addVisitor($visitor);
|
||||
$traverser->traverse($node);
|
||||
|
||||
$this->assertEquals($node, $visitor->beforeTraverseNode);
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'Stmt_Namespace',
|
||||
'Name',
|
||||
'Stmt_Echo',
|
||||
'Scalar_String',
|
||||
'Expr_Print',
|
||||
'Scalar_String',
|
||||
),
|
||||
$visitor->enteredNodes
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'Name',
|
||||
'Scalar_String',
|
||||
'Stmt_Echo',
|
||||
'Scalar_String',
|
||||
'Expr_Print',
|
||||
'Stmt_Namespace',
|
||||
),
|
||||
$visitor->leftNodes
|
||||
);
|
||||
|
||||
$this->assertEquals($node, $visitor->afterTraverseNode);
|
||||
}
|
||||
|
||||
function testModifyingTraverse() {
|
||||
$node = $this->getTestNode();
|
||||
|
||||
$visitor = new Unit_ModifyingNodeVisitor;
|
||||
$traverser = new PHPParser_NodeTraverser;
|
||||
|
||||
$traverser->addVisitor($visitor);
|
||||
$traverser->traverse($node);
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
new PHPParser_Node_Stmt_Echo(array(
|
||||
'exprs' => array(
|
||||
new PHPParser_Node_Scalar_String(array(
|
||||
'value' => 'Foo Bar',
|
||||
'isBinary' => false,
|
||||
'type' => PHPParser_Node_Scalar_String::SINGLE_QUOTED
|
||||
))
|
||||
)
|
||||
)),
|
||||
),
|
||||
$node
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Unit_NodeVisitor extends PHPParser_NodeVisitorAbstract
|
||||
{
|
||||
public $beforeTraverseNode;
|
||||
public $enteredNodes;
|
||||
public $leftNodes;
|
||||
public $afterTraverseNode;
|
||||
|
||||
public function __construct() {
|
||||
$this->enteredNodes = $this->leftNodes = array();
|
||||
}
|
||||
|
||||
public function beforeTraverse(&$node) {
|
||||
$this->beforeTraverseNode = $node;
|
||||
}
|
||||
|
||||
public function enterNode(PHPParser_NodeAbstract &$node) {
|
||||
$this->enteredNodes[] = $node->getType();
|
||||
}
|
||||
|
||||
public function leaveNode(PHPParser_NodeAbstract &$node) {
|
||||
$this->leftNodes[] = $node->getType();
|
||||
}
|
||||
|
||||
public function afterTraverse(&$node) {
|
||||
$this->afterTraverseNode = $node;
|
||||
}
|
||||
}
|
||||
|
||||
class Unit_ModifyingNodeVisitor extends PHPParser_NodeVisitorAbstract
|
||||
{
|
||||
public function leaveNode(PHPParser_NodeAbstract &$node) {
|
||||
// delete namespace nodes by merging them
|
||||
if ($node instanceof PHPParser_Node_Stmt_Namespace) {
|
||||
return $node->stmts;
|
||||
// remove print nodes completely
|
||||
} elseif ($node instanceof PHPParser_Node_Expr_Print) {
|
||||
return false;
|
||||
// change string contents to 'Foo Bar'
|
||||
} elseif ($node instanceof PHPParser_Node_Scalar_String) {
|
||||
$node->value = 'Foo Bar';
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user