File size: 9,857 Bytes
148b631 |
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
"""
test_cases.py - Test cases for code completion validation.
Defines specific tests to evaluate if RippleGPT understands
hierarchical code structures.
"""
from dataclasses import dataclass
from typing import List, Callable, Optional
import re
@dataclass
class TestCase:
"""Represents a code completion test case."""
name: str
category: str
prompt: str
expected_patterns: List[str] # Regex patterns that MUST appear in output
forbidden_patterns: List[str] = None # Patterns that MUST NOT appear
max_tokens: int = 50
description: str = ""
def __post_init__(self):
if self.forbidden_patterns is None:
self.forbidden_patterns = []
# =============================================================================
# CATEGORY 1: BRACKET CLOSING
# Tests if the model can close parentheses, braces, and brackets
# =============================================================================
BRACKET_TESTS = [
TestCase(
name="simple_parenthesis",
category="brackets",
prompt="def hello(name",
expected_patterns=[r"\)"], # Should close parenthesis
max_tokens=20,
description="Should close simple function parenthesis"
),
TestCase(
name="multiple_args",
category="brackets",
prompt="def calculate(a, b, c",
expected_patterns=[r"\)", r":"], # Should close and add ':'
max_tokens=20,
description="Should close parenthesis with multiple arguments"
),
TestCase(
name="nested_parenthesis",
category="brackets",
prompt="result = sum(range(10",
expected_patterns=[r"\)\)"], # Should close both
max_tokens=20,
description="Should close nested parentheses"
),
TestCase(
name="list_bracket",
category="brackets",
prompt="items = [1, 2, 3",
expected_patterns=[r"\]"],
max_tokens=20,
description="Should close list bracket"
),
TestCase(
name="dict_brace",
category="brackets",
prompt='data = {"name": "test"',
expected_patterns=[r"\}"],
max_tokens=20,
description="Should close dictionary brace"
),
TestCase(
name="function_call_chain",
category="brackets",
prompt="text.strip().lower(",
expected_patterns=[r"\)"],
max_tokens=20,
description="Should close parenthesis in method chain"
),
]
# =============================================================================
# CATEGORY 2: PYTHON INDENTATION
# Tests if the model maintains correct indentation after blocks
# =============================================================================
INDENTATION_TESTS = [
TestCase(
name="if_indent",
category="indentation",
prompt="if x > 0:\n",
expected_patterns=[r"^ \S", r"^\t\S"], # Should indent 4 spaces or tab
max_tokens=30,
description="Should indent after if statement"
),
TestCase(
name="for_indent",
category="indentation",
prompt="for i in range(10):\n",
expected_patterns=[r" \S"],
max_tokens=30,
description="Should indent after for loop"
),
TestCase(
name="def_indent",
category="indentation",
prompt="def process(data):\n",
expected_patterns=[r" "],
max_tokens=30,
description="Should indent function body"
),
TestCase(
name="class_indent",
category="indentation",
prompt="class MyClass:\n",
expected_patterns=[r" "],
max_tokens=30,
description="Should indent class body"
),
TestCase(
name="nested_indent",
category="indentation",
prompt="def foo():\n if True:\n",
expected_patterns=[r" \S"], # 8 spaces (double indentation)
max_tokens=30,
description="Should maintain nested indentation"
),
TestCase(
name="try_except_indent",
category="indentation",
prompt="try:\n x = 1\nexcept:\n",
expected_patterns=[r" "],
max_tokens=30,
description="Should indent except block"
),
]
# =============================================================================
# CATEGORY 3: CODE STRUCTURE
# Tests if the model understands common code patterns
# =============================================================================
STRUCTURE_TESTS = [
TestCase(
name="return_statement",
category="structure",
prompt="def add(a, b):\n return a",
expected_patterns=[r"\+\s*b", r"a \+ b"],
max_tokens=20,
description="Should complete addition operation"
),
TestCase(
name="for_loop_pattern",
category="structure",
prompt="for i in range(",
expected_patterns=[r"\d+\)"], # Number followed by )
max_tokens=20,
description="Should complete range() with number"
),
TestCase(
name="import_statement",
category="structure",
prompt="import os\nimport sys\nimport ",
expected_patterns=[r"[a-z]+"], # Module name
forbidden_patterns=[r"^\d"], # Must not start with digit
max_tokens=20,
description="Should suggest valid module name"
),
TestCase(
name="list_comprehension",
category="structure",
prompt="squares = [x**2 for x in ",
expected_patterns=[r"range\(|list\(|\["],
max_tokens=30,
description="Should complete list comprehension"
),
TestCase(
name="method_definition",
category="structure",
prompt="class Dog:\n def __init__(self",
expected_patterns=[r"\)", r":"],
max_tokens=30,
description="Should complete __init__ definition"
),
TestCase(
name="conditional_else",
category="structure",
prompt="if condition:\n do_something()\nelse",
expected_patterns=[r":"],
max_tokens=20,
description="Should add ':' after else"
),
]
# =============================================================================
# CATEGORY 4: LONG CONTEXT
# Tests if the model maintains coherence in longer code
# =============================================================================
LONG_CONTEXT_TESTS = [
TestCase(
name="function_body",
category="long_context",
prompt="""def calculate_average(numbers):
if not numbers:
return 0
total = 0
for num in numbers:
total +="""
,
expected_patterns=[r"num"], # Should use loop variable
max_tokens=20,
description="Should recall loop variable"
),
TestCase(
name="class_method_reference",
category="long_context",
prompt="""class Calculator:
def __init__(self):
self.result = 0
def add(self, value):
self.result +="""
,
expected_patterns=[r"value"], # Should use parameter
max_tokens=20,
description="Should reference method parameter"
),
TestCase(
name="variable_reuse",
category="long_context",
prompt="""data = load_file("input.txt")
processed = clean_data(data)
result = analyze("""
,
expected_patterns=[r"processed|data"], # Should use defined variable
max_tokens=20,
description="Should reuse previously defined variable"
),
]
# =============================================================================
# CATEGORY 5: PYTHON IDIOMS
# Tests knowledge of Python idioms
# =============================================================================
PYTHON_IDIOM_TESTS = [
TestCase(
name="with_statement",
category="python_idioms",
prompt='with open("file.txt", "r") as',
expected_patterns=[r"f:|file:|handle:"],
max_tokens=20,
description="Should complete with statement"
),
TestCase(
name="f_string",
category="python_idioms",
prompt='name = "World"\ngreeting = f"Hello, {',
expected_patterns=[r"name"],
max_tokens=20,
description="Should use variable in f-string"
),
TestCase(
name="lambda",
category="python_idioms",
prompt="double = lambda x:",
expected_patterns=[r"x\s*\*\s*2|2\s*\*\s*x"],
max_tokens=20,
description="Should complete lambda correctly"
),
TestCase(
name="enumerate",
category="python_idioms",
prompt="for i, item in enumerate(",
expected_patterns=[r"[a-z_]+\)"], # iterable followed by )
max_tokens=20,
description="Should complete enumerate"
),
]
def get_all_test_cases() -> List[TestCase]:
"""Returns all test cases."""
return (
BRACKET_TESTS +
INDENTATION_TESTS +
STRUCTURE_TESTS +
LONG_CONTEXT_TESTS +
PYTHON_IDIOM_TESTS
)
def get_tests_by_category(category: str) -> List[TestCase]:
"""Returns tests for a specific category."""
all_tests = get_all_test_cases()
return [t for t in all_tests if t.category == category]
def get_categories() -> List[str]:
"""Returns list of available categories."""
return [
"brackets",
"indentation",
"structure",
"long_context",
"python_idioms"
]
if __name__ == '__main__':
# List all available tests
print("📋 Available Test Cases:")
print("=" * 60)
for category in get_categories():
tests = get_tests_by_category(category)
print(f"\n[{category.upper()}] ({len(tests)} tests)")
for test in tests:
print(f" • {test.name}: {test.description}")
print(f"\n📊 Total: {len(get_all_test_cases())} tests")
|