|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Feature::Dedent |
| 5 | + */ |
| 6 | + |
| 7 | +use Tester\Assert; |
| 8 | + |
| 9 | +require __DIR__ . '/../bootstrap.php'; |
| 10 | + |
| 11 | + |
| 12 | +function dedent(string $template, array $params = []): string |
| 13 | +{ |
| 14 | + $latte = createLatte(); |
| 15 | + $latte->setFeature(Latte\Feature::Dedent); |
| 16 | + return $latte->renderToString($template, $params); |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +test('feature disabled by default', function () { |
| 21 | + $latte = createLatte(); |
| 22 | + Assert::same(" Hello\n", $latte->renderToString("{if true}\n Hello\n{/if}")); |
| 23 | +}); |
| 24 | + |
| 25 | + |
| 26 | +test('basic dedent with spaces', function () { |
| 27 | + Assert::same("Hello\n", dedent("{if true}\n Hello\n{/if}")); |
| 28 | +}); |
| 29 | + |
| 30 | + |
| 31 | +test('basic dedent with tab', function () { |
| 32 | + Assert::same("Hello\n", dedent("{if true}\n\tHello\n{/if}")); |
| 33 | +}); |
| 34 | + |
| 35 | + |
| 36 | +test('multiple lines', function () { |
| 37 | + Assert::same("Hello\nWorld\n", dedent("{if true}\n\tHello\n\tWorld\n{/if}")); |
| 38 | +}); |
| 39 | + |
| 40 | + |
| 41 | +test('deeper indentation preserved', function () { |
| 42 | + Assert::same("Hello\n\tIndented\n", dedent("{if true}\n\tHello\n\t\tIndented\n{/if}")); |
| 43 | +}); |
| 44 | + |
| 45 | + |
| 46 | +test('nested tags', function () { |
| 47 | + Assert::same("Hello\n", dedent("{if true}\n\t{if true}\n\t\tHello\n\t{/if}\n{/if}")); |
| 48 | +}); |
| 49 | + |
| 50 | + |
| 51 | +test('nested tags with expression', function () { |
| 52 | + $result = dedent("{if true}\n\t{if true}\n\t\t{=\$x}\n\t{/if}\n{/if}", ['x' => 'val']); |
| 53 | + Assert::same("val\n", $result); |
| 54 | +}); |
| 55 | + |
| 56 | + |
| 57 | +test('if/else branches dedented independently', function () { |
| 58 | + Assert::same("A\n", dedent("{if true}\n\tA\n{else}\n\tB\n{/if}")); |
| 59 | + Assert::same("B\n", dedent("{if false}\n\tA\n{else}\n\tB\n{/if}")); |
| 60 | +}); |
| 61 | + |
| 62 | + |
| 63 | +test('foreach', function () { |
| 64 | + $result = dedent("{foreach \$items as \$item}\n\t{\$item}\n{/foreach}", ['items' => ['a', 'b']]); |
| 65 | + Assert::same("a\nb\n", $result); |
| 66 | +}); |
| 67 | + |
| 68 | + |
| 69 | +test('no indentation - no change', function () { |
| 70 | + Assert::same("Hello\n", dedent("{if true}\nHello\n{/if}")); |
| 71 | +}); |
| 72 | + |
| 73 | + |
| 74 | +test('expression on indented line', function () { |
| 75 | + $result = dedent("{if true}\n\t{=\$x}\n{/if}", ['x' => 'val']); |
| 76 | + Assert::same("val\n", $result); |
| 77 | +}); |
| 78 | + |
| 79 | + |
| 80 | +test('mixed text and expression on same line', function () { |
| 81 | + $result = dedent("{if true}\n\tHello {=\$x} World\n{/if}", ['x' => 'dear']); |
| 82 | + Assert::same("Hello dear World\n", $result); |
| 83 | +}); |
| 84 | + |
| 85 | + |
| 86 | +test('block tag', function () { |
| 87 | + Assert::same("Hello\n", dedent("{block test}\n\tHello\n{/block}")); |
| 88 | +}); |
| 89 | + |
| 90 | + |
| 91 | +test('capture tag', function () { |
| 92 | + Assert::same("Hello\n", dedent("{capture \$var}\n\tHello\n{/capture}{\$var}")); |
| 93 | +}); |
| 94 | + |
| 95 | + |
| 96 | +test('inconsistent indentation throws exception', function () { |
| 97 | + Assert::exception( |
| 98 | + fn() => dedent("{if true}\n\tHello\nWorld\n{/if}"), |
| 99 | + Latte\CompileException::class, |
| 100 | + 'Inconsistent indentation%a%', |
| 101 | + ); |
| 102 | +}); |
| 103 | + |
| 104 | + |
| 105 | +test('inconsistent indentation reports line number', function () { |
| 106 | + try { |
| 107 | + dedent("{if true}\n\tHello\nWorld\n{/if}"); |
| 108 | + } catch (Latte\CompileException $e) { |
| 109 | + Assert::same(3, $e->position->line); |
| 110 | + return; |
| 111 | + } |
| 112 | + Assert::fail('Exception expected'); |
| 113 | +}); |
0 commit comments