File size: 7,983 Bytes
e7c3249 |
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 |
import os
from typing import Dict, Tuple
import pytest
from gpt_engineer.core.chat_to_files import parse_diffs
from gpt_engineer.core.diff import is_similar
from gpt_engineer.core.files_dict import file_to_lines_dict
THIS_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
example_diff = """
Irrelevant line to be ignored
another irrelevant line to be ignored
```diff
--- example.txt
+++ example.txt
@@ -12,3 +12,4 @@
sample text 1
sample text 2
+ added extra line here
- original text A
+ updated original text A with changes
@@ -35,4 +36,5 @@
checking status:
- perform operation X
+ perform operation X only if specific condition holds
+ new operation related to condition
evaluating next step:
- execute step Y
+ revised execution of step Y
```
"""
example_multiple_diffs = """
I apologize for the oversight. Let's correct the `calculator.py` file with the proper git diff format, ensuring that the context lines match the original code exactly.
```diff
--- calculator.py
+++ calculator.py
@@ -1,3 +1,3 @@
class Calculator:
- def add(self, a, b):
- return a - b # Logical
+ def add(self, a, b): # Adds two numbers
+ return a + b
```
Now, let's create the `main.py` file with the correct git diff format:
```diff
--- /dev/null
+++ main.py
@@ -0,0 +1,7 @@
+from calculator import Calculator
+
+# Function to demonstrate the usage of the Calculator class
+def main():
+ calc = Calculator()
+if __name__ == "__main__":
+ main()
```
These changes should now correctly apply to the provided code and create a simple calculator program with a command-line interface.
"""
example_line_dist_diff = """
Irrelevant line to be ignored
another irrelevant line to be ignored
```diff
--- example.txt
+++ example.txt
@@ -10,4 +13,5 @@
sample text 1
sample text 2
+ added extra line here
- original text A
+ updated original text A with changes
@@ -33,14 +363,5 @@
checking status:
- perform operation X
+ perform operation X only if specific condition holds
+ new operation related to condition
evaluating next step:
- execute step Y
+ revised execution of step Y
```
"""
add_example = """
Uninteresting stuff
```diff
--- /dev/null
+++ new_file.txt
@@ -0,0 +1,3 @@
+First example line
+
+Last example line
```
"""
file_example = """# Introduction
@Analysis
Overview: outcomes
%
Background: *context*
Method: []
!
Theories: ?
> Leading up...
sample text 1
sample text 2
original text A
a
Challenges: ~
Perspectives: <>
Strategy: {#}
+
Outcomes: ^^^
Future: |||
x
Y
Z
code
checking status:
perform operation X
evaluating next step:
execute step Y
End.
Conclusion: ***
"""
# Single function tests
def test_basic_similarity():
assert is_similar("abc", "cab")
assert not is_similar("abc", "def")
def test_case_insensitivity_and_whitespace():
assert is_similar("A b C", "c a b")
assert not is_similar("Abc", "D e F")
def test_length_and_character_frequency():
assert is_similar("aabbc", "bacba")
assert not is_similar("aabbcc", "abbcc")
def test_edge_cases():
assert not is_similar("", "a")
assert is_similar("a", "a")
def insert_string_in_lined_string(string, to_insert, line_number):
split_string = string.split("\n")
split_string.insert(line_number - 1, to_insert)
return "\n".join(split_string)
def test_diff_changing_one_file():
diffs = parse_diffs(example_diff)
for filename, diff in diffs.items():
string_diff = diff.diff_to_string()
correct_diff = "\n".join(example_diff.strip().split("\n")[4:-1])
assert string_diff == correct_diff
def test_diff_adding_one_file():
add_diff = parse_diffs(add_example)
for filename, diff in add_diff.items():
string_add_diff = diff.diff_to_string()
correct_add_diff = "\n".join(add_example.strip().split("\n")[2:-1])
assert string_add_diff == correct_add_diff
def test_diff_changing_two_files():
merged_diff = parse_diffs(example_diff + add_example)
correct_diff = "\n".join(example_diff.strip().split("\n")[4:-1])
correct_add_diff = "\n".join(add_example.strip().split("\n")[2:-1])
assert merged_diff["example.txt"].diff_to_string() == correct_diff
assert merged_diff["new_file.txt"].diff_to_string() == correct_add_diff
def test_validate_diff_correct():
lines_dict = file_to_lines_dict(file_example)
diffs = parse_diffs(example_diff)
# This is a test in its own right since it full of exceptions, would something go wrong
list(diffs.values())[0].validate_and_correct(lines_dict)
def test_correct_distorted_numbers():
lines_dict = file_to_lines_dict(file_example)
diffs = parse_diffs(example_line_dist_diff)
# This is a test in its own right since it full of exceptions, would something go wrong
list(diffs.values())[0].validate_and_correct(lines_dict)
correct_diff = "\n".join(example_diff.strip().split("\n")[4:-1])
assert diffs["example.txt"].diff_to_string() == correct_diff
def test_correct_skipped_lines():
distorted_example = insert_string_in_lined_string(
file_example, "#\n#comment\n#\n#", 14
)
diffs = parse_diffs(example_diff)
list(diffs.values())[0].validate_and_correct(file_to_lines_dict(distorted_example))
with open(
os.path.join(
THIS_FILE_DIR,
"improve_function_test_cases",
"corrected_diff_from_missing_lines",
),
"r",
) as f:
corrected_diff_from_missing_lines = f.read()
assert (
diffs["example.txt"].diff_to_string().strip()
== corrected_diff_from_missing_lines.strip()
)
def test_correct_skipped_lines_and_number_correction():
distorted_example = insert_string_in_lined_string(
file_example, "#\n#comment\n#\n#", 14
)
diffs = parse_diffs(example_line_dist_diff)
# list(diffs.values())[0].validate_and_correct(file_to_lines_dict(distorted_example))
for diff in diffs.values():
problems = diff.validate_and_correct(file_to_lines_dict(distorted_example))
print(problems)
with open(
os.path.join(
THIS_FILE_DIR,
"improve_function_test_cases",
"corrected_diff_from_missing_lines",
),
"r",
) as f:
corrected_diff_from_missing_lines = f.read()
assert (
diffs["example.txt"].diff_to_string().strip()
== corrected_diff_from_missing_lines.strip()
)
def test_diff_regex():
diff = parse_diffs(example_diff)
assert len(diff) == 1
diffs = parse_diffs(example_multiple_diffs)
assert len(diffs) == 2
def parse_chats_with_regex(
diff_file_name: str, code_file_name: str
) -> Tuple[str, str, Dict]:
# Load the diff
with open(
os.path.join(THIS_FILE_DIR, "improve_function_test_cases", diff_file_name), "r"
) as f:
diff_content = f.read()
# Load the corresponding code
with open(
os.path.join(THIS_FILE_DIR, "improve_function_test_cases", code_file_name), "r"
) as f:
code_content = f.read()
# Parse the diffs
diffs = parse_diffs(diff_content)
return diff_content, code_content, diffs
# test parse diff
def test_controller_diff():
parse_chats_with_regex("controller_chat", "controller_code")
def test_simple_calculator_diff():
parse_chats_with_regex("simple_calculator_chat", "simple_calculator_code")
def test_complex_temperature_converter_diff():
parse_chats_with_regex("temperature_converter_chat", "temperature_converter_code")
def test_complex_task_master_diff():
parse_chats_with_regex("task_master_chat", "task_master_code")
def test_long_file_diff():
parse_chats_with_regex("wheaties_example_chat", "wheaties_example_code")
if __name__ == "__main__":
pytest.main()
|