-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHarness.cpp
More file actions
72 lines (66 loc) · 1.46 KB
/
TestHarness.cpp
File metadata and controls
72 lines (66 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <apollo/Scripting/Scripting.h>
#include <apollo/Utilities/TestHarness.h>
extern "C"
{
#include "lua.h"
}
namespace TestHarness
{
LuaScript* script = NULL;
void Init ()
{
if (!script)
{
script = new LuaScript ( "Tests/Tests" );
}
}
bool InvokeTest ( const std::string& testName, const std::vector<std::string>& testParameters )
{
Init();
lua_State* L = script->RawState();
lua_getglobal(L, ("test_" + testName).c_str());
if (!lua_isnoneornil(L, -1))
{
if (!lua_isfunction(L, -1))
{
printf("Test %s FAILED: test is not a function!\n", testName.c_str());
return false;
}
int nargs = 0;
for (std::vector<std::string>::const_iterator iter = testParameters.begin(); iter != testParameters.end(); iter++)
{
lua_pushlstring(L, iter->data(), iter->length());
nargs++;
}
int rc = lua_pcall(L, nargs, 0, 0);
if (rc == 0)
{
printf("Test %s PASSED!\n", testName.c_str());
return true;
}
else if (rc == LUA_ERRRUN)
{
printf("Test %s FAILED: %s\n", testName.c_str(), lua_tostring(L, -1));
lua_pop(L, 1);
return false;
}
else if (rc == LUA_ERRMEM)
{
printf("Test %s FAILED: out of memory!\n", testName.c_str());
return false;
}
else if (rc == LUA_ERRERR)
{
printf("Test %s FAILED: internal inconsistency (existence of error function)!\n", testName.c_str());
return false;
}
}
else
{
printf("Test %s FAILED: test is undefined!\n", testName.c_str());
return false;
}
return false;
}
}