From c7b84d36e9e19c69c267d5031efc51aff4ca328c Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Mon, 5 Jul 2021 13:24:15 +0200
Subject: [PATCH] DRAFT: setup tests with conan

---
 test/CMakeLists.txt      | 49 +++++++++++++++++++++++++++++----
 test/test_connection.cpp | 59 ++++++++++++++++++----------------------
 2 files changed, 71 insertions(+), 37 deletions(-)

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 309f61b..612e01e 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -18,15 +18,51 @@
 # along with this program. If not, see <https://www.gnu.org/licenses/>.
 #
 
-# append all the test cases here (file name without the ".cpp" suffix)
+#######################################################################
+### append test cases here (file name without the ".cpp" suffix)
+#######################################################################
 set(test_cases
     test_connection
     )
 
-#find_package(caosdb REQUIRED)
-#message(STATUS "Found libcaosdb: ${caosdb_DIR}")
+#######################################################
+### Linting with clang-tidy and include-what-you-use
+#######################################################
+option(LINTING "clang-tidy and iwye" ON)
+if(LINTING)
+    find_program(iwyu
+        NAMES include-what-you-use iwyu
+        PATHS ${CMAKE_SOURCE_DIR}/tools/include-what-you-use/${iwyu_os}/bin)
+    if(NOT iwyu)
+        message(WARNING "include-what-you-use: Not found")
+    else()
+        message(STATUS "include-what-you-use: ${iwyu}")
+        set(_CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu}
+            "-Xiwyu" "--no_fwd_decls"
+            "-Xiwyu" "--cxx17ns")
+    endif()
+
+    find_program(clang_tidy NAMES clang-tidy clang-tidy-11)
+    if(NOT clang_tidy)
+        message(WARNING "clang-tidy: Not found")
+    else()
+        message(STATUS "clang-tidy: ${clang_tidy}")
+        set(_CMAKE_CXX_CLANG_TIDY "${clang_tidy}"
+            "--header-filter=caosdb/.*[^\(\.pb\.h\)]$"
+            "--warnings-as-errors=*"
+            "--fix")
+        set(_CMAKE_CXX_CLANG_TIDY_CHECKS
+          "--checks=*,-fuchsia-*,-llvm-include-order,-llvmlibc-*,-cert-err58-cpp,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-modernize-use-trailing-return-type,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-cppcoreguidelines-avoid-goto,-hicpp-avoid-goto")
+    endif()
+else()
+    message(STATUS "LINTING is OFF")
+endif()
+
+
+###################################################
+### Set up tests using GoogleTest (GTest)
+###################################################
 
-#include(FetchGTest)
 # add special cmake functions for gtest
 include(GoogleTest REQUIRED)
 
@@ -41,7 +77,10 @@ foreach (i RANGE "${len_test_cases}")
         ${CONAN_LIBS_OPENSSL} ${CONAN_LIBS_C-ARES} ${CONAN_LIBS_BZIP2}
         ${CONAN_LIBS_PROTOBUF} ${CONAN_LIBS_ZLIB})
     target_include_directories(${test_case_name} PUBLIC ${CONAN_INCLUDE_DIRS})
-    set_target_properties(${test_case_name} PROPERTIES CXX_CLANG_TIDY "")
+    set_target_properties(${test_case_name}
+        PROPERTIES
+        CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY};${_CMAKE_CXX_CLANG_TIDY_CHECKS}"
+        CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}")
     gtest_discover_tests(${test_case_name}
         PROPERTIES
             LABELS "caosdb-cpplib-int-tests")
diff --git a/test/test_connection.cpp b/test/test_connection.cpp
index c6362b8..f2bc829 100644
--- a/test/test_connection.cpp
+++ b/test/test_connection.cpp
@@ -19,67 +19,60 @@
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
  *
  */
