(code: str)
| 49 | |
| 50 | |
| 51 | def is_complete_example(code: str) -> bool: |
| 52 | # Check if the code block is a complete, runnable example.A complete example should have a main function with all code inside it. |
| 53 | # Code blocks with statements outside of functions are not complete examples. |
| 54 | has_main = "int main()" in code or "int main (" in code |
| 55 | |
| 56 | # Check if there are statements outside of any function |
| 57 | # look for lines that look like function calls |
| 58 | # or object declarations at the top level |
| 59 | lines = code.split("\n") |
| 60 | brace_depth = 0 |
| 61 | in_main = False |
| 62 | |
| 63 | for line in lines: |
| 64 | stripped = line.strip() |
| 65 | if not stripped or stripped.startswith("//"): |
| 66 | continue |
| 67 | |
| 68 | # Track braces |
| 69 | for char in stripped: |
| 70 | if char == "{": |
| 71 | brace_depth += 1 |
| 72 | elif char == "}": |
| 73 | brace_depth -= 1 |
| 74 | |
| 75 | # Check if entering main |
| 76 | if "int main" in stripped: |
| 77 | in_main = True |
| 78 | continue |
| 79 | |
| 80 | # If code is not inside any braces and not in a struct/class declaration, |
| 81 | # and we see what looks like a function call or object usage, |
| 82 | # this is a uncompleted example |
| 83 | if brace_depth == 0 and not in_main: |
| 84 | # Skip struct/class/enum declarations |
| 85 | if any( |
| 86 | keyword in stripped |
| 87 | for keyword in [ |
| 88 | "struct ", |
| 89 | "class ", |
| 90 | "enum ", |
| 91 | "using ", |
| 92 | "namespace ", |
| 93 | "#include", |
| 94 | "FORY_STRUCT", |
| 95 | "FORY_ENUM", |
| 96 | ] |
| 97 | ): |
| 98 | continue |
| 99 | # Skip forward declarations |
| 100 | if stripped.endswith(";"): |
| 101 | continue |
| 102 | # If we see code that looks like it's executing (not declaring), |
| 103 | # this is not a complete example |
| 104 | if re.search(r"\w+\s*\([^)]*\)\s*;", stripped) and not re.search( |
| 105 | r"^(struct|class|enum|using|namespace|#include|FORY_)", stripped |
| 106 | ): |
| 107 | return False |
| 108 |
no test coverage detected