94 lines
3.1 KiB
Zig
94 lines
3.1 KiB
Zig
const std = @import("std");
|
|
const testing = std.testing;
|
|
const patterns = @import("src/patterns.zig");
|
|
|
|
test "parse multi-field struct like SDL_Rect" {
|
|
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 struct_decl = decls[0].struct_decl;
|
|
try testing.expectEqualStrings("SDL_Rect", struct_decl.name);
|
|
|
|
// Should have 4 fields: x, y, w, h
|
|
try testing.expectEqual(@as(usize, 4), struct_decl.fields.len);
|
|
|
|
// Check first line: int x, y
|
|
try testing.expectEqualStrings("x", struct_decl.fields[0].name);
|
|
try testing.expectEqualStrings("int", struct_decl.fields[0].type_name);
|
|
|
|
try testing.expectEqualStrings("y", struct_decl.fields[1].name);
|
|
try testing.expectEqualStrings("int", struct_decl.fields[1].type_name);
|
|
|
|
// Check second line: int w, h
|
|
try testing.expectEqualStrings("w", struct_decl.fields[2].name);
|
|
try testing.expectEqualStrings("int", struct_decl.fields[2].type_name);
|
|
|
|
try testing.expectEqualStrings("h", struct_decl.fields[3].name);
|
|
try testing.expectEqualStrings("int", struct_decl.fields[3].type_name);
|
|
}
|
|
|
|
test "parse SDL_Point with multi-field" {
|
|
const allocator = testing.allocator;
|
|
|
|
const source =
|
|
\\typedef struct SDL_Point {
|
|
\\ int x, y;
|
|
\\} SDL_Point;
|
|
;
|
|
|
|
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 struct_decl = decls[0].struct_decl;
|
|
try testing.expectEqualStrings("SDL_Point", struct_decl.name);
|
|
try testing.expectEqual(@as(usize, 2), struct_decl.fields.len);
|
|
}
|