Skip to content

Commit a9102df

Browse files
committed
tests: various tests
Value conversion, table access and creation
1 parent 3157d5c commit a9102df

File tree

1 file changed

+63
-23
lines changed

1 file changed

+63
-23
lines changed

src/ziglua.zig

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ pub const Lua = struct {
776776
/// Does the equivalent of t[`i`] = v where t is the table at the given `index`
777777
/// and v is the value at the top of the stack
778778
/// Pops the value from the stack. Does not use __newindex metavalue
779-
pub fn rawSetI(lua: *Lua, index: i32, i: Integer) void {
779+
pub fn rawSetIndex(lua: *Lua, index: i32, i: Integer) void {
780780
c.lua_rawseti(lua.state, index, i);
781781
}
782782

@@ -847,7 +847,7 @@ pub const Lua = struct {
847847

848848
/// Does the equivalent to t[`n`] = v where t is the value at the given `index`
849849
/// and v is the value on the top of the stack. Pops the value from the stack
850-
pub fn setI(lua: *Lua, index: i32, n: Integer) void {
850+
pub fn setIndex(lua: *Lua, index: i32, n: Integer) void {
851851
c.lua_seti(lua.state, index, n);
852852
}
853853

@@ -899,11 +899,10 @@ pub const Lua = struct {
899899
}
900900

901901
/// Converts the zero-terminated string `str` to a number, pushes that number onto the stack,
902-
/// and returns the total size of the string (length + 1)
903-
pub fn stringToNumber(lua: *Lua, str: [:0]const u8) !usize {
902+
/// Returns an error if conversion failed
903+
pub fn stringToNumber(lua: *Lua, str: [:0]const u8) !void {
904904
const size = c.lua_stringtonumber(lua.state, str);
905905
if (size == 0) return Error.Fail;
906-
return size;
907906
}
908907

909908
/// Converts the Lua value at the given `index` into a boolean
@@ -913,9 +912,9 @@ pub const Lua = struct {
913912
}
914913

915914
/// Converts a value at the given `index` into a CFn
916-
/// Returns null if the value is not a CFn
917-
pub fn toCFunction(lua: *Lua, index: i32) ?CFn {
918-
return c.lua_tocfunction(lua.state, index);
915+
/// Returns an error if the value is not a CFn
916+
pub fn toCFunction(lua: *Lua, index: i32) !CFn {
917+
return c.lua_tocfunction(lua.state, index) orelse return Error.Fail;
919918
}
920919

921920
/// Marks the given index in the stack as a to-be-closed slot
@@ -976,10 +975,11 @@ pub const Lua = struct {
976975

977976
/// Converts the value at the given `index` to a Lua thread (wrapped with a `Lua` struct)
978977
/// The thread does _not_ contain an allocator because it is not the main thread and should therefore not be used with `deinit()`
979-
pub fn toThread(lua: *Lua, index: i32) ?Lua {
978+
/// Returns an error if the value is not a thread
979+
pub fn toThread(lua: *Lua, index: i32) !Lua {
980980
const thread = c.lua_tothread(lua.state, index);
981981
if (thread) |thread_ptr| return Lua{ .state = thread_ptr };
982-
return null;
982+
return Error.Fail;
983983
}
984984

985985
/// If the value at the given `index` is a full userdata, returns its memory-block address
@@ -1958,7 +1958,7 @@ test "compare" {
19581958
try expect(lua.compare(-2, -1, .lt));
19591959
}
19601960

1961-
test "type of" {
1961+
test "type of and getting values" {
19621962
var lua = try Lua.init(testing.allocator);
19631963
defer lua.deinit();
19641964

@@ -2017,6 +2017,13 @@ test "type of" {
20172017
try expect(lua.isBoolean(13));
20182018

20192019
try expectEqualStrings("hello world 10", lua.toString(12).?);
2020+
2021+
// the created thread should equal the main thread (but created thread has no allocator ref)
2022+
try expectEqual(lua.state, (try lua.toThread(7)).state);
2023+
try expectEqual(@as(CFn, wrap(add)), try lua.toCFunction(10));
2024+
2025+
try expectEqual(@as(Number, 0.1), try lua.toNumberX(6));
2026+
try expectEqual(@as(Integer, 1), try lua.toIntegerX(3));
20202027
}
20212028

20222029
test "typenames" {
@@ -2354,16 +2361,59 @@ test "table access" {
23542361
try lua.doString("b = a.bool");
23552362
try expectEqual(LuaType.boolean, lua.getGlobal("b"));
23562363
try expect(lua.toBoolean(-1));
2364+
2365+
// create array [1, 2, 3, 4, 5]
2366+
lua.createTable(0, 0);
2367+
var index: Integer = 1;
2368+
while (index <= 5) : (index += 1) {
2369+
lua.pushInteger(index);
2370+
lua.setIndex(-2, index);
2371+
}
2372+
try expectEqual(@as(Unsigned, 5), lua.rawLen(-1));
2373+
try expectEqual(@as(Integer, 5), lua.lenAux(-1));
2374+
2375+
// add a few more
2376+
while (index <= 10) : (index += 1) {
2377+
lua.pushInteger(index);
2378+
lua.rawSetIndex(-2, index);
2379+
}
2380+
try expectEqual(@as(Unsigned, 10), lua.rawLen(-1));
2381+
}
2382+
2383+
test "conversions" {
2384+
var lua = try Lua.init(testing.allocator);
2385+
defer lua.deinit();
2386+
2387+
// number conversion
2388+
var value: Integer = undefined;
2389+
try Lua.numberToInteger(3.14, &value);
2390+
try expectEqual(@as(Integer, 3), value);
2391+
try expectError(Error.Fail, Lua.numberToInteger(@intToFloat(Number, max_integer) + 10, &value));
2392+
2393+
// string conversion
2394+
try lua.stringToNumber("1");
2395+
try expect(lua.isInteger(-1));
2396+
try expectEqual(@as(Integer, 1), lua.toInteger(1));
2397+
2398+
try lua.stringToNumber(" 1.0 ");
2399+
try expect(lua.isNumber(-1));
2400+
try expectEqual(@as(Number, 1.0), lua.toNumber(-1));
2401+
2402+
try expectError(Error.Fail, lua.stringToNumber("a"));
2403+
try expectError(Error.Fail, lua.stringToNumber("1.a"));
2404+
try expectError(Error.Fail, lua.stringToNumber(""));
2405+
2406+
// index conversion
2407+
try expectEqual(@as(i32, 2), lua.absIndex(-1));
2408+
try expectEqual(@as(i32, 1), lua.absIndex(-2));
23572409
}
23582410

23592411
test "refs" {
23602412
// temporary test that includes a reference to all functions so
23612413
// they will be type-checked
23622414

23632415
// stdlib
2364-
_ = Lua.absIndex;
23652416
_ = Lua.closeSlot;
2366-
_ = Lua.createTable;
23672417
_ = Lua.dump;
23682418
_ = Lua.raiseError;
23692419
_ = Lua.getIUserValue;
@@ -2372,27 +2422,18 @@ test "refs" {
23722422
_ = Lua.newThread;
23732423
_ = Lua.newUserdataUV;
23742424
_ = Lua.next;
2375-
_ = Lua.numberToInteger;
23762425
_ = Lua.rawEqual;
23772426
_ = Lua.rawGetP;
2378-
_ = Lua.rawLen;
23792427
_ = Lua.rawSet;
2380-
_ = Lua.rawSetI;
23812428
_ = Lua.rawSetP;
23822429
_ = Lua.resetThread;
2383-
_ = Lua.setI;
23842430
_ = Lua.setIUserValue;
23852431
_ = Lua.setMetatable;
23862432
_ = Lua.setTable;
23872433
_ = Lua.setWarnF;
23882434
_ = Lua.status;
2389-
_ = Lua.stringToNumber;
2390-
_ = Lua.toCFunction;
23912435
_ = Lua.toClose;
2392-
_ = Lua.toIntegerX;
2393-
_ = Lua.toNumberX;
23942436
_ = Lua.toPointer;
2395-
_ = Lua.toThread;
23962437
_ = Lua.toUserdata;
23972438
_ = Lua.upvalueIndex;
23982439
_ = Lua.warning;
@@ -2436,7 +2477,6 @@ test "refs" {
24362477
_ = Lua.getMetatableAux;
24372478
_ = Lua.getSubtable;
24382479
_ = Lua.gSub;
2439-
_ = Lua.lenAux;
24402480
_ = Lua.loadBuffer;
24412481
_ = Lua.loadBufferX;
24422482
_ = Lua.loadFile;

0 commit comments

Comments
 (0)