terminal2: selectWord more tests

This commit is contained in:
Mitchell Hashimoto
2024-03-06 17:20:26 -08:00
parent 56fc4d7a1e
commit f03b9f95e0
2 changed files with 73 additions and 6 deletions

View File

@ -4740,6 +4740,7 @@ test "Screen: selectWord" {
}
}
// X
test "Screen: selectWord across soft-wrap" {
const testing = std.testing;
const alloc = testing.allocator;

View File

@ -1068,12 +1068,6 @@ pub fn selectWord(self: *Screen, pin: Pin) ?Selection {
const rac = p.rowAndCell();
const cell = rac.cell;
// If we are going to the next row and it isn't wrapped, we
// return the previous.
if (p.x == 0 and !rac.row.wrap) {
break :end prev;
}
// If we reached an empty cell its always a boundary
if (!cell.hasText()) break :end prev;
@ -1085,6 +1079,12 @@ pub fn selectWord(self: *Screen, pin: Pin) ?Selection {
) != null;
if (this_boundary != expect_boundary) break :end prev;
// If we are going to the next row and it isn't wrapped, we
// return the previous.
if (p.x == p.page.data.size.cols - 1 and !rac.row.wrap) {
break :end p;
}
prev = p;
}
@ -4145,3 +4145,69 @@ test "Screen: selectWord" {
} }, s.pages.pointFromPin(.screen, sel.end().*).?);
}
}
test "Screen: selectWord across soft-wrap" {
const testing = std.testing;
const alloc = testing.allocator;
var s = try init(alloc, 5, 10, 0);
defer s.deinit();
try s.testWriteString(" 1234012\n 123");
{
const contents = try s.dumpStringAlloc(alloc, .{ .screen = .{} });
defer alloc.free(contents);
try testing.expectEqualStrings(" 1234\n012\n 123", contents);
}
// Going forward
{
var sel = s.selectWord(s.pages.pin(.{ .active = .{
.x = 1,
.y = 0,
} }).?).?;
defer sel.deinit(&s);
try testing.expectEqual(point.Point{ .screen = .{
.x = 1,
.y = 0,
} }, s.pages.pointFromPin(.screen, sel.start().*).?);
try testing.expectEqual(point.Point{ .screen = .{
.x = 2,
.y = 1,
} }, s.pages.pointFromPin(.screen, sel.end().*).?);
}
// Going backward
{
var sel = s.selectWord(s.pages.pin(.{ .active = .{
.x = 1,
.y = 1,
} }).?).?;
defer sel.deinit(&s);
try testing.expectEqual(point.Point{ .screen = .{
.x = 1,
.y = 0,
} }, s.pages.pointFromPin(.screen, sel.start().*).?);
try testing.expectEqual(point.Point{ .screen = .{
.x = 2,
.y = 1,
} }, s.pages.pointFromPin(.screen, sel.end().*).?);
}
// Going forward and backward
{
var sel = s.selectWord(s.pages.pin(.{ .active = .{
.x = 3,
.y = 0,
} }).?).?;
defer sel.deinit(&s);
try testing.expectEqual(point.Point{ .screen = .{
.x = 1,
.y = 0,
} }, s.pages.pointFromPin(.screen, sel.start().*).?);
try testing.expectEqual(point.Point{ .screen = .{
.x = 2,
.y = 1,
} }, s.pages.pointFromPin(.screen, sel.end().*).?);
}
}