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 {
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

21
test/framework/utils.hh

@ -8,6 +8,7 @@
#include <pEp/message.h>
#include <pEp/identity_list.h>
#include <pEp/group.h>
#include <exception>
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<typename T>
std::string vector_to_string(std::vector<T> v);
void print_exception(const std::exception& e, int level = 0);
}
} // namespace Test
} // 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