Esempio n. 1
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;
}
Esempio n. 2
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;
}
Esempio n. 3
0
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;
}
Esempio n. 4
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;
}