summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/35_include
blob: 83c34bb56ac2082b77cf2ccb2f53a10af8a4f942 (plain)
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
The `include()` function executes the specified path as ucode script,
optionally setting a different execution scope for the invoked file.

If the specified path is relative, it is treated as being relative to the
source file currently being executed or the current working directory in
case the interpreter executes code from stdin or a command line argument.

Throws an exception if the given path value is not a string.

Throws an exception if a scope argument is specified and not a valid object.

Throws an exception if the given path could not be found or opened.

Throws an exception if the given file could not be compiled.

Returns no value.

-- Testcase --
{%
	let real_printf = printf;

	// include by relative path
	include("files/include.uc");

	printf("---\n");

	// include by absolute path
	include(TESTFILES_PATH + "/include.uc");

	printf("---\n");

	// include with overridden scope
	include("files/include.uc", {
		printf: function(...args) {
			real_printf("This is the wrapped printf() getting called!\n");

			return real_printf(...args);
		}
	});

	printf("---\n");

	// include with isolated scope
	include("files/include.uc", proto({
		printf: function(...args) {
			real_printf("This is the wrapped printf() getting called!\n");

			return real_printf(...args);
		}
	}, {}));
%}
-- End --

-- File include.uc --
{%
	printf("This is the include file running! Can I access the global env? %s\n",
		REQUIRE_SEARCH_PATH ? "Yes!" : "No.");
%}
-- End --

-- Expect stdout --
This is the include file running! Can I access the global env? Yes!
---
This is the include file running! Can I access the global env? Yes!
---
This is the wrapped printf() getting called!
This is the include file running! Can I access the global env? Yes!
---
This is the wrapped printf() getting called!
This is the include file running! Can I access the global env? No.
-- End --


An invalid path value triggers an exception.

-- Testcase --
{%
	include(true);
%}
-- End --

-- Expect stderr --
Type error: Passed filename is not a string
In line 2, byte 14:

 `    include(true);`
  Near here ------^


-- End --


An invalid scope value triggers an exception.

-- Testcase --
{%
	include("test", true);
%}
-- End --

-- Expect stderr --
Type error: Passed scope value is not an object
In line 2, byte 22:

 `    include("test", true);`
  Near here --------------^


-- End --


A not found file triggers an exception.

-- Testcase --
{%
	include("files/doesnotexist.uc");
%}
-- End --

-- Expect stderr --
Runtime error: Include file not found
In line 2, byte 33:

 `    include("files/doesnotexist.uc");`
  Near here -------------------------^


-- End --


A compilation error in the file triggers an exception.

-- Testcase --
{%
	include("files/broken.uc");
%}
-- End --

-- File broken.uc --
{%
	// Unclosed object to force syntax error
	return {
%}
-- End --

-- Expect stderr --
Runtime error: Unable to compile source file './files/broken.uc':

  | Syntax error: Expecting label
  | In line 3, byte 11:
  |
  |  `    return {`
  |   Near here --^

In line 2, byte 27:

 `    include("files/broken.uc");`
  Near here -------------------^


-- End --


Ensure that included files inherit the parse mode of their calling file.

-- Testcase --
{% include("files/inctest.uc"); %}
-- End --

-- File inctest.uc --
print("Test\n");
-- End --

-- Expect stdout --
print("Test\n");
-- End --


-- Testcase --
include("files/inctest.uc");
-- End --

-- Args --
-R
-- End --

-- File inctest.uc --
print("Test\n");
-- End --

-- Expect stdout --
Test
-- End --