summaryrefslogtreecommitdiff
path: root/test/lib
diff options
context:
space:
mode:
Diffstat (limited to 'test/lib')
-rw-r--r--test/lib/editor.zig36
1 files changed, 36 insertions, 0 deletions
diff --git a/test/lib/editor.zig b/test/lib/editor.zig
index 2b234cd8..2784b176 100644
--- a/test/lib/editor.zig
+++ b/test/lib/editor.zig
@@ -178,3 +178,39 @@ test "with a parse handed in, the tree-sitter walks run in the library; without,
_ = try ed.ranges(a, &out);
try std.testing.expectEqual(pe.Selection{ .anchor = 0, .head = 25 }, out[0]);
}
+
+test "a byte range replaced is one step to undo, the ranges following it; a drag joined is one step, refused whole when it does not fit" {
+ const gpa = std.testing.allocator;
+ const ed = try gpa.create(Ed);
+ defer gpa.destroy(ed);
+ ed.* = .{};
+ try ed.load("a = 12 + 3;\n");
+ try typeKeys(ed, gpa, "9l");
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var buf: [4096]u8 = undefined;
+ var out: [pe.max_selections]pe.Selection = undefined;
+ // a drag on the number: its first frame a step, the rest joined to it
+ try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 6, "130", false));
+ _ = try ed.ranges(a, &out);
+ try std.testing.expectEqual(pe.Selection{ .anchor = 10, .head = 11 }, out[0]); // still on `3`
+ const steps = ed.undo.count;
+ try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "131", true));
+ try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "14", true));
+ try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf));
+ try std.testing.expectEqual(steps, ed.undo.count);
+ _ = try ed.ranges(a, &out);
+ try std.testing.expectEqual(pe.Selection{ .anchor = 9, .head = 10 }, out[0]);
+ try typeKeys(ed, gpa, "u");
+ try std.testing.expectEqualStrings("a = 12 + 3;\n", text(ed, &buf));
+ try typeKeys(ed, gpa, "U");
+ try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf));
+ // what would not fit: refused, the value as it was
+ const kept = try gpa.create(Ed);
+ defer gpa.destroy(kept);
+ kept.* = ed.*;
+ const ys: [4096]u8 = @splat('y');
+ try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.replace(a, 0, 1, &ys, false));
+ try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed));
+}