Browse Source

Tests: add util functions

add vector_to_string<T>()
add print_exception() - to print nested exceptions
LIB-12
heck 4 years ago
parent
commit
591b5c0fc2
  1. 30
      test/framework/utils.cc
  2. 21
      test/framework/utils.hh
  3. 24
      test/framework/utils.hxx

30
test/framework/utils.cc

@ -18,7 +18,7 @@ namespace pEp {
namespace Log { namespace Log {
void logH1(string msg) void logH1(string msg)
{ {
char decoration{ '=' }; char decoration{'='};
cout << endl cout << endl
<< endl << endl
<< std::string(30, decoration) << ' ' << msg << ' ' << std::string(30, decoration) << ' ' << msg << ' '
@ -27,7 +27,7 @@ namespace pEp {
void logH2(string msg) void logH2(string msg)
{ {
char decoration{ '-' }; char decoration{'-'};
cout << endl cout << endl
<< std::string(10, decoration) << ' ' << msg << ' ' << std::string(10, decoration) << ' ' << msg << ' '
<< std::string(10, decoration) << endl; << std::string(10, decoration) << endl;
@ -35,7 +35,7 @@ namespace pEp {
} // namespace Log } // namespace Log
namespace Utils { 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; stringstream builder;
@ -79,14 +79,14 @@ namespace pEp {
return builder.str(); 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; stringstream builder;
if (idl != nullptr) { if (idl != nullptr) {
builder << endl; builder << endl;
builder << std::string(indent, '\t') << "[" << endl; builder << std::string(indent, '\t') << "[" << endl;
indent++; 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; builder << identity_to_string(curr->ident, full, indent) << endl;
} }
indent--; indent--;
@ -98,7 +98,7 @@ namespace pEp {
return builder.str(); 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; stringstream builder;
if (member != nullptr) { if (member != nullptr) {
@ -116,14 +116,14 @@ namespace pEp {
return builder.str(); 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; stringstream builder;
if (mbl != nullptr) { if (mbl != nullptr) {
builder << endl; builder << endl;
builder << std::string(indent, '\t') << "[" << endl; builder << std::string(indent, '\t') << "[" << endl;
indent++; indent++;
for (member_list* curr_member = mbl; curr_member != nullptr; for (member_list *curr_member = mbl; curr_member != nullptr;
curr_member = curr_member->next) { curr_member = curr_member->next) {
builder << member_to_string(curr_member->member, full, indent) << endl; builder << member_to_string(curr_member->member, full, indent) << endl;
} }
@ -136,7 +136,7 @@ namespace pEp {
return builder.str(); 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; stringstream builder;
if (group != nullptr) { if (group != nullptr) {
@ -160,6 +160,18 @@ namespace pEp {
return builder.str(); 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 Utils
} // namespace Test } // namespace Test
} // namespace pEp } // namespace pEp

21
test/framework/utils.hh

@ -8,6 +8,7 @@
#include <pEp/message.h> #include <pEp/message.h>
#include <pEp/identity_list.h> #include <pEp/identity_list.h>
#include <pEp/group.h> #include <pEp/group.h>
#include <exception>
namespace pEp { namespace pEp {
namespace Test { namespace Test {
@ -16,12 +17,20 @@ namespace pEp {
void logH2(std::string msg); void logH2(std::string msg);
} }
namespace Utils { namespace Utils {
std::string identity_to_string(::pEp_identity* ident, 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 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 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 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 group_to_string(::pEp_group *group, bool full = true, int indent = 0);
template<typename T>
std::string vector_to_string(std::vector<T> v);
void print_exception(const std::exception& e, int level = 0);
} }
} // namespace Test } // namespace Test
} // namespace pEp } // namespace pEp
#endif
#include "utils.hxx"
#endif // LIBPEPADAPTER_UTILS_HH

24
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 <sstream>
namespace pEp {
namespace Test {
namespace Utils {
template<typename T>
std::string vector_to_string(std::vector<T> v)
{
std::stringstream ss;
for (const T& elem : v) {
ss << elem << std::endl;
}
return ss.str();
}
}
} // namespace Test
} // namespace pEp
#endif // LIBPEPADAPTER_UTILS_HXX
Loading…
Cancel
Save