Skip to content

Commit 2e5280a

Browse files
committed
Test structs and arrays as returns
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
1 parent e4e76ba commit 2e5280a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/tests.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ fn testMixedTypes(n: i32, s: []const u8, flag: bool) struct { i32, []const u8, b
123123
return .{ n + 1, s, !flag };
124124
}
125125

126+
fn testStructReturn(x: i32, y: i32) struct { x: i32, y: i32, sum: i32 } {
127+
return .{ .x = x, .y = y, .sum = x + y };
128+
}
129+
130+
fn testArrayReturn(n: i32) [3]i32 {
131+
return [_]i32{ n, n * 2, n * 3 };
132+
}
133+
126134
test "Zig function integration" {
127135
const lua = try Lua.init(&std.testing.allocator);
128136
defer lua.deinit();
@@ -179,6 +187,24 @@ test "Zig function integration" {
179187
try expect(std.mem.eql(u8, mixed_result[1], "test"));
180188
try expect(mixed_result[2]); // !false = true
181189

190+
// Test 10: Function returning struct (should create Lua table)
191+
try globals.set("structFunc", testStructReturn);
192+
const struct_x = try lua.eval("return structFunc(5, 7).x", .{}, i32);
193+
try expectEq(struct_x, 5);
194+
const struct_y = try lua.eval("return structFunc(5, 7).y", .{}, i32);
195+
try expectEq(struct_y, 7);
196+
const struct_sum = try lua.eval("return structFunc(5, 7).sum", .{}, i32);
197+
try expectEq(struct_sum, 12);
198+
199+
// Test 11: Function returning array (should create Lua table with integer indices)
200+
try globals.set("arrayFunc", testArrayReturn);
201+
const array_1 = try lua.eval("return arrayFunc(10)[1]", .{}, i32); // Lua is 1-indexed
202+
try expectEq(array_1, 10);
203+
const array_2 = try lua.eval("return arrayFunc(10)[2]", .{}, i32);
204+
try expectEq(array_2, 20);
205+
const array_3 = try lua.eval("return arrayFunc(10)[3]", .{}, i32);
206+
try expectEq(array_3, 30);
207+
182208
try expectEq(lua.top(), 0);
183209
}
184210

0 commit comments

Comments
 (0)