|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import unittest |
6 | | -from openevolve.utils.code_utils import apply_diff, extract_diffs |
| 6 | + |
| 7 | +from openevolve.utils.code_utils import ( |
| 8 | + _format_block_lines, |
| 9 | + apply_diff, |
| 10 | + extract_diffs, |
| 11 | + format_diff_summary, |
| 12 | +) |
7 | 13 |
|
8 | 14 |
|
9 | 15 | class TestCodeUtils(unittest.TestCase): |
@@ -89,5 +95,92 @@ def hello(): |
89 | 95 | ) |
90 | 96 |
|
91 | 97 |
|
| 98 | +class TestFormatDiffSummary(unittest.TestCase): |
| 99 | + """Tests for format_diff_summary showing actual diff content""" |
| 100 | + |
| 101 | + def test_single_line_changes(self): |
| 102 | + """Single-line changes should show inline format""" |
| 103 | + diff_blocks = [("x = 1", "x = 2")] |
| 104 | + result = format_diff_summary(diff_blocks) |
| 105 | + self.assertEqual(result, "Change 1: 'x = 1' to 'x = 2'") |
| 106 | + |
| 107 | + def test_multi_line_changes_show_actual_content(self): |
| 108 | + """Multi-line changes should show actual SEARCH/REPLACE content""" |
| 109 | + diff_blocks = [ |
| 110 | + ( |
| 111 | + "def old():\n return False", |
| 112 | + "def new():\n return True", |
| 113 | + ) |
| 114 | + ] |
| 115 | + result = format_diff_summary(diff_blocks) |
| 116 | + # Should contain actual code, not "2 lines" |
| 117 | + self.assertIn("def old():", result) |
| 118 | + self.assertIn("return False", result) |
| 119 | + self.assertIn("def new():", result) |
| 120 | + self.assertIn("return True", result) |
| 121 | + self.assertIn("Replace:", result) |
| 122 | + self.assertIn("with:", result) |
| 123 | + # Should NOT contain generic line count |
| 124 | + self.assertNotIn("2 lines", result) |
| 125 | + |
| 126 | + def test_multiple_diff_blocks(self): |
| 127 | + """Multiple diff blocks should be numbered""" |
| 128 | + diff_blocks = [ |
| 129 | + ("a = 1", "a = 2"), |
| 130 | + ("def foo():\n pass", "def bar():\n return 1"), |
| 131 | + ] |
| 132 | + result = format_diff_summary(diff_blocks) |
| 133 | + self.assertIn("Change 1:", result) |
| 134 | + self.assertIn("Change 2:", result) |
| 135 | + self.assertIn("'a = 1' to 'a = 2'", result) |
| 136 | + self.assertIn("def foo():", result) |
| 137 | + self.assertIn("def bar():", result) |
| 138 | + |
| 139 | + def test_configurable_max_line_len(self): |
| 140 | + """max_line_len parameter should control line truncation""" |
| 141 | + long_line = "x" * 50 |
| 142 | + # Must be multi-line to trigger block format (single-line uses inline format) |
| 143 | + diff_blocks = [(long_line + "\nline2", "short\nline2")] |
| 144 | + # With default (100), no truncation |
| 145 | + result_default = format_diff_summary(diff_blocks) |
| 146 | + self.assertNotIn("...", result_default) |
| 147 | + # With max_line_len=30, should truncate the long line |
| 148 | + result_short = format_diff_summary(diff_blocks, max_line_len=30) |
| 149 | + self.assertIn("...", result_short) |
| 150 | + |
| 151 | + def test_configurable_max_lines(self): |
| 152 | + """max_lines parameter should control block truncation""" |
| 153 | + many_lines = "\n".join([f"line{i}" for i in range(20)]) |
| 154 | + diff_blocks = [(many_lines, "replacement")] |
| 155 | + # With max_lines=10, should truncate |
| 156 | + result = format_diff_summary(diff_blocks, max_lines=10) |
| 157 | + self.assertIn("... (10 more lines)", result) |
| 158 | + |
| 159 | + def test_block_lines_basic_formatting(self): |
| 160 | + """Lines should be indented with 2 spaces""" |
| 161 | + lines = ["line1", "line2"] |
| 162 | + result = _format_block_lines(lines) |
| 163 | + self.assertEqual(result, " line1\n line2") |
| 164 | + |
| 165 | + def test_block_lines_long_line_truncation(self): |
| 166 | + """Lines over 100 chars should be truncated by default""" |
| 167 | + long_line = "x" * 150 |
| 168 | + result = _format_block_lines([long_line]) |
| 169 | + self.assertIn("...", result) |
| 170 | + self.assertLess(len(result.split("\n")[0]), 110) |
| 171 | + |
| 172 | + def test_block_lines_many_lines_truncation(self): |
| 173 | + """More than 30 lines should show truncation message by default""" |
| 174 | + lines = [f"line{i}" for i in range(50)] |
| 175 | + result = _format_block_lines(lines) |
| 176 | + self.assertIn("... (20 more lines)", result) |
| 177 | + self.assertEqual(len(result.split("\n")), 31) |
| 178 | + |
| 179 | + def test_block_lines_empty_input(self): |
| 180 | + """Empty input should return '(empty)'""" |
| 181 | + result = _format_block_lines([]) |
| 182 | + self.assertEqual(result, " (empty)") |
| 183 | + |
| 184 | + |
92 | 185 | if __name__ == "__main__": |
93 | 186 | unittest.main() |
0 commit comments