Skip to content

Commit 087033a

Browse files
committed
tests: add more to table tests
1 parent f55b6d5 commit 087033a

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/ziglua.zig

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ pub const Lua = struct {
769769
}
770770

771771
/// Similar to `Lua.setTable()` but does a raw assignment (without metamethods)
772-
pub fn rawSet(lua: *Lua, index: i32) void {
772+
pub fn rawSetTable(lua: *Lua, index: i32) void {
773773
c.lua_rawset(lua.state, index);
774774
}
775775

@@ -1242,8 +1242,10 @@ pub const Lua = struct {
12421242
/// Pushes onto the stack the field `e` from the metatable of the object at index `obj`
12431243
/// and returns the type of the pushed value
12441244
/// TODO: possibly return an error if nil
1245-
pub fn getMetaField(lua: *Lua, obj: i32, field: [:0]const u8) LuaType {
1246-
return @intToEnum(LuaType, c.luaL_getmetafield(lua.state, obj, field));
1245+
pub fn getMetaField(lua: *Lua, obj: i32, field: [:0]const u8) !LuaType {
1246+
const val_type = @intToEnum(LuaType, c.luaL_getmetafield(lua.state, obj, field));
1247+
if (val_type == .nil) return Error.Fail;
1248+
return val_type;
12471249
}
12481250

12491251
/// Pushes onto the stack the metatable associated with the name `type_name` in the registry
@@ -2382,8 +2384,28 @@ test "table access" {
23822384
try expectEqual(LuaType.number, lua.rawGetTable(1));
23832385
try expectEqual(@as(Integer, 1234), lua.toInteger(-1));
23842386

2387+
// a.name = "ziglua"
2388+
_ = lua.pushString("name");
2389+
_ = lua.pushString("ziglua");
2390+
lua.setTable(1);
2391+
2392+
// a.lang = "zig"
2393+
_ = lua.pushString("lang");
2394+
_ = lua.pushString("zig");
2395+
lua.rawSetTable(1);
2396+
23852397
try expectError(Error.Fail, lua.getMetatable(1));
23862398

2399+
// create a metatable (it isn't a useful one)
2400+
lua.newTable();
2401+
lua.pushCFunction(wrap(add));
2402+
lua.setField(-2, "__len");
2403+
lua.setMetatable(1);
2404+
2405+
try lua.getMetatable(1);
2406+
_ = try lua.getMetaField(1, "__len");
2407+
try expectError(Error.Fail, lua.getMetaField(1, "__index"));
2408+
23872409
lua.pushBoolean(true);
23882410
lua.setField(1, "bool");
23892411

@@ -2499,11 +2521,8 @@ test "refs" {
24992521
_ = Lua.newUserdataUV;
25002522
_ = Lua.next;
25012523
_ = Lua.rawGetP;
2502-
_ = Lua.rawSet;
25032524
_ = Lua.rawSetP;
25042525
_ = Lua.setIUserValue;
2505-
_ = Lua.setMetatable;
2506-
_ = Lua.setTable;
25072526
_ = Lua.toClose;
25082527
_ = Lua.toPointer;
25092528
_ = Lua.toUserdata;
@@ -2544,7 +2563,6 @@ test "refs" {
25442563
_ = Lua.raiseErrorAux;
25452564
_ = Lua.exeResult;
25462565
_ = Lua.fileResult;
2547-
_ = Lua.getMetaField;
25482566
_ = Lua.getMetatableAux;
25492567
_ = Lua.getSubtable;
25502568
_ = Lua.gSub;

0 commit comments

Comments
 (0)