Partial cleanup merge test

This commit is contained in:
Auguste Rame 2023-02-10 02:34:18 -05:00
parent 0278e9147b
commit feeba612b9
No known key found for this signature in database
GPG Key ID: 3A5E3F90DF2AAEFE
2 changed files with 89 additions and 2 deletions

View File

@ -1416,14 +1416,94 @@ test diffCharsToLines {
try tmp_vector.append("beta\n");
try diffCharsToLines(arena.allocator(), diffs.items, tmp_vector.items);
try std.testing.expectEqualDeep([_]Diff{
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
Diff{ .operation = .equal, .text = "alpha\nbeta\nalpha\n" },
Diff{ .operation = .insert, .text = "beta\nalpha\nbeta\n" },
}, diffs.items[0..2].*);
}), diffs.items);
// TODO: Implement exhaustive tests
}
test diffCleanupMerge {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
// Cleanup a messy diff.
var diffs = std.ArrayListUnmanaged(Diff){};
try std.testing.expectEqualDeep(@as([]const Diff, &[0]Diff{}), diffs.items); // Null case
try diffs.appendSlice(arena.allocator(), &[_]Diff{ .{ .operation = .equal, .text = "a" }, .{ .operation = .delete, .text = "b" }, .{ .operation = .insert, .text = "c" } });
try diffCleanupMerge(arena.allocator(), &diffs);
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{ .{ .operation = .equal, .text = "a" }, .{ .operation = .delete, .text = "b" }, .{ .operation = .insert, .text = "c" } }), diffs.items); // No change case
diffs.items.len = 0;
try diffs.appendSlice(arena.allocator(), &[_]Diff{ .{ .operation = .equal, .text = "a" }, .{ .operation = .equal, .text = "b" }, .{ .operation = .equal, .text = "c" } });
try diffCleanupMerge(arena.allocator(), &diffs);
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{.{ .operation = .equal, .text = "abc" }}), diffs.items); // Merge equalities
diffs.items.len = 0;
try diffs.appendSlice(arena.allocator(), &[_]Diff{ .{ .operation = .delete, .text = "a" }, .{ .operation = .delete, .text = "b" }, .{ .operation = .delete, .text = "c" } });
try diffCleanupMerge(arena.allocator(), &diffs);
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{.{ .operation = .delete, .text = "abc" }}), diffs.items); // Merge deletions
diffs.items.len = 0;
try diffs.appendSlice(arena.allocator(), &[_]Diff{ .{ .operation = .insert, .text = "a" }, .{ .operation = .insert, .text = "b" }, .{ .operation = .insert, .text = "c" } });
try diffCleanupMerge(arena.allocator(), &diffs);
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{.{ .operation = .insert, .text = "abc" }}), diffs.items); // Merge insertions
diffs.items.len = 0;
try diffs.appendSlice(arena.allocator(), &[_]Diff{
.{ .operation = .delete, .text = "a" },
.{ .operation = .insert, .text = "b" },
.{ .operation = .delete, .text = "c" },
.{ .operation = .insert, .text = "d" },
.{ .operation = .equal, .text = "e" },
.{ .operation = .equal, .text = "f" },
});
try diffCleanupMerge(arena.allocator(), &diffs);
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
.{ .operation = .delete, .text = "ac" },
.{ .operation = .insert, .text = "bd" },
.{ .operation = .equal, .text = "ef" },
}), diffs.items); // Merge interweave
diffs.items.len = 0;
try diffs.appendSlice(arena.allocator(), &[_]Diff{
.{ .operation = .delete, .text = "a" },
.{ .operation = .insert, .text = "abc" },
.{ .operation = .delete, .text = "dc" },
});
try diffCleanupMerge(arena.allocator(), &diffs);
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
.{ .operation = .equal, .text = "a" },
.{ .operation = .delete, .text = "d" },
.{ .operation = .insert, .text = "b" },
.{ .operation = .equal, .text = "c" },
}), diffs.items); // Prefix and suffix detection
diffs.items.len = 0;
try diffs.appendSlice(arena.allocator(), &[_]Diff{
.{ .operation = .equal, .text = "x" },
.{ .operation = .delete, .text = "a" },
.{ .operation = .insert, .text = "abc" },
.{ .operation = .delete, .text = "dc" },
.{ .operation = .equal, .text = "y" },
});
try diffCleanupMerge(arena.allocator(), &diffs);
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
.{ .operation = .equal, .text = "xa" },
.{ .operation = .delete, .text = "d" },
.{ .operation = .insert, .text = "b" },
.{ .operation = .equal, .text = "cy" },
}), diffs.items); // Prefix and suffix detection with equalities
}
fn rebuildtexts(allocator: std.mem.Allocator, diffs: std.ArrayListUnmanaged(Diff)) ![2][]const u8 {
var text = [2]std.ArrayList(u8){
std.ArrayList(u8).init(allocator),

View File

@ -1,5 +1,12 @@
# diffz
An implementation of Google's diff-match-patch.
Currently implemented:
- [x] Diff
- [ ] Match
- [ ] Patch
## License
This library is based off of https://github.com/google/diff-match-patch, which is licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0). This library itself is licensed under the MIT License, see `LICENSE`.