Skip to content

Commit

Permalink
fix new files errors in diff; update dedicated salvage_correct_hunks …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
similato87 committed Apr 6, 2024
1 parent c782c2e commit 605e499
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 239 deletions.
17 changes: 8 additions & 9 deletions gpt_engineer/core/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,13 @@ def find_start_line(self, lines_dict: dict, problems: list) -> bool:
if is_similar(line[1], line_content) and line[1] != "":
start_line = line_number - 1
break
# if the start line is not found, this should be a comment from LLM
# if the start line is not found, append a problem message
if start_line is None:
self.relabel_line(index, ADD)
# check if all the hunks are add lines
if all(self.lines[i][0] == ADD for i in range(len(self.lines))):
problems.append(
f"In {self.hunk_to_string()}:can not find the starting line of the diff"
)
return False
continue
problems.append(
f"In {self.hunk_to_string()}:can not find the starting line of the diff"
)
return False

else:
# the line prior to the start line is found now we insert it to the first place as the start line
self.start_line_pre_edit = start_line
Expand Down Expand Up @@ -329,6 +326,8 @@ def __init__(self, filename_pre, filename_post) -> None:

def is_new_file(self) -> bool:
"""Determines if the diff represents a new file."""
if self.filename_pre == "/dev/null":
return True
return any(hunk.is_new_file for hunk in self.hunks)

def diff_to_string(self) -> str:
Expand Down
99 changes: 99 additions & 0 deletions tests/core/improve_function_test_cases/create_two_new_files_chat
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
To implement the requested functionality, we'll create a Python program that processes the input, evaluates the service history of a car, and outputs the appropriate message based on the criteria provided. We'll structure the program into several parts:

1. **Parsing Input**: A function to parse the input string into a structured format.
2. **Service History Evaluation**: A class to evaluate the service history entries and determine the status of the odometer and service frequency.
3. **Main Logic**: The main logic that ties everything together and outputs the final result.

Let's start by creating the necessary files and writing the initial code.

### File: requirements.txt
This file will list the dependencies required for the project. Since the project does not require external libraries, this file will be empty but included for completeness.

```diff
```

### File: service_history_evaluator.py
This file will contain the class responsible for evaluating the service history.

```diff
--- /dev/null
+++ service_history_evaluator.py
@@ -0,0 +1,47 @@
+from datetime import datetime, timedelta
+
+class ServiceHistoryEvaluator:
+ """
+ Evaluates the service history of a car to determine if the odometer has been tampered with
+ or if the service frequency is insufficient.
+ """
+ def __init__(self, entries):
+ self.entries = entries
+
+ def evaluate(self):
+ # Placeholder for the evaluation logic
+ pass
+
+ def _parse_date(self, year, month):
+ """
+ Parses the year and month into a datetime object.
+ """
+ return datetime(year, month, 1)
+
+ def _calculate_distance(self, start, end):
+ """
+ Calculates the minimum and maximum possible distance traveled between two services.
+ """
+ months_diff = (end.year - start.year) * 12 + end.month - start.month
+ min_distance = 2000 * months_diff
+ max_distance = 20000 * months_diff
+ return min_distance, max_distance
+
+ def _check_odometer_rollover(self, prev_reading, current_reading, max_distance):
+ """
+ Checks if the odometer could have rolled over between two readings.
+ """
+ if prev_reading > current_reading:
+ return current_reading + (99999 - prev_reading) <= max_distance
+ else:
+ return current_reading - prev_reading <= max_distance
+
+ def _check_service_frequency(self):
+ """
+ Checks if the service frequency is within the legal limits.
+ """
+ # Placeholder for the service frequency check logic
+ pass
```

### File: main.py
This file will contain the main logic and the entry point of the program.

```diff
--- /dev/null
+++ main.py
@@ -0,0 +1,29 @@
+import sys
+from service_history_evaluator import ServiceHistoryEvaluator
+
+def parse_input(input_str):
+ """
+ Parses the input string into a structured format.
+ """
+ lines = input_str.split('\n')
+ num_entries = int(lines[0])
+ entries = [tuple(map(int, line.split())) for line in lines[1:num_entries+1]]
+ return entries
+
+def main(input_str):
+ entries = parse_input(input_str)
+ evaluator = ServiceHistoryEvaluator(entries)
+ result = evaluator.evaluate()
+ print(result)
+
+if __name__ == "__main__":
+ if len(sys.argv) != 2:
+ print("Usage: python main.py 'input1 input2 etc'")
+ sys.exit(1)
+ input_str = sys.argv[1].replace("'", "")
+ main(input_str)
```
Empty file.
116 changes: 11 additions & 105 deletions tests/core/test_chat_to_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import pytest

from gpt_engineer.core.chat_to_files import apply_diffs, parse_diffs
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 FilesDict, file_to_lines_dict
from gpt_engineer.core.files_dict import file_to_lines_dict

