Debian Codegraph @

scripts/compile_to_inline_config_test.py

raw symbols SCIP
1 import tempfile
2 import unittest
3 from pathlib import Path
4
5 from compile_to_inline_config import compile_config
6
7
8 class CompileToInlineConfigTest(unittest.TestCase):
9 def test_group_dict_preserves_structure_and_match_policy(self):
10 with tempfile.TemporaryDirectory() as temp_dir:
11 root = Path(temp_dir)
12 dict_dir = root / "dict"
13 dict_dir.mkdir()
14 (dict_dir / "First.txt").write_text("a\tX\n", encoding="utf-8")
15 (dict_dir / "Second.txt").write_text("ab\tY\n", encoding="utf-8")
16
17 config_path = root / "short_first.json"
18 config_path.write_text(
19 """
20 {
21 "name": "short-first group test",
22 "segmentation": {
23 "type": "mmseg",
24 "dict": {
25 "type": "group",
26 "match_policy": "short_circuit",
27 "dicts": [
28 { "type": "text", "file": "First.txt" },
29 { "type": "text", "file": "Second.txt" }
30 ]
31 }
32 },
33 "conversion_chain": [
34 {
35 "dict": {
36 "type": "group",
37 "match_policy": "short_circuit",
38 "dicts": [
39 { "type": "text", "file": "First.txt" },
40 { "type": "text", "file": "Second.txt" }
41 ]
42 }
43 }
44 ]
45 }
46 """,
47 encoding="utf-8",
48 )
49
50 compiled = compile_config(config_path, dict_dir)
51
52 inlined_dicts = [
53 {"type": "inline", "entries": {"a": "X"}},
54 {"type": "inline", "entries": {"ab": "Y"}},
55 ]
56
57 segmentation_dict = compiled["segmentation"]["dict"]
58 self.assertEqual("group", segmentation_dict["type"])
59 self.assertEqual("short_circuit", segmentation_dict["match_policy"])
60 self.assertEqual(inlined_dicts, segmentation_dict["dicts"])
61
62 conversion_dict = compiled["conversion_chain"][0]["dict"]
63 self.assertEqual("group", conversion_dict["type"])
64 self.assertEqual("short_circuit", conversion_dict["match_policy"])
65 self.assertEqual(inlined_dicts, conversion_dict["dicts"])
66
67 def test_group_dict_without_match_policy_compiles_cleanly(self):
68 with tempfile.TemporaryDirectory() as temp_dir:
69 root = Path(temp_dir)
70 dict_dir = root / "dict"
71 dict_dir.mkdir()
72 (dict_dir / "Only.txt").write_text("x\tY\n", encoding="utf-8")
73
74 config_path = root / "no_policy.json"
75 config_path.write_text(
76 """
77 {
78 "name": "no match_policy test",
79 "conversion_chain": [
80 {
81 "dict": {
82 "type": "group",
83 "dicts": [
84 { "type": "text", "file": "Only.txt" }
85 ]
86 }
87 }
88 ]
89 }
90 """,
91 encoding="utf-8",
92 )
93
94 compiled = compile_config(config_path, dict_dir)
95
96 conversion_dict = compiled["conversion_chain"][0]["dict"]
97 self.assertEqual("group", conversion_dict["type"])
98 self.assertNotIn("match_policy", conversion_dict)
99
100
101 class NestedGroupPolicyTest(unittest.TestCase):
102 """Two-level group nesting: union×union, short_circuit×union, union×short_circuit."""
103
104 def setUp(self):
105 self._tmp = tempfile.TemporaryDirectory()
106 root = Path(self._tmp.name)
107 self.dict_dir = root / "dict"
108 self.dict_dir.mkdir()
109 self.config_dir = root / "config"
110 self.config_dir.mkdir()
111 # "x" appears in both A and C to verify first-occurrence wins.
112 (self.dict_dir / "A.txt").write_text("a\tA_val\nx\tX_from_A\n", encoding="utf-8")
113 (self.dict_dir / "B.txt").write_text("b\tB_val\n", encoding="utf-8")
114 (self.dict_dir / "C.txt").write_text("c\tC_val\nx\tX_from_C\n", encoding="utf-8")
115
116 def tearDown(self):
117 self._tmp.cleanup()
118
119 def _config(self, name: str, body: str) -> Path:
120 path = self.config_dir / name
121 path.write_text(
122 f'{{"name": "{name}", "conversion_chain": [{{"dict": {body}}}]}}',
123 encoding="utf-8",
124 )
125 return path
126
127 def _dict(self, compiled) -> dict:
128 return compiled["conversion_chain"][0]["dict"]
129
130 def test_union_of_union_flattens_to_single_inline(self):
131 # union( union(A, B), C ) → one flat inline; "x" from A wins over C.
132 config = self._config(
133 "union_of_union.json",
134 """{
135 "type": "group", "match_policy": "union",
136 "dicts": [
137 { "type": "group", "match_policy": "union",
138 "dicts": [
139 {"type": "text", "file": "A.txt"},
140 {"type": "text", "file": "B.txt"}
141 ]
142 },
143 {"type": "text", "file": "C.txt"}
144 ]
145 }""",
146 )
147 d = self._dict(compile_config(config, self.dict_dir))
148 self.assertEqual("inline", d["type"])
149 self.assertEqual(
150 {"a": "A_val", "b": "B_val", "c": "C_val", "x": "X_from_A"},
151 d["entries"],
152 )
153
154 def test_short_circuit_of_union_keeps_outer_group(self):
155 # short_circuit( union(A, B), C ) → outer group is preserved;
156 # the inner union child is compiled down to a merged inline.
157 config = self._config(
158 "sc_of_union.json",
159 """{
160 "type": "group", "match_policy": "short_circuit",
161 "dicts": [
162 { "type": "group", "match_policy": "union",
163 "dicts": [
164 {"type": "text", "file": "A.txt"},
165 {"type": "text", "file": "B.txt"}
166 ]
167 },
168 {"type": "text", "file": "C.txt"}
169 ]
170 }""",
171 )
172 d = self._dict(compile_config(config, self.dict_dir))
173 self.assertEqual("group", d["type"])
174 self.assertEqual("short_circuit", d["match_policy"])
175 self.assertEqual(2, len(d["dicts"]))
176 # Inner union(A, B) flattens; "x" from A wins over B (B has no "x").
177 inner = d["dicts"][0]
178 self.assertEqual("inline", inner["type"])
179 self.assertEqual({"a": "A_val", "b": "B_val", "x": "X_from_A"}, inner["entries"])
180 # C stays as its own inline.
181 outer_c = d["dicts"][1]
182 self.assertEqual("inline", outer_c["type"])
183 self.assertEqual({"c": "C_val", "x": "X_from_C"}, outer_c["entries"])
184
185 def test_union_of_short_circuit_falls_back_to_group(self):
186 # union( short_circuit(A, B), C ) → union cannot absorb short_circuit;
187 # falls back to a union group; the sc child is compiled but kept as group.
188 config = self._config(
189 "union_of_sc.json",
190 """{
191 "type": "group", "match_policy": "union",
192 "dicts": [
193 { "type": "group", "match_policy": "short_circuit",
194 "dicts": [
195 {"type": "text", "file": "A.txt"},
196 {"type": "text", "file": "B.txt"}
197 ]
198 },
199 {"type": "text", "file": "C.txt"}
200 ]
201 }""",
202 )
203 d = self._dict(compile_config(config, self.dict_dir))
204 self.assertEqual("group", d["type"])
205 self.assertEqual("union", d["match_policy"])
206 self.assertEqual(2, len(d["dicts"]))
207 # The short_circuit child is compiled (file→inline) but its group is kept.
208 sc_child = d["dicts"][0]
209 self.assertEqual("group", sc_child["type"])
210 self.assertEqual("short_circuit", sc_child["match_policy"])
211 self.assertEqual(
212 [
213 {"type": "inline", "entries": {"a": "A_val", "x": "X_from_A"}},
214 {"type": "inline", "entries": {"b": "B_val"}},
215 ],
216 sc_child["dicts"],
217 )
218 # C is compiled to inline.
219 c_child = d["dicts"][1]
220 self.assertEqual("inline", c_child["type"])
221 self.assertEqual({"c": "C_val", "x": "X_from_C"}, c_child["entries"])
222
223
224 if __name__ == "__main__":
225 unittest.main()