diff --git a/.gitignore b/.gitignore index 3f3389f41..82c6ff6ef 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,9 @@ example unittest *.swp +*.gcov + +covtest +unittest.gcda +unittest.gcno + diff --git a/Makefile b/Makefile index 51ccf6561..0518690af 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,22 @@ -all: example -# unittest +all: covtest example 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/ -test: example + +test: covtest + +runtest: example pkill example || exit 0 ./example & python test.py || exit 0 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 + diff --git a/routing.h b/routing.h index 62f7d8039..539c3aa85 100644 --- a/routing.h +++ b/routing.h @@ -9,6 +9,8 @@ #include "common.h" #include "http_response.h" +#include "http_request.h" +#include "utility.h" //TEST #include @@ -66,6 +68,8 @@ namespace flask { static_assert(black_magic::CallHelper>::value, "Handler type is mismatched with URL paramters"); + static_assert(!std::is_same::value, + "Handler function cannot have void return type; valid return types: string, int, flask::resposne"); handler_ = [f = std::move(f)]{ return response(f()); }; @@ -308,7 +312,7 @@ public: { ParamType::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) { diff --git a/unittest.cpp b/unittest.cpp index eaf4466a2..5bdd91185 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -1,10 +1,64 @@ #include +#include +#include "routing.h" using namespace std; +using namespace flask; + +struct Test { Test(); virtual void test() = 0; }; +vector 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<