From 04f275fea9186a75836b589022a9fa410aea7b02 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 14 Feb 2011 14:09:04 +0000 Subject: Added gcov (coverage measurement) in unittests. --- server/src/luaquerymapper.cc | 145 ++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 90 deletions(-) (limited to 'server/src/luaquerymapper.cc') diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc index aefbabf..fcdce85 100644 --- a/server/src/luaquerymapper.cc +++ b/server/src/luaquerymapper.cc @@ -204,101 +204,66 @@ std::string LUAQueryMapper::automap(const std::string &name) #ifdef TEST_LUAQUERYMAPPER +//deps: exception.cc log.cc debug.cc +//cflags: -I.. ${LUA_CFLAGS} +//libs:${LUA_LIBS} +#include -int main() -{ - QueryResult res; +TEST_BEGIN; - time_t now = time(NULL); +QueryResult res; - res.groups["test"].timestamp = now; - res.groups["test"].source = "test app"; - res.groups["test"].values["somevalue"] = "hello world"; - res.groups["test"].values["pi"] = "3.1416"; - - printf("%s\n", loadresultstring(res).c_str()); - - LUAQueryMapper mapper; - mapper.addQueryResult(res); - - // Test simple value forwarding - std::string luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source"; - Value value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - - // Do some calculations - luamap = "return 2 * tonumber(test.pi.value), test.pi.timestamp, test.pi.source"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "6.2832" || value.timestamp != now || value.source != "test app") - return 1; - - // Attempt to access nonexisting value (should throw an exception) - try { - luamap = "return test.somevalue2.value, test.somevalue2.timestamp, test.somevalue2.source"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto onandon; - } - return 1; - onandon: - - // Attempt to access nonexisting group (should throw an exception) - try { - luamap = "return test2.somevalue.value, test2.somevalue.timestamp, test2.somevalue.source"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto stillonandon; - } - return 1; - stillonandon: +time_t now = time(NULL); + +res.groups["test"].timestamp = now; +res.groups["test"].source = "test app"; +res.groups["test"].values["somevalue"] = "hello world"; +res.groups["test"].values["pi"] = "3.1416"; - // Switch order of return vars (should throw an exception) - try { - luamap = "return test.somevalue.source, test.somevalue.value, test.somevalue.timestamp"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto onandonagain; - } - return 1; - onandonagain: - - // Syntax error (should throw an exception) - try { - luamap = "this(is{] not() - a != legal lua program!]"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto stillonandonagain; - } - return 1; - stillonandonagain: +//printf("%s\n", loadresultstring(res).c_str()); - // And finally test if we haven't broken enything while being hostile to the lua engine... - luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; +LUAQueryMapper mapper; +mapper.addQueryResult(res); - return 0; -} +// Test simple value forwarding +std::string luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source"; +Value value = mapper.map(luamap); + +TEST_EQUAL_STR(value.value, "hello world", "Test value"); +TEST_EQUAL_INT(value.timestamp, now, "Test timestamp"); +TEST_EQUAL_STR(value.source, "test app", "Test source"); + +// Do some calculations +luamap = "return 2 * tonumber(test.pi.value), test.pi.timestamp, test.pi.source"; +value = mapper.map(luamap); + +TEST_EQUAL_STR(value.value, "6.2832", "Test value"); +TEST_EQUAL_INT(value.timestamp, now, "Test timestamp"); +TEST_EQUAL_STR(value.source, "test app", "Test source"); + +// Attempt to access nonexisting value (should throw an exception) +luamap = "return test.somevalue2.value, test.somevalue2.timestamp, test.somevalue2.source"; +TEST_EXCEPTION(mapper.map(luamap), Exception, "Throw exception"); + +// Attempt to access nonexisting group (should throw an exception) +luamap = "return test2.somevalue.value, test2.somevalue.timestamp, test2.somevalue.source"; +TEST_EXCEPTION(mapper.map(luamap), Exception, "Throw exception"); + +// Switch order of return vars (should throw an exception) +luamap = "return test.somevalue.source, test.somevalue.value, test.somevalue.timestamp"; +TEST_EXCEPTION(mapper.map(luamap), Exception, "Throw exception"); + +// Syntax error (should throw an exception) +luamap = "this(is{] not() - a != legal lua program!]"; +TEST_EXCEPTION(mapper.map(luamap), Exception, "Throw exception"); + +// And finally test if we haven't broken enything while being hostile to the lua engine... +luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source"; +TEST_NOEXCEPTION(mapper.map(luamap), "Throw no exception"); +TEST_EQUAL_STR(value.value, "6.2832", "Test value"); +TEST_EQUAL_INT(value.timestamp, now, "Test timestamp"); +TEST_EQUAL_STR(value.source, "test app", "Test source"); + +TEST_END; #endif/*TEST_LUAQUERYMAPPER*/ -- cgit v1.2.3