|
| 1 | +local h = require("tests.helpers") |
| 2 | +local patch = require("codecompanion.strategies.chat.agents.tools.helpers.patch") |
| 3 | + |
| 4 | +local new_set = MiniTest.new_set |
| 5 | + |
| 6 | +local function readfile(path) |
| 7 | + local lines = vim.fn.readfile(path) |
| 8 | + return table.concat(lines, "\n") |
| 9 | +end |
| 10 | + |
| 11 | +local function apply_patch(input_str, patch_str) |
| 12 | + -- 1. parse changes |
| 13 | + local changes, had_begin_end_markers = patch.parse_changes(patch_str) |
| 14 | + |
| 15 | + -- 2. read file into lines |
| 16 | + local lines = vim.split(input_str, "\n", { plain = true }) |
| 17 | + |
| 18 | + -- 3. apply changes |
| 19 | + for _, change in ipairs(changes) do |
| 20 | + local new_lines = patch.apply_change(lines, change) |
| 21 | + if new_lines == nil then |
| 22 | + if had_begin_end_markers then |
| 23 | + error(string.format("Bad/Incorrect diff:\n\n%s\n\nNo changes were applied", patch.get_change_string(change))) |
| 24 | + else |
| 25 | + error("Invalid patch format: missing Begin/End markers") |
| 26 | + end |
| 27 | + else |
| 28 | + lines = new_lines |
| 29 | + end |
| 30 | + end |
| 31 | + return table.concat(lines, "\n") |
| 32 | +end |
| 33 | + |
| 34 | +T = new_set() |
| 35 | + |
| 36 | +T["patch"] = new_set() |
| 37 | +T["patch"]["simple patch"] = function() |
| 38 | + local input_str = "line1\nline2\nline3" |
| 39 | + local patch_str = "*** Begin Patch\nline1\n-line2\n+new_line2\nline3\n*** End Patch" |
| 40 | + local output_str = apply_patch(input_str, patch_str) |
| 41 | + local expected_output = "line1\nnew_line2\nline3" |
| 42 | + h.eq(output_str, expected_output) |
| 43 | +end |
| 44 | + |
| 45 | +T["patch"]["simple test from fixtures"] = function() |
| 46 | + local input_str = readfile("tests/fixtures/files-input-1.html") |
| 47 | + local patch_str = readfile("tests/fixtures/files-diff-1.1.patch") |
| 48 | + local output_str = apply_patch(input_str, patch_str) |
| 49 | + local expected_output = readfile("tests/fixtures/files-output-1.1.html") |
| 50 | + h.eq(output_str, expected_output) |
| 51 | +end |
| 52 | + |
| 53 | +T["patch"]["empty lines"] = function() |
| 54 | + local input_str = readfile("tests/fixtures/files-input-1.html") |
| 55 | + local patch_str = readfile("tests/fixtures/files-diff-1.3.patch") |
| 56 | + local output_str = apply_patch(input_str, patch_str) |
| 57 | + local expected_output = readfile("tests/fixtures/files-output-1.3.html") |
| 58 | + h.eq(output_str, expected_output) |
| 59 | +end |
| 60 | + |
| 61 | +T["patch"]["multiple patches"] = function() |
| 62 | + local input_str = readfile("tests/fixtures/files-input-1.html") |
| 63 | + local patch_str = readfile("tests/fixtures/files-diff-1.4.patch") |
| 64 | + local output_str = apply_patch(input_str, patch_str) |
| 65 | + local expected_output = readfile("tests/fixtures/files-output-1.4.html") |
| 66 | + h.eq(output_str, expected_output) |
| 67 | +end |
| 68 | + |
| 69 | +T["patch"]["no BEGIN and END markers"] = function() |
| 70 | + local input_str = readfile("tests/fixtures/files-input-1.html") |
| 71 | + local patch_str = readfile("tests/fixtures/files-diff-1.5.patch") |
| 72 | + local output_str = apply_patch(input_str, patch_str) |
| 73 | + local expected_output = readfile("tests/fixtures/files-output-1.5.html") |
| 74 | + h.eq(output_str, expected_output) |
| 75 | +end |
| 76 | + |
| 77 | +T["patch"]["multiple continuation"] = function() |
| 78 | + local input_str = readfile("tests/fixtures/files-input-2.html") |
| 79 | + local patch_str = readfile("tests/fixtures/files-diff-2.1.patch") |
| 80 | + local output_str = apply_patch(input_str, patch_str) |
| 81 | + local expected_output = readfile("tests/fixtures/files-output-2.1.html") |
| 82 | + h.eq(output_str, expected_output) |
| 83 | +end |
| 84 | + |
| 85 | +T["patch"]["spaces"] = function() |
| 86 | + local input_str = readfile("tests/fixtures/files-input-2.html") |
| 87 | + local patch_str = readfile("tests/fixtures/files-diff-2.2.patch") |
| 88 | + local output_str = apply_patch(input_str, patch_str) |
| 89 | + local expected_output = readfile("tests/fixtures/files-output-2.2.html") |
| 90 | + h.eq(output_str, expected_output) |
| 91 | +end |
| 92 | + |
| 93 | +T["patch"]["html spaces flexible"] = function() |
| 94 | + local input_str = readfile("tests/fixtures/files-input-3.html") |
| 95 | + local patch_str = readfile("tests/fixtures/files-diff-3.patch") |
| 96 | + local output_str = apply_patch(input_str, patch_str) |
| 97 | + local expected_output = readfile("tests/fixtures/files-output-3.html") |
| 98 | + h.eq(output_str, expected_output) |
| 99 | +end |
| 100 | + |
| 101 | +T["patch"]["html line breaks"] = function() |
| 102 | + local input_str = readfile("tests/fixtures/files-input-4.html") |
| 103 | + local patch_str = readfile("tests/fixtures/files-diff-4.patch") |
| 104 | + local output_str = apply_patch(input_str, patch_str) |
| 105 | + local expected_output = readfile("tests/fixtures/files-output-4.html") |
| 106 | + h.eq(output_str, expected_output) |
| 107 | +end |
| 108 | + |
| 109 | +T["patch"]["lua dashes"] = function() |
| 110 | + local input_str = readfile("tests/fixtures/files-input-5.lua") |
| 111 | + local patch_str = readfile("tests/fixtures/files-diff-5.patch") |
| 112 | + local output_str = apply_patch(input_str, patch_str) |
| 113 | + local expected_output = readfile("tests/fixtures/files-output-5.lua") |
| 114 | + h.eq(output_str, expected_output) |
| 115 | +end |
| 116 | + |
| 117 | +return T |
0 commit comments