Example #1
0
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;
}
Example #2
0
	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;
	}
Example #3
0
bool TestLua::testWrapper()
{
    LuaState lua;

    Engine::getEngine()->setCurrentGame(new Game());
    Handle<Character> testCharacter(new Character());
    testCharacter->setGameId("testId");
    testCharacter->setName("Test Name");

    lua.loadString("Character = import(\"Character\")\n"
                   "name = \"none\"\n"
                   "function testFunc()\n"
                   "	obj = Character.new(\"testId\")\n"
                   "	name = obj:name()\n"
                   "	obj:name(name..\" changed\")\n"
                   "end");

    string name = lua.getGlobalString("name");
    am_equalsStr("none", name.c_str());

    assert(lua.hasGlobalFunction("testFunc"));
    lua_acall(lua, 0, 0);

    name = lua.getGlobalString("name");
    am_equalsStr("Test Name", name.c_str());

    am_equalsStr("Test Name changed", testCharacter->getName().c_str());

    lua.close();
    return true;
}
Example #4
0
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;
}
Example #5
0
	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;
	}
Example #6
0
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;
}
Example #7
0
	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;
	}
Example #8
0
	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;
	}