Lua tests: generation of each object

Test generation of every object, both via des.object and obj.new.
Expose FIRST_OBJECT and LAST_OBJECT numbers to lua.
Add lua nh.int_to_objname, a function to convert integer value to
object base name and class.
Allow creating new nethack lua object by specifying id and class.
This commit is contained in:
Pasi Kallinen
2026-01-26 17:49:45 +02:00
parent b5ca1a3ed8
commit 11bed1f55b
7 changed files with 122 additions and 16 deletions

View File

@@ -180,6 +180,27 @@ function test_object()
des.object({ name = "Random object" });
des.object({ class = "*", name = "Random stone" });
des.object({ id ="broadsword", name = "Dragonbane" })
for i = nhc.FIRST_OBJECT, nhc.LAST_OBJECT do
local oid, oclass = nh.int_to_objname(i);
if (oid ~= "") then
local o = des.object({ id = oid, class = oclass });
local o_t = o:totable();
-- crysknife reverts to worm tooth on the floor
if not(oid == "crysknife" and o_t.otyp_name == "worm tooth") then
if (o_t.otyp_name ~= oid) then
error("object name \"" .. o_t.otyp_name .. "\" created, wanted \"" .. oid .. "\"");
end
if (o_t.oclass ~= oclass) then
local str = string.format("object class \"%s\" created, wanted \"%s\" (%s)", o_t.oclass, oclass, oid);
error(str);
end
end
end
end
des.reset_level();
des.level_init();
end

View File

@@ -82,3 +82,22 @@ box:addcontent(o5);
local o6 = obj.new("statue");
o6:addcontent(obj.new("spellbook"));
-- generate one of each object, check the name and class matches
for i = nhc.FIRST_OBJECT, nhc.LAST_OBJECT do
local oid, oclass = nh.int_to_objname(i);
if (oid ~= "") then
local oi = obj.new({ id = oid, class = oclass });
local oi_t = oi:totable();
if (oi_t.otyp_name ~= oid) then
error("object name \"" .. oi_t.otyp_name .. "\" created, wanted \"" .. oid .. "\"");
end
if (oi_t.oclass ~= oclass) then
local str = string.format("object class \"%s\" created, wanted \"%s\" (%s)", oi_t.oclass, oclass, oid);
error(str);
end
end
end