//! The text buffer through the C ABI alone: after every command, a shared text buffer and its //! view, and an edit buffer and its editor view, look exactly like fresh ones built from the //! model's text with the same options. The text, line count and widths, the wrapped rows, the //! measured sizes, the selection and the text it copies all match. Every call goes through `ot`, //! the opentui.h prototypes, which this test binary binds to this library's own exports. Checking //! another library means building the test against that library's exports instead. const std = @import("std"); const model = @import("model.zig"); const ot = @import("context_abi_c"); /// The edit-marks and text-buffer-view tests' strings: ASCII, a wide character, line breaks, a /// tab, a base and a combining mark, a ZWJ sequence in parts and whole, two regional indicators, /// a zero-width space, a prepended mark, and a word with its space. const pieces = [_][]const u8{ "a", "ab", "\u{65e5}", "\n", "a\nb", "\u{65e5}a", "\u{1f468}", "\u{200d}\u{1f469}", "\u{1f1fa}", "\u{1f1f8}", "e", "\u{301}", "\t", " ", "word ", "\u{200b}", "\u{0600}", "\u{1f469}\u{200d}\u{1f4bb}", }; const text_max = 384; /// Widths that measure probes, besides the view's own. const probes = [_]u32{ 0, 1, 5, 13 }; const height = 4; const Op = union(enum) { /// `at` is a byte offset into the model's text, wrapped into it; a buffer loaded fresh with the /// text turns it, and `at + len`, into display cells. insert: struct { at: u32, piece: u8 }, delete: struct { at: u32, len: u32 }, replace: struct { at: u32, len: u32, piece: u8 }, append: u8, /// `count` pieces drawn from `seed`. set_text: struct { seed: u16, count: u8 }, clear: bool, wrap_mode: u32, width: u32, /// The text buffer's only; the edit buffer keeps the method it was made with. width_method: u32, tab_width: u32, select: struct { start: u32, end: u32 }, select_all, }; fn record(comptime T: type) T { var value = std.mem.zeroes(T); value.struct_size = @sizeOf(T); value.abi_version = ot.OT_CONTEXT_ABI_VERSION; return value; } fn ok(status: ot.ot_status) !void { if (status != ot.OT_OK) return error.UnexpectedStatus; } fn selection(range: [2]u32) ot.ot_editor_selection { var value = record(ot.ot_editor_selection); value.operation = ot.OT_EDITOR_SELECT_SET; value.start = range[0]; value.end = range[1]; return value; } /// The options a subject and its fresh twin share. const Options = struct { method: u32, text_method: u32, wrap_mode: u32 = ot.OT_SCENE_WRAP_WORD, width: u32 = 10, tab_width: u32 = 4, }; /// A text buffer and its view, and an edit buffer and its editor view. const Pair = struct { text: ot.ot_handle, text_view: ot.ot_handle, edit: ot.ot_handle, editor: ot.ot_handle, fn init(context: ?*ot.ot_context, options: Options, bytes: []const u8) !Pair { var self: Pair = undefined; var create = record(ot.ot_edit_buffer_options); create.width_method = options.text_method; try ok(ot.ot_text_buffer_create(context, &create, &self.text)); try ok(ot.ot_text_buffer_set_tab_width(context, &self.text, options.tab_width)); try ok(ot.ot_text_buffer_set_text(context, &self.text, bytes.ptr, @intCast(bytes.len))); try ok(ot.ot_text_buffer_view_create(context, &self.text, &self.text_view)); create.width_method = options.method; try ok(ot.ot_edit_buffer_create(context, &create, &self.edit)); try ok(ot.ot_edit_buffer_set_tab_width(context, &self.edit, options.tab_width)); try ok(ot.ot_edit_buffer_set_text(context, &self.edit, bytes.ptr, @intCast(bytes.len), 0)); try ok(ot.ot_editor_view_create(context, &self.edit, options.width, height, &self.editor)); try self.setWrapMode(context, options.wrap_mode); try self.setWidth(context, options.width); return self; } fn deinit(self: *const Pair, context: ?*ot.ot_context) void { _ = ot.ot_text_buffer_destroy(context, &self.text); _ = ot.ot_edit_buffer_destroy(context, &self.edit); } fn setWrapMode(self: *const Pair, context: ?*ot.ot_context, mode: u32) !void { try ok(ot.ot_text_buffer_view_command(context, &self.text_view, ot.OT_TEXT_VIEW_WRAP_MODE, mode)); try ok(ot.ot_editor_view_command(context, &self.editor, ot.OT_EDITOR_WRAP_MODE, mode)); } fn setWidth(self: *const Pair, context: ?*ot.ot_context, width: u32) !void { try ok(ot.ot_text_buffer_view_command(context, &self.text_view, ot.OT_TEXT_VIEW_WRAP_WIDTH, width)); var viewport = record(ot.ot_editor_viewport); viewport.width = width; viewport.height = height; try ok(ot.ot_editor_view_set_viewport(context, &self.editor, &viewport, 1, 0)); } fn select(self: *const Pair, context: ?*ot.ot_context, selected: Selected) !void { var changed: u32 = 0; if (selected.text) |range| try ok(ot.ot_text_buffer_view_select(context, &self.text_view, &selection(range), &changed)); if (selected.edit) |range| try ok(ot.ot_editor_view_select(context, &self.editor, &selection(range), &changed)); } }; /// The document ranges a host selected on each view. The model restates them after each edit: /// what an edit does to a selection is the host's policy, not this contract. const Selected = struct { text: ?[2]u32 = null, edit: ?[2]u32 = null }; /// What a host can read of one buffer and its view. const Look = struct { text: []const u8, /// Byte count, display length (text buffer only), line count, tab width. info: [4]u32, rows: []const ot.ot_scene_text_line, /// The rows' count and widest row as get_lines reports them, and the view's line count. lines: [3]u32, measured: [probes.len + 1][2]u32, /// Present, start, end. selection: [3]u32, copy: []const u8, /// The buffer's own range for the selection. range: []const u8, /// The buffer's range over the whole document. whole: []const u8, }; const Copy = fn (?*ot.ot_context, [*c]const ot.ot_handle, [*c]u8, u32, [*c]u32) callconv(.c) ot.ot_status; fn copyOut(arena: std.mem.Allocator, context: ?*ot.ot_context, handle: ot.ot_handle, copy: *const Copy) ![]u8 { var count: u32 = 0; try ok(copy(context, &handle, null, 0, &count)); const bytes = try arena.alloc(u8, count); if (count > 0) try ok(copy(context, &handle, bytes.ptr, count, &count)); return bytes[0..count]; } fn textRange(arena: std.mem.Allocator, context: ?*ot.ot_context, handle: ot.ot_handle, start: u32, end: u32) ![]u8 { var count: u32 = 0; try ok(ot.ot_text_buffer_get_range(context, &handle, start, end, null, 0, &count)); const bytes = try arena.alloc(u8, count); if (count > 0) try ok(ot.ot_text_buffer_get_range(context, &handle, start, end, bytes.ptr, count, &count)); return bytes[0..count]; } fn editRange(arena: std.mem.Allocator, context: ?*ot.ot_context, handle: ot.ot_handle, start: u32, end: u32) ![]u8 { var count: u32 = 0; try ok(ot.ot_edit_buffer_get_range(context, &handle, 0, 0, start, 0, end, null, 0, &count)); const bytes = try arena.alloc(u8, count); if (count > 0) try ok(ot.ot_edit_buffer_get_range(context, &handle, 0, 0, start, 0, end, bytes.ptr, count, &count)); return bytes[0..count]; } fn lookText(arena: std.mem.Allocator, context: ?*ot.ot_context, pair: *const Pair, width: u32) !Look { var look: Look = undefined; look.text = try copyOut(arena, context, pair.text, ot.ot_text_buffer_get_text); var info = record(ot.ot_text_buffer_info); try ok(ot.ot_text_buffer_get_info(context, &pair.text, &info)); look.info = .{ info.byte_count, info.text_length, info.line_count, info.tab_width }; var measure = record(ot.ot_editor_measure); try ok(ot.ot_text_buffer_view_get_lines(context, &pair.text_view, 1, 0, null, 0, &measure)); const rows = try arena.alloc(ot.ot_scene_text_line, measure.line_count); try ok(ot.ot_text_buffer_view_get_lines(context, &pair.text_view, 1, 0, rows.ptr, measure.line_count, &measure)); look.rows = rows; var view = record(ot.ot_editor_view_info); try ok(ot.ot_text_buffer_view_get_info(context, &pair.text_view, &view)); look.lines = .{ measure.line_count, measure.width_cols_max, view.virtual_line_count }; for (probes ++ [_]u32{width}, &look.measured) |probe, *out| { var measured = record(ot.ot_editor_measure); try ok(ot.ot_text_buffer_view_measure(context, &pair.text_view, probe, 64, &measured)); out.* = .{ measured.line_count, measured.width_cols_max }; } look.selection = .{ view.selection_present, view.selection_start, view.selection_end }; look.copy = try copyOut(arena, context, pair.text_view, ot.ot_text_buffer_view_get_selected_text); look.range = if (view.selection_present != 0) try textRange(arena, context, pair.text, view.selection_start, view.selection_end) else ""; look.whole = try textRange(arena, context, pair.text, 0, std.math.maxInt(u32)); return look; } fn lookEdit(arena: std.mem.Allocator, context: ?*ot.ot_context, pair: *const Pair, width: u32) !Look { var look: Look = undefined; look.text = try copyOut(arena, context, pair.edit, ot.ot_edit_buffer_get_text); var info = record(ot.ot_edit_buffer_info); try ok(ot.ot_edit_buffer_get_info(context, &pair.edit, &info)); look.info = .{ info.byte_count, 0, info.line_count, info.tab_width }; var measure = record(ot.ot_editor_measure); try ok(ot.ot_editor_view_get_lines(context, &pair.editor, 1, null, 0, &measure)); const rows = try arena.alloc(ot.ot_scene_text_line, measure.line_count); try ok(ot.ot_editor_view_get_lines(context, &pair.editor, 1, rows.ptr, measure.line_count, &measure)); look.rows = rows; var view = record(ot.ot_editor_view_info); try ok(ot.ot_editor_view_get_info(context, &pair.editor, 0, &view)); look.lines = .{ measure.line_count, measure.width_cols_max, view.total_virtual_line_count }; for (probes ++ [_]u32{width}, &look.measured) |probe, *out| { var measured = record(ot.ot_editor_measure); try ok(ot.ot_editor_view_measure(context, &pair.editor, probe, 64, &measured)); out.* = .{ measured.line_count, measured.width_cols_max }; } var selected = record(ot.ot_editor_view_info); try ok(ot.ot_editor_view_get_selection(context, &pair.editor, &selected)); look.selection = .{ selected.selection_present, selected.selection_start, selected.selection_end }; look.copy = try copyOut(arena, context, pair.editor, ot.ot_editor_view_get_selected_text); look.range = if (selected.selection_present != 0) try editRange(arena, context, pair.edit, selected.selection_start, selected.selection_end) else ""; look.whole = try editRange(arena, context, pair.edit, 0, std.math.maxInt(u32)); return look; } /// The first property `edited` breaks against `fresh` and the model's text, named after the bug /// class it catches and prefixed with the buffer kind. A range is in display cells, so zero-width /// text after the last cell is in no range: the whole range is compared with a fresh buffer's. fn compare(comptime side: []const u8, arena: std.mem.Allocator, gate: bool, edited: Look, fresh: Look, bytes: []const u8) !?[]const u8 { // Stale totals: byte count, display length, line count or tab width kept from older text. if (!std.meta.eql(edited.info, fresh.info)) return side ++ " info"; // Stale wrap: rows, their breaks and widths, or the line count kept from older text or options. if (!std.meta.eql(edited.lines, fresh.lines) or !std.mem.eql(u8, std.mem.sliceAsBytes(edited.rows), std.mem.sliceAsBytes(fresh.rows))) return side ++ " wrap"; // A measure cache keyed on less than the text and options. if (!std.meta.eql(edited.measured, fresh.measured)) return side ++ " measure"; // A selection snapped against stale grapheme bounds. if (!std.meta.eql(edited.selection, fresh.selection)) return side ++ " selection"; // The wrong copied range. if (!std.mem.eql(u8, edited.copy, fresh.copy) and !try known(arena, gate, edited.copy, fresh.copy)) return side ++ " copy"; // The view copies other bytes than its buffer gives for the same range. if (!std.mem.eql(u8, edited.copy, edited.range)) return side ++ " copy range"; // A copy that is not a piece of the text. if (std.mem.indexOf(u8, bytes, edited.copy) == null) return side ++ " copy outside text"; // A range over the whole document that misses text a fresh buffer gives. if (!std.mem.eql(u8, edited.whole, fresh.whole) and !try known(arena, gate, edited.whole, fresh.whole)) return side ++ " whole range"; return null; } /// A known defect the walks gate: after an edit leaves zero-width text at a chunk edge (an insert /// before a leading joiner or mark, an appended zero-width space), copies and ranges leave it out, /// where a buffer loaded fresh with the same text keeps it. Whether `edited` and `fresh` differ /// only by the alphabet's zero-width characters; the fixed rows below fail without the gate. fn known(arena: std.mem.Allocator, gate: bool, edited: []const u8, fresh: []const u8) !bool { if (!gate) return false; var both = [2][]const u8{ edited, fresh }; for (&both) |*bytes| { for ([_][]const u8{ "\u{200b}", "\u{200d}", "\u{301}" }) |zero_width| bytes.* = try std.mem.replaceOwned(u8, arena, bytes.*, zero_width, ""); } if (!std.mem.eql(u8, both[0], both[1])) return false; model.count("gated zero-width copy"); return true; } const Harness = struct { /// The width method both buffers are made with. method: u32, /// Whether the known zero-width copy difference passes. gate: bool = true, pub const Command = Op; pub fn generate(_: *Harness, arena: std.mem.Allocator, random: std.Random, steps: usize) ![]Op { const commands = try arena.alloc(Op, steps); for (commands) |*command| { const at = random.int(u32); const len = random.uintLessThan(u32, 12) + 1; const piece = random.uintLessThan(u8, pieces.len); command.* = switch (random.uintLessThan(u8, 24)) { 0...3 => .{ .insert = .{ .at = at, .piece = piece } }, 4...6 => .{ .delete = .{ .at = at, .len = len } }, 7...8 => .{ .replace = .{ .at = at, .len = len, .piece = piece } }, 9...10 => .{ .append = piece }, 11 => .{ .set_text = .{ .seed = random.int(u16), .count = random.uintLessThan(u8, 24) } }, 12 => .{ .clear = random.boolean() }, 13...14 => .{ .wrap_mode = random.uintLessThan(u32, 3) }, 15...16 => .{ .width = random.uintLessThan(u32, 16) }, 17 => .{ .width_method = random.uintLessThan(u32, 4) }, 18 => .{ .tab_width = @as(u32, 2) << random.uintLessThan(u5, 3) }, 19...21 => .{ .select = .{ .start = random.uintLessThan(u32, 160), .end = random.uintLessThan(u32, 160) } }, else => .select_all, }; } return commands; } pub fn simplify(_: *Harness, arena: std.mem.Allocator, command: Op) ![]const Op { return switch (command) { .insert => |value| { var list: std.ArrayListUnmanaged(Op) = .empty; if (value.at != 0) try list.append(arena, .{ .insert = .{ .at = 0, .piece = value.piece } }); if (value.piece != 0) try list.append(arena, .{ .insert = .{ .at = value.at, .piece = 0 } }); return list.items; }, .delete => |value| if (value.len > 1) try arena.dupe(Op, &.{.{ .delete = .{ .at = value.at, .len = value.len / 2 } }}) else &.{}, .replace => |value| try arena.dupe(Op, &.{ .{ .delete = .{ .at = value.at, .len = value.len } }, .{ .insert = .{ .at = value.at, .piece = value.piece } } }), .set_text => |value| if (value.count > 0) try arena.dupe(Op, &.{.{ .set_text = .{ .seed = value.seed, .count = value.count / 2 } }}) else &.{}, else => &.{}, }; } pub fn format(_: *Harness, command: Op, writer: *std.Io.Writer) void { var buffer: [text_max]u8 = undefined; const text = switch (command) { .insert => |value| pieces[value.piece], .replace => |value| pieces[value.piece], .append => |piece| pieces[piece], .set_text => |value| drawn(&buffer, value.seed, value.count), else => "", }; writer.print("{any} \"{f}\"", .{ command, std.zig.fmtString(text) }) catch {}; } pub fn run(self: *Harness, arena: std.mem.Allocator, commands: []const Op) !?model.Failure { var config = record(ot.ot_context_options); config.object_capacity = 16; config.render_cells_max = 16; var context: ?*ot.ot_context = null; try ok(ot.ot_context_create(&config, &context)); defer std.debug.assert(ot.ot_context_destroy(context) == ot.OT_OK); var options: Options = .{ .method = self.method, .text_method = self.method }; var bytes: std.ArrayListUnmanaged(u8) = .empty; const pair = try Pair.init(context, options, ""); defer pair.deinit(context); var selected: Selected = .{}; for (commands, 0..) |command, step| { const effect = try apply(arena, context, &pair, &bytes, &options, &selected, command); switch (effect) { // A rejected NO_ZWJ change that changed the method, or a change that moved the // selection off its text. .@"rejected width method changed", .@"selection left its text" => if (model.fail(@tagName(effect), step)) |failure| return failure, .edited => try pair.select(context, selected), .none, .view => {}, } if (effect != .none) model.count(@tagName(command)); // A lost, doubled or misplaced edit leaves the model behind: the seed stops there. if (!std.mem.eql(u8, try copyOut(arena, context, pair.text, ot.ot_text_buffer_get_text), bytes.items)) return .{ .property = "text text", .step = step }; if (!std.mem.eql(u8, try copyOut(arena, context, pair.edit, ot.ot_edit_buffer_get_text), bytes.items)) return .{ .property = "edit text", .step = step }; const fresh = try Pair.init(context, options, bytes.items); defer fresh.deinit(context); try fresh.select(context, selected); const edit = try compare("edit", arena, self.gate, try lookEdit(arena, context, &pair, options.width), try lookEdit(arena, context, &fresh, options.width), bytes.items); const text = try compare("text", arena, self.gate, try lookText(arena, context, &pair, options.width), try lookText(arena, context, &fresh, options.width), bytes.items); if (text) |property| if (model.fail(property, step)) |failure| return failure; if (edit) |property| if (model.fail(property, step)) |failure| return failure; } return null; } const Effect = enum { none, edited, view, @"rejected width method changed", @"selection left its text" }; fn apply(arena: std.mem.Allocator, context: ?*ot.ot_context, pair: *const Pair, bytes: *std.ArrayListUnmanaged(u8), options: *Options, selected: *Selected, command: Op) !Effect { switch (command) { .insert, .delete, .replace => { const at: u32, const len: u32, const inserted: []const u8 = switch (command) { .insert => |value| .{ value.at, 0, pieces[value.piece] }, .delete => |value| .{ value.at, value.len, "" }, .replace => |value| .{ value.at, value.len, pieces[value.piece] }, else => unreachable, }; if (bytes.items.len + inserted.len > text_max) return .none; // The text this edit gives a buffer loaded fresh with the text before it. const fresh = try loaded(context, options.*, bytes.items); defer _ = ot.ot_edit_buffer_destroy(context, &fresh); const total: u32 = @intCast(bytes.items.len); const first = at % (total + 1); var start: u32 = 0; var end: u32 = 0; try ok(ot.ot_edit_buffer_convert_offset(context, &fresh, ot.OT_EDIT_BYTE_TO_CELL, first, &start)); try ok(ot.ot_edit_buffer_convert_offset(context, &fresh, ot.OT_EDIT_BYTE_TO_CELL, @min(first + len, total), &end)); if (start == end and inserted.len == 0) return .none; for ([_]ot.ot_handle{ fresh, pair.edit }) |edit| try editCells(context, edit, start, end, inserted); const text = try copyOut(arena, context, fresh, ot.ot_edit_buffer_get_text); bytes.clearRetainingCapacity(); try bytes.appendSlice(arena, text); // The shared text buffer has no positional edit: it takes the whole new text. try ok(ot.ot_text_buffer_set_text(context, &pair.text, bytes.items.ptr, @intCast(bytes.items.len))); }, .append => |index| { const piece = pieces[index]; if (bytes.items.len + piece.len > text_max) return .none; // Typing at the end (cursor setting clamps to the text). Typing at column 0 goes // before zero-width text there, so in text that is only zero-width it is no // append: the model skips what a buffer loaded fresh would not put at the end. const fresh = try loaded(context, options.*, bytes.items); defer _ = ot.ot_edit_buffer_destroy(context, &fresh); for ([_]ot.ot_handle{ fresh, pair.edit }, 0..) |edit, subject| { try ok(ot.ot_edit_buffer_set_cursor(context, &edit, std.math.maxInt(u32), std.math.maxInt(u32))); try ok(ot.ot_edit_buffer_insert_text(context, &edit, piece.ptr, @intCast(piece.len))); if (subject > 0) continue; const text = try copyOut(arena, context, fresh, ot.ot_edit_buffer_get_text); if (!std.mem.startsWith(u8, text, bytes.items) or !std.mem.eql(u8, text[bytes.items.len..], piece)) return .none; } try ok(ot.ot_text_buffer_append(context, &pair.text, piece.ptr, @intCast(piece.len))); try bytes.appendSlice(arena, piece); }, .set_text => |value| { var buffer: [text_max]u8 = undefined; const text = drawn(&buffer, value.seed, value.count); try ok(ot.ot_edit_buffer_set_text(context, &pair.edit, text.ptr, @intCast(text.len), 0)); try ok(ot.ot_text_buffer_set_text(context, &pair.text, text.ptr, @intCast(text.len))); bytes.clearRetainingCapacity(); try bytes.appendSlice(arena, text); }, .clear => |reset| { try ok(ot.ot_edit_buffer_command(context, &pair.edit, ot.OT_EDIT_CLEAR, 0)); try ok(ot.ot_text_buffer_clear(context, &pair.text, @intFromBool(reset))); bytes.clearRetainingCapacity(); }, .wrap_mode => |mode| { if (mode == options.wrap_mode) return .none; try pair.setWrapMode(context, mode); options.wrap_mode = mode; return .view; }, .width => |width| { if (width == options.width) return .none; try pair.setWidth(context, width); options.width = width; return .view; }, .width_method => |method| { if (method == options.text_method) return .none; const before = try copyOut(arena, context, pair.text_view, ot.ot_text_buffer_view_get_selected_text); const status = ot.ot_text_buffer_set_width_method(context, &pair.text, method); // NO_ZWJ on either side is rejected and changes nothing. if (method == ot.OT_WIDTH_METHOD_NO_ZWJ or options.text_method == ot.OT_WIDTH_METHOD_NO_ZWJ) { return if (status == ot.OT_INVALID_ARGUMENT) .view else .@"rejected width method changed"; } try ok(status); options.text_method = method; const after = try copyOut(arena, context, pair.text_view, ot.ot_text_buffer_view_get_selected_text); if (!std.mem.eql(u8, before, after)) return .@"selection left its text"; // The kept range, in the new method's cells, is what a fresh view selects. if (selected.text != null) { var view = record(ot.ot_editor_view_info); try ok(ot.ot_text_buffer_view_get_info(context, &pair.text_view, &view)); selected.text = .{ view.selection_start, view.selection_end }; } return .view; }, .tab_width => |width| { if (width == options.tab_width) return .none; try ok(ot.ot_text_buffer_set_tab_width(context, &pair.text, width)); try ok(ot.ot_edit_buffer_set_tab_width(context, &pair.edit, width)); options.tab_width = width; }, .select, .select_all => { const range: [2]u32 = switch (command) { .select => |value| .{ value.start, value.end }, else => .{ 0, std.math.maxInt(u32) }, }; selected.* = .{ .text = range, .edit = range }; try pair.select(context, selected.*); return .view; }, } return .edited; } /// An edit buffer with the options' method and tab width, loaded with `bytes`. fn loaded(context: ?*ot.ot_context, options: Options, bytes: []const u8) !ot.ot_handle { var create = record(ot.ot_edit_buffer_options); create.width_method = options.method; var edit: ot.ot_handle = undefined; try ok(ot.ot_edit_buffer_create(context, &create, &edit)); errdefer _ = ot.ot_edit_buffer_destroy(context, &edit); try ok(ot.ot_edit_buffer_set_tab_width(context, &edit, options.tab_width)); try ok(ot.ot_edit_buffer_set_text(context, &edit, bytes.ptr, @intCast(bytes.len), 0)); return edit; } /// Deletes display cells [start, end) of an edit buffer, then inserts at start. fn editCells(context: ?*ot.ot_context, edit: ot.ot_handle, start: u32, end: u32, inserted: []const u8) !void { if (end != start) { var from = record(ot.ot_edit_position); var to = record(ot.ot_edit_position); try ok(ot.ot_edit_buffer_get_position(context, &edit, ot.OT_EDIT_POSITION_OFFSET, start, 0, &from)); try ok(ot.ot_edit_buffer_get_position(context, &edit, ot.OT_EDIT_POSITION_OFFSET, end, 0, &to)); try ok(ot.ot_edit_buffer_delete_range(context, &edit, from.row, from.col, to.row, to.col)); } if (inserted.len == 0) return; try ok(ot.ot_edit_buffer_command(context, &edit, ot.OT_EDIT_CURSOR_OFFSET, start)); try ok(ot.ot_edit_buffer_insert_text(context, &edit, inserted.ptr, @intCast(inserted.len))); } }; /// `count` pieces drawn from `seed`, up to the text limit. fn drawn(buffer: *[text_max]u8, seed: u16, count: u8) []const u8 { var prng = std.Random.DefaultPrng.init(seed); var len: usize = 0; for (0..count) |_| { const piece = pieces[prng.random().uintLessThan(usize, pieces.len)]; if (len + piece.len > buffer.len) break; @memcpy(buffer[len..][0..piece.len], piece); len += piece.len; } return buffer[0..len]; } test "Text buffer ABI: edited buffers and views look like fresh ones built from the final text" { for ([_]u32{ ot.OT_WIDTH_METHOD_UNICODE, ot.OT_WIDTH_METHOD_WCWIDTH, ot.OT_WIDTH_METHOD_NO_ZWJ, ot.OT_WIDTH_METHOD_UNICODE_WIDE }) |method| { var harness: Harness = .{ .method = method }; try model.check(Harness, &harness, .{ .seeds = 16, .steps = 32 }); } } test "Text buffer ABI: copies after an edit lose zero-width text that a fresh buffer keeps (known, gated)" { // Each row fails its property today. When the defect is fixed, a row passes and this test // fails with a message: delete it and `known`. var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); var harness: Harness = .{ .method = ot.OT_WIDTH_METHOD_UNICODE, .gate = false }; const rows = [_]struct { property: []const u8, trace: []const Op }{ // "a" typed in front of a leading joiner: "\u{200d}a\u{1f469}" copies as "a\u{1f469}". .{ .property = "edit whole range", .trace = &.{ .{ .append = 7 }, .{ .insert = .{ .at = 0, .piece = 0 } } } }, // A zero-width space appended to "a": "a\u{200b}" copies as "a". .{ .property = "text whole range", .trace = &.{ .{ .insert = .{ .at = 0, .piece = 0 } }, .{ .append = 15 } } }, }; for (rows) |row| { const failure = try harness.run(arena_state.allocator(), row.trace) orelse { std.debug.print("the defect is fixed: remove the gate and these rows\n", .{}); return error.KnownDefectFixed; }; try std.testing.expectEqualStrings(row.property, failure.property); } }