Skip to content

Commit

Permalink
test: start adding fuzz testing
Browse files Browse the repository at this point in the history
  • Loading branch information
zenith391 committed Dec 31, 2024
1 parent 08c7310 commit 9f037eb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,36 @@ test "backend: text field" {

field.setReadOnly(true);
field.setReadOnly(false);

try std.testing.fuzz(fuzzTextField, .{});
}

fn fuzzTextField(input: []const u8) !void {
var field = try backend.TextField.create();
defer field.deinit();
field.setText(input);
if (std.unicode.utf8ValidateSlice(input)) {
// This constraint only holds if the input is valid UTF-8
try std.testing.expectEqualStrings(input, field.getText());
}
}

test "backend: button" {
try std.testing.fuzz(fuzzButton, .{});
}

fn fuzzButton(input: []const u8) !void {
var button = try backend.Button.create();
defer button.deinit();

const null_terminated_input = try std.testing.allocator.dupeZ(u8, input);
defer std.testing.allocator.free(null_terminated_input);

button.setLabel(null_terminated_input);
if (std.unicode.utf8ValidateSlice(input)) {
// This constraint only holds if the input is valid UTF-8
try std.testing.expectEqualStrings(null_terminated_input, button.getLabel());
}
}

test "backend: scrollable" {
Expand Down
2 changes: 1 addition & 1 deletion src/backends/gtk/TextField.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn create() common.BackendError!TextField {
}

pub fn setText(self: *TextField, text: []const u8) void {
var view = std.unicode.Utf8View.initUnchecked(text);
var view = std.unicode.Utf8View.init(text) catch return;
var iterator = view.iterator();
var numChars: c_int = 0;
while (iterator.nextCodepoint() != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextField.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const TextField = struct {

peer: ?backend.TextField = null,
widget_data: TextField.WidgetData = .{},
/// The text this TextField contains
/// The text displayed by this TextField. This is assumed to be valid UTF-8.
text: Atom([]const u8) = Atom([]const u8).of(""),
/// Whether the TextField is read-only
readOnly: Atom(bool) = Atom(bool).of(false),
Expand Down

0 comments on commit 9f037eb

Please sign in to comment.