1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +01:00

Add test coverage for Node_Name

And fix bugs found while doing so.
This commit is contained in:
nikic 2011-09-28 18:11:28 +02:00
parent 92f793001b
commit 62183807ee
2 changed files with 130 additions and 7 deletions

View File

@ -124,7 +124,7 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
* @param string|array|self $name Name to append
*/
public function append($name) {
$this->parts = array_merge($this->prepareName($name), $name->parts);
$this->parts = array_merge($this->parts, $this->prepareName($name));
}
/**
@ -133,7 +133,7 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
* @param string|array|self $name The name to set the first part to
*/
public function setFirst($name) {
$this->parts = array_merge($this->prepareName($name), array_slice($this->parts, 1));
array_splice($this->parts, 0, 1, $this->prepareName($name));
}
/**
@ -142,7 +142,7 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
* @param string|array|self $name The name to set the last part to
*/
public function setLast($name) {
$this->parts = array_merge($this->prepareName($name), array_slice($this->parts, 0, -1));
array_splice($this->parts, -1, 1, $this->prepareName($name));
}
/**
@ -156,12 +156,14 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
protected function prepareName($name) {
if (is_string($name)) {
return explode('\\', $name);
} elseif (is_array($name)) {
return $name;
} elseif ($name instanceof self) {
return $name->parts;
} elseif (!is_array($name)) {
throw new InvalidArgumentException(
'When changing a name you need to pass either a string, an array or a Name node'
);
}
throw new InvalidArgumentException(
'When changing a name you need to pass either a string, an array or a Name node'
);
}
}

View File

@ -0,0 +1,121 @@
<?php
class PHPParser_Tests_Node_NameTest extends PHPUnit_Framework_TestCase
{
public function testConstruct() {
$name = new PHPParser_Node_Name(array('foo', 'bar'));
$this->assertEquals(array('foo', 'bar'), $name->parts);
$name = new PHPParser_Node_Name('foo\bar');
$this->assertEquals(array('foo', 'bar'), $name->parts);
}
public function testGet() {
$name = new PHPParser_Node_Name('foo');
$this->assertEquals('foo', $name->getFirst());
$this->assertEquals('foo', $name->getLast());
$name = new PHPParser_Node_Name('foo\bar');
$this->assertEquals('foo', $name->getFirst());
$this->assertEquals('bar', $name->getLast());
}
public function testToString() {
$name = new PHPParser_Node_Name('foo\bar');
$this->assertEquals('foo\bar', (string) $name);
$this->assertEquals('foo\bar', $name->toString());
$this->assertEquals('foo_bar', $name->toString('_'));
}
public function testSet() {
$name = new PHPParser_Node_Name('foo');
$name->set('foo\bar');
$this->assertEquals('foo\bar', $name->toString());
$name->set(array('foo', 'bar'));
$this->assertEquals('foo\bar', $name->toString());
$name->set(new PHPParser_Node_Name('foo\bar'));
$this->assertEquals('foo\bar', $name->toString());
}
public function testSetFirst() {
$name = new PHPParser_Node_Name('foo');
$name->setFirst('bar');
$this->assertEquals('bar', $name->toString());
$name->setFirst('A\B');
$this->assertEquals('A\B', $name->toString());
$name->setFirst('C');
$this->assertEquals('C\B', $name->toString());
$name->setFirst('D\E');
$this->assertEquals('D\E\B', $name->toString());
}
public function testSetLast() {
$name = new PHPParser_Node_Name('foo');
$name->setLast('bar');
$this->assertEquals('bar', $name->toString());
$name->setLast('A\B');
$this->assertEquals('A\B', $name->toString());
$name->setLast('C');
$this->assertEquals('A\C', $name->toString());
$name->setLast('D\E');
$this->assertEquals('A\D\E', $name->toString());
}
public function testAppend() {
$name = new PHPParser_Node_Name('foo');
$name->append('bar');
$this->assertEquals('foo\bar', $name->toString());
$name->append('bar\foo');
$this->assertEquals('foo\bar\bar\foo', $name->toString());
}
public function testPrepend() {
$name = new PHPParser_Node_Name('foo');
$name->prepend('bar');
$this->assertEquals('bar\foo', $name->toString());
$name->prepend('foo\bar');
$this->assertEquals('foo\bar\bar\foo', $name->toString());
}
public function testIs() {
$name = new PHPParser_Node_Name('foo');
$this->assertTrue ($name->isUnqualified());
$this->assertFalse($name->isQualified());
$this->assertFalse($name->isFullyQualified());
$this->assertFalse($name->isRelative());
$name = new PHPParser_Node_Name('foo\bar');
$this->assertFalse($name->isUnqualified());
$this->assertTrue ($name->isQualified());
$this->assertFalse($name->isFullyQualified());
$this->assertFalse($name->isRelative());
$name = new PHPParser_Node_Name_FullyQualified('foo');
$this->assertFalse($name->isUnqualified());
$this->assertFalse($name->isQualified());
$this->assertTrue ($name->isFullyQualified());
$this->assertFalse($name->isRelative());
$name = new PHPParser_Node_Name_Relative('foo');
$this->assertFalse($name->isUnqualified());
$this->assertFalse($name->isQualified());
$this->assertFalse($name->isFullyQualified());
$this->assertTrue ($name->isRelative());
}
}