diff options
author | ipknHama <ipknhama@gmail.com> | 2014-04-15 05:11:37 +0900 |
---|---|---|
committer | ipknHama <ipknhama@gmail.com> | 2014-04-15 05:11:37 +0900 |
commit | f8ee6d4dadbc8f28095346b91d0a3ed3ed495c4f (patch) | |
tree | a4cd5acfd25f91fa17ee1c24f07c0fcd7c127eb7 /unittest.cpp | |
parent | b69a40c15b1f947acdb7f8dcfdd25a4fc3685339 (diff) | |
download | crow-f8ee6d4dadbc8f28095346b91d0a3ed3ed495c4f.tar.gz crow-f8ee6d4dadbc8f28095346b91d0a3ed3ed495c4f.zip |
add test for parameter tagging, response
Diffstat (limited to 'unittest.cpp')
-rw-r--r-- | unittest.cpp | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/unittest.cpp b/unittest.cpp index 5bdd911..5ebf15e 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -1,6 +1,7 @@ #include <iostream> #include <vector> #include "routing.h" +#include "utility.h" using namespace std; using namespace flask; @@ -9,8 +10,23 @@ vector<Test*> tests; Test::Test() { tests.push_back(this); } bool failed__ = false; -void fail() { failed__ = true; } +void error_print() +{ + cerr << endl; +} + +template <typename A, typename ...Args> +void error_print(A a, Args...args) +{ + cerr<<a; + error_print(args...); +} +template <typename ...Args> +void fail(Args...args) { error_print(args...);failed__ = true; } + +#define ASSERT_EQUAL(a, b) if (a != b) fail("Assert fail: expected ", (a), " actual " , b, ", " #a " == " #b ", at " __FILE__ ":",__LINE__) +#define ASSERT_NOTEQUAL(a, b) if (a != b) fail("Assert fail: not expected ", (a), ", " #a " != " #b ", at " __FILE__ ":",__LINE__) #define TEST(x) struct test##x:public Test{void test();}x##_; \ void test##x::test() @@ -18,6 +34,8 @@ TEST(Rule) { Rule r("/http/"); r.name("abc"); + + // empty handler - fail to validate try { r.validate(); @@ -29,14 +47,38 @@ TEST(Rule) int x = 0; + // registering handler r([&x]{x = 1;return "";}); + r.validate(); - if (x!=0) - fail(); + + // executing handler + ASSERT_EQUAL(0, x); r.handle(request(), routing_params()); - if (x == 0) - fail(); + ASSERT_EQUAL(1, x); +} + +TEST(ParameterTagging) +{ + ASSERT_EQUAL(1, black_magic::get_parameter_tag("<int>")); + ASSERT_EQUAL(2, black_magic::get_parameter_tag("<uint>")); + ASSERT_EQUAL(3, black_magic::get_parameter_tag("<float>")); + ASSERT_EQUAL(3, black_magic::get_parameter_tag("<double>")); + ASSERT_EQUAL(4, black_magic::get_parameter_tag("<str>")); + ASSERT_EQUAL(4, black_magic::get_parameter_tag("<string>")); + ASSERT_EQUAL(5, black_magic::get_parameter_tag("<path>")); + ASSERT_EQUAL(6*6+6+1, black_magic::get_parameter_tag("<int><int><int>")); + ASSERT_EQUAL(6*6+6+2, black_magic::get_parameter_tag("<uint><int><int>")); + ASSERT_EQUAL(6*6+6*3+2, black_magic::get_parameter_tag("<uint><double><int>")); + + // url definition parsed in compile time, build into *one number*, and given to template argument + static_assert(is_same<black_magic::S<uint64_t, double, int64_t>, black_magic::arguments<6*6+6*3+2>::type>::value, "tag to type container"); +} +TEST(response) +{ + ASSERT_EQUAL(100, response(100).code); + ASSERT_EQUAL(200, response("Hello there").code); } int testmain() |