diff --git a/tests/automatic/common/cpp/CMakeLists.txt b/tests/automatic/common/cpp/CMakeLists.txt index e01bd7c0969c093174eed27a254e0cda628a7b40..fe0347972654b743fd4d4dc831319ddb91cac185 100644 --- a/tests/automatic/common/cpp/CMakeLists.txt +++ b/tests/automatic/common/cpp/CMakeLists.txt @@ -6,7 +6,7 @@ set(SOURCE_FILES # Library ################################ add_library(${TARGET_NAME} STATIC ${SOURCE_FILES}) -target_include_directories(${TARGET_NAME} PUBLIC include) +target_include_directories(${TARGET_NAME} PUBLIC include ${ASAPO_CXX_COMMON_INCLUDE_DIR}) set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX) #Add all necessary common libraries diff --git a/tests/automatic/common/cpp/include/testing.h b/tests/automatic/common/cpp/include/testing.h index 798187d8040281c83bf014bcf85b631f1055774b..d127dfd931f4cafb6f0667d5e841657b2eede329 100644 --- a/tests/automatic/common/cpp/include/testing.h +++ b/tests/automatic/common/cpp/include/testing.h @@ -1,15 +1,68 @@ #ifndef ASAPO_TESTING_H #define ASAPO_TESTING_H +#include <common/error.h> #include <string> namespace asapo { -void M_AssertEq(const std::string& expected, const std::string& got); -void M_AssertEq(int expected, int got); -void M_AssertTrue(bool value, std::string name = ""); -void M_AssertContains(const std::string& whole, const std::string& sub); +void _M_INTERNAL_PrintComment(const std::string& comment); +void _M_INTERNAL_ThrowError(const std::string& expected, const std::string& got, const std::string& comment); +void _M_AssertTrue(bool value, const std::string& comment); +void _M_AssertContains(const std::string& whole, const std::string& sub, const std::string& comment); + +void _M_AssertEq(const std::string& expected, const std::string& got, const std::string& comment); +void _M_AssertEq(int expected, int got, const std::string& comment); + +// Error checks +void _M_AssertEq(const Error& expected, const Error& got, const std::string& comment); +// decltype(nullptr) == nullptr_t but we are using an old bamboo compiler that does not know this +void _M_AssertEq(const decltype(nullptr)& expected, const Error& got, const std::string& comment); +template<class ErrorTemplateType> +inline void _M_AssertEq(const ErrorTemplateType& expected, const Error& got, const std::string& comment) { + _M_INTERNAL_PrintComment(comment); + if (expected != got) { + _M_INTERNAL_ThrowError(expected.Text(), got ? got->Explain() : "No error", comment); + } +} + +// Function that helps to convert the __LINE__ to a string +#define _M_INTERNAL_TO_STRING_WRAPPER(x) #x +#define _M_INTERNAL_TO_STRING(x) _M_INTERNAL_TO_STRING_WRAPPER(x) +#define _M_INTERNAL_COMMENT_PREFIX "Line " _M_INTERNAL_TO_STRING(__LINE__) ": " + +// These macros are handling optional arguments +// https://stackoverflow.com/a/3048361/3338196 +#define _M_AssertEq_2_ARGS(e, g) \ + asapo::_M_AssertEq(e, g, _M_INTERNAL_COMMENT_PREFIX "Expect " # g " to be " # e) +#define _M_AssertEq_3_ARGS(e, g, c) \ + asapo::_M_AssertEq(e, g, _M_INTERNAL_COMMENT_PREFIX c) + +#define _M_AssertContains_2_ARGS(whole, sub) \ + asapo::_M_AssertContains(whole, sub, _M_INTERNAL_COMMENT_PREFIX "Expect " # whole " to contain substring " # sub) +#define _M_AssertContains_3_ARGS(whole, sub, c) \ + asapo::_M_AssertContains(whole, sub, _M_INTERNAL_COMMENT_PREFIX c) + +#define _M_AssertTrue_1_ARGS(value) \ + asapo::_M_AssertTrue(value, _M_INTERNAL_COMMENT_PREFIX "Expect " # value " to be true") +#define _M_AssertTrue_2_ARGS(value, c) \ + asapo::_M_AssertTrue(value, _M_INTERNAL_COMMENT_PREFIX c) + +#define _M_GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4 +#define _M_MACRO_CHOOSER_2_3(func, ...) _M_GET_4TH_ARG(__VA_ARGS__, _ ## func ## _3_ARGS, _ ## func ## _2_ARGS, ) +#define _M_GET_3TH_ARG(arg1, arg2, arg3, ...) arg3 +#define _M_MACRO_CHOOSER_1_2(func, ...) _M_GET_3TH_ARG(__VA_ARGS__, _ ## func ## _2_ARGS, _ ## func ## _1_ARGS, ) + +// MSVC Fix... >:( +// https://stackoverflow.com/a/5134656/3338196 +#define _M_EXPAND( x ) x + +// Calls available to the user +// Can be used like the real _M_Assert... functions, the comment is optional +#define M_AssertEq(...) _M_EXPAND(_M_MACRO_CHOOSER_2_3(M_AssertEq, __VA_ARGS__)(__VA_ARGS__)) +#define M_AssertContains(...) _M_EXPAND(_M_MACRO_CHOOSER_2_3(M_AssertContains, __VA_ARGS__)(__VA_ARGS__)) +#define M_AssertTrue(...) _M_EXPAND(_M_MACRO_CHOOSER_1_2(M_AssertTrue, __VA_ARGS__)(__VA_ARGS__)) } diff --git a/tests/automatic/common/cpp/src/testing.cpp b/tests/automatic/common/cpp/src/testing.cpp index ca14d633be5124850e88daf241bd26c5cebb56f4..17b333cd7c4701fdfde372f473a667403de8da84 100644 --- a/tests/automatic/common/cpp/src/testing.cpp +++ b/tests/automatic/common/cpp/src/testing.cpp @@ -5,55 +5,72 @@ namespace asapo { +std::string EraseSpaces(const std::string& str) { + auto tmp = str; + auto end_pos = std::remove(tmp.begin(), tmp.end(), ' '); + tmp.erase(end_pos, tmp.end()); + return tmp; +} + +void _M_INTERNAL_PrintComment(const std::string& comment) { + std::cout << comment << std::endl; +} + +void _M_INTERNAL_ThrowError(const std::string& expected, const std::string& got, const std::string& comment) { + std::cerr << "Assert failed: " << comment << std::endl + << "Expected:\t'" << expected << "'" << std::endl + << "Obtained:\t'" << got << "'" << std::endl; + exit(EXIT_FAILURE); +} + +/// Generic equal check template<typename T> -void T_AssertEq(const T& expected, const T& got) { +void T_AssertEq(const T& expected, const T& got, const std::string& comment) { + _M_INTERNAL_PrintComment(comment); if (expected != got) { - std::cerr << "Assert failed:\n" - << "Expected:\t'" << expected << "'\n" - << "Obtained:\t'" << got << "'\n"; + std::cerr << "Assert failed: " << comment << std::endl + << "Expected:\t'" << expected << "'" << std::endl + << "Obtained:\t'" << got << "'" << std::endl; exit(EXIT_FAILURE); } } -void M_AssertTrue(bool value, std::string name) { - std::cout << "asserting " << name << std::endl; +void _M_AssertTrue(bool value, const std::string& comment) { + _M_INTERNAL_PrintComment(comment); if (!value) { - std::cerr << "Assert failed: " << name << "\n" - << "Expected:\t'" << "1" << "'\n" - << "Obtained:\t'" << value << "'\n"; - exit(EXIT_FAILURE); + _M_INTERNAL_ThrowError("true", "false", comment); } - -} - -void M_AssertEq(const std::string& expected, const std::string& got) { - T_AssertEq(expected, got); } -void M_AssertEq(int expected, int got) { - T_AssertEq(expected, got); -} - - -std::string EraseSpaces(const std::string& str) { - auto tmp = str; - auto end_pos = std::remove(tmp.begin(), tmp.end(), ' '); - tmp.erase(end_pos, tmp.end()); - return tmp; -} -void M_AssertContains( const std::string& whole, const std::string& sub) { +void _M_AssertContains(const std::string& whole, const std::string& sub, const std::string& comment) { auto whole_t = EraseSpaces(whole); auto sub_t = EraseSpaces(sub); + _M_INTERNAL_PrintComment(comment); if (whole_t.find(sub_t) == std::string::npos) { - std::cerr << "Assert failed:\n" - << "Got (spaces erased):\t'" << whole_t << "'\n" - << "Expected containes (spaces erased):\t'" << sub_t << "'\n"; + std::cerr << "Assert failed: " << std::endl + << "Got (spaces erased):\t'" << whole_t << "'" << std::endl + << "Expected contains (spaces erased):\t'" << sub_t << "'" << std::endl; exit(EXIT_FAILURE); } } +void _M_AssertEq(const std::string& expected, const std::string& got, const std::string& comment) { + T_AssertEq(expected, got, comment); +} + +void _M_AssertEq(int expected, int got, const std::string& comment) { + T_AssertEq(expected, got, comment); +} + +void _M_AssertEq(const Error& expected, const Error& got, const std::string& comment) { + T_AssertEq(expected, got, comment); +} + +void _M_AssertEq(const decltype(nullptr)& expected, const Error& got, const std::string& comment) { + T_AssertEq(Error{}, got, comment); +} } diff --git a/tests/automatic/consumer/consumer_api/consumer_api.cpp b/tests/automatic/consumer/consumer_api/consumer_api.cpp index 73ebf67fe962db006eb8520a4a6a0a6600eae6d9..d9065c56e36752e0be9d22dec784694e12187fc1 100644 --- a/tests/automatic/consumer/consumer_api/consumer_api.cpp +++ b/tests/automatic/consumer/consumer_api/consumer_api.cpp @@ -6,10 +6,6 @@ #include "consumer/data_broker.h" #include "testing.h" -using asapo::M_AssertEq; -using asapo::M_AssertTrue; - - struct Args { std::string server; std::string run_name; diff --git a/tests/automatic/consumer/next_multithread_broker/next_multithread_broker.cpp b/tests/automatic/consumer/next_multithread_broker/next_multithread_broker.cpp index deea0768f29ff3844d6f3c7d1ac50cf1487b2535..df24e18c1e33de2109b93fb840fab8acade22c86 100644 --- a/tests/automatic/consumer/next_multithread_broker/next_multithread_broker.cpp +++ b/tests/automatic/consumer/next_multithread_broker/next_multithread_broker.cpp @@ -5,9 +5,6 @@ #include "consumer/data_broker.h" #include "testing.h" -using asapo::M_AssertEq; -using asapo::M_AssertTrue; - void Assert(std::vector<asapo::FileInfos> file_infos, int nthreads, int nfiles) { std::vector<std::string> expect, result; for (int i = 1; i <= nfiles; i++) { diff --git a/tests/automatic/curl_http_client/curl_http_client_command/curl_httpclient_command.cpp b/tests/automatic/curl_http_client/curl_http_client_command/curl_httpclient_command.cpp index 264d88c6befd767c99e69ae1b6dcbc52d35c3fd8..63d202b67c4c24f0eac1f0678a518b264b6e7b82 100644 --- a/tests/automatic/curl_http_client/curl_http_client_command/curl_httpclient_command.cpp +++ b/tests/automatic/curl_http_client/curl_http_client_command/curl_httpclient_command.cpp @@ -5,10 +5,6 @@ #include "../../../consumer/api/cpp/src/server_data_broker.h" #include "preprocessor/definitions.h" -using asapo::M_AssertEq; -using asapo::M_AssertContains; -using asapo::M_AssertTrue; - struct Args { std::string uri_authorizer; std::string uri_fts; diff --git a/tests/automatic/json_parser/parse_config_file/parse_config_file.cpp b/tests/automatic/json_parser/parse_config_file/parse_config_file.cpp index eb4bb5903f42af50f1856f5abea9a4ec458c9cf4..3ebdc9f119e0548b61b77c9cb4f762ddcb80c668 100644 --- a/tests/automatic/json_parser/parse_config_file/parse_config_file.cpp +++ b/tests/automatic/json_parser/parse_config_file/parse_config_file.cpp @@ -6,9 +6,6 @@ #include "testing.h" -using asapo::M_AssertEq; - - using namespace asapo; struct Settings { diff --git a/tests/automatic/mongo_db/connect/connect_mongodb.cpp b/tests/automatic/mongo_db/connect/connect_mongodb.cpp index 9c156098c3d58503bb5387ecb1b18cc85603a896..8715aae9998f278a74ab44c6e9c56ba7999637b7 100644 --- a/tests/automatic/mongo_db/connect/connect_mongodb.cpp +++ b/tests/automatic/mongo_db/connect/connect_mongodb.cpp @@ -5,7 +5,6 @@ #include "testing.h" #include "database/db_error.h" -using asapo::M_AssertContains; using asapo::Error; void Assert(const Error& error, const std::string& expect) { diff --git a/tests/automatic/mongo_db/insert_retrieve/insert_retrieve_mongodb.cpp b/tests/automatic/mongo_db/insert_retrieve/insert_retrieve_mongodb.cpp index abeb042ebada81c563aa8d89579c4285bad5856c..8cf88e458b71793b6745a747a4c12a40e05f05f0 100644 --- a/tests/automatic/mongo_db/insert_retrieve/insert_retrieve_mongodb.cpp +++ b/tests/automatic/mongo_db/insert_retrieve/insert_retrieve_mongodb.cpp @@ -4,13 +4,8 @@ #include "../../../common/cpp/src/database/mongodb_client.h" #include "testing.h" - -using asapo::M_AssertContains; -using asapo::M_AssertTrue; using asapo::Error; - - void Assert(const Error& error, const std::string& expect) { std::string result; if (error == nullptr) { @@ -66,7 +61,7 @@ int main(int argc, char* argv[]) { db_new.Connect("127.0.0.1", "data"); err = db_new.GetById("test", fi.id, &fi_db); M_AssertTrue(fi_db == fi, "get record from db"); - Assert(err, "OK"); + M_AssertEq(nullptr, err); err = db_new.GetById("test", 0, &fi_db); Assert(err, "No record"); } diff --git a/tests/automatic/mongo_db/insert_retrieve_dataset/insert_retrieve_dataset_mongodb.cpp b/tests/automatic/mongo_db/insert_retrieve_dataset/insert_retrieve_dataset_mongodb.cpp index 944bfed46380403f52662a9f6da1013c7e6013d4..6cbaba64124a730d1acfff4ab6c18fba3741eb63 100644 --- a/tests/automatic/mongo_db/insert_retrieve_dataset/insert_retrieve_dataset_mongodb.cpp +++ b/tests/automatic/mongo_db/insert_retrieve_dataset/insert_retrieve_dataset_mongodb.cpp @@ -4,13 +4,8 @@ #include "../../../common/cpp/src/database/mongodb_client.h" #include "testing.h" - -using asapo::M_AssertContains; -using asapo::M_AssertTrue; - using asapo::Error; - void Assert(const Error& error, const std::string& expect) { std::string result; if (error == nullptr) { @@ -71,7 +66,7 @@ int main(int argc, char* argv[]) { asapo::FileInfo fi_db; err = db.GetDataSetById("test", subset_id, fi.id, &fi_db); M_AssertTrue(fi_db == fi, "get record from db"); - Assert(err, "OK"); + M_AssertEq(nullptr, err); err = db.GetDataSetById("test", 0, 0, &fi_db); Assert(err, "No record"); } diff --git a/tests/automatic/mongo_db/upsert/upsert_mongodb.cpp b/tests/automatic/mongo_db/upsert/upsert_mongodb.cpp index 89ec2de5334e3b7f40d728afd59708a70f5c8688..f1420bcf66c8c230edacc789edd3b3645874e16e 100644 --- a/tests/automatic/mongo_db/upsert/upsert_mongodb.cpp +++ b/tests/automatic/mongo_db/upsert/upsert_mongodb.cpp @@ -4,11 +4,8 @@ #include "../../../common/cpp/src/database/mongodb_client.h" #include "testing.h" - -using asapo::M_AssertContains; using asapo::Error; - void Assert(const Error& error, const std::string& expect) { std::string result; if (error == nullptr) { diff --git a/tests/automatic/system_io/ip_tcp_network/client_serv/ip_tcp_network.cpp b/tests/automatic/system_io/ip_tcp_network/client_serv/ip_tcp_network.cpp index 2c3dea1c339af3b61eec490f24af4d8ed327d552..a0a0c224b1ad74a57888953c06129652a5bdd21d 100644 --- a/tests/automatic/system_io/ip_tcp_network/client_serv/ip_tcp_network.cpp +++ b/tests/automatic/system_io/ip_tcp_network/client_serv/ip_tcp_network.cpp @@ -13,7 +13,6 @@ using asapo::AddressFamilies; using asapo::SocketTypes; using asapo::SocketProtocols; using asapo::FileDescriptor; -using asapo::M_AssertEq; using namespace std::chrono; diff --git a/tests/automatic/system_io/ip_tcp_network/client_serv_multicon/multicon.cpp b/tests/automatic/system_io/ip_tcp_network/client_serv_multicon/multicon.cpp index 12a9fbc2ec52495ced87c95a065b01650d7035ee..bb40830c99291fc256210b9cdb3a727d7120b250 100644 --- a/tests/automatic/system_io/ip_tcp_network/client_serv_multicon/multicon.cpp +++ b/tests/automatic/system_io/ip_tcp_network/client_serv_multicon/multicon.cpp @@ -14,7 +14,6 @@ using asapo::AddressFamilies; using asapo::SocketTypes; using asapo::SocketProtocols; using asapo::SocketDescriptor; -using asapo::M_AssertEq; using namespace std::chrono; diff --git a/tests/automatic/system_io/read_file_content/read_file_content.cpp b/tests/automatic/system_io/read_file_content/read_file_content.cpp index 8e495e7227040ba0f046a561e74bdb1c443182f7..3fe4893f55fb1fadb3221f1c53f53439f633ca73 100644 --- a/tests/automatic/system_io/read_file_content/read_file_content.cpp +++ b/tests/automatic/system_io/read_file_content/read_file_content.cpp @@ -32,6 +32,6 @@ int main(int argc, char* argv[]) { result = err->Explain(); } - asapo::M_AssertContains(result, expect); + M_AssertContains(result, expect); return 0; } diff --git a/tests/automatic/system_io/read_folder_content/read_folder_content.cpp b/tests/automatic/system_io/read_folder_content/read_folder_content.cpp index dea3ca1a992dd27574787ae19ad66d8826accd05..017d6fa767eeb2cb1392cfb697bd5c60ebf6ffc2 100644 --- a/tests/automatic/system_io/read_folder_content/read_folder_content.cpp +++ b/tests/automatic/system_io/read_folder_content/read_folder_content.cpp @@ -6,10 +6,6 @@ using asapo::IO; using asapo::Error; - -using asapo::M_AssertEq; -using asapo::M_AssertContains; - int main(int argc, char* argv[]) { if (argc != 3) { std::cout << "Wrong number of arguments" << std::endl; diff --git a/tests/automatic/system_io/read_string_from_file/read_string_from_file.cpp b/tests/automatic/system_io/read_string_from_file/read_string_from_file.cpp index 9c12ac41761f8323411ef628e063853282437362..acc7703835e4f3ddd5f3ce5bb649972433831c51 100644 --- a/tests/automatic/system_io/read_string_from_file/read_string_from_file.cpp +++ b/tests/automatic/system_io/read_string_from_file/read_string_from_file.cpp @@ -22,6 +22,6 @@ int main(int argc, char* argv[]) { result = err->Explain(); } - asapo::M_AssertContains(result, expect); + M_AssertContains(result, expect); return 0; } diff --git a/tests/automatic/system_io/read_subdirectories/read_subdirectories.cpp b/tests/automatic/system_io/read_subdirectories/read_subdirectories.cpp index ecb33cb30a364ea9a6c036b04b275a805d8a81c7..0b35490412043ddc9298448e84d6e2c1a4cd0f6e 100644 --- a/tests/automatic/system_io/read_subdirectories/read_subdirectories.cpp +++ b/tests/automatic/system_io/read_subdirectories/read_subdirectories.cpp @@ -6,10 +6,6 @@ using asapo::IO; using asapo::Error; - -using asapo::M_AssertEq; -using asapo::M_AssertContains; - int main(int argc, char* argv[]) { if (argc != 3) { std::cout << "Wrong number of arguments" << std::endl; diff --git a/tests/automatic/system_io/resolve_hostname_to_ip/resolve_hostname_to_ip.cpp b/tests/automatic/system_io/resolve_hostname_to_ip/resolve_hostname_to_ip.cpp index 71c5984a307c0aa09fe31e9ae37bad965a51de3e..c45cd6b193e1b0b8a12926cce025bb8ecc1cef97 100644 --- a/tests/automatic/system_io/resolve_hostname_to_ip/resolve_hostname_to_ip.cpp +++ b/tests/automatic/system_io/resolve_hostname_to_ip/resolve_hostname_to_ip.cpp @@ -6,10 +6,6 @@ using asapo::Error; using asapo::ErrorType; -using asapo::M_AssertEq; -using asapo::M_AssertTrue; - - void Check(const std::string& expected_ip_address, const std::string& hostname) { std::cout << "Checking: " << hostname << std::endl; Error err; diff --git a/tests/automatic/system_io/write_data_to_file/write_data_to_file.cpp b/tests/automatic/system_io/write_data_to_file/write_data_to_file.cpp index 8231f5f4b18df122ddfcda8771cc7070bfc815c6..3879e7c9c1e6e3d2cdfdda83023e10678c313a10 100644 --- a/tests/automatic/system_io/write_data_to_file/write_data_to_file.cpp +++ b/tests/automatic/system_io/write_data_to_file/write_data_to_file.cpp @@ -36,7 +36,7 @@ void AssertGoodResult(const std::unique_ptr<IO>& io, const Error& err, const Fil Error read_err; uint64_t size = params.length; auto read_data = io->GetDataFromFile(params.fname, &size, &read_err); - asapo::M_AssertContains(std::string(read_data.get(), read_data.get() + params.length), "123"); + M_AssertContains(std::string(read_data.get(), read_data.get() + params.length), "123"); } void AssertBadResult(const Error& err, const Args& params) { @@ -44,7 +44,7 @@ void AssertBadResult(const Error& err, const Args& params) { std::cerr << "Should be error" << std::endl; exit(EXIT_FAILURE); } - asapo::M_AssertContains(err->Explain(), params.message); + M_AssertContains(err->Explain(), params.message); }