Skip to content

Commit bf502b7

Browse files
committed
diffCharsToLines test
1 parent 74e5425 commit bf502b7

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

DiffMatchPatch.zig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub const Diff = struct {
2727
value.text,
2828
});
2929
}
30+
31+
pub fn eql(a: Diff, b: Diff) bool {
32+
return a.operation == b.operation and std.mem.eql(u8, a.text, b.text);
33+
}
3034
};
3135

3236
/// Number of microseconds to map a diff before giving up (0 for infinity).
@@ -1387,3 +1391,32 @@ test diffLinesToChars {
13871391
// try testing.expectEqualStrings("", result.chars_2);
13881392
// try testing.expectEqualDeep(tmp_array_list.items, result.line_array.items);
13891393
}
1394+
1395+
test diffCharsToLines {
1396+
var arena = std.heap.ArenaAllocator.init(testing.allocator);
1397+
defer arena.deinit();
1398+
1399+
try std.testing.expect((Diff{ .operation = .equal, .text = "a" }).eql(Diff{ .operation = .equal, .text = "a" }));
1400+
try std.testing.expect(!(Diff{ .operation = .insert, .text = "a" }).eql(Diff{ .operation = .equal, .text = "a" }));
1401+
try std.testing.expect(!(Diff{ .operation = .equal, .text = "a" }).eql(Diff{ .operation = .equal, .text = "b" }));
1402+
try std.testing.expect(!(Diff{ .operation = .equal, .text = "a" }).eql(Diff{ .operation = .delete, .text = "b" }));
1403+
1404+
// Convert chars up to lines.
1405+
var diffs = std.ArrayList(Diff).init(arena.allocator());
1406+
try diffs.appendSlice(&.{
1407+
Diff{ .operation = .equal, .text = try arena.allocator().dupe(u8, "\u{0001}\u{0002}\u{0001}") },
1408+
Diff{ .operation = .insert, .text = try arena.allocator().dupe(u8, "\u{0002}\u{0001}\u{0002}") },
1409+
});
1410+
var tmp_vector = std.ArrayList([]const u8).init(arena.allocator());
1411+
try tmp_vector.append("");
1412+
try tmp_vector.append("alpha\n");
1413+
try tmp_vector.append("beta\n");
1414+
try diffCharsToLines(arena.allocator(), diffs.items, tmp_vector.items);
1415+
1416+
try std.testing.expectEqualDeep([_]Diff{
1417+
Diff{ .operation = .equal, .text = "alpha\nbeta\nalpha\n" },
1418+
Diff{ .operation = .insert, .text = "beta\nalpha\nbeta\n" },
1419+
}, diffs.items[0..2].*);
1420+
1421+
// TODO: Implement exhaustive tests
1422+
}

0 commit comments

Comments
 (0)