From 591b5c0fc28dfad7e091b6bf953e886ed9bc4374 Mon Sep 17 00:00:00 2001 From: heck Date: Fri, 16 Apr 2021 03:33:32 +0200 Subject: [PATCH] Tests: add util functions add vector_to_string() add print_exception() - to print nested exceptions --- test/framework/utils.cc | 30 +++++++++++++++++++++--------- test/framework/utils.hh | 21 +++++++++++++++------ test/framework/utils.hxx | 24 ++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 test/framework/utils.hxx diff --git a/test/framework/utils.cc b/test/framework/utils.cc index c9df5e7..ccb9b47 100644 --- a/test/framework/utils.cc +++ b/test/framework/utils.cc @@ -18,7 +18,7 @@ namespace pEp { namespace Log { void logH1(string msg) { - char decoration{ '=' }; + char decoration{'='}; cout << endl << endl << std::string(30, decoration) << ' ' << msg << ' ' @@ -27,7 +27,7 @@ namespace pEp { void logH2(string msg) { - char decoration{ '-' }; + char decoration{'-'}; cout << endl << std::string(10, decoration) << ' ' << msg << ' ' << std::string(10, decoration) << endl; @@ -35,7 +35,7 @@ namespace pEp { } // namespace Log namespace Utils { - string identity_to_string(::pEp_identity* ident, bool full, int indent) + string identity_to_string(::pEp_identity *ident, bool full, int indent) { stringstream builder; @@ -79,14 +79,14 @@ namespace pEp { return builder.str(); } - std::string identitylist_to_string(::identity_list* idl, bool full, int indent) + std::string identitylist_to_string(::identity_list *idl, bool full, int indent) { stringstream builder; if (idl != nullptr) { builder << endl; builder << std::string(indent, '\t') << "[" << endl; indent++; - for (::identity_list* curr = idl; curr != nullptr; curr = curr->next) { + for (::identity_list *curr = idl; curr != nullptr; curr = curr->next) { builder << identity_to_string(curr->ident, full, indent) << endl; } indent--; @@ -98,7 +98,7 @@ namespace pEp { return builder.str(); } - string member_to_string(::pEp_member* member, bool full, int indent) + string member_to_string(::pEp_member *member, bool full, int indent) { stringstream builder; if (member != nullptr) { @@ -116,14 +116,14 @@ namespace pEp { return builder.str(); } - string memberlist_to_string(::member_list* mbl, bool full, int indent) + string memberlist_to_string(::member_list *mbl, bool full, int indent) { stringstream builder; if (mbl != nullptr) { builder << endl; builder << std::string(indent, '\t') << "[" << endl; indent++; - for (member_list* curr_member = mbl; curr_member != nullptr; + for (member_list *curr_member = mbl; curr_member != nullptr; curr_member = curr_member->next) { builder << member_to_string(curr_member->member, full, indent) << endl; } @@ -136,7 +136,7 @@ namespace pEp { return builder.str(); } - string group_to_string(::pEp_group* group, bool full, int indent) + string group_to_string(::pEp_group *group, bool full, int indent) { stringstream builder; if (group != nullptr) { @@ -160,6 +160,18 @@ namespace pEp { return builder.str(); } + + void print_exception(const exception& e, int level) + { + cerr << string(level, ' ') << "exception: " << e.what() << endl; + try { + rethrow_if_nested(e); + } catch (const exception& e) { + print_exception(e, level + 1); + } catch (...) { + } + } + } // namespace Utils } // namespace Test } // namespace pEp diff --git a/test/framework/utils.hh b/test/framework/utils.hh index ccbc7a9..9a1119a 100644 --- a/test/framework/utils.hh +++ b/test/framework/utils.hh @@ -8,6 +8,7 @@ #include #include #include +#include namespace pEp { namespace Test { @@ -16,12 +17,20 @@ namespace pEp { void logH2(std::string msg); } namespace Utils { - std::string identity_to_string(::pEp_identity* ident, bool full = true, int indent = 0); - std::string identitylist_to_string(::identity_list * idl, bool full = true, int indent = 0); - std::string member_to_string(::pEp_member* member, bool full = true, int indent = 0); - std::string memberlist_to_string(::member_list* mbl, bool full = true, int indent = 0); - std::string group_to_string(::pEp_group* group, bool full = true, int indent = 0); + std::string identity_to_string(::pEp_identity *ident, bool full = true, int indent = 0); + std::string identitylist_to_string(::identity_list *idl, bool full = true, int indent = 0); + std::string member_to_string(::pEp_member *member, bool full = true, int indent = 0); + std::string memberlist_to_string(::member_list *mbl, bool full = true, int indent = 0); + std::string group_to_string(::pEp_group *group, bool full = true, int indent = 0); + + template + std::string vector_to_string(std::vector v); + + void print_exception(const std::exception& e, int level = 0); } } // namespace Test } // namespace pEp -#endif \ No newline at end of file + +#include "utils.hxx" + +#endif // LIBPEPADAPTER_UTILS_HH \ No newline at end of file diff --git a/test/framework/utils.hxx b/test/framework/utils.hxx new file mode 100644 index 0000000..0a24b19 --- /dev/null +++ b/test/framework/utils.hxx @@ -0,0 +1,24 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_UTILS_HXX +#define LIBPEPADAPTER_UTILS_HXX + +#include + +namespace pEp { + namespace Test { + namespace Utils { + template + std::string vector_to_string(std::vector v) + { + std::stringstream ss; + for (const T& elem : v) { + ss << elem << std::endl; + } + return ss.str(); + } + } + } // namespace Test +} // namespace pEp +#endif // LIBPEPADAPTER_UTILS_HXX \ No newline at end of file