bool TestLuaTileSet::testSimple() { LuaState lua; assert(lua.loadString("TileSet = import(\"TileSet\")\n" "set = TileSet.new(\"testTileSet\")\n" "function getName()\n" " return set:name()\n" "end\n" "function getFullName()\n" " return set:full_name()\n" "end\n" "function setFullName(name)\n" " set:full_name(name)\n" "end\n" )); assert(lua.hasGlobalFunction("getName")); lua_acall(lua, 0, 1); am_equalsStr("testTileSet", lua_tostring(lua, -1)); lua.pop(1); assert(lua.hasGlobalFunction("setFullName")); lua.push("New Full Name"); lua_acall(lua, 1, 0); assert(lua.hasGlobalFunction("getFullName")); lua_acall(lua, 0, 1); am_equalsStr("New Full Name", lua_tostring(lua, -1)); lua.pop(1); return true; }
bool TestLuaTileSet::testTiles() { LuaState lua; assert(lua.loadString("TileSet = import(\"TileSet\")\n" "Tile = import(\"Tile\")\n" "set = TileSet.new(\"testTileSet\")\n" "function addTile(tile)\n" " set:add_tile(tile)\n" "end\n" "function removeTile(tile)\n" " set:remove_tile(tile)\n" "end\n" "function getTile(tileName)\n" " return set:tile(tileName)\n" "end\n" "function hasTile(tile)\n" " return set:has_tile(tile)\n" "end\n" )); assert(lua.hasGlobalFunction("hasTile")); lua.push("myTile"); lua_acall(lua, 1, 1); assert(!lua_toboolean(lua, -1)); lua.pop(1); Tile *tile = new Tile("myTile"); assert(lua.hasGlobalFunction("addTile")); wrapRefObject<Tile>(lua, tile); lua_acall(lua, 1, 0); assert(lua.hasGlobalFunction("hasTile")); lua.push("myTile"); lua_acall(lua, 1, 1); assert(lua_toboolean(lua, -1)); lua.pop(1); assert(lua.hasGlobalFunction("hasTile")); wrapRefObject<Tile>(lua, tile); lua_acall(lua, 1, 1); assert(lua_toboolean(lua, -1)); lua.pop(1); assert(lua.hasGlobalFunction("getTile")); lua.push("myTile"); lua_acall(lua, 1, 1); lua_getmetatable(lua, -1); Tile *get = castUData<Tile>(lua, 1); lua.pop(1); assert(tile == get); assert(lua.hasGlobalFunction("removeTile")); lua.push("myTile"); lua_acall(lua, 1, 0); assert(lua.hasGlobalFunction("hasTile")); lua.push("myTile"); lua_acall(lua, 1, 1); assert(!lua_toboolean(lua, -1)); lua.pop(1); return true; }
bool TestLua::testScripts() { LuaState lua; lua.loadString("function testFunc(x, y)\n" " return x * y\n" "end"); assert(lua.hasGlobalFunction("testFunc")); lua.push(4); lua.push(5); lua_acall(lua, 2, 1); int result = lua_tointeger(lua, -1); am_equals(20, result); lua.pop(1); am_equals(0, lua_gettop(lua)); assert(!lua.hasGlobalFunction("notafunc")); am_equals(0, lua_gettop(lua)); assert(lua.loadString("function notafunc()\n" " return \"hello there\"\n" "end")); assert(lua.hasGlobalFunction("testFunc")); lua.push(4); lua.push(5); lua_acall(lua, 2, 1); result = lua_tointeger(lua, -1); am_equals(20, result); lua.pop(1); am_equals(0, lua_gettop(lua)); assert(lua.hasGlobalFunction("notafunc")); am_equals(1, lua_gettop(lua)); lua_acall(lua, 0, 1); const char *callResult = lua_tostring(lua, -1); am_equalsStr(callResult, "hello there"); lua.pop(1); am_equals(0, lua_gettop(lua)); // You can redefine functions! // And load them at run-time! // Must find a way of making use of this. assert(lua.loadString("function notafunc()\n" " return \"how are you?\"\n" "end")); am_equals(0, lua_gettop(lua)); assert(lua.hasGlobalFunction("notafunc")); am_equals(1, lua_gettop(lua)); lua_acall(lua, 0, 1); callResult = lua_tostring(lua, -1); am_equalsStr(callResult, "how are you?"); lua.pop(1); am_equals(0, lua_gettop(lua)); lua.close(); return true; }