|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Latte\Compiler\Nodes\StatementNode; |
| 6 | +use Latte\Essential\Nodes\IfNode; |
| 7 | +use Latte\Essential\Nodes\ForeachNode; |
| 8 | +use Tester\Assert; |
| 9 | + |
| 10 | +require __DIR__ . '/../bootstrap.php'; |
| 11 | + |
| 12 | + |
| 13 | +test('simple tag has one tagPosition', function () { |
| 14 | + $engine = new Latte\Engine; |
| 15 | + $ast = $engine->parse('{=$var}'); |
| 16 | + |
| 17 | + $node = $ast->main->children[0]; |
| 18 | + Assert::type(Latte\Compiler\Nodes\PrintNode::class, $node); |
| 19 | + Assert::count(1, $node->tagPositions); |
| 20 | + Assert::same(0, $node->tagPositions[0]->offset); |
| 21 | + Assert::same(7, $node->tagPositions[0]->length); |
| 22 | +}); |
| 23 | + |
| 24 | + |
| 25 | +test('paired tag has two tagPositions', function () { |
| 26 | + $engine = new Latte\Engine; |
| 27 | + $ast = $engine->parse('{if $cond}text{/if}'); |
| 28 | + |
| 29 | + $node = $ast->main->children[0]; |
| 30 | + Assert::type(IfNode::class, $node); |
| 31 | + Assert::count(2, $node->tagPositions); |
| 32 | + |
| 33 | + // opening tag {if $cond} |
| 34 | + Assert::same(0, $node->tagPositions[0]->offset); |
| 35 | + Assert::same(10, $node->tagPositions[0]->length); |
| 36 | + |
| 37 | + // closing tag {/if} |
| 38 | + Assert::same(14, $node->tagPositions[1]->offset); |
| 39 | + Assert::same(5, $node->tagPositions[1]->length); |
| 40 | +}); |
| 41 | + |
| 42 | + |
| 43 | +test('if/else has three tagPositions', function () { |
| 44 | + $engine = new Latte\Engine; |
| 45 | + $ast = $engine->parse('{if $a}text{else}more{/if}'); |
| 46 | + |
| 47 | + $node = $ast->main->children[0]; |
| 48 | + Assert::type(IfNode::class, $node); |
| 49 | + Assert::count(3, $node->tagPositions); |
| 50 | + |
| 51 | + // {if $a} |
| 52 | + Assert::same(0, $node->tagPositions[0]->offset); |
| 53 | + Assert::same(7, $node->tagPositions[0]->length); |
| 54 | + |
| 55 | + // {else} |
| 56 | + Assert::same(11, $node->tagPositions[1]->offset); |
| 57 | + Assert::same(6, $node->tagPositions[1]->length); |
| 58 | + |
| 59 | + // {/if} |
| 60 | + Assert::same(21, $node->tagPositions[2]->offset); |
| 61 | + Assert::same(5, $node->tagPositions[2]->length); |
| 62 | +}); |
| 63 | + |
| 64 | + |
| 65 | +test('if/elseif/else has four tagPositions', function () { |
| 66 | + $engine = new Latte\Engine; |
| 67 | + $ast = $engine->parse('{if $a}A{elseif $b}B{else}C{/if}'); |
| 68 | + |
| 69 | + $node = $ast->main->children[0]; |
| 70 | + Assert::type(IfNode::class, $node); |
| 71 | + Assert::count(4, $node->tagPositions); |
| 72 | + |
| 73 | + // {if $a} |
| 74 | + Assert::same(0, $node->tagPositions[0]->offset); |
| 75 | + Assert::same(7, $node->tagPositions[0]->length); |
| 76 | + |
| 77 | + // {elseif $b} |
| 78 | + Assert::same(8, $node->tagPositions[1]->offset); |
| 79 | + Assert::same(11, $node->tagPositions[1]->length); |
| 80 | + |
| 81 | + // {else} |
| 82 | + Assert::same(20, $node->tagPositions[2]->offset); |
| 83 | + Assert::same(6, $node->tagPositions[2]->length); |
| 84 | + |
| 85 | + // {/if} |
| 86 | + Assert::same(27, $node->tagPositions[3]->offset); |
| 87 | + Assert::same(5, $node->tagPositions[3]->length); |
| 88 | +}); |
| 89 | + |
| 90 | + |
| 91 | +test('foreach has two tagPositions', function () { |
| 92 | + $engine = new Latte\Engine; |
| 93 | + $ast = $engine->parse('{foreach $items as $item}x{/foreach}'); |
| 94 | + |
| 95 | + $node = $ast->main->children[0]; |
| 96 | + Assert::type(ForeachNode::class, $node); |
| 97 | + Assert::count(2, $node->tagPositions); |
| 98 | + |
| 99 | + // {foreach $items as $item} |
| 100 | + Assert::same(0, $node->tagPositions[0]->offset); |
| 101 | + Assert::same(25, $node->tagPositions[0]->length); |
| 102 | + |
| 103 | + // {/foreach} |
| 104 | + Assert::same(26, $node->tagPositions[1]->offset); |
| 105 | + Assert::same(10, $node->tagPositions[1]->length); |
| 106 | +}); |
| 107 | + |
| 108 | + |
| 109 | +test('foreach/else has three tagPositions', function () { |
| 110 | + $engine = new Latte\Engine; |
| 111 | + $ast = $engine->parse('{foreach $items as $item}x{else}empty{/foreach}'); |
| 112 | + |
| 113 | + $node = $ast->main->children[0]; |
| 114 | + Assert::type(ForeachNode::class, $node); |
| 115 | + Assert::count(3, $node->tagPositions); |
| 116 | + |
| 117 | + // {foreach $items as $item} |
| 118 | + Assert::same(0, $node->tagPositions[0]->offset); |
| 119 | + Assert::same(25, $node->tagPositions[0]->length); |
| 120 | + |
| 121 | + // {else} |
| 122 | + Assert::same(26, $node->tagPositions[1]->offset); |
| 123 | + Assert::same(6, $node->tagPositions[1]->length); |
| 124 | + |
| 125 | + // {/foreach} |
| 126 | + Assert::same(37, $node->tagPositions[2]->offset); |
| 127 | + Assert::same(10, $node->tagPositions[2]->length); |
| 128 | +}); |
| 129 | + |
| 130 | + |
| 131 | +test('nested if tags have correct tagPositions', function () { |
| 132 | + $engine = new Latte\Engine; |
| 133 | + $ast = $engine->parse('{if $a}{if $b}x{/if}{/if}'); |
| 134 | + |
| 135 | + // Outer if |
| 136 | + $outer = $ast->main->children[0]; |
| 137 | + Assert::type(IfNode::class, $outer); |
| 138 | + Assert::count(2, $outer->tagPositions); |
| 139 | + Assert::same(0, $outer->tagPositions[0]->offset); // {if $a} |
| 140 | + Assert::same(20, $outer->tagPositions[1]->offset); // {/if} |
| 141 | + |
| 142 | + // Inner if |
| 143 | + $inner = $outer->then->children[0]; |
| 144 | + Assert::type(IfNode::class, $inner); |
| 145 | + Assert::count(2, $inner->tagPositions); |
| 146 | + Assert::same(7, $inner->tagPositions[0]->offset); // {if $b} |
| 147 | + Assert::same(15, $inner->tagPositions[1]->offset); // {/if} |
| 148 | +}); |
| 149 | + |
| 150 | + |
| 151 | +test('unpaired tag has one tagPosition', function () { |
| 152 | + $engine = new Latte\Engine; |
| 153 | + $ast = $engine->parse('{var $x = 1}'); |
| 154 | + |
| 155 | + // {var} goes to head, not main |
| 156 | + $node = $ast->head->children[0]; |
| 157 | + Assert::count(1, $node->tagPositions); |
| 158 | + Assert::same(0, $node->tagPositions[0]->offset); |
| 159 | + Assert::same(12, $node->tagPositions[0]->length); |
| 160 | +}); |
0 commit comments