zls/tests/utility/diff.zig
Auguste Rame bf19ed3ea9
Switch to Diff Match Patch (diffz) (#982)
* Add tests, note about correctness issue

* Use diffz (DiffMatchPatch)
2023-02-11 14:21:10 -05:00

31 lines
977 B
Zig

const std = @import("std");
const zls = @import("zls");
const allocator = std.testing.allocator;
fn gen(alloc: std.mem.Allocator, rand: std.rand.Random) ![]const u8 {
var buffer = try alloc.alloc(u8, rand.intRangeAtMost(usize, 16, 1024));
for (buffer) |*b| b.* = rand.intRangeAtMost(u8, 32, 126);
return buffer;
}
test "diff - random" {
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
var rand = std.rand.DefaultPrng.init(0);
var index: usize = 0;
while (index < 100) : (index += 1) {
defer _ = arena.reset(.retain_capacity);
const pre = try gen(arena.allocator(), rand.random());
const post = try gen(arena.allocator(), rand.random());
var edits = try zls.diff.edits(arena.allocator(), pre, post, .@"utf-8");
const applied = try zls.diff.applyTextEdits(arena.allocator(), pre, edits.items, .@"utf-8");
try std.testing.expectEqualStrings(post, applied);
}
}