modify terminal tests to avoid append

This commit is contained in:
Mitchell Hashimoto
2022-05-10 19:06:50 -07:00
parent bc7b3a0996
commit e172cffd4d

View File

@ -163,17 +163,17 @@ fn csiDispatch(
// HPA - Cursor Horizontal Position Absolute (Alias, see '`') // HPA - Cursor Horizontal Position Absolute (Alias, see '`')
'G' => if (action.params.len == 0) { 'G' => if (action.params.len == 0) {
try self.setCursorPosition(self.cursor.y + 1, 1); try self.setCursorPos(self.cursor.y + 1, 1);
} else { } else {
try self.setCursorPosition(self.cursor.y + 1, action.params[0]); try self.setCursorPos(self.cursor.y + 1, action.params[0]);
}, },
// CUP - Set Cursor Position. // CUP - Set Cursor Position.
'H' => { 'H' => {
switch (action.params.len) { switch (action.params.len) {
0 => try self.setCursorPosition(1, 1), 0 => try self.setCursorPos(1, 1),
1 => try self.setCursorPosition(action.params[0], 1), 1 => try self.setCursorPos(action.params[0], 1),
2 => try self.setCursorPosition(action.params[0], action.params[1]), 2 => try self.setCursorPos(action.params[0], action.params[1]),
else => log.warn("unimplemented CSI: {}", .{action}), else => log.warn("unimplemented CSI: {}", .{action}),
} }
}, },
@ -242,16 +242,16 @@ fn csiDispatch(
// HPA - Cursor Horizontal Position Absolute // HPA - Cursor Horizontal Position Absolute
'`' => if (action.params.len == 0) { '`' => if (action.params.len == 0) {
try self.setCursorPosition(self.cursor.y + 1, 1); try self.setCursorPos(self.cursor.y + 1, 1);
} else { } else {
try self.setCursorPosition(self.cursor.y + 1, action.params[0]); try self.setCursorPos(self.cursor.y + 1, action.params[0]);
}, },
// VPA - Cursor Vertical Position Absolute // VPA - Cursor Vertical Position Absolute
'd' => if (action.params.len == 0) { 'd' => if (action.params.len == 0) {
try self.setCursorPosition(1, self.cursor.x + 1); try self.setCursorPos(1, self.cursor.x + 1);
} else { } else {
try self.setCursorPosition(action.params[0], self.cursor.x + 1); try self.setCursorPos(action.params[0], self.cursor.x + 1);
}, },
// SGR - Select Graphic Rendition // SGR - Select Graphic Rendition
@ -361,7 +361,7 @@ pub fn reverseIndex(self: *Terminal, alloc: Allocator) !void {
// the right-most column. If row is 0, it is adjusted to 1. If row is // the right-most column. If row is 0, it is adjusted to 1. If row is
// greater than the bottom-most row it is adjusted to the bottom-most // greater than the bottom-most row it is adjusted to the bottom-most
// row. // row.
pub fn setCursorPosition(self: *Terminal, row: usize, col: usize) !void { pub fn setCursorPos(self: *Terminal, row: usize, col: usize) !void {
self.cursor.x = @minimum(self.cols, col) -| 1; self.cursor.x = @minimum(self.cols, col) -| 1;
self.cursor.y = @minimum(self.rows, row) -| 1; self.cursor.y = @minimum(self.rows, row) -| 1;
} }
@ -636,7 +636,7 @@ test "Terminal: input with no control characters" {
defer t.deinit(testing.allocator); defer t.deinit(testing.allocator);
// Basic grid writing // Basic grid writing
try t.append(testing.allocator, "hello"); for ("hello") |c| try t.print(testing.allocator, c);
try testing.expectEqual(@as(usize, 0), t.cursor.y); try testing.expectEqual(@as(usize, 0), t.cursor.y);
try testing.expectEqual(@as(usize, 5), t.cursor.x); try testing.expectEqual(@as(usize, 5), t.cursor.x);
{ {
@ -646,12 +646,15 @@ test "Terminal: input with no control characters" {
} }
} }
test "Terminal: C0 control LF and CR" { test "Terminal: linefeed and carriage return" {
var t = try init(testing.allocator, 80, 80); var t = try init(testing.allocator, 80, 80);
defer t.deinit(testing.allocator); defer t.deinit(testing.allocator);
// Basic grid writing // Basic grid writing
try t.append(testing.allocator, "hello\r\nworld"); for ("hello") |c| try t.print(testing.allocator, c);
t.carriageReturn();
t.linefeed(testing.allocator);
for ("world") |c| try t.print(testing.allocator, c);
try testing.expectEqual(@as(usize, 1), t.cursor.y); try testing.expectEqual(@as(usize, 1), t.cursor.y);
try testing.expectEqual(@as(usize, 5), t.cursor.x); try testing.expectEqual(@as(usize, 5), t.cursor.x);
{ {
@ -661,14 +664,15 @@ test "Terminal: C0 control LF and CR" {
} }
} }
test "Terminal: C0 control BS" { test "Terminal: backspace" {
const alloc = testing.allocator;
var t = try init(testing.allocator, 80, 80); var t = try init(testing.allocator, 80, 80);
defer t.deinit(testing.allocator); defer t.deinit(testing.allocator);
// BS // BS
try t.append(testing.allocator, "hello"); for ("hello") |c| try t.print(alloc, c);
try t.appendChar(testing.allocator, @enumToInt(ansi.C0.BS)); t.backspace();
try t.append(testing.allocator, "y"); try t.print(alloc, 'y');
try testing.expectEqual(@as(usize, 0), t.cursor.y); try testing.expectEqual(@as(usize, 0), t.cursor.y);
try testing.expectEqual(@as(usize, 5), t.cursor.x); try testing.expectEqual(@as(usize, 5), t.cursor.x);
{ {
@ -679,43 +683,20 @@ test "Terminal: C0 control BS" {
} }
test "Terminal: horizontal tabs" { test "Terminal: horizontal tabs" {
var t = try init(testing.allocator, 80, 5); const alloc = testing.allocator;
defer t.deinit(testing.allocator); var t = try init(alloc, 80, 5);
defer t.deinit(alloc);
// HT // HT
try t.append(testing.allocator, "1\t"); try t.print(alloc, '1');
try t.horizontalTab(alloc);
try testing.expectEqual(@as(usize, 8), t.cursor.x); try testing.expectEqual(@as(usize, 8), t.cursor.x);
// HT // HT
try t.append(testing.allocator, "\t"); try t.horizontalTab(alloc);
try testing.expectEqual(@as(usize, 16), t.cursor.x); try testing.expectEqual(@as(usize, 16), t.cursor.x);
} }
test "Terminal: CUP (ESC [ H)" {
var t = try init(testing.allocator, 80, 80);
defer t.deinit(testing.allocator);
// X, Y both specified
try t.append(testing.allocator, "\x1B[5;10H");
try testing.expectEqual(@as(usize, 4), t.cursor.y);
try testing.expectEqual(@as(usize, 9), t.cursor.x);
// Y only
try t.append(testing.allocator, "\x1B[5H");
try testing.expectEqual(@as(usize, 4), t.cursor.y);
try testing.expectEqual(@as(usize, 0), t.cursor.x);
// 0, 0 default
try t.append(testing.allocator, "\x1B[H");
try testing.expectEqual(@as(usize, 0), t.cursor.y);
try testing.expectEqual(@as(usize, 0), t.cursor.x);
// invalid
try t.append(testing.allocator, "\x1B[1;2;3H");
try testing.expectEqual(@as(usize, 0), t.cursor.y);
try testing.expectEqual(@as(usize, 0), t.cursor.x);
}
test "Terminal: setCursorPosition" { test "Terminal: setCursorPosition" {
var t = try init(testing.allocator, 80, 80); var t = try init(testing.allocator, 80, 80);
defer t.deinit(testing.allocator); defer t.deinit(testing.allocator);
@ -724,12 +705,12 @@ test "Terminal: setCursorPosition" {
try testing.expectEqual(@as(usize, 0), t.cursor.y); try testing.expectEqual(@as(usize, 0), t.cursor.y);
// Setting it to 0 should keep it zero (1 based) // Setting it to 0 should keep it zero (1 based)
try t.setCursorPosition(0, 0); try t.setCursorPos(0, 0);
try testing.expectEqual(@as(usize, 0), t.cursor.x); try testing.expectEqual(@as(usize, 0), t.cursor.x);
try testing.expectEqual(@as(usize, 0), t.cursor.y); try testing.expectEqual(@as(usize, 0), t.cursor.y);
// Should clamp to size // Should clamp to size
try t.setCursorPosition(81, 81); try t.setCursorPos(81, 81);
try testing.expectEqual(@as(usize, 79), t.cursor.x); try testing.expectEqual(@as(usize, 79), t.cursor.x);
try testing.expectEqual(@as(usize, 79), t.cursor.y); try testing.expectEqual(@as(usize, 79), t.cursor.y);
} }