MCPcopy Create free account
hub / github.com/apache/fory / wrap_code_as_test

Function wrap_code_as_test

ci/extract_cpp_doc_code.py:112–181  ·  view source on GitHub ↗
(code: str, doc_file: str, block_index: int)

Source from the content-addressed store, hash-verified

110
111
112def wrap_code_as_test(code: str, doc_file: str, block_index: int) -> str:
113 # Wrap a code snippet as a complete, compilable test file.
114
115 includes = set()
116
117 if "#include" not in code:
118 includes.add('#include "fory/serialization/fory.h"')
119
120 if "std::string" in code and "#include <string>" not in code:
121 includes.add("#include <string>")
122 if "std::vector" in code and "#include <vector>" not in code:
123 includes.add("#include <vector>")
124 if "std::map" in code and "#include <map>" not in code:
125 includes.add("#include <map>")
126 if "std::set" in code and "#include <set>" not in code:
127 includes.add("#include <set>")
128 if "std::unordered_map" in code and "#include <unordered_map>" not in code:
129 includes.add("#include <unordered_map>")
130 if "std::unordered_set" in code and "#include <unordered_set>" not in code:
131 includes.add("#include <unordered_set>")
132 if "std::optional" in code and "#include <optional>" not in code:
133 includes.add("#include <optional>")
134 if "std::shared_ptr" in code and "#include <memory>" not in code:
135 includes.add("#include <memory>")
136 if "std::unique_ptr" in code and "#include <memory>" not in code:
137 includes.add("#include <memory>")
138 if "std::variant" in code and "#include <variant>" not in code:
139 includes.add("#include <variant>")
140 if "std::chrono" in code and "#include <chrono>" not in code:
141 includes.add("#include <chrono>")
142 if "std::make_shared" in code and "#include <memory>" not in code:
143 includes.add("#include <memory>")
144 if "std::make_unique" in code and "#include <memory>" not in code:
145 includes.add("#include <memory>")
146 if "assert(" in code and "#include <cassert>" not in code:
147 includes.add("#include <cassert>")
148 if "std::cout" in code and "#include <iostream>" not in code:
149 includes.add("#include <iostream>")
150 if "RowEncoder" in code and '#include "fory/encoder/row_encoder.h"' not in code:
151 includes.add('#include "fory/encoder/row_encoder.h"')
152 if "Row" in code and '#include "fory/row/row.h"' not in code:
153 includes.add('#include "fory/row/row.h"')
154
155 include_section = "\n".join(sorted(includes))
156
157 if "int main()" in code or "int main (" in code:
158 # Only add namespace if not already present
159 if "using namespace" not in code:
160 code = f"using namespace fory::serialization;\n\n{code}"
161 return f"""// Auto-generated test from {doc_file}
162
163{include_section}
164
165{code}
166"""
167 else:
168 return f"""// Auto-generated test from {doc_file}, block {block_index}
169

Callers 1

process_markdown_fileFunction · 0.85

Calls 2

addMethod · 0.65
setFunction · 0.50

Tested by

no test coverage detected