-
-#include "caosdb/connection.h"
-#include "caosdb/authentication.h"
-#include <gtest/gtest-message.h>
-#include <gtest/gtest-test-part.h>
-#include <memory>
-#include "gtest/gtest_pred_impl.h"
-#include "caosdb/utils.h"
-
-using caosdb::connection::InsecureCaosDBConnectionConfig;
-using caosdb::connection::SslCaosDBConnectionConfig;
-using caosdb::connection::PemFileCACertProvider;
-using caosdb::connection::CaosDBConnection;
+#include <gtest/gtest-message.h>    // for Message
+#include <gtest/gtest-test-part.h>  // for SuiteApiResolver, TestPartResult
+#include <memory>                   // for make_shared, allocator, shared_ptr
+#include <string>                   // for string
+#include "caosdb/authentication.h"  // for PlainPasswordAuthenticator
+#include "caosdb/connection.h"      // for InsecureCaosDBConnectionConfig
+#include "caosdb/exceptions.h"      // for AuthenticationError, ConnectionError
+#include "caosdb/info.h"            // for VersionInfo
+#include "caosdb/utils.h"           // for get_env_var
+#include "gtest/gtest_pred_impl.h"  // for Test, TEST, EXPECT_EQ, EXPECT_THROW
+
+namespace caosdb::connection {
+using caosdb::exceptions::AuthenticationError;
+using caosdb::exceptions::ConnectionError;
 using caosdb::authentication::PlainPasswordAuthenticator;
-using caosdb::info::v1alpha1::VersionInfo;
 
 TEST(test_connection, config_somehost_25323) {
   auto port = 25323;
-  auto host = "somehost";
-  auto config = std::make_unique<InsecureCaosDBConnectionConfig>(host, port);
+  const auto *pHost = "somehost";
+  auto config = std::make_unique<InsecureCaosDBConnectionConfig>(pHost, port);
 
-  ASSERT_EQ(host, config->getHost());
-  ASSERT_EQ(port, config->getPort());
+  EXPECT_EQ(pHost, config->getHost());
+  EXPECT_EQ(port, config->getPort());
 }
 
 TEST(test_connection, connect_somehost_42347_fails) {
   auto port = 42347;
-  auto host = "somehost";
-  auto config = std::make_shared<InsecureCaosDBConnectionConfig>(host, port);
+  const auto *pHost = "somehost";
+  auto config = std::make_shared<InsecureCaosDBConnectionConfig>(pHost, port);
   CaosDBConnection connection(config);
 
-  // TODO ConnectionError
-  ASSERT_THROW(connection.getVersionInfo(), std::runtime_error);
+  EXPECT_THROW(connection.getVersionInfo(), ConnectionError);
 }
 
 TEST(test_connection, connection_insecure_authentication_error) {
   auto port = 8080;
   const auto host = caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost");
-  auto major = 0;
-  auto minor = 5;
 
   auto config = std::make_shared<InsecureCaosDBConnectionConfig>(host, port);
   auto connection = CaosDBConnection(config);
 
-  // TODO AuthenticationError
-  ASSERT_THROW(connection.getVersionInfo(), std::runtime_error);
+  EXPECT_THROW(connection.getVersionInfo(), AuthenticationError);
 }
 
 TEST(test_connection, connection_ssl_authentication_error) {
   auto port = 8443;
   const auto host = caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost");
   const auto path = caosdb::utils::get_env_var("CAOSDB_SERVER_CA_PEM", std::string());
-  auto major = 0;
-  auto minor = 5;
 
   auto ssloptions = std::make_shared<PemFileCACertProvider>(path);
   auto config = std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions);
   auto connection = CaosDBConnection(config);
 
-  // TODO AuthenticationError
-  ASSERT_THROW(connection.getVersionInfo(), std::runtime_error);
+  EXPECT_THROW(connection.getVersionInfo(), AuthenticationError);
 }
 
 TEST(test_connection, connection_ssl_authentication_success) {
@@ -98,6 +91,8 @@ TEST(test_connection, connection_ssl_authentication_success) {
   auto connection = CaosDBConnection(config);
 
   auto v_info = connection.getVersionInfo();
-  ASSERT_EQ(major, v_info.major());
-  ASSERT_EQ(minor, v_info.minor());
+  EXPECT_EQ(major, v_info->GetMajor());
+  EXPECT_EQ(minor, v_info->GetMinor());
 }
+
+} //namespace caosdb::connection
-- 
GitLab