File size: 5,713 Bytes
ea8c728 | 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 | """
Tests for code utilities in openevolve.utils.code_utils
"""
import unittest
from openevolve.utils.code_utils import (
_format_block_lines,
apply_diff,
extract_diffs,
format_diff_summary,
)
class TestCodeUtils(unittest.TestCase):
"""Tests for code utilities"""
def test_extract_diffs(self):
"""Test extracting diffs from a response"""
diff_text = """
Let's improve this code:
<<<<<<< SEARCH
def hello():
print("Hello")
=======
def hello():
print("Hello, World!")
>>>>>>> REPLACE
Another change:
<<<<<<< SEARCH
x = 1
=======
x = 2
>>>>>>> REPLACE
"""
diffs = extract_diffs(diff_text)
self.assertEqual(len(diffs), 2)
self.assertEqual(
diffs[0][0],
""" def hello():
print(\"Hello\")""",
)
self.assertEqual(
diffs[0][1],
""" def hello():
print(\"Hello, World!\")""",
)
self.assertEqual(diffs[1][0], " x = 1")
self.assertEqual(diffs[1][1], " x = 2")
def test_apply_diff(self):
"""Test applying diffs to code"""
original_code = """
def hello():
print("Hello")
x = 1
y = 2
"""
diff_text = """
<<<<<<< SEARCH
def hello():
print("Hello")
=======
def hello():
print("Hello, World!")
>>>>>>> REPLACE
<<<<<<< SEARCH
x = 1
=======
x = 2
>>>>>>> REPLACE
"""
expected_code = """
def hello():
print("Hello, World!")
x = 2
y = 2
"""
result = apply_diff(original_code, diff_text)
# Normalize whitespace for comparison
self.assertEqual(
result,
expected_code,
)
class TestFormatDiffSummary(unittest.TestCase):
"""Tests for format_diff_summary showing actual diff content"""
def test_single_line_changes(self):
"""Single-line changes should show inline format"""
diff_blocks = [("x = 1", "x = 2")]
result = format_diff_summary(diff_blocks)
self.assertEqual(result, "Change 1: 'x = 1' to 'x = 2'")
def test_multi_line_changes_show_actual_content(self):
"""Multi-line changes should show actual SEARCH/REPLACE content"""
diff_blocks = [
(
"def old():\n return False",
"def new():\n return True",
)
]
result = format_diff_summary(diff_blocks)
# Should contain actual code, not "2 lines"
self.assertIn("def old():", result)
self.assertIn("return False", result)
self.assertIn("def new():", result)
self.assertIn("return True", result)
self.assertIn("Replace:", result)
self.assertIn("with:", result)
# Should NOT contain generic line count
self.assertNotIn("2 lines", result)
def test_multiple_diff_blocks(self):
"""Multiple diff blocks should be numbered"""
diff_blocks = [
("a = 1", "a = 2"),
("def foo():\n pass", "def bar():\n return 1"),
]
result = format_diff_summary(diff_blocks)
self.assertIn("Change 1:", result)
self.assertIn("Change 2:", result)
self.assertIn("'a = 1' to 'a = 2'", result)
self.assertIn("def foo():", result)
self.assertIn("def bar():", result)
def test_configurable_max_line_len(self):
"""max_line_len parameter should control line truncation"""
long_line = "x" * 50
# Must be multi-line to trigger block format (single-line uses inline format)
diff_blocks = [(long_line + "\nline2", "short\nline2")]
# With default (100), no truncation
result_default = format_diff_summary(diff_blocks)
self.assertNotIn("...", result_default)
# With max_line_len=30, should truncate the long line
result_short = format_diff_summary(diff_blocks, max_line_len=30)
self.assertIn("...", result_short)
def test_configurable_max_lines(self):
"""max_lines parameter should control block truncation"""
many_lines = "\n".join([f"line{i}" for i in range(20)])
diff_blocks = [(many_lines, "replacement")]
# With max_lines=10, should truncate
result = format_diff_summary(diff_blocks, max_lines=10)
self.assertIn("... (10 more lines)", result)
def test_block_lines_basic_formatting(self):
"""Lines should be indented with 2 spaces"""
lines = ["line1", "line2"]
result = _format_block_lines(lines)
self.assertEqual(result, " line1\n line2")
def test_block_lines_long_line_truncation(self):
"""Lines over 100 chars should be truncated by default"""
long_line = "x" * 150
result = _format_block_lines([long_line])
self.assertIn("...", result)
self.assertLess(len(result.split("\n")[0]), 110)
def test_block_lines_many_lines_truncation(self):
"""More than 30 lines should show truncation message by default"""
lines = [f"line{i}" for i in range(50)]
result = _format_block_lines(lines)
self.assertIn("... (20 more lines)", result)
self.assertEqual(len(result.split("\n")), 31)
def test_block_lines_empty_input(self):
"""Empty input should return '(empty)'"""
result = _format_block_lines([])
self.assertEqual(result, " (empty)")
if __name__ == "__main__":
unittest.main()
|