From e70380e003ae28822a6b903193c9a699cc02c405 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Thu, 7 Aug 2014 04:46:28 +0900 Subject: cmake update, added template test --- tests/CMakeLists.txt | 23 ++++++++++++++--------- tests/template/CMakeLists.txt | 29 +++++++++++++++++++++++++++++ tests/template/mustachetest.cc | 33 --------------------------------- tests/template/mustachetest.cpp | 33 +++++++++++++++++++++++++++++++++ tests/template/test.py | 15 ++++++++------- tests/test.py | 28 ---------------------------- 6 files changed, 84 insertions(+), 77 deletions(-) create mode 100644 tests/template/CMakeLists.txt delete mode 100644 tests/template/mustachetest.cc create mode 100644 tests/template/mustachetest.cpp delete mode 100644 tests/test.py (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 45943ed..d35925c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,16 +2,21 @@ cmake_minimum_required(VERSION 2.8) project (crow_test) -set(PROJECT_INCLUDE_DIR -${PROJECT_SOURCE_DIR}/include -${PROJECT_SOURCE_DIR}/http-parser -) - set(TEST_SRCS unittest.cpp ) -add_executable(test ${TEST_SRCS}) -target_link_libraries(test crow) -target_link_libraries( test ${Boost_LIBRARIES} ) -set_target_properties(test PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y") +add_executable(unittest ${TEST_SRCS}) +target_link_libraries(unittest crow) +target_link_libraries(unittest ${Boost_LIBRARIES} ) + +set_target_properties(unittest PROPERTIES COMPILE_FLAGS "--coverage -fprofile-arcs -ftest-coverage") + +target_link_libraries(unittest gcov) + +add_subdirectory(template) +#CXXFLAGS="-g -O0 -Wall -W -Wshadow -Wunused-variable \ +#Wunused-parameter -Wunused-function -Wunused -Wno-system-headers \ +#-Wno-deprecated -Woverloaded-virtual -Wwrite-strings -fprofile-arcs -ftest-coverage" +#CFLAGS="-g -O0 -Wall -W -fprofile-arcs -ftest-coverage" +#LDFLAGS="-fprofile-arcs -ftest-coverage" diff --git a/tests/template/CMakeLists.txt b/tests/template/CMakeLists.txt new file mode 100644 index 0000000..1d4ec30 --- /dev/null +++ b/tests/template/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required(VERSION 2.8) +project (template_test) + + +set(PROJECT_INCLUDE_DIR +${PROJECT_SOURCE_DIR}/include +) + +set(TEST_SRCS +mustachetest.cpp +) + +add_executable(mustachetest ${TEST_SRCS}) +#target_link_libraries(unittest crow) +#target_link_libraries(unittest ${Boost_LIBRARIES} ) +set_target_properties(mustachetest PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y") + +message(${PROJECT_SOURCE_DIR}) +message(${CMAKE_CURRENT_BINARY_DIR}) +file(COPY DIRECTORY . DESTINATION ${CMAKE_CURRENT_BINARY_DIR} + FILES_MATCHING + PATTERN "*.json") + +add_custom_command(OUTPUT test.py + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/test.py ${CMAKE_CURRENT_BINARY_DIR}/test.py + DEPENDS ${PROJECT_SOURCE_DIR}/test.py + ) +add_custom_target(template_test_copy ALL DEPENDS test.py) diff --git a/tests/template/mustachetest.cc b/tests/template/mustachetest.cc deleted file mode 100644 index c4ac2c9..0000000 --- a/tests/template/mustachetest.cc +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -#include -#include "../mustache.h" -#include "../json.h" -using namespace std; -using namespace crow; -using namespace crow::mustache; - -string read_all(const string& filename) -{ - ifstream is(filename); - return {istreambuf_iterator(is), istreambuf_iterator()}; -} - -int main() -{ - auto data = json::load(read_all("data")); - auto templ = compile(read_all("template")); - auto partials = json::load(read_all("partials")); - set_loader([&](std::string name)->std::string - { - if (partials.count(name)) - { - return partials[name].s(); - } - return ""; - }); - context ctx(data); - cout << templ.render(ctx); - return 0; -} diff --git a/tests/template/mustachetest.cpp b/tests/template/mustachetest.cpp new file mode 100644 index 0000000..1caf537 --- /dev/null +++ b/tests/template/mustachetest.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include "mustache.h" +#include "json.h" +using namespace std; +using namespace crow; +using namespace crow::mustache; + +string read_all(const string& filename) +{ + ifstream is(filename); + return {istreambuf_iterator(is), istreambuf_iterator()}; +} + +int main() +{ + auto data = json::load(read_all("data")); + auto templ = compile(read_all("template")); + auto partials = json::load(read_all("partials")); + set_loader([&](std::string name)->std::string + { + if (partials.count(name)) + { + return partials[name].s(); + } + return ""; + }); + context ctx(data); + cout << templ.render(ctx); + return 0; +} diff --git a/tests/template/test.py b/tests/template/test.py index 22fcca0..57bcaf8 100755 --- a/tests/template/test.py +++ b/tests/template/test.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import glob import json import os @@ -14,15 +15,15 @@ for testfile in glob.glob("*.json"): open('partials', 'w').write(json.dumps(test["partials"])) else: open('partials', 'w').write("{}") - ret = subprocess.check_output("./mustachetest") - print testfile, test["name"] + ret = subprocess.check_output("./mustachetest").decode('utf8') + print(testfile, test["name"]) if ret != test["expected"]: if 'partials' in test: - print 'partials:', json.dumps(test["partials"]) - print json.dumps(test["data"]) - print test["template"] - print 'Expected:',repr(test["expected"]) - print 'Actual:',repr(ret) + print('partials:', json.dumps(test["partials"])) + print(json.dumps(test["data"])) + print(test["template"]) + print('Expected:',repr(test["expected"])) + print('Actual:',repr(ret)) assert ret == test["expected"] os.unlink('data') os.unlink('template') diff --git a/tests/test.py b/tests/test.py deleted file mode 100644 index e2bf651..0000000 --- a/tests/test.py +++ /dev/null @@ -1,28 +0,0 @@ -import urllib -assert "Hello World!" == urllib.urlopen('http://localhost:18080').read() -assert "About Crow example." == urllib.urlopen('http://localhost:18080/about').read() -assert 404 == urllib.urlopen('http://localhost:18080/list').getcode() -assert "3 bottles of beer!" == urllib.urlopen('http://localhost:18080/hello/3').read() -assert "100 bottles of beer!" == urllib.urlopen('http://localhost:18080/hello/100').read() -assert 400 == urllib.urlopen('http://localhost:18080/hello/500').getcode() -assert "3" == urllib.urlopen('http://localhost:18080/add_json', data='{"a":1,"b":2}').read() -assert "3" == urllib.urlopen('http://localhost:18080/add/1/2').read() - -# test persistent connection -import socket -import time -s = socket.socket() -s.connect(('localhost', 18080)) -for i in xrange(10): - s.send('''GET / HTTP/1.1 -Host: localhost\r\n\r\n'''); - assert 'Hello World!' in s.recv(1024) - -# test timeout -s = socket.socket() -s.connect(('localhost', 18080)) -print 'ERROR REQUEST' -s.send('''GET / HTTP/1.1 -hHhHHefhwjkefhklwejfklwejf -''') -print s.recv(1024) -- cgit v1.2.3-54-g00ecf