diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..859898f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +third/* linguist-vendored +help/* linguist-documentation \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e62cbf..3a83174 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ if(ENABLE_CODE_COVERAGE) endif() # Required libraries +find_package(Boost 1.71.0 COMPONENTS unit_test_framework REQUIRED) find_package(PkgConfig REQUIRED) find_package(OpenSSL REQUIRED) if(NOT OPENSSL_FOUND) @@ -63,14 +64,13 @@ if(NOT CURL_FOUND) endif() include_directories(${CURL_INCLUDE_DIRS}) -find_library(cpprest REQUIRED) -include_directories(${CPP_REST_INCLUDE_DIR}) - - include_directories( ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/third ) +# Enable CTest +enable_testing() + add_subdirectory(src) -#add_subdirectory(test) +add_subdirectory(test) diff --git a/Dockerfile b/Dockerfile index 4121475..c40894f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,7 @@ doxygen \ git \ graphviz \ libboost-dev \ +libboost-test-dev \ libcurl4-openssl-dev \ libssl-dev \ ninja-build \ diff --git a/Jenkinsfile b/Jenkinsfile index 371f55b..b7f1729 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,58 +1,72 @@ +def unittestBadge = addEmbeddableBadgeConfiguration(id: "unittest", subject: "unit test") + pipeline { agent { dockerfile true } - stages{ - stage("parallel execution"){ - parallel{ - stage("doxygen"){ - steps{ - sh 'doxygen' - } - } + stages{ + stage("parallel execution"){ + parallel{ + stage("doxygen"){ + steps{ + sh 'doxygen' + } + } - stage("validation"){ - steps{ - sh 'tools/validate/includeGuard.sh' - } - } - stage("build and test"){ - stages{ - stage("prepare"){ - steps{ - sh ''' - mkdir -p build - cd build - cmake .. -G Ninja - ''' - } - } + stage("validation"){ + steps{ + sh 'tools/validate/includeGuard.sh' + } + } + stage("build and test"){ + stages{ + stage("prepare"){ + steps{ + sh ''' + mkdir -p build + cd build + cmake .. -G Ninja + ''' + } + } - stage("build"){ - steps{ - sh ''' - cd build - ninja - ''' - } - } + stage("build"){ + steps{ + sh ''' + cd build + ninja + ''' + } + } - stage("test"){ - steps{ - echo "test" - } - } - } - } - } - } + stage("test"){ + steps{ + script{ + unittestBadge.setStatus('running') + try{ + sh ''' + cd build + ctest --output_on_failure + ''' + unittestBadge.setStatus('passing') + }catch(Exception error){ + unittestBadge.setStatus('failed') + error 'unittest failed' + } + } + } + } + } + } + } + } - stage("upload artifact"){ - steps{ - archiveArtifacts allowEmptyArchive: true, artifacts: 'help/**/*.*', onlyIfSuccessful: true - } - } + stage("upload artifact"){ + steps{ + archiveArtifacts allowEmptyArchive: true, artifacts: 'help/**/*.*', onlyIfSuccessful: true + } + } - } + } } diff --git a/README.md b/README.md index c7dc836..5c06c11 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +![https://jenkins.milkcocoa.info/job/CocoaTweet/job/master](https://jenkins.milkcocoa.info/job/CocoaTweet/job/master/badge/icon?style=plastic) +![](http://jenkins.milkcocoa.info/job/CocoaTweet/job/master/badge/icon?style=plastic&config=unittest) +![](https://img.shields.io/badge/libcurl-libcurl4--openssl-blue) +![](https://img.shields.io/badge/libssl-1.1.1f-blue) + # CocoaTweet This is a library for using Twitter API from C++ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 663f71f..4f96f35 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,9 +2,4 @@ add_subdirectory(cocoatweet) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}) add_executable(${PROJECT_NAME} main.cc) -target_link_libraries(${PROJECT_NAME} - lib-cocoatweet - OpenSSL::SSL - OpenSSL::Crypto - ${CURL_LIBRARIES} -) +target_link_libraries(${PROJECT_NAME} lib-cocoatweet) \ No newline at end of file diff --git a/src/cocoatweet/CMakeLists.txt b/src/cocoatweet/CMakeLists.txt index c5bb89d..ef17d9c 100644 --- a/src/cocoatweet/CMakeLists.txt +++ b/src/cocoatweet/CMakeLists.txt @@ -1,4 +1,11 @@ file(GLOB_RECURSE SOURCES ./*.cc) add_library(lib-cocoatweet ${SOURCES}) +target_link_libraries(lib-cocoatweet PUBLIC + Boost::boost + OpenSSL::SSL + OpenSSL::Crypto + ${CURL_LIBRARIES} +) + target_include_directories(lib-cocoatweet PUBLIC ${PROJECT_SOURCE_DIR}/src) set_target_properties(lib-cocoatweet PROPERTIES OUTPUT_NAME cocoatweet) diff --git a/src/cocoatweet/oauth/key.h b/src/cocoatweet/oauth/key.h index 3b4b29f..07aba49 100644 --- a/src/cocoatweet/oauth/key.h +++ b/src/cocoatweet/oauth/key.h @@ -11,7 +11,11 @@ class Key { const std::string accessTokenSecret_; public: - Key() = default; + Key() + : consumerKey_(""), + consumerSecret_(""), + accessToken_(""), + accessTokenSecret_("") {} Key(const std::string& _consumerKey, const std::string& _consumerSecret, const std::string& _accessToken, const std::string& _accessTokenSecret) : consumerKey_(_consumerKey), diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..fe3c3f1 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,15 @@ +file(GLOB_RECURSE SOURCES ./*.cc) + +foreach(TEST_SOURCE_FILE ${SOURCES}) + file(RELATIVE_PATH SRC_RELPATH ${CMAKE_CURRENT_LIST_DIR} ${TEST_SOURCE_FILE}) + string(REGEX REPLACE "\.cc$" "" TEST_MODULE_NAME "test/${SRC_RELPATH}") + string(REPLACE "/" "_" TEST_EXECUTABLE_NAME ${TEST_MODULE_NAME}) + + add_executable(${TEST_EXECUTABLE_NAME} ${TEST_SOURCE_FILE}) + target_compile_definitions(${TEST_EXECUTABLE_NAME} PRIVATE BOOST_TEST_MODULE=${TEST_MODULE_NAME}) + target_link_libraries(${TEST_EXECUTABLE_NAME} + lib-cocoatweet + Boost::unit_test_framework + ) + add_test(${TEST_MODULE_NAME} ${EXECUTABLE_OUTPUT_PATH}/${TEST_EXECUTABLE_NAME}) +endforeach() diff --git a/test/oauth/key.cc b/test/oauth/key.cc new file mode 100644 index 0000000..c5bd0c7 --- /dev/null +++ b/test/oauth/key.cc @@ -0,0 +1,35 @@ +#define BOOST_TEST_DYN_LINK + +#include + +#include "cocoatweet/oauth/key.h" + +BOOST_AUTO_TEST_SUITE(oauth_key) +BOOST_AUTO_TEST_CASE(test01) { + CocoaTweet::OAuth::Key key; + + BOOST_TEST(key.consumerKey() == ""); + BOOST_TEST(key.consumerSecret() == ""); + BOOST_TEST(key.accessToken() == ""); + BOOST_TEST(key.accessTokenSecret() == ""); +} + +BOOST_AUTO_TEST_CASE(test02) { + CocoaTweet::OAuth::Key key("consumerKey", "consumerSecret", "accessToken", "accessTokenSecret"); + + BOOST_TEST(key.consumerKey() == "consumerKey"); + BOOST_TEST(key.consumerSecret() == "consumerSecret"); + BOOST_TEST(key.accessToken() == "accessToken"); + BOOST_TEST(key.accessTokenSecret() == "accessTokenSecret"); + + auto noSecret = key.noSecret(); + BOOST_TEST(noSecret.at("oauth_consumer_key") == "consumerKey"); + BOOST_TEST(noSecret.at("oauth_token") == "accessToken"); + + auto secret = key.secret(); + BOOST_TEST(secret.at("oauth_consumer_key") == "consumerSecret"); + BOOST_TEST(secret.at("oauth_token") == "accessTokenSecret"); +} + + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tools/validate/includeGuard.sh b/tools/validate/includeGuard.sh index a66b80f..cfb7657 100755 --- a/tools/validate/includeGuard.sh +++ b/tools/validate/includeGuard.sh @@ -8,7 +8,7 @@ for f in $(git ls-files | grep -E '^src\/.*\.h$'); do # ファイルパスから正しいインクルードガードの文字列を生成する s1="$(echo "$f" | sed -r 's/^src\///; s/[\/\.-]+/_/g; s/^.*$/\U&/')_" - # ファイルからインクルードガードを読み込む + # ファイルからインクルードガードを読み込む s2=$(grep -Pzo '#ifndef\s+\K\b(\w+)\b(?=\s+#define\s+\b\1\b)' "$f" | tr '\0' '\n') if [ -z "$s2" ]; then