Debian Codegraph @

test/lua/unit/lupa.lua

raw
1 --[[
2 Copyright (c) 2026, Namecheap Inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ]]--
26
27 context("Lupa Jinja template engine unit tests", function()
28 local lupa = require("lupa")
29
30 -- Helper: expand template with optional env table (simulates env vars as strings)
31 local function expand(template, env)
32 return lupa.expand(template, env or {})
33 end
34
35 -- Helper: expand with env vars (all values are strings, like real RSPAMD_ env vars)
36 local function expand_env(template, vars)
37 return lupa.expand(template, { env = vars })
38 end
39
40 -- =========================================================================
41 -- VALIDATION FILTERS (with env var string inputs)
42 -- =========================================================================
43
44 -- mandatory
45 test("mandatory: passes non-empty string", function()
46 assert_equal(expand('{{ "test" | mandatory("fail") }}'), 'test')
47 end)
48
49 test("mandatory: works with env var", function()
50 assert_equal(expand_env('{{ env.KEY | mandatory("KEY required") }}', { KEY = "my_key" }), 'my_key')
51 end)
52
53 test("mandatory: crashes on empty string", function()
54 local ok, err = pcall(expand, '{{ "" | mandatory("VALUE_REQUIRED") }}')
55 assert_false(ok)
56 assert_match('VALUE_REQUIRED', err)
57 end)
58
59 test("mandatory: crashes on nil env var", function()
60 local ok, err = pcall(expand_env, '{{ env.MISSING | mandatory("MISSING_VAR") }}', {})
61 assert_false(ok)
62 assert_match('MISSING_VAR', err)
63 end)
64
65 -- require_int
66 test("require_int: valid integer string", function()
67 assert_equal(expand('{{ "42" | require_int }}'), '42')
68 end)
69
70 test("require_int: works with env var", function()
71 assert_equal(expand_env('{{ env.PORT | require_int }}', { PORT = "8080" }), '8080')
72 end)
73
74 test("require_int: zero is valid", function()
75 assert_equal(expand('{{ "0" | require_int }}'), '0')
76 end)
77
78 test("require_int: negative is valid", function()
79 assert_equal(expand('{{ "-1" | require_int }}'), '-1')
80 end)
81
82 test("require_int: crashes on float string", function()
83 local ok, err = pcall(expand, '{{ "3.14" | require_int("NOT_INT") }}')
84 assert_false(ok)
85 assert_match('NOT_INT', err)
86 end)
87
88 test("require_int: crashes on non-numeric string", function()
89 local ok, err = pcall(expand, '{{ "abc" | require_int("NOT_INT") }}')
90 assert_false(ok)
91 assert_match('NOT_INT', err)
92 end)
93
94 -- require_number
95 test("require_number: valid float string", function()
96 assert_equal(expand('{{ "3.14" | require_number }}'), '3.14')
97 end)
98
99 test("require_number: valid integer string", function()
100 assert_equal(expand('{{ "42" | require_number }}'), '42')
101 end)
102
103 test("require_number: works with env var", function()
104 assert_equal(expand_env('{{ env.THRESHOLD | require_number }}', { THRESHOLD = "8.0" }), '8.0')
105 end)
106
107 test("require_number: negative is valid", function()
108 assert_equal(expand('{{ "-4.5" | require_number }}'), '-4.5')
109 end)
110
111 test("require_number: crashes on non-numeric", function()
112 local ok, err = pcall(expand, '{{ "abc" | require_number("NOT_NUM") }}')
113 assert_false(ok)
114 assert_match('NOT_NUM', err)
115 end)
116
117 -- require_bool
118 test("require_bool: accepts all UCL boolean strings", function()
119 for _, v in ipairs({"true", "false", "yes", "no", "on", "off", "1", "0", "TRUE", "False", "YES", "NO", "On", "OFF"}) do
120 local result = expand('{{ "' .. v .. '" | require_bool }}')
121 assert_equal(result, v, 'require_bool should accept "' .. v .. '"')
122 end
123 end)
124
125 test("require_bool: works with env var", function()
126 assert_equal(expand_env('{{ env.ENABLED | require_bool }}', { ENABLED = "true" }), 'true')
127 assert_equal(expand_env('{{ env.ENABLED | require_bool }}', { ENABLED = "YES" }), 'YES')
128 end)
129
130 test("require_bool: crashes on invalid", function()
131 local ok, err = pcall(expand, '{{ "maybe" | require_bool("NOT_BOOL") }}')
132 assert_false(ok)
133 assert_match('NOT_BOOL', err)
134 end)
135
136 -- require_duration
137 test("require_duration: parses all duration formats", function()
138 assert_equal(expand('{{ "30s" | require_duration }}'), '30')
139 assert_equal(expand('{{ "5min" | require_duration }}'), '300')
140 assert_equal(expand('{{ "5m" | require_duration }}'), '300')
141 assert_equal(expand('{{ "1h" | require_duration }}'), '3600')
142 assert_equal(expand('{{ "10d" | require_duration }}'), '864000')
143 assert_equal(expand('{{ "1w" | require_duration }}'), '604800')
144 assert_equal(expand('{{ "1y" | require_duration }}'), '31536000')
145 assert_equal(expand('{{ "500ms" | require_duration }}'), '0.5')
146 assert_equal(expand('{{ "42" | require_duration }}'), '42')
147 end)
148
149 test("require_duration: works with env var", function()
150 assert_equal(expand_env('{{ env.TIMEOUT | require_duration }}', { TIMEOUT = "30s" }), '30')
151 assert_equal(expand_env('{{ env.INTERVAL | require_duration }}', { INTERVAL = "5min" }), '300')
152 end)
153
154 test("require_duration: crashes on invalid", function()
155 local ok, err = pcall(expand, '{{ "abc" | require_duration("BAD_DUR") }}')
156 assert_false(ok)
157 assert_match('BAD_DUR', err)
158 end)
159
160 -- require_json
161 test("require_json: valid JSON passes through", function()
162 assert_equal(expand('{{ \'["a","b"]\' | require_json }}'), '["a","b"]')
163 end)
164
165 test("require_json: works with env var", function()
166 assert_equal(expand_env('{{ env.MODULES | require_json }}', { MODULES = '["mod1","mod2"]' }), '["mod1","mod2"]')
167 end)
168
169 test("require_json: crashes on invalid", function()
170 local ok, err = pcall(expand, '{% set x = "[unclosed" | require_json("BAD_JSON") %}')
171 assert_false(ok)
172 assert_match('BAD_JSON', err)
173 end)
174
175 -- require_size / tobytes
176 test("require_size: accepts valid sizes", function()
177 for _, v in ipairs({"150Mb", "1Gb", "512Kb", "1024", "0", "100b", "0Kb"}) do
178 assert_equal(expand('{{ "' .. v .. '" | require_size }}'), v)
179 end
180 end)
181
182 test("require_size: works with env var", function()
183 assert_equal(expand_env('{{ env.MAX_SIZE | require_size }}', { MAX_SIZE = "150Mb" }), '150Mb')
184 end)
185
186 test("require_size: crashes on invalid", function()
187 local ok, err = pcall(expand, '{{ "abc" | require_size("BAD_SIZE") }}')
188 assert_false(ok)
189 assert_match('BAD_SIZE', err)
190 end)
191
192 test("require_size: crashes on negative", function()
193 local ok, err = pcall(expand, '{{ "-5Mb" | require_size("NEG_SIZE") }}')
194 assert_false(ok)
195 assert_match('NEG_SIZE', err)
196 end)
197
198 test("tobytes: converts correctly", function()
199 assert_equal(expand('{{ "150Mb" | tobytes }}'), '157286400')
200 assert_equal(expand('{{ "1Gb" | tobytes }}'), '1073741824')
201 assert_equal(expand('{{ "512Kb" | tobytes }}'), '524288')
202 assert_equal(expand('{{ "1024" | tobytes }}'), '1024')
203 assert_equal(expand('{{ "100b" | tobytes }}'), '100')
204 assert_equal(expand('{{ "0" | tobytes }}'), '0')
205 end)
206
207 -- =========================================================================
208 -- PARSING FILTERS
209 -- =========================================================================
210
211 -- fromjson
212 test("fromjson: parse JSON object", function()
213 assert_equal(expand('{% set obj = \'{"a":1,"b":"hello"}\' | fromjson %}{{ obj.a }},{{ obj.b }}'), '1,hello')
214 end)
215
216 test("fromjson: parse JSON array", function()
217 assert_equal(expand('{% set arr = \'["x","y","z"]\' | fromjson %}{{ arr[1] }},{{ arr[2] }},{{ arr[3] }}'), 'x,y,z')
218 end)
219
220 test("fromjson: works with env var", function()
221 assert_equal(expand_env('{% set arr = env.LIST | fromjson %}{{ arr[1] }},{{ arr[2] }}', { LIST = '["a","b"]' }), 'a,b')
222 end)
223
224 test("fromjson: nested object access", function()
225 assert_equal(expand('{% set obj = \'{"a":{"b":{"c":"deep"}}}\' | fromjson %}{{ obj.a.b.c }}'), 'deep')
226 end)
227
228 test("fromjson: iterate array with for loop", function()
229 assert_equal(expand('{% set arr = \'["x","y","z"]\' | fromjson %}{% for item in arr %}{{ item }}{% endfor %}'), 'xyz')
230 end)
231
232 -- split
233 test("split: basic CSV", function()
234 assert_equal(expand('{% set arr = "a,b,c" | split(",") %}{{ arr[1] }},{{ arr[2] }},{{ arr[3] }}'), 'a,b,c')
235 end)
236
237 test("split: with max_splits", function()
238 assert_equal(expand('{% set arr = "a,b,c,d" | split(",", 2) %}{{ arr | length }}'), '3')
239 end)
240
241 -- trim
242 test("trim: strips whitespace", function()
243 assert_equal(expand('{{ " hello " | trim }}'), 'hello')
244 end)
245
246 test("trim: leading and trailing spaces", function()
247 assert_equal(expand('{{ " hello " | trim }}'), 'hello')
248 end)
249
250 -- =========================================================================
251 -- TYPE TESTS (all must work with string inputs like env vars)
252 -- =========================================================================
253
254 -- is_defined / is_undefined / is_nil / is_none
255 test("is_defined: true for set env var", function()
256 assert_equal(expand_env('{% if is_defined(env.X) %}yes{% else %}no{% endif %}', { X = "val" }), 'yes')
257 end)
258
259 test("is_defined: false for missing env var", function()
260 assert_equal(expand_env('{% if is_defined(env.MISSING) %}yes{% else %}no{% endif %}', {}), 'no')
261 end)
262
263 test("is_nil: true for missing env var", function()
264 assert_equal(expand_env('{% if is_nil(env.MISSING) %}yes{% else %}no{% endif %}', {}), 'yes')
265 end)
266
267 test("is_none: alias for is_nil", function()
268 assert_equal(expand_env('{% if is_none(env.MISSING) %}yes{% else %}no{% endif %}', {}), 'yes')
269 end)
270
271 test("is_undefined: alias for is_nil", function()
272 assert_equal(expand_env('{% if is_undefined(env.MISSING) %}yes{% else %}no{% endif %}', {}), 'yes')
273 end)
274
275 -- is_string
276 test("is_string: true for env var (always string)", function()
277 assert_equal(expand_env('{% if is_string(env.X) %}yes{% else %}no{% endif %}', { X = "hello" }), 'yes')
278 end)
279
280 test("is_string: true for numeric env var (still string)", function()
281 assert_equal(expand_env('{% if is_string(env.X) %}yes{% else %}no{% endif %}', { X = "42" }), 'yes')
282 end)
283
284 -- is_number (string-aware)
285 test("is_number: true for integer string", function()
286 assert_equal(expand('{% if is_number("42") %}yes{% else %}no{% endif %}'), 'yes')
287 end)
288
289 test("is_number: true for float string", function()
290 assert_equal(expand('{% if is_number("3.14") %}yes{% else %}no{% endif %}'), 'yes')
291 end)
292
293 test("is_number: true for negative string", function()
294 assert_equal(expand('{% if is_number("-1") %}yes{% else %}no{% endif %}'), 'yes')
295 end)
296
297 test("is_number: true for zero string", function()
298 assert_equal(expand('{% if is_number("0") %}yes{% else %}no{% endif %}'), 'yes')
299 end)
300
301 test("is_number: false for non-numeric string", function()
302 assert_equal(expand('{% if is_number("abc") %}yes{% else %}no{% endif %}'), 'no')
303 end)
304
305 test("is_number: works with env var", function()
306 assert_equal(expand_env('{% if is_number(env.PORT) %}yes{% else %}no{% endif %}', { PORT = "8080" }), 'yes')
307 assert_equal(expand_env('{% if is_number(env.NAME) %}yes{% else %}no{% endif %}', { NAME = "test" }), 'no')
308 end)
309
310 test("is_number: true for actual Lua number", function()
311 assert_equal(expand('{% set x = 42 %}{% if is_number(x) %}yes{% else %}no{% endif %}'), 'yes')
312 end)
313
314 -- is_integer (string-aware)
315 test("is_integer: true for integer string", function()
316 assert_equal(expand('{% if is_integer("42") %}yes{% else %}no{% endif %}'), 'yes')
317 end)
318
319 test("is_integer: false for float string", function()
320 assert_equal(expand('{% if is_integer("3.14") %}yes{% else %}no{% endif %}'), 'no')
321 end)
322
323 test("is_integer: true for zero string", function()
324 assert_equal(expand('{% if is_integer("0") %}yes{% else %}no{% endif %}'), 'yes')
325 end)
326
327 test("is_integer: works with env var", function()
328 assert_equal(expand_env('{% if is_integer(env.DB) %}yes{% else %}no{% endif %}', { DB = "15" }), 'yes')
329 assert_equal(expand_env('{% if is_integer(env.PROB) %}yes{% else %}no{% endif %}', { PROB = "0.5" }), 'no')
330 end)
331
332 -- is_float
333 test("is_float: true for float string", function()
334 assert_equal(expand('{% if is_float("3.14") %}yes{% else %}no{% endif %}'), 'yes')
335 end)
336
337 test("is_float: false for integer string", function()
338 assert_equal(expand('{% if is_float("42") %}yes{% else %}no{% endif %}'), 'no')
339 end)
340
341 -- is_boolean
342 test("is_boolean: false for string 'true'", function()
343 assert_equal(expand('{% if is_boolean("true") %}yes{% else %}no{% endif %}'), 'no')
344 end)
345
346 test("is_boolean: true for actual boolean", function()
347 assert_equal(expand('{% set x = true %}{% if is_boolean(x) %}yes{% else %}no{% endif %}'), 'yes')
348 end)
349
350 -- is_mapping / is_table
351 test("is_table: true for parsed JSON object", function()
352 assert_equal(expand('{% set obj = \'{"a":1}\' | fromjson %}{% if is_table(obj) %}yes{% else %}no{% endif %}'), 'yes')
353 end)
354
355 test("is_table: false for string", function()
356 assert_equal(expand('{% if is_table("hello") %}yes{% else %}no{% endif %}'), 'no')
357 end)
358
359 -- is_true / is_false (UCL-aware, string inputs)
360 test("is_true: accepts all UCL truthy strings", function()
361 for _, v in ipairs({"true", "TRUE", "True", "yes", "YES", "Yes", "on", "ON", "On", "1"}) do
362 assert_equal(expand('{% if is_true("' .. v .. '") %}yes{% else %}no{% endif %}'), 'yes', 'is_true("' .. v .. '")')
363 end
364 end)
365
366 test("is_true: rejects non-truthy", function()
367 for _, v in ipairs({"false", "no", "off", "0", "maybe", "", "2"}) do
368 assert_equal(expand('{% if is_true("' .. v .. '") %}yes{% else %}no{% endif %}'), 'no', 'is_true("' .. v .. '")')
369 end
370 end)
371
372 test("is_true: works with env var", function()
373 assert_equal(expand_env('{% if is_true(env.ENABLED) %}yes{% else %}no{% endif %}', { ENABLED = "true" }), 'yes')
374 assert_equal(expand_env('{% if is_true(env.ENABLED) %}yes{% else %}no{% endif %}', { ENABLED = "YES" }), 'yes')
375 assert_equal(expand_env('{% if is_true(env.ENABLED) %}yes{% else %}no{% endif %}', { ENABLED = "false" }), 'no')
376 end)
377
378 test("is_false: accepts all UCL falsy strings", function()
379 for _, v in ipairs({"false", "FALSE", "False", "no", "NO", "No", "off", "OFF", "Off", "0"}) do
380 assert_equal(expand('{% if is_false("' .. v .. '") %}yes{% else %}no{% endif %}'), 'yes', 'is_false("' .. v .. '")')
381 end
382 end)
383
384 test("is_false: rejects non-falsy", function()
385 for _, v in ipairs({"true", "yes", "on", "1", "maybe", "", "2"}) do
386 assert_equal(expand('{% if is_false("' .. v .. '") %}yes{% else %}no{% endif %}'), 'no', 'is_false("' .. v .. '")')
387 end
388 end)
389
390 test("is_false: works with env var", function()
391 assert_equal(expand_env('{% if is_false(env.ENABLED) %}yes{% else %}no{% endif %}', { ENABLED = "false" }), 'yes')
392 assert_equal(expand_env('{% if is_false(env.ENABLED) %}yes{% else %}no{% endif %}', { ENABLED = "true" }), 'no')
393 end)
394
395 -- is_json
396 test("is_json: valid JSON", function()
397 assert_equal(expand('{% if is_json(\'["a"]\') %}yes{% else %}no{% endif %}'), 'yes')
398 end)
399
400 test("is_json: valid JSON object", function()
401 assert_equal(expand('{% if is_json(\'{"k":"v"}\') %}yes{% else %}no{% endif %}'), 'yes')
402 end)
403
404 test("is_json: invalid JSON", function()
405 assert_equal(expand('{% if is_json("{broken") %}yes{% else %}no{% endif %}'), 'no')
406 end)
407
408 test("is_json: empty string", function()
409 assert_equal(expand('{% if is_json("") %}yes{% else %}no{% endif %}'), 'no')
410 end)
411
412 test("is_json: works with env var", function()
413 assert_equal(expand_env('{% if is_json(env.DATA) %}yes{% else %}no{% endif %}', { DATA = '["a"]' }), 'yes')
414 assert_equal(expand_env('{% if is_json(env.DATA) %}yes{% else %}no{% endif %}', { DATA = 'broken' }), 'no')
415 end)
416
417 -- is_size
418 test("is_size: valid sizes", function()
419 for _, v in ipairs({"150Mb", "1Gb", "512Kb", "1024", "0", "100b", "0Kb"}) do
420 assert_equal(expand('{% if is_size("' .. v .. '") %}yes{% else %}no{% endif %}'), 'yes', 'is_size("' .. v .. '")')
421 end
422 end)
423
424 test("is_size: rejects invalid", function()
425 for _, v in ipairs({"abc", "", "-5Mb"}) do
426 assert_equal(expand('{% if is_size("' .. v .. '") %}yes{% else %}no{% endif %}'), 'no', 'is_size("' .. v .. '")')
427 end
428 end)
429
430 test("is_size: works with env var", function()
431 assert_equal(expand_env('{% if is_size(env.MAX) %}yes{% else %}no{% endif %}', { MAX = "150Mb" }), 'yes')
432 assert_equal(expand_env('{% if is_size(env.MAX) %}yes{% else %}no{% endif %}', { MAX = "garbage" }), 'no')
433 end)
434
435 -- =========================================================================
436 -- NUMBER TESTS
437 -- =========================================================================
438
439 test("is_odd: works", function()
440 assert_equal(expand('{% if is_odd(3) %}yes{% else %}no{% endif %}'), 'yes')
441 assert_equal(expand('{% if is_odd(4) %}yes{% else %}no{% endif %}'), 'no')
442 end)
443
444 test("is_even: works", function()
445 assert_equal(expand('{% if is_even(4) %}yes{% else %}no{% endif %}'), 'yes')
446 assert_equal(expand('{% if is_even(3) %}yes{% else %}no{% endif %}'), 'no')
447 end)
448
449 test("is_divisibleby: works", function()
450 assert_equal(expand('{% if is_divisibleby(10, 5) %}yes{% else %}no{% endif %}'), 'yes')
451 assert_equal(expand('{% if is_divisibleby(10, 3) %}yes{% else %}no{% endif %}'), 'no')
452 end)
453
454 -- =========================================================================
455 -- COMPARISON TESTS
456 -- =========================================================================
457
458 test("is_eq: string comparison", function()
459 assert_equal(expand('{% if is_eq("hello", "hello") %}yes{% else %}no{% endif %}'), 'yes')
460 assert_equal(expand('{% if is_eq("hello", "world") %}yes{% else %}no{% endif %}'), 'no')
461 end)
462
463 test("is_ne: not equal", function()
464 assert_equal(expand('{% if is_ne("a", "b") %}yes{% else %}no{% endif %}'), 'yes')
465 end)
466
467 test("is_lt / is_gt: comparison", function()
468 assert_equal(expand('{% if is_lt(1, 2) %}yes{% else %}no{% endif %}'), 'yes')
469 assert_equal(expand('{% if is_gt(2, 1) %}yes{% else %}no{% endif %}'), 'yes')
470 end)
471
472 test("is_le / is_ge: comparison", function()
473 assert_equal(expand('{% if is_le(1, 1) %}yes{% else %}no{% endif %}'), 'yes')
474 assert_equal(expand('{% if is_ge(1, 1) %}yes{% else %}no{% endif %}'), 'yes')
475 end)
476
477 test("is_eq: works with env var", function()
478 assert_equal(expand_env('{% if is_eq(env.MODE, "strict") %}yes{% else %}no{% endif %}', { MODE = "strict" }), 'yes')
479 assert_equal(expand_env('{% if is_eq(env.MODE, "strict") %}yes{% else %}no{% endif %}', { MODE = "lax" }), 'no')
480 end)
481
482 -- =========================================================================
483 -- STRING TESTS
484 -- =========================================================================
485
486 test("is_in: substring check", function()
487 assert_equal(expand('{% if is_in("@", "[email protected]") %}yes{% else %}no{% endif %}'), 'yes')
488 assert_equal(expand('{% if is_in("@", "nodomain") %}yes{% else %}no{% endif %}'), 'no')
489 end)
490
491 test("is_in: table membership", function()
492 assert_equal(expand('{% set t = \'["a","b","c"]\' | fromjson %}{% if is_in("b", t) %}yes{% else %}no{% endif %}'), 'yes')
493 assert_equal(expand('{% set t = \'["a","b","c"]\' | fromjson %}{% if is_in("d", t) %}yes{% else %}no{% endif %}'), 'no')
494 end)
495
496 test("is_startswith: works", function()
497 assert_equal(expand('{% if is_startswith("hello world", "hello") %}yes{% else %}no{% endif %}'), 'yes')
498 assert_equal(expand('{% if is_startswith("hello world", "world") %}yes{% else %}no{% endif %}'), 'no')
499 end)
500
501 test("is_endswith: works", function()
502 assert_equal(expand('{% if is_endswith("hello.min", "min") %}yes{% else %}no{% endif %}'), 'yes')
503 assert_equal(expand('{% if is_endswith("hello.min", "max") %}yes{% else %}no{% endif %}'), 'no')
504 end)
505
506 test("is_match: Lua pattern", function()
507 assert_equal(expand('{% if is_match("hello123", "^%a+%d+$") %}yes{% else %}no{% endif %}'), 'yes')
508 assert_equal(expand('{% if is_match("hello", "^%d+$") %}yes{% else %}no{% endif %}'), 'no')
509 end)
510
511 test("is_lower: works", function()
512 assert_equal(expand('{% if is_lower("hello") %}yes{% else %}no{% endif %}'), 'yes')
513 assert_equal(expand('{% if is_lower("Hello") %}yes{% else %}no{% endif %}'), 'no')
514 end)
515
516 test("is_upper: works", function()
517 assert_equal(expand('{% if is_upper("HELLO") %}yes{% else %}no{% endif %}'), 'yes')
518 assert_equal(expand('{% if is_upper("Hello") %}yes{% else %}no{% endif %}'), 'no')
519 end)
520
521 test("is_sameas: alias for is_eq", function()
522 assert_equal(expand('{% if is_sameas("a", "a") %}yes{% else %}no{% endif %}'), 'yes')
523 end)
524
525 -- =========================================================================
526 -- CONTROL FLOW AND SCOPING
527 -- =========================================================================
528
529 test("variables set in if blocks persist after endif", function()
530 assert_equal(expand('{% set x = "original" %}{% if 1 == 1 %}{% set x = "redefined" %}{% endif %}{{ x }}'), 'redefined')
531 end)
532
533 test("elseif works", function()
534 assert_equal(expand('{% set x = "b" %}{% if x == "a" %}A{% elseif x == "b" %}B{% else %}C{% endif %}'), 'B')
535 end)
536
537 test("range is 1-based", function()
538 assert_equal(expand('{% for i in range(3) %}{{ i }},{% endfor %}'), '1,2,3,')
539 end)
540
541 test("range with start and stop", function()
542 assert_equal(expand('{% for i in range(2, 5) %}{{ i }},{% endfor %}'), '2,3,4,5,')
543 end)
544
545 test("loop.last works in for", function()
546 assert_equal(expand('{% set arr = \'["a","b","c"]\' | fromjson %}{% for item in arr %}{{ item }}{% if not loop.last %},{% endif %}{% endfor %}'), 'a,b,c')
547 end)
548
549 test("loop.index works in for", function()
550 assert_equal(expand('{% for i in range(3) %}{{ loop.index }},{% endfor %}'), '1,2,3,')
551 end)
552
553 -- =========================================================================
554 -- REAL-WORLD ENV VAR PATTERNS
555 -- =========================================================================
556
557 test("pattern: boolean env var with is_true", function()
558 assert_equal(
559 expand_env('{% if is_true(env.FEATURE_ENABLED) %}enabled = true;{% else %}enabled = false;{% endif %}', { FEATURE_ENABLED = "yes" }),
560 'enabled = true;'
561 )
562 end)
563
564 test("pattern: default with require_bool", function()
565 assert_equal(
566 expand_env('{% set enabled = env.FEATURE | default "true" | require_bool %}enabled = {{ enabled }};', {}),
567 'enabled = true;'
568 )
569 end)
570
571 test("pattern: duration with capping", function()
572 assert_equal(
573 expand_env('{% set timeout = env.TIMEOUT | default "30s" | require_duration %}{% if timeout > 300 %}{% set timeout = "5min" %}{% endif %}timeout = {{ timeout }};', { TIMEOUT = "600s" }),
574 'timeout = 5min;'
575 )
576 end)
577
578 test("pattern: JSON array iteration", function()
579 assert_equal(
580 expand_env('{% set modules = env.DISABLED | default \'["a"]\' | fromjson %}{% for m in modules %}{{ m }},{% endfor %}', { DISABLED = '["x","y","z"]' }),
581 'x,y,z,'
582 )
583 end)
584
585 test("pattern: size validation with tobytes", function()
586 assert_equal(
587 expand_env('{% set bytes = env.MAX_SIZE | default "150Mb" | require_size | tobytes %}{{ bytes }}', { MAX_SIZE = "1Gb" }),
588 '1073741824'
589 )
590 end)
591
592 test("pattern: conditional validation with mandatory", function()
593 local ok, err = pcall(expand_env,
594 '{% set pct = env.PERCENT | default "0.5" | require_number %}{% set pct_f = pct | float %}{% if pct_f >= 1 %}{% set _err = "" | mandatory("PERCENT must be < 1") %}{% endif %}',
595 { PERCENT = "1.5" }
596 )
597 assert_false(ok)
598 assert_match('PERCENT must be < 1', err)
599 end)
600 end)