Skip to content

Commit 1fc7ff6

Browse files
committed
fix: add test for checkNumeric
1 parent 436ceb7 commit 1fc7ff6

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/lib.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,21 +3363,22 @@ pub const Lua = opaque {
33633363
/// * Pushes: `0`
33643364
/// * Errors: `explained in text / on purpose`
33653365
pub fn checkNumeric(lua: *Lua, comptime T: type, arg: i32) T {
3366-
if (@typeInfo(T) == .int) return std.math.cast(T, lua.checkNumber(arg)) orelse {
3366+
if (comptime @typeInfo(T) != .int) return @floatCast(lua.checkNumber(arg));
3367+
return std.math.cast(T, lua.checkInteger(arg)) orelse {
33673368
const error_msg = comptime msg: {
33683369
var buf: [1024]u8 = undefined;
33693370
const info = @typeInfo(T).int;
33703371
const signedness = switch (info.signedness) {
33713372
.unsigned => "u",
33723373
.signed => "i",
33733374
};
3374-
break :msg std.fmt.bufPrintZ(&buf, "Integer argument doesn't fit inside {s}{d} range [{d}, {d}]", .{
3375+
const output = std.fmt.bufPrintZ(&buf, "integer argument doesn't fit inside {s}{d} range [{d}, {d}]", .{
33753376
signedness, info.bits, std.math.minInt(T), std.math.maxInt(T),
33763377
}) catch unreachable;
3378+
break :msg output[0..output.len :0].*;
33773379
};
3378-
lua.argError(arg, error_msg);
3380+
lua.argError(arg, &error_msg);
33793381
};
3380-
return @floatCast(lua.checkNumber(arg));
33813382
}
33823383

33833384
/// Checks whether the function argument `arg` is a number and returns this number cast to an i32

src/tests.zig

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,12 +3056,38 @@ test "error union for CFn" {
30563056
};
30573057
}
30583058

3059-
test "pushNumeric and toNumeric" {
3059+
test "checkNumeric and toNumeric" {
3060+
const error_msg = "bad argument #1 to '?' (integer argument doesn't fit inside u8 range [0, 255])";
3061+
30603062
const lua: *Lua = try .init(testing.allocator);
30613063
defer lua.deinit();
30623064

3063-
const num: u32 = 100;
3064-
lua.pushInteger(num);
3065-
const pull = lua.toNumeric(u32, lua.getTop());
3066-
try std.testing.expectEqual(num, pull);
3065+
lua.pushFunction(zlua.wrap(struct {
3066+
fn f(l: *Lua) i32 {
3067+
_ = l.checkNumeric(u8, 1);
3068+
return 1;
3069+
}
3070+
}.f));
3071+
const idx = lua.getTop();
3072+
3073+
lua.pushValue(idx);
3074+
lua.pushInteger(128);
3075+
try lua.protectedCall(.{
3076+
.args = 1,
3077+
.results = 1,
3078+
});
3079+
const val = lua.toNumeric(u8, lua.getTop());
3080+
try std.testing.expectEqual(128, val);
3081+
3082+
lua.pushValue(idx);
3083+
lua.pushInteger(256);
3084+
if (lua.protectedCall(.{
3085+
.args = 1,
3086+
.results = 0,
3087+
})) |_| {
3088+
return error.ExpectedError;
3089+
} else |_| {
3090+
const string = lua.toStringEx(lua.getTop());
3091+
try std.testing.expectEqualStrings(error_msg, string);
3092+
}
30673093
}

0 commit comments

Comments
 (0)