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 TestLua::testInheritance() { LuaState lua; lua.registerWrapper("Base", test::Base_register, test::Base::LUA_ID); lua.registerWrapper("Child", test::Child_register, test::Child::LUA_ID); int loadResult = lua.loadString("Base, Child = import(\"Base\", \"Child\")\n" "base = Base.new()\n" "base:set_name(\"Melli\")\n" "child = Child.new()\n" "child:set_age(23)\n" "child:set_name(\"Alan\")\n" "function getBase()\n" " return base\n" "end\n" "function getChild()\n" " return child\n" "end\n" ); if (!loadResult) { lua.logStack("ERR LOADING"); return false; } assert(lua.hasGlobalFunction("getBase")); lua_acall(lua, 0, 1); test::Base *base = castUData<test::Base>(lua, -1); assert(base != NULL); am_equalsStr("Melli", base->getName()); assert(lua.hasGlobalFunction("getChild")); lua_acall(lua, 0, 1); test::Child *child = castUData<test::Child>(lua, -1); assert(child != NULL); am_equals(23, child->getAge()); lua.close(); return true; }
bool TestLuaMap::testSimple() { LuaState lua; int loadResult = lua.loadString("Map = import(\"Map\")\n" "Engine = import(\"Engine\")\n" "local result = loadfile(\"data\\\\tileTypes2.lua\")\n" "if (result == nil) then\n" " am_log(\"Error loading\")\n" "else\n" " am_log(\"No error\")\n" "end\n" "result()\n" "map = Map.new(\"testMap\", 6, 7)\n" "map:full_name(\"The Region of Test\")\n" "Engine.clear_using_tile_set()\n" "Engine.using_tile_set(\"nature\")\n" "map:tiles({\n" " \"brick:0\", \"brick:2\", \"grass:0\", \"grass:2\", \"grass:3\", \"dirt:3\",\n" " \"brick:1\", \"brick:0\", \"grass:1\", \"dirt:0\", \"dirt:2\", \"grass:1\",\n" " \"brick:0\", \"brick:2\", \"grass:2\", \"dirt:3\", \"dirt:0\", \"grass:3\",\n" " \"brick:2\", \"brick:1\", \"grass:3\", \"dirt:2\", \"dirt:1\", \"swamp\",\n" " \"brick:1\", \"brick:0\", \"grass:1\", \"grass:0\", \"swamp\", \"water\",\n" " \"brick:2\", \"brick:2\", \"grass:2\", \"grass:3\", \"water\", \"water\",\n" " \"brick:2\", \"brick:2\", \"grass:0\", \"swamp\", \"water\", \"water\",\n" " \"brick:2\", \"brick:2\", \"grass:3\", \"swamp\", \"water\", \"water\"\n" "})\n" "function getMap()\n" " return map\n" "end\n" ); if (!loadResult) { lua.logStack("LOAD ERR"); } assert(loadResult); assert(lua.hasGlobalFunction("getMap")); lua_acall(lua, 0, 1); Map *map = castUData<Map>(lua, -1); assert(map != NULL); am_equals(6, map->getMapWidth()); am_equals(7, map->getMapHeight()); am_equalsStr("The Region of Test", map->getFullName().c_str()); Tile *tile = map->getTile(0, 0); assert(tile); am_equalsStr("brick", tile->getName().c_str()); am_equals(0, map->getTileInstance(0, 0)->getTileFrame()); tile = map->getTile(3, 0); assert(tile); am_equalsStr("grass", tile->getName().c_str()); am_equals(2, map->getTileInstance(3, 0)->getTileFrame()); tile = map->getTile(4, 4); assert(tile); am_equalsStr("swamp", tile->getName().c_str()); am_equals(0, map->getTileInstance(4, 4)->getTileFrame()); tile = map->getTile(5, 6); assert(tile); am_equalsStr("water", tile->getName().c_str()); am_equals(0, map->getTileInstance(5, 6)->getTileFrame()); return true; }
bool TestLuaQuest::testSimple() { LuaState lua; Engine *prevEng = Engine::getEngine(); Engine *eng = new Engine(); Engine::setEngine(eng); Handle<Game> game(new Game(eng)); eng->setCurrentGame(game); Handle<Character> main(new Character()); main->setGameId("testMainChar"); game->setMainCharacter(main); int loadResult = lua.loadString("Quest, Character, Engine, Game, CoinPurse = import(" "\"Quest\", \"Character\", \"Engine\", \"Game\", \"CoinPurse\")\n" "local quest = Quest.new(\"testQuest\")\n" "Quest.add_quest(quest)\n" "local game = Engine.game()\n" "local main = game:main()\n" "local questStarted = false\n" "function startQuest(event)\n" " main:on(\"talk\", finishQuest)\n" " main:off(\"talk\", startQuest)\n" "end\n" "function finishQuest(event)\n" " main:off(\"talk\", finishQuest)\n" " main:add_experience(1000)\n" " main:coin_purse():add_coin(50)\n" " quest:set_complete(true)\n" "end\n" "main:on(\"talk\", startQuest)\n" ); if (!loadResult) { lua.logStack("LOAD ERR"); } assert(loadResult); lua_Debug ar; lua_getstack(lua, 1, &ar); Quest *quest = Engine::getGame()->getQuest("testQuest"); assert(!quest->isCompleted()); am_equals(0, main->getExperience()); am_equals(0, main->getLevel()); am_equals(0u, main->getCoinPurse()->getCoin()); Handle<Event> startEvent(new Event("talk")); main->fireEvent<Event>(startEvent); main->fireEvent<Event>(startEvent); am_equals(1000, main->getExperience()); am_equals(1, main->getLevel()); am_equals(50u, main->getCoinPurse()->getCoin()); Engine::setEngine(prevEng); delete eng; return true; }