Skip to content

Commit 119e964

Browse files
committed
tests: add auxlib check functions
1 parent a70d122 commit 119e964

File tree

2 files changed

+94
-14
lines changed

2 files changed

+94
-14
lines changed

src/tests.zig

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,25 +1128,105 @@ test "getstack" {
11281128
);
11291129
}
11301130

1131+
test "aux check functions" {
1132+
var lua = try Lua.init(testing.allocator);
1133+
defer lua.deinit();
1134+
1135+
const function = ziglua.wrap(struct {
1136+
fn inner(l: *Lua) i32 {
1137+
l.checkAny(1);
1138+
_ = l.checkInteger(2);
1139+
_ = l.checkBytes(3);
1140+
_ = l.checkNumber(4);
1141+
_ = l.checkString(5);
1142+
_ = l.checkType(6, .boolean);
1143+
return 0;
1144+
}
1145+
}.inner);
1146+
1147+
lua.pushFunction(function);
1148+
lua.protectedCall(0, 0, 0) catch {
1149+
try expectEqualStrings("bad argument #1 to '?' (value expected)", try lua.toBytes(-1));
1150+
lua.pop(-1);
1151+
};
1152+
1153+
lua.pushFunction(function);
1154+
lua.pushNil();
1155+
lua.protectedCall(1, 0, 0) catch {
1156+
try expectEqualStrings("bad argument #2 to '?' (number expected, got no value)", try lua.toBytes(-1));
1157+
lua.pop(-1);
1158+
};
1159+
1160+
lua.pushFunction(function);
1161+
lua.pushNil();
1162+
lua.pushInteger(3);
1163+
lua.protectedCall(2, 0, 0) catch {
1164+
try expectEqualStrings("bad argument #3 to '?' (string expected, got no value)", try lua.toBytes(-1));
1165+
lua.pop(-1);
1166+
};
1167+
1168+
lua.pushFunction(function);
1169+
lua.pushNil();
1170+
lua.pushInteger(3);
1171+
_ = lua.pushBytes("hello world");
1172+
lua.protectedCall(3, 0, 0) catch {
1173+
try expectEqualStrings("bad argument #4 to '?' (number expected, got no value)", try lua.toBytes(-1));
1174+
lua.pop(-1);
1175+
};
1176+
1177+
lua.pushFunction(function);
1178+
lua.pushNil();
1179+
lua.pushInteger(3);
1180+
_ = lua.pushBytes("hello world");
1181+
lua.pushNumber(4);
1182+
lua.protectedCall(4, 0, 0) catch {
1183+
try expectEqualStrings("bad argument #5 to '?' (string expected, got no value)", try lua.toBytes(-1));
1184+
lua.pop(-1);
1185+
};
1186+
1187+
lua.pushFunction(function);
1188+
lua.pushNil();
1189+
lua.pushInteger(3);
1190+
_ = lua.pushBytes("hello world");
1191+
lua.pushNumber(4);
1192+
_ = lua.pushString("hello world");
1193+
lua.protectedCall(5, 0, 0) catch {
1194+
try expectEqualStrings("bad argument #6 to '?' (boolean expected, got no value)", try lua.toBytes(-1));
1195+
lua.pop(-1);
1196+
};
1197+
1198+
lua.pushFunction(function);
1199+
lua.pushNil();
1200+
lua.pushInteger(3);
1201+
_ = lua.pushBytes("hello world");
1202+
lua.pushNumber(4);
1203+
_ = lua.pushString("hello world");
1204+
lua.pushBoolean(true);
1205+
lua.protectedCall(6, 0, 0) catch {
1206+
try expectEqualStrings("bad argument #6 to '?' (boolean expected, got no value)", try lua.toBytes(-1));
1207+
lua.pop(-1);
1208+
};
1209+
1210+
lua.pushFunction(function);
1211+
lua.pushNil();
1212+
lua.pushInteger(3);
1213+
_ = lua.pushBytes("hello world");
1214+
lua.pushNumber(4);
1215+
_ = lua.pushString("hello world");
1216+
lua.pushBoolean(true);
1217+
try lua.protectedCall(6, 0, 0);
1218+
}
1219+
11311220
test "refs" {
11321221
// temporary test that includes a reference to all functions so
11331222
// they will be type-checked
11341223

1135-
// debug
1136-
_ = Lua.getStack;
1137-
11381224
// auxlib
11391225
_ = Lua.argCheck;
11401226
_ = Lua.argError;
11411227
_ = Lua.argExpected;
11421228
_ = Lua.callMeta;
1143-
_ = Lua.checkAny;
1144-
_ = Lua.checkInteger;
1145-
_ = Lua.checkLString;
1146-
_ = Lua.checkNumber;
11471229
_ = Lua.checkOption;
1148-
_ = Lua.checkString;
1149-
_ = Lua.checkType;
11501230
_ = Lua.checkUserdata;
11511231
_ = Lua.checkVersion;
11521232
_ = Lua.doFile;

src/ziglua.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,12 +1280,12 @@ pub const Lua = struct {
12801280
return c.luaL_checkinteger(lua.state, arg);
12811281
}
12821282

1283-
/// Checks whether the function argument `arg` is a string and returns the string
1284-
pub fn checkLString(lua: *Lua, arg: i32) ?[]const u8 {
1283+
/// Checks whether the function argument `arg` is a slice of bytes and returns the slice
1284+
pub fn checkBytes(lua: *Lua, arg: i32) [:0]const u8 {
12851285
var length: usize = 0;
1286-
if (c.luaL_checklstring(lua.state, arg, @ptrCast([*c]usize, &length))) |str| {
1287-
return str[0..length];
1288-
} else return null;
1286+
const str = c.luaL_checklstring(lua.state, arg, @ptrCast([*c]usize, &length));
1287+
// luaL_checklstring never returns null (throws lua error)
1288+
return str.?[0..length :0];
12891289
}
12901290

12911291
/// Checks whether the function argument `arg` is a number and returns the number

0 commit comments

Comments
 (0)