const std = @import("std"); const testing = std.testing; const patterns = @import("src/patterns.zig"); test "SDL_Rect: two-field lines" { const allocator = testing.allocator; const source = \\typedef struct SDL_Rect { \\ int x, y; \\ int w, h; \\} SDL_Rect; ; var scanner = patterns.Scanner.init(allocator, source); const decls = try scanner.scan(); defer { for (decls) |decl| { switch (decl) { .struct_decl => |s| { allocator.free(s.name); if (s.doc_comment) |doc| allocator.free(doc); for (s.fields) |field| { allocator.free(field.name); allocator.free(field.type_name); if (field.comment) |c| allocator.free(c); } allocator.free(s.fields); }, else => {}, } } allocator.free(decls); } try testing.expectEqual(@as(usize, 1), decls.len); const s = decls[0].struct_decl; try testing.expectEqualStrings("SDL_Rect", s.name); try testing.expectEqual(@as(usize, 4), s.fields.len); try testing.expectEqualStrings("x", s.fields[0].name); try testing.expectEqualStrings("int", s.fields[0].type_name); try testing.expectEqualStrings("y", s.fields[1].name); try testing.expectEqualStrings("int", s.fields[1].type_name); try testing.expectEqualStrings("w", s.fields[2].name); try testing.expectEqualStrings("int", s.fields[2].type_name); try testing.expectEqualStrings("h", s.fields[3].name); try testing.expectEqualStrings("int", s.fields[3].type_name); } test "SDL_FRect: three-field line" { const allocator = testing.allocator; const source = \\typedef struct SDL_FRect { \\ float x, y, w; \\ float h; \\} SDL_FRect; ; var scanner = patterns.Scanner.init(allocator, source); const decls = try scanner.scan(); defer { for (decls) |decl| { switch (decl) { .struct_decl => |s| { allocator.free(s.name); if (s.doc_comment) |doc| allocator.free(doc); for (s.fields) |field| { allocator.free(field.name); allocator.free(field.type_name); if (field.comment) |c| allocator.free(c); } allocator.free(s.fields); }, else => {}, } } allocator.free(decls); } try testing.expectEqual(@as(usize, 1), decls.len); const s = decls[0].struct_decl; try testing.expectEqual(@as(usize, 4), s.fields.len); try testing.expectEqualStrings("x", s.fields[0].name); try testing.expectEqualStrings("float", s.fields[0].type_name); try testing.expectEqualStrings("y", s.fields[1].name); try testing.expectEqualStrings("float", s.fields[1].type_name); try testing.expectEqualStrings("w", s.fields[2].name); try testing.expectEqualStrings("float", s.fields[2].type_name); try testing.expectEqualStrings("h", s.fields[3].name); try testing.expectEqualStrings("float", s.fields[3].type_name); } test "Mixed: single and multi-field" { const allocator = testing.allocator; const source = \\typedef struct Mixed { \\ int a; \\ int b, c; \\ float d; \\ float e, f, g; \\} Mixed; ; var scanner = patterns.Scanner.init(allocator, source); const decls = try scanner.scan(); defer { for (decls) |decl| { switch (decl) { .struct_decl => |s| { allocator.free(s.name); if (s.doc_comment) |doc| allocator.free(doc); for (s.fields) |field| { allocator.free(field.name); allocator.free(field.type_name); if (field.comment) |c| allocator.free(c); } allocator.free(s.fields); }, else => {}, } } allocator.free(decls); } try testing.expectEqual(@as(usize, 1), decls.len); const s = decls[0].struct_decl; try testing.expectEqual(@as(usize, 7), s.fields.len); const expected = [_]struct { name: []const u8, type: []const u8 }{ .{ .name = "a", .type = "int" }, .{ .name = "b", .type = "int" }, .{ .name = "c", .type = "int" }, .{ .name = "d", .type = "float" }, .{ .name = "e", .type = "float" }, .{ .name = "f", .type = "float" }, .{ .name = "g", .type = "float" }, }; for (expected, 0..) |exp, i| { try testing.expectEqualStrings(exp.name, s.fields[i].name); try testing.expectEqualStrings(exp.type, s.fields[i].type_name); } }