aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--Makefile18
-rw-r--r--routing.h6
-rw-r--r--unittest.cpp62
4 files changed, 83 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
index 3f3389f..82c6ff6 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 51ccf65..0518690 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
+ 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 62f7d80..539c3aa 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 <iostream>
@@ -66,6 +68,8 @@ namespace flask
{
static_assert(black_magic::CallHelper<Func, black_magic::S<>>::value,
"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)]{
return response(f());
};
@@ -308,7 +312,7 @@ public:
{ 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)
{
diff --git a/unittest.cpp b/unittest.cpp
index eaf4466..5bdd911 100644
--- a/unittest.cpp
+++ b/unittest.cpp
@@ -1,10 +1,64 @@
#include <iostream>
+#include <vector>
+#include "routing.h"
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()
{
- //cout << strtol("+123999999999999999999", NULL, 10) <<endl;
- //cout <<errno <<endl;
- cout << strtol("+9223372036854775807", NULL, 10) <<endl;
- cout <<errno <<endl;
+ return testmain();
}