summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/File.zig b/src/File.zig
index 92e5b210..8f3d4bb9 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -2328,6 +2328,54 @@ pub fn paintWordSelection(
}
}
+test "undo and redo walk back and forth through every step of random edits, joined entries and all" {
+ const gpa = std.testing.allocator;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ const pane = try p.setTestFile("seed text\nof two lines\n");
+ const f = &pane.file.?;
+ const j = &f.history.journal;
+ var prng = std.Random.DefaultPrng.init(17);
+ const r = prng.random();
+ // The text at each step's edge, the last the text now.
+ var texts: std.ArrayList([]u8) = .empty;
+ defer {
+ for (texts.items) |t| gpa.free(t);
+ texts.deinit(gpa);
+ }
+ var at: usize = 0;
+ for (0..80) |_| {
+ const was = j.done;
+ try texts.append(gpa, try gpa.dupe(u8, f.content));
+ pushUndo(p, pane);
+ // A step that changed nothing goes on, its edge where it was.
+ if (j.done == was) gpa.free(texts.pop().?);
+ // Up to six edits: most where the last ended, as typing goes on.
+ for (0..1 + r.uintLessThan(usize, 6)) |_| {
+ const len = f.content.len;
+ at = if (r.uintLessThan(u8, 4) == 0) r.uintAtMost(usize, len) else @min(at, len);
+ const del = r.uintAtMost(usize, @min(3, len - at));
+ var ins: [3]u8 = undefined;
+ const n = r.uintAtMost(usize, 3);
+ for (ins[0..n]) |*c| c.* = "ab\n"[r.uintLessThan(usize, 3)];
+ try std.testing.expect(replace(p, f, at, del, ins[0..n]));
+ at += n;
+ }
+ }
+ try texts.append(gpa, try gpa.dupe(u8, f.content));
+ try std.testing.expectEqual(texts.items.len - 1, j.done);
+ var k = texts.items.len - 1;
+ while (k > 0) {
+ k -= 1;
+ undoStep(p, pane, true);
+ try std.testing.expectEqualStrings(texts.items[k], f.content);
+ }
+ for (texts.items[1..]) |t| {
+ undoStep(p, pane, false);
+ try std.testing.expectEqualStrings(t, f.content);
+ }
+}
+
test "undo keeps its steps within a byte bound, the newest always" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });