• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/35_include

  1 The `include()` function executes the specified path as ucode script,
  2 optionally setting a different execution scope for the invoked file.
  3 
  4 If the specified path is relative, it is treated as being relative to the
  5 source file currently being executed or the current working directory in
  6 case the interpreter executes code from stdin or a command line argument.
  7 
  8 Throws an exception if the given path value is not a string.
  9 
 10 Throws an exception if a scope argument is specified and not a valid object.
 11 
 12 Throws an exception if the given path could not be found or opened.
 13 
 14 Throws an exception if the given file could not be compiled.
 15 
 16 Returns no value.
 17 
 18 -- Testcase --
 19 {%
 20         let real_printf = printf;
 21 
 22         // include by relative path
 23         include("files/include.uc");
 24 
 25         printf("---\n");
 26 
 27         // include by absolute path
 28         include(TESTFILES_PATH + "/include.uc");
 29 
 30         printf("---\n");
 31 
 32         // include with overridden scope
 33         include("files/include.uc", {
 34                 printf: function(...args) {
 35                         real_printf("This is the wrapped printf() getting called!\n");
 36 
 37                         return real_printf(...args);
 38                 }
 39         });
 40 
 41         printf("---\n");
 42 
 43         // include with isolated scope
 44         include("files/include.uc", proto({
 45                 printf: function(...args) {
 46                         real_printf("This is the wrapped printf() getting called!\n");
 47 
 48                         return real_printf(...args);
 49                 }
 50         }, {}));
 51 %}
 52 -- End --
 53 
 54 -- File include.uc --
 55 {%
 56         printf("This is the include file running! Can I access the global env? %s\n",
 57                 REQUIRE_SEARCH_PATH ? "Yes!" : "No.");
 58 %}
 59 -- End --
 60 
 61 -- Expect stdout --
 62 This is the include file running! Can I access the global env? Yes!
 63 ---
 64 This is the include file running! Can I access the global env? Yes!
 65 ---
 66 This is the wrapped printf() getting called!
 67 This is the include file running! Can I access the global env? Yes!
 68 ---
 69 This is the wrapped printf() getting called!
 70 This is the include file running! Can I access the global env? No.
 71 -- End --
 72 
 73 
 74 An invalid path value triggers an exception.
 75 
 76 -- Testcase --
 77 {%
 78         include(true);
 79 %}
 80 -- End --
 81 
 82 -- Expect stderr --
 83 Type error: Passed filename is not a string
 84 In line 2, byte 14:
 85 
 86  `    include(true);`
 87   Near here ------^
 88 
 89 
 90 -- End --
 91 
 92 
 93 An invalid scope value triggers an exception.
 94 
 95 -- Testcase --
 96 {%
 97         include("test", true);
 98 %}
 99 -- End --
100 
101 -- Expect stderr --
102 Type error: Passed scope value is not an object
103 In line 2, byte 22:
104 
105  `    include("test", true);`
106   Near here --------------^
107 
108 
109 -- End --
110 
111 
112 A not found file triggers an exception.
113 
114 -- Testcase --
115 {%
116         include("files/doesnotexist.uc");
117 %}
118 -- End --
119 
120 -- Expect stderr --
121 Runtime error: Include file not found
122 In line 2, byte 33:
123 
124  `    include("files/doesnotexist.uc");`
125   Near here -------------------------^
126 
127 
128 -- End --
129 
130 
131 A compilation error in the file triggers an exception.
132 
133 -- Testcase --
134 {%
135         include("files/broken.uc");
136 %}
137 -- End --
138 
139 -- File broken.uc --
140 {%
141         // Unclosed object to force syntax error
142         return {
143 %}
144 -- End --
145 
146 -- Expect stderr --
147 Runtime error: Unable to compile source file './files/broken.uc':
148 
149   | Syntax error: Expecting label
150   | In line 4, byte 1:
151   |
152   |  `    return {`
153   |   Near here --^
154 
155 In line 2, byte 27:
156 
157  `    include("files/broken.uc");`
158   Near here -------------------^
159 
160 
161 -- End --
162 
163 
164 Ensure that included files inherit the parse mode of their calling file.
165 
166 -- Testcase --
167 {% include("files/inctest.uc"); %}
168 -- End --
169 
170 -- File inctest.uc --
171 print("Test\n");
172 -- End --
173 
174 -- Expect stdout --
175 print("Test\n");
176 -- End --
177 
178 
179 -- Testcase --
180 include("files/inctest.uc");
181 -- End --
182 
183 -- Args --
184 -R
185 -- End --
186 
187 -- File inctest.uc --
188 print("Test\n");
189 -- End --
190 
191 -- Expect stdout --
192 Test
193 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt