Skip to content

Commit ec9799b

Browse files
committed
tests: add gsub and loadbuffer tests
1 parent 6fe8c49 commit ec9799b

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/tests.zig

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,24 @@ test "checkOption" {
13251325
lua.pop(1);
13261326
}
13271327

1328+
test "gSub" {
1329+
var lua = try Lua.init(testing.allocator);
1330+
defer lua.deinit();
1331+
1332+
_ = lua.gSub("-gity -!", "-", "zig");
1333+
try expectEqualStrings("ziggity zig!", try lua.toBytes(-1));
1334+
}
1335+
1336+
test "loadBuffer" {
1337+
var lua = try Lua.init(testing.allocator);
1338+
defer lua.deinit();
1339+
1340+
_ = try lua.loadBuffer("global = 10", "chunkname");
1341+
try lua.protectedCall(0, ziglua.mult_return, 0);
1342+
try lua.getGlobal("global");
1343+
try expectEqual(@as(Integer, 10), try lua.toInteger(-1));
1344+
}
1345+
13281346
test "refs" {
13291347
// temporary test that includes a reference to all functions so
13301348
// they will be type-checked
@@ -1338,9 +1356,6 @@ test "refs" {
13381356
_ = Lua.exeResult;
13391357
_ = Lua.fileResult;
13401358
_ = Lua.getSubtable;
1341-
_ = Lua.gSub;
1342-
_ = Lua.loadBuffer;
1343-
_ = Lua.loadBufferX;
13441359
_ = Lua.loadFile;
13451360
_ = Lua.loadFileX;
13461361
_ = Lua.testUserdata;

src/ziglua.zig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,24 +1456,24 @@ pub const Lua = struct {
14561456
}
14571457

14581458
/// The same as `Lua.loadBufferX` with `mode` set to null
1459-
pub fn loadBuffer(lua: *Lua, buf: [:0]const u8, size: usize, name: [:0]const u8) i32 {
1460-
// translate-c failure
1461-
return c.luaL_loadbufferx(lua.state, buf, size, name, null);
1459+
pub fn loadBuffer(lua: *Lua, buf: []const u8, name: [:0]const u8) !void {
1460+
try lua.loadBufferX(buf, name, .binary_text);
14621461
}
14631462

14641463
/// Loads a buffer as a Lua chunk
14651464
/// TODO: There isn't a real reason to allow null mofe with loadBuffer
1466-
pub fn loadBufferX(lua: *Lua, buf: [:0]const u8, size: usize, name: [:0]const u8, mode: ?Mode) i32 {
1467-
const mode_str = blk: {
1468-
if (mode == null) break :blk "bt";
1469-
1470-
break :blk switch (mode.?) {
1471-
.binary => "b",
1472-
.text => "t",
1473-
.binary_text => "bt",
1474-
};
1465+
pub fn loadBufferX(lua: *Lua, buf: []const u8, name: [:0]const u8, mode: Mode) !void {
1466+
const mode_str = switch (mode) {
1467+
.binary => "b",
1468+
.text => "t",
1469+
.binary_text => "bt",
14751470
};
1476-
return c.luaL_loadbufferx(lua.state, buf, size, name, mode_str);
1471+
switch (c.luaL_loadbufferx(lua.state, buf.ptr, buf.len, name, mode_str)) {
1472+
StatusCode.ok => return,
1473+
StatusCode.err_syntax => return error.Syntax,
1474+
StatusCode.err_memory => return error.Memory,
1475+
else => unreachable,
1476+
}
14771477
}
14781478

14791479
/// Equivalent to `Lua.loadFileX()` with mode equal to null

0 commit comments

Comments
 (0)