preparing unittest, assert on handler with void return type

This commit is contained in:
ipknHama 2014-04-15 03:02:48 +09:00
parent 86346f211f
commit b69a40c15b
4 changed files with 84 additions and 10 deletions

6
.gitignore vendored
View File

@ -24,3 +24,9 @@ example
unittest unittest
*.swp *.swp
*.gcov
covtest
unittest.gcda
unittest.gcno

View File

@ -1,12 +1,22 @@
all: example all: covtest example
# unittest
example: example.cpp flask.h http_server.h http_connection.h parser.h http_response.h routing.h common.h example: example.cpp flask.h http_server.h http_connection.h parser.h http_response.h routing.h common.h
g++ -Wall -g -O2 -std=c++11 -o example example.cpp http-parser/http_parser.c -pthread -lboost_system -lboost_thread -I http-parser/ g++ -Wall -g -O2 -std=c++11 -o example example.cpp http-parser/http_parser.c -pthread -lboost_system -lboost_thread -I http-parser/
test: example
test: covtest
runtest: example
pkill example || exit 0 pkill example || exit 0
./example & ./example &
python test.py || exit 0 python test.py || exit 0
pkill example pkill example
unittest: unittest.cpp routing.h
g++ -g -std=c++11 -o unittest unittest.cpp unittest: unittest.cpp routing.h
g++ -Wall -g -O2 -std=c++11 -o unittest unittest.cpp
./unittest
covtest: unittest.cpp routing.h
g++ -Wall -g -O2 -std=c++11 --coverage -o covtest unittest.cpp -fkeep-inline-functions -fno-default-inline -fno-inline-small-functions
./covtest
gcov -r unittest.cpp

View File

@ -9,6 +9,8 @@
#include "common.h" #include "common.h"
#include "http_response.h" #include "http_response.h"
#include "http_request.h"
#include "utility.h"
//TEST //TEST
#include <iostream> #include <iostream>
@ -66,6 +68,8 @@ namespace flask
{ {
static_assert(black_magic::CallHelper<Func, black_magic::S<>>::value, static_assert(black_magic::CallHelper<Func, black_magic::S<>>::value,
"Handler type is mismatched with URL paramters"); "Handler type is mismatched with URL paramters");
static_assert(!std::is_same<void, decltype(f())>::value,
"Handler function cannot have void return type; valid return types: string, int, flask::resposne");
handler_ = [f = std::move(f)]{ handler_ = [f = std::move(f)]{
return response(f()); return response(f());
}; };
@ -308,7 +312,7 @@ public:
{ ParamType::PATH, "<path>" }, { ParamType::PATH, "<path>" },
}; };
for(auto it = begin(paramTraits); it != end(paramTraits); ++it) for(auto it = std::begin(paramTraits); it != std::end(paramTraits); ++it)
{ {
if (url.compare(i, it->name.size(), it->name) == 0) if (url.compare(i, it->name.size(), it->name) == 0)
{ {

View File

@ -1,10 +1,64 @@
#include <iostream> #include <iostream>
#include <vector>
#include "routing.h"
using namespace std; using namespace std;
using namespace flask;
struct Test { Test(); virtual void test() = 0; };
vector<Test*> tests;
Test::Test() { tests.push_back(this); }
bool failed__ = false;
void fail() { failed__ = true; }
#define TEST(x) struct test##x:public Test{void test();}x##_; \
void test##x::test()
TEST(Rule)
{
Rule r("/http/");
r.name("abc");
try
{
r.validate();
fail();
}
catch(runtime_error& e)
{
}
int x = 0;
r([&x]{x = 1;return "";});
r.validate();
if (x!=0)
fail();
r.handle(request(), routing_params());
if (x == 0)
fail();
}
int testmain()
{
bool failed = false;
for(auto t:tests)
{
failed__ = false;
t->test();
if (failed__)
{
cerr << "F";
failed = true;
}
else
cerr << ".";
}
cerr<<endl;
return failed ? -1 : 0;
}
int main() int main()
{ {
//cout << strtol("+123999999999999999999", NULL, 10) <<endl; return testmain();
//cout <<errno <<endl;
cout << strtol("+9223372036854775807", NULL, 10) <<endl;
cout <<errno <<endl;
} }