1 module tests.statements.import_; 2 3 private 4 { 5 import tests.asserts; 6 } 7 8 9 // Base import 10 unittest 11 { 12 // Import all macros 13 assertRender( 14 `{% import './tests/files/test.dj' %}` ~ 15 `{{ test() }}` ~ 16 `{{ test1() }}` ~ 17 `{{ test2() }}` 18 , 19 "TEST" ~ 20 "TEST1" ~ 21 "TEST2" 22 ); 23 24 // Import specific macro 25 assertRender( 26 `{% from './tests/files/test.dj' import test %}` ~ 27 `{{ test() }}` 28 , 29 "TEST" 30 ); 31 32 // Import with renaming 33 assertRender( 34 `{% from './tests/files/test.dj' import test as renamed %}` ~ 35 `{{ renamed() }}` 36 , 37 "TEST" 38 ); 39 40 // Mixed imports 41 assertRender( 42 `{% from './tests/files/test.dj' import test1 as r1, test2 as r2, test %}` ~ 43 `{{ test() }}` ~ 44 `{{ r1() }}` ~ 45 `{{ r2() }}` 46 , 47 "TEST" ~ 48 "TEST1" ~ 49 "TEST2" 50 ); 51 } 52 53 // Exception cases 54 unittest 55 { 56 // Non-existing file 57 assertException( 58 `{% import './tests/files/notexisting.dj' %}` 59 ); 60 61 // Undefined `test3` 62 assertException( 63 `{% from './tests/files/test.dj' import test3 %}` 64 ); 65 66 // Undefined test 67 assertException( 68 `{% from './tests/files/test.dj' import test as renamed %}` ~ 69 `{{ test() }}` 70 ); 71 } 72 73 // Context behavior 74 unittest 75 { 76 // Import without context by default 77 assertRender( 78 `{% set a = 10 %}` ~ 79 `{% from './tests/files/context.dj' import print_a %}` ~ 80 `{{ print_a() }}` 81 , 82 "" 83 ); 84 85 // Import without context manually 86 assertRender( 87 `{% set a = 10 %}` ~ 88 `{% from './tests/files/context.dj' import print_a without context %}` ~ 89 `{{ print_a() }}` 90 , 91 "" 92 ); 93 94 // Import with context manually 95 assertRender( 96 `{% set a = 10 %}` ~ 97 `{% from './tests/files/context.dj' import print_a with context %}` ~ 98 `{{ print_a() }}` 99 , 100 "10" 101 ); 102 }