1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
* - (c) John MacFarlane
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Tests\Unit\Node;
use PHPUnit\Framework\TestCase;
final class NodeTest extends TestCase
{
public function testInsertBeforeElementWhichDoesNotHaveAPreviousOne(): void
{
$root = new SimpleNode();
$root->appendChild($targetNode = new SimpleNode());
$newNode = new SimpleNode();
$targetNode->insertBefore($newNode);
$this->assertNull($newNode->previous());
$this->assertSame($targetNode, $newNode->next());
$this->assertSame($newNode, $targetNode->previous());
}
public function testInsertBeforeElementWhichAlreadyHasPrevious(): void
{
$root = new SimpleNode();
$root->appendChild($firstNode = new SimpleNode());
$root->appendChild($targetNode = new SimpleNode());
$newNode = new SimpleNode();
$targetNode->insertBefore($newNode);
$this->assertSame($firstNode, $newNode->previous());
$this->assertSame($targetNode, $newNode->next());
$this->assertSame($newNode, $targetNode->previous());
}
public function testPrependChildToChildlessParent(): void
{
$root = new SimpleNode();
$root->prependChild($newNode = new SimpleNode());
$this->assertSame($newNode, $root->firstChild());
$this->assertSame($root, $newNode->parent());
$this->assertNull($newNode->previous());
}
public function testPrependChildToParentWhichAlreadyHasChildren(): void
{
$root = new SimpleNode();
$root->prependChild($existingChild = new SimpleNode());
$this->assertSame($existingChild, $root->firstChild());
$newNode = new SimpleNode();
$root->prependChild($newNode);
$this->assertCount(2, $root->children());
$this->assertSame($newNode, $root->firstChild());
$this->assertSame($root, $newNode->parent());
$this->assertNull($newNode->previous());
$this->assertSame($existingChild, $newNode->next());
$this->assertSame($newNode, $existingChild->previous());
}
public function testDetachChildren(): void
{
$root = new SimpleNode();
$root->appendChild($child1 = new SimpleNode());
$root->appendChild($child2 = new SimpleNode());
$root->detachChildren();
$this->assertCount(0, $root->children());
$this->assertNull($root->firstChild());
$this->assertNull($root->lastChild());
$this->assertNull($child1->parent());
$this->assertNull($child2->parent());
$this->assertSame($child2, $child1->next());
$this->assertSame($child1, $child2->previous());
}
public function testReplaceChildren(): void
{
$root = new SimpleNode();
$root->appendChild($oldChild = new SimpleNode());
$newChildren = [
$newChild1 = new SimpleNode(),
$newChild2 = new SimpleNode(),
];
$root->replaceChildren($newChildren);
$this->assertCount(2, $root->children());
$this->assertSame($newChild1, $root->firstChild());
$this->assertSame($newChild2, $root->lastChild());
$this->assertSame($root, $newChild1->parent());
$this->assertSame($root, $newChild2->parent());
$this->assertNull($oldChild->parent());
}
public function testInsertAfterWithParent(): void
{
$root = new SimpleNode();
$root->appendChild($child1 = new SimpleNode());
$root->appendChild($child2 = new SimpleNode());
$otherRoot = new SimpleNode();
$otherRoot->appendChild($child3 = new SimpleNode());
$otherRoot->appendChild($child4 = new SimpleNode());
$child1->insertAfter($child3);
$this->assertCount(3, $root->children());
$this->assertCount(1, $otherRoot->children());
$this->assertSame($child1, $root->firstChild());
$this->assertSame($child2, $root->lastChild());
$this->assertSame($root, $child2->parent());
}
public function testInsertAfterWithoutParent(): void
{
$node1 = new SimpleNode();
$node2 = new SimpleNode();
$node1->insertAfter($node2);
$this->assertSame($node2, $node1->next());
$this->assertSame($node1, $node2->previous());
}
public function testInsertBeforeWithParent(): void
{
$root = new SimpleNode();
$root->appendChild($child1 = new SimpleNode());
$root->appendChild($child2 = new SimpleNode());
$root->appendChild($child3 = new SimpleNode());
$otherRoot = new SimpleNode();
$otherRoot->appendChild($child4 = new SimpleNode());
$otherRoot->appendChild($child5 = new SimpleNode());
$child2->insertBefore($child4);
$this->assertCount(4, $root->children());
$this->assertCount(1, $otherRoot->children());
$this->assertSame($child1, $root->children()[0]);
$this->assertSame($child4, $root->children()[1]);
$this->assertSame($child2, $root->children()[2]);
$this->assertSame($child3, $root->children()[3]);
$this->assertSame($root, $child4->parent());
}
public function testInsertBeforeWithoutParent(): void
{
$node1 = new SimpleNode();
$node2 = new SimpleNode();
$node1->insertBefore($node2);
$this->assertSame($node1, $node2->next());
$this->assertSame($node2, $node1->previous());
}
public function testClone(): void
{
// Build our intial AST
$root = new SimpleNode();
$root->appendChild($child1 = new SimpleNode());
$root->appendChild($child2 = new SimpleNode());
$child1->appendChild($grandChild1 = new SimpleNode());
$child1->appendChild($grandChild2 = new SimpleNode());
$grandChild2->appendChild($greatGrandChild1 = new SimpleNode());
// Set values on each node to indicate they are originals
$walker = $root->walker();
while ($event = $walker->next()) {
$event->getNode()->value = 'original';
}
// Clone one of the children
$cloneOfChild1 = clone $child1;
// Set values throught the cloned node to indicate they are clones
$walker = $cloneOfChild1->walker();
while ($event = $walker->next()) {
$event->getNode()->value = 'cloned';
}
// Now check the original to ensure nothing changed there
$walker = $root->walker();
while ($event = $walker->next()) {
$this->assertSame('original', $event->getNode()->value);
}
}
}
|