//*****************************************************************************
//  luaxtest.cpp
//
//  Test suite for luax.
//
//	File maintained by:
//		Young Tae Son (yson@ea.com)
//
//	(c) 2005 Electronic Arts Inc.
//*****************************************************************************

//- Includes ------------------------------------------------------------------
#include <Windows.h>
#include "luax/luax.h"
//#include "unit/unit.h"

#include "lua.h"
#include <algorithm>

#undef min

extern "C" {
    FILE * __cdecl LuaFOpen(const char *name, const char *mode)
    {
        return fopen(name, mode);
    }

    void *LuaRealloc(void* oldBlock, size_t oldSize, size_t size)
    {
        return realloc(oldBlock, size);
    }

    void LuaFree(void *block, size_t size)
    {
        free(block);
    }
}

class TestLuax //: public EATest::TestSuite
{
public:
	//---------------------------------------------------------------------
	virtual void Initialize()
	{
		//SuiteName("TestLuax");
		//EATEST_REGISTER("TestLuax", "Testing TestLuax", TestLuax, Test);
	}

	//---------------------------------------------------------------------
	void Setup()	
	{
	}

	//---------------------------------------------------------------------
	void Teardown()	
	{
	}

	//---------------------------------------------------------------------
	void Test(void)
	{
        const char* code = "function pp() dbgPrint(\"Hello, lua\"); end";

        Luax::Script script;
        script.LoadFile("test.lua");
        script.LoadBuffer(code, strlen(code), "mycode");
        script.CallFunction("pp");
        const char* msg = script.CallFunction("hoyhoy", "Burns", 99);
        OutputDebugString( msg );
	}
}
TestLuaxSingleton;

void dbgPrint(const char* msg)
{
    if( msg )
    {
        OutputDebugString(msg);
    }
    else
    {
        OutputDebugString("{ empty string }");
    }
    OutputDebugString("\n");
}
LUA_EXPORT(dbgPrint);

void LuaStateAsParam(lua_State* luaState, int bla)
{
    (void)luaState;
    (void)bla;
}
LUA_EXPORT(LuaStateAsParam);

int GetTableAsParam(Luax::LuaTable tbl, int stuff)
{
    int i = tbl["zz"];
    tbl[2] = "yy";
    return stuff * i;
}
LUA_EXPORT(GetTableAsParam);

int TestParams(int p1, int p2, int p3, int p4, int p5, int p6)
{
    return p1 + p2;
}
LUA_EXPORT(TestParams);

class Bah
{
public:
    Bah() { m = 1; }
    ~Bah() { m = 0; }
    void Set(int v) { m = v; }
    int Get() const { return m; }
private:
    int m;
};
LUA_EXPORT_CLASS(Bah);
LUA_EXPORT_MEMBER(Bah, Set);
LUA_EXPORT_MEMBER(Bah, Get);

namespace HoHo
{
    class HoyHoy
    {
    public:
        HoyHoy(int i) : m(i) {}  // no default constructor!
        int DoStuff() { return m * 2; }
    private:
        int m;
        HoyHoy& operator=(const HoyHoy&);
        HoyHoy( const HoyHoy& ); // copy constructor cannot be used!
    };
}

// this is impossible because this class don't have default and copy constructor
// LUA_EXPORT_CLASS(HoyHoy);

// but this is okay.  But you cannot do HoyHoy:new from Lua.
typedef HoHo::HoyHoy HoHo_HoyHoy;  // notice the lame typedef I had to make becuase the macro can't parse ::
LUA_EXPORT_CLASS_PTR(HoHo_HoyHoy);
LUA_EXPORT_MEMBER(HoHo_HoyHoy, DoStuff);

void DoTest()
{
	TestLuaxSingleton.Initialize();
	TestLuaxSingleton.Setup();
	TestLuaxSingleton.Test();
	TestLuaxSingleton.Teardown();
}
