Skip to content

Commit e08ee69

Browse files
committed
Add tests for the mustache functions.
1 parent 4e65601 commit e08ee69

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

tests/test-mustache.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import os
4+
import importlib.util
5+
import importlib.machinery
6+
7+
# Import from runprompt by path (no .py extension)
8+
runprompt_path = os.path.join(
9+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
10+
"runprompt"
11+
)
12+
loader = importlib.machinery.SourceFileLoader("runprompt", runprompt_path)
13+
spec = importlib.util.spec_from_loader("runprompt", loader)
14+
runprompt = importlib.util.module_from_spec(spec)
15+
spec.loader.exec_module(runprompt)
16+
render_template = runprompt.render_template
17+
18+
19+
def test(name, template, variables, expected):
20+
result = render_template(template, variables)
21+
if result == expected:
22+
print("✅ %s" % name)
23+
return True
24+
else:
25+
print("❌ %s" % name)
26+
print(" Expected: %r" % expected)
27+
print(" Got: %r" % result)
28+
return False
29+
30+
31+
def main():
32+
passed = 0
33+
failed = 0
34+
35+
# Basic variable interpolation
36+
if test("simple variable", "Hello {{name}}!", {"name": "World"}, "Hello World!"):
37+
passed += 1
38+
else:
39+
failed += 1
40+
41+
if test("multiple variables", "{{a}} and {{b}}", {"a": "X", "b": "Y"}, "X and Y"):
42+
passed += 1
43+
else:
44+
failed += 1
45+
46+
if test("missing variable", "Hello {{name}}!", {}, "Hello !"):
47+
passed += 1
48+
else:
49+
failed += 1
50+
51+
if test("variable with spaces", "{{ name }}", {"name": "World"}, "World"):
52+
passed += 1
53+
else:
54+
failed += 1
55+
56+
if test("number variable", "Count: {{n}}", {"n": 42}, "Count: 42"):
57+
passed += 1
58+
else:
59+
failed += 1
60+
61+
if test("empty template", "", {"name": "World"}, ""):
62+
passed += 1
63+
else:
64+
failed += 1
65+
66+
if test("no variables", "Hello World!", {"name": "Test"}, "Hello World!"):
67+
passed += 1
68+
else:
69+
failed += 1
70+
71+
print("")
72+
print("Passed: %d, Failed: %d" % (passed, failed))
73+
sys.exit(0 if failed == 0 else 1)
74+
75+
76+
if __name__ == "__main__":
77+
main()

0 commit comments

Comments
 (0)