forked from raphaelmansuy/code2prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_template_include.py
106 lines (78 loc) · 3.59 KB
/
test_template_include.py
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
import pytest
from code2prompt.core.template_processor import process_template
import os
def test_include_feature(tmp_path):
# Create a main template
main_template = tmp_path / "main.j2"
main_template.write_text("Main: {% include 'sub.j2' %}")
# Create a sub-template
sub_template = tmp_path / "sub.j2"
sub_template.write_text("Sub: {{ variable }}")
template_content = main_template.read_text()
files_data = []
user_inputs = {"variable": "test"}
result = process_template(template_content, files_data, user_inputs, str(main_template))
assert result == "Main: Sub: test"
def test_nested_include(tmp_path):
# Create a main template
main_template = tmp_path / "main.j2"
main_template.write_text("Main: {% include 'sub1.j2' %}")
# Create sub-templates
sub1_template = tmp_path / "sub1.j2"
sub1_template.write_text("Sub1: {% include 'sub2.j2' %}")
sub2_template = tmp_path / "sub2.j2"
sub2_template.write_text("Sub2: {{ variable }}")
template_content = main_template.read_text()
files_data = []
user_inputs = {"variable": "nested"}
result = process_template(template_content, files_data, user_inputs, str(main_template))
assert result == "Main: Sub1: Sub2: nested"
def test_multiple_includes(tmp_path):
# Create a main template
main_template = tmp_path / "main.j2"
main_template.write_text("Main: {% include 'sub1.j2' %} and {% include 'sub2.j2' %}")
# Create sub-templates
sub1_template = tmp_path / "sub1.j2"
sub1_template.write_text("Sub1: {{ var1 }}")
sub2_template = tmp_path / "sub2.j2"
sub2_template.write_text("Sub2: {{ var2 }}")
template_content = main_template.read_text()
files_data = []
user_inputs = {"var1": "first", "var2": "second"}
result = process_template(template_content, files_data, user_inputs, str(main_template))
assert result == "Main: Sub1: first and Sub2: second"
def test_include_with_context(tmp_path):
# Create a main template
main_template = tmp_path / "main.j2"
main_template.write_text("Main: {% include 'sub.j2' %}")
# Create a sub-template
sub_template = tmp_path / "sub.j2"
sub_template.write_text("Sub: {{ main_var }} and {{ sub_var }}")
template_content = main_template.read_text()
files_data = []
user_inputs = {"main_var": "from main", "sub_var": "from sub"}
result = process_template(template_content, files_data, user_inputs, str(main_template))
assert result == "Main: Sub: from main and from sub"
def test_include_with_files_data(tmp_path):
# Create a main template
main_template = tmp_path / "main.j2"
main_template.write_text("Main: {% include 'sub.j2' %}")
# Create a sub-template
sub_template = tmp_path / "sub.j2"
sub_template.write_text("Sub: {{ files[0].name }}")
template_content = main_template.read_text()
files_data = [{"name": "test_file.py", "content": "print('Hello')"}]
user_inputs = {}
result = process_template(template_content, files_data, user_inputs, str(main_template))
assert result == "Main: Sub: test_file.py"
#def test_circular_include(tmp_path):
# Create templates with circular inclusion
# template1 = tmp_path / "template1.j2"
# template1.write_text("T1: {% include 'template2.j2' %}")
# template2 = tmp_path / "template2.j2"
# template2.write_text("T2: {% include 'template1.j2' %}")
# template_content = template1.read_text()
# files_data = []
# user_inputs = {}
# with pytest.raises(ValueError, match="Circular include detected"):
# process_template(template_content, files_data, user_inputs, str(template1))