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 ++++++++++++++++--------------------------- server/src/widgetvalue.cc | 42 +++++++++++-- 2 files changed, 93 insertions(+), 94 deletions(-) (limited to 'server/src') 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*/ diff --git a/server/src/widgetvalue.cc b/server/src/widgetvalue.cc index 053eecc..3b68e8e 100644 --- a/server/src/widgetvalue.cc +++ b/server/src/widgetvalue.cc @@ -40,8 +40,10 @@ static bool getMapValue(Value &value, maps_t::iterator li = maps.begin(); while(li != maps.end()) { Map &_map = *li; - if(_map.attributes["name"] == map) { - luamap = _map.attributes["lua"]; + if(_map.attributes.find("name") != _map.attributes.end() && + _map.attributes["name"] == map) { + if(_map.attributes.find("lua") != _map.attributes.end()) + luamap = _map.attributes["lua"]; } li++; } @@ -145,8 +147,7 @@ bool getValue(Value &value, TEST_BEGIN; -pracro_debug_init(); -pracro_debug_parse("+all"); +debug_parse("+all"); time_t now = time(NULL); @@ -349,6 +350,39 @@ time_t now = time(NULL); TEST_EQUAL_STR(value.source, v.source, "Got the right source?"); } +{ + Conf::db_max_ttl = 1000; + Conf::pentominos_max_ttl = 500; + + Value value; + + attr_t attr; + attr["name"] = "foo"; + attr["value"] = "hello"; + attr["map"] = "bar"; + + maps_t maps; + Map m; + char tbuf[32]; sprintf(tbuf, "%ld", now - 1); + std::string val = "le valu"; + m.attributes["name"] = "bar"; + m.attributes["lua"] = "return '"+val+"', "+tbuf+", 'artefact'"; + maps.push_back(m); + LUAQueryMapper mapper; + + Values values; + Value v; + v.value = "world"; + v.source = "pracro"; + v.timestamp = now ; + values["foo"] = v; + + TEST_TRUE(getValue(value, attr, maps, mapper, values), "Got value?"); + TEST_EQUAL_STR(value.value, v.value, "Got the right value?"); + TEST_EQUAL_INT(value.timestamp, v.timestamp, "Got the right timestamp?"); + TEST_EQUAL_STR(value.source, v.source, "Got the right source?"); +} + TEST_END; #endif/*TEST_WIDGETVALUE*/ -- cgit v1.2.3