THIS_FILE_DIR = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -224,7 +224,7 @@ def test_correct_skipped_lines():
with open(
os.path.join(
THIS_FILE_DIR,
"chat_to_files_test_cases",
"improve_function_test_cases",
"corrected_diff_from_missing_lines",
),
"r",
Expand All @@ -248,7 +248,7 @@ def test_correct_skipped_lines_and_number_correction():
with open(
os.path.join(
THIS_FILE_DIR,
"chat_to_files_test_cases",
"improve_function_test_cases",
"corrected_diff_from_missing_lines",
),
"r",
Expand All @@ -273,13 +273,13 @@ def parse_chats_with_regex(
) -> Tuple[str, str, Dict]:
# Load the diff
with open(
os.path.join(THIS_FILE_DIR, "chat_to_files_test_cases", diff_file_name), "r"
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, "chat_to_files_test_cases", code_file_name), "r"
os.path.join(THIS_FILE_DIR, "improve_function_test_cases", code_file_name), "r"
) as f:
code_content = f.read()

Expand All @@ -291,117 +291,23 @@ def parse_chats_with_regex(

# test parse diff
def test_controller_diff():
parse_chats_with_regex("controller_diff", "controller_code")
parse_chats_with_regex("controller_chat", "controller_code")


def test_simple_calculator_diff():
parse_chats_with_regex("simple_calculator_diff", "simple_calculator_code")
parse_chats_with_regex("simple_calculator_chat", "simple_calculator_code")


def test_complex_temperature_converter_diff():
parse_chats_with_regex("temperature_converter_diff", "temperature_converter_code")
parse_chats_with_regex("temperature_converter_chat", "temperature_converter_code")


def test_complex_task_master_diff():
parse_chats_with_regex("task_master_diff", "task_master_code")
parse_chats_with_regex("task_master_chat", "task_master_code")


def test_long_file_diff():
parse_chats_with_regex("wheaties_example_diff", "wheaties_example_code")


# Test diff parsing and validation
def test_validation_and_apply_complex_diff():
task_master_diff, task_master_code, diffs = parse_chats_with_regex(
"task_master_diff", "task_master_code"
)
files = FilesDict({"taskmaster.py": task_master_code})
for file_name, diff in diffs.items():
# if diff is a new file, validation and correction is unnecessary
if not diff.is_new_file():
problems = diff.validate_and_correct(
file_to_lines_dict(files["taskmaster.py"])
)
print(problems)

apply_diffs(diffs, files)


def test_validation_and_apply_long_diff():
wheaties_diff, wheaties_code, diffs = parse_chats_with_regex(
"wheaties_example_diff", "wheaties_example_code"
)

files = FilesDict({"VMClonetest.ps1": wheaties_code})
for file_name, diff in diffs.items():
# if diff is a new file, validation and correction is unnecessary
if not diff.is_new_file():
problems = diff.validate_and_correct(
file_to_lines_dict(files["VMClonetest.ps1"])
)
print(problems)

apply_diffs(diffs, files)


def test_validation_and_apply_wrong_diff():
example_diff, example_code, diffs = parse_chats_with_regex(
"vgvishesh_example_diff", "vgvishesh_example_code"
)
files = FilesDict({"src/components/SocialLinks.tsx": example_code})
for file_name, diff in diffs.items():
# if diff is a new file, validation and correction is unnecessary
if not diff.is_new_file():
problems = diff.validate_and_correct(
file_to_lines_dict(files["src/components/SocialLinks.tsx"])
)
print(problems)

apply_diffs(diffs, files)


def test_validation_and_apply_non_change_diff():
example_diff, example_code, diffs = parse_chats_with_regex(
"vgvishesh_example_2_diff", "vgvishesh_example_2_code"
)
files = FilesDict({"src/App.tsx": example_code})
for file_name, diff in diffs.items():
# if diff is a new file, validation and correction is unnecessary
if not diff.is_new_file():
problems = diff.validate_and_correct(
file_to_lines_dict(files["src/App.tsx"])
)
print(problems)

apply_diffs(diffs, files)


def test_validation_and_apply_diff_on_apps_benchmark_6():
example_diff, example_code, diffs = parse_chats_with_regex(
"apps_benchmark_6_diff", "apps_benchmark_6_code"
)
files = FilesDict({"main.py": example_code})
for file_name, diff in diffs.items():
# if diff is a new file, validation and correction is unnecessary
if not diff.is_new_file():
problems = diff.validate_and_correct(file_to_lines_dict(files["main.py"]))
print(problems)

print(apply_diffs(diffs, files))


def test_validation_and_apply_diff_on_apps_benchmark_6_v2():
example_diff, example_code, diffs = parse_chats_with_regex(
"apps_benchmark_6_v2_diff", "apps_benchmark_6_v2_code"
)
files = FilesDict({"main.py": example_code})
for file_name, diff in diffs.items():
# if diff is a new file, validation and correction is unnecessary
if not diff.is_new_file():
problems = diff.validate_and_correct(file_to_lines_dict(files["main.py"]))
print(problems)

print(apply_diffs(diffs, files))
parse_chats_with_regex("wheaties_example_chat", "wheaties_example_code")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 605e499

Please sign in to comment.