Backlog/lib/sdl3/parser/mock_codegen_test.zig

181 lines
6.3 KiB
Zig

const std = @import("std");
const testing = std.testing;
const patterns = @import("patterns.zig");
const mock_codegen = @import("mock_codegen.zig");
test "mock generation - simple function" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const params = try allocator.dupe(patterns.ParamDecl, &[_]patterns.ParamDecl{
.{ .name = "debug_mode", .type_name = "bool" },
});
const func = patterns.FunctionDecl{
.name = "SDL_CreateGPUDevice",
.return_type = "SDL_GPUDevice*",
.params = params,
.doc_comment = null,
};
const decls = try allocator.dupe(patterns.Declaration, &[_]patterns.Declaration{
.{ .function_decl = func },
});
const output = try mock_codegen.MockCodeGen.generate(allocator, decls);
// Should contain function declaration
try testing.expect(std.mem.indexOf(u8, output, "SDL_CreateGPUDevice") != null);
// Should contain parameter
try testing.expect(std.mem.indexOf(u8, output, "bool debug_mode") != null);
// Should void the parameter
try testing.expect(std.mem.indexOf(u8, output, "(void)debug_mode") != null);
// Should return NULL for pointer
try testing.expect(std.mem.indexOf(u8, output, "return NULL") != null);
}
test "mock generation - void function" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const params = try allocator.dupe(patterns.ParamDecl, &[_]patterns.ParamDecl{
.{ .name = "device", .type_name = "SDL_GPUDevice*" },
});
const func = patterns.FunctionDecl{
.name = "SDL_DestroyGPUDevice",
.return_type = "void",
.params = params,
.doc_comment = null,
};
const decls = try allocator.dupe(patterns.Declaration, &[_]patterns.Declaration{
.{ .function_decl = func },
});
const output = try mock_codegen.MockCodeGen.generate(allocator, decls);
// Should not have return statement for void
try testing.expect(std.mem.indexOf(u8, output, "return") == null);
// Should void the parameter
try testing.expect(std.mem.indexOf(u8, output, "(void)device") != null);
}
test "mock generation - opaque type forward declaration" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const opaque_type = patterns.OpaqueType{
.name = "SDL_GPUDevice",
.doc_comment = null,
};
const decls = try allocator.dupe(patterns.Declaration, &[_]patterns.Declaration{
.{ .opaque_type = opaque_type },
});
const output = try mock_codegen.MockCodeGen.generate(allocator, decls);
// Should have typedef struct
try testing.expect(std.mem.indexOf(u8, output, "typedef struct SDL_GPUDevice SDL_GPUDevice") != null);
}
test "mock generation - header and includes" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const decls = try allocator.dupe(patterns.Declaration, &[_]patterns.Declaration{});
const output = try mock_codegen.MockCodeGen.generate(allocator, decls);
// Should have standard headers
try testing.expect(std.mem.indexOf(u8, output, "#include <stdint.h>") != null);
try testing.expect(std.mem.indexOf(u8, output, "#include <stdbool.h>") != null);
// Should have auto-generated comment
try testing.expect(std.mem.indexOf(u8, output, "Auto-generated") != null);
}
test "mock generation - function with multiple parameters" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const params = try allocator.dupe(patterns.ParamDecl, &[_]patterns.ParamDecl{
.{ .name = "render_pass", .type_name = "SDL_GPURenderPass*" },
.{ .name = "viewport", .type_name = "const SDL_GPUViewport*" },
});
const func = patterns.FunctionDecl{
.name = "SDL_SetGPUViewport",
.return_type = "void",
.params = params,
.doc_comment = null,
};
const decls = try allocator.dupe(patterns.Declaration, &[_]patterns.Declaration{
.{ .function_decl = func },
});
const output = try mock_codegen.MockCodeGen.generate(allocator, decls);
// Should have both parameters
try testing.expect(std.mem.indexOf(u8, output, "render_pass") != null);
try testing.expect(std.mem.indexOf(u8, output, "viewport") != null);
// Should void both
try testing.expect(std.mem.indexOf(u8, output, "(void)render_pass") != null);
try testing.expect(std.mem.indexOf(u8, output, "(void)viewport") != null);
}
test "mock generation - function returning bool" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const params = try allocator.dupe(patterns.ParamDecl, &[_]patterns.ParamDecl{
.{ .name = "format", .type_name = "SDL_GPUShaderFormat" },
});
const func = patterns.FunctionDecl{
.name = "SDL_GPUSupportsShaderFormats",
.return_type = "bool",
.params = params,
.doc_comment = null,
};
const decls = try allocator.dupe(patterns.Declaration, &[_]patterns.Declaration{
.{ .function_decl = func },
});
const output = try mock_codegen.MockCodeGen.generate(allocator, decls);
// Should return false for bool
try testing.expect(std.mem.indexOf(u8, output, "return false") != null);
}
test "mock generation - function returning int" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const params = try allocator.dupe(patterns.ParamDecl, &[_]patterns.ParamDecl{});
const func = patterns.FunctionDecl{
.name = "SDL_GetGPUDeviceCount",
.return_type = "int",
.params = params,
.doc_comment = null,
};
const decls = try allocator.dupe(patterns.Declaration, &[_]patterns.Declaration{
.{ .function_decl = func },
});
const output = try mock_codegen.MockCodeGen.generate(allocator, decls);
// Should return 0 for int
try testing.expect(std.mem.indexOf(u8, output, "return 0") != null);
}