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 TestLua::testLoadScripts() { LuaState lua; bool loaded = lua.loadString("name = \"Melli\"\n" "local age = 23\n" "function getName()\n" " return name\n" "end\n" "local function getAge()\n" " return age\n" "end\n" "function getNameAndAge()\n" " return getName(), getAge()\n" "end"); if (!loaded) { lua.logStack("ERR"); return false; } assert(lua.hasGlobalFunction("getName")); lua_acall(lua, 0, 1); am_equalsStr("Melli", lua_tostring(lua, -1)); lua.pop(1); assert(!lua.hasGlobalFunction("getAge")); assert(lua.hasGlobalFunction("getNameAndAge")); lua_acall(lua, 0, 2); am_equalsStr("Melli", lua_tostring(lua, -2)); am_equals(23, lua_tointeger(lua, -1)); loaded = lua.loadString("name = \"Alan\"\n" "local age = 24\n" "local function getAge()\n" " return age\n" "end\n"); if (!loaded) { lua.logStack("ERR"); return false; } assert(lua.hasGlobalFunction("getName")); lua_acall(lua, 0, 1); am_equalsStr("Alan", lua_tostring(lua, -1)); lua.pop(1); assert(!lua.hasGlobalFunction("getAge")); assert(lua.hasGlobalFunction("getNameAndAge")); lua_acall(lua, 0, 2); am_equalsStr("Alan", lua_tostring(lua, -2)); am_equals(23, lua_tointeger(lua, -1)); return true; }
/*bool TileType::loadFromDef(JsonValue value) { if (value.has("fullName", JV_STR)) { mFullName = value["fullName"].getCStr(); } return true; }*/ bool TileType::loadFromDef(LuaState &lua) { if (!lua_istable(lua, -1)) { return false; } if (lua.isTableString("fullName")) { mFullName = lua_tostring(lua, -1); lua.pop(1); } return true; }
bool TestLua::testSimple() { LuaState lua; assert(lua.getLua() != NULL); int ref = lua.newTable("table1"); lua.setTableValue("name", "Melli"); lua.setTableValue("age", 22); lua.pop(1); assert(!lua_istable(lua, -1)); lua.getTable("table1"); assert(lua_istable(lua, -1)); lua.pop(1); assert(!lua_istable(lua, -1)); lua.getTable(ref); assert(lua_istable(lua, -1)); const char *name = lua.getTableString("name"); assert(name != NULL); am_equalsStr("Melli", name); int age = 0; assert(lua.getTableInt("age", age)); am_equals(22, age); name = lua.getTableString("name"); assert(name != NULL); am_equalsStr("Melli", name); lua.pop(1); am_equals(0, lua_gettop(lua)); lua.close(); 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; }