Browse Source

Test: Utils - add const, add log() and lograw()

LIB-11
heck 4 years ago
parent
commit
42855fdeee
  1. 78
      test/framework/utils.cc
  2. 32
      test/framework/utils.hh

78
test/framework/utils.cc

@ -18,27 +18,42 @@ using namespace pEp;
namespace pEp { namespace pEp {
namespace Test { namespace Test {
namespace Log { namespace Log {
void logH1(string msg) void log(const string &msg)
{ {
lograw(msg + "\n");
}
void logH1(const string &msg)
{
stringstream tmp;
char decoration{ '=' }; char decoration{ '=' };
cout << endl tmp << endl
<< endl << endl
<< std::string(30, decoration) << ' ' << msg << ' ' << std::string(30, decoration) << ' ' << msg << ' '
<< std::string(30, decoration) << endl; << std::string(30, decoration) << endl;
lograw(tmp.str());
} }
void logH2(string msg) void logH2(const string &msg)
{ {
stringstream tmp;
char decoration{ '-' }; char decoration{ '-' };
cout << endl tmp << endl
<< std::string(10, decoration) << ' ' << msg << ' ' << std::string(10, decoration) << ' ' << msg << ' '
<< std::string(10, decoration) << endl; << std::string(10, decoration) << endl;
lograw(tmp.str());
}
void lograw(const string &msg)
{
cerr << msg;
} }
} // namespace Log } // namespace Log
namespace Utils { namespace Utils {
string to_string(::pEp_identity *ident, bool full, int indent) string to_string(const ::pEp_identity *const ident, bool full, int indent)
{ {
stringstream builder; stringstream builder;
if (ident != nullptr) { if (ident != nullptr) {
@ -81,14 +96,14 @@ namespace pEp {
return builder.str(); return builder.str();
} }
std::string to_string(::identity_list *idl, bool full, int indent) std::string to_string(const ::identity_list *const 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 (const ::identity_list *curr = idl; curr != nullptr; curr = curr->next) {
builder << to_string(curr->ident, full, indent) << endl; builder << to_string(curr->ident, full, indent) << endl;
} }
indent--; indent--;
@ -100,7 +115,7 @@ namespace pEp {
return builder.str(); return builder.str();
} }
string to_string(::pEp_member *member, bool full, int indent) string to_string(const ::pEp_member *const member, bool full, int indent)
{ {
stringstream builder; stringstream builder;
if (member != nullptr) { if (member != nullptr) {
@ -118,14 +133,14 @@ namespace pEp {
return builder.str(); return builder.str();
} }
string to_string(::member_list *mbl, bool full, int indent) string to_string(const ::member_list *const 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 (const member_list *curr_member = mbl; curr_member != nullptr;
curr_member = curr_member->next) { curr_member = curr_member->next) {
builder << to_string(curr_member->member, full, indent) << endl; builder << to_string(curr_member->member, full, indent) << endl;
} }
@ -138,22 +153,21 @@ namespace pEp {
return builder.str(); return builder.str();
} }
string to_string(::pEp_group *group, bool full, int indent) string to_string(const ::pEp_group *const group, bool full, int indent)
{ {
stringstream builder; stringstream builder;
if (group != nullptr) { if (group != nullptr) {
builder << endl; builder << endl;
builder << std::string(indent, '\t') << "{" << endl; builder << std::string(indent, '\t') << "{" << endl;
indent++; indent++;
builder << std::string(indent, '\t') << "group_identity: "
<< to_string(group->group_identity, full, indent) << endl;
builder << std::string(indent, '\t') builder << std::string(indent, '\t')
<< "manager: " << to_string(group->manager, full, indent) << "group_identity: " << to_string(group->group_identity, full, indent)
<< endl; << endl;
builder << std::string(indent, '\t')
<< "manager: " << to_string(group->manager, full, indent) << endl;
builder << std::string(indent, '\t') << "active: " << group->active << endl; builder << std::string(indent, '\t') << "active: " << group->active << endl;
builder << std::string(indent, '\t') builder << std::string(indent, '\t')
<< "members: " << to_string(group->members, full, indent) << "members: " << to_string(group->members, full, indent) << endl;
<< endl;
indent--; indent--;
builder << std::string(indent, '\t') << "]"; builder << std::string(indent, '\t') << "]";
} else { } else {
@ -163,17 +177,31 @@ namespace pEp {
return builder.str(); return builder.str();
} }
void print_exception(const exception &e, int level) string nested_exception_to_string(const exception &e, int level, string src)
{ {
cerr << string(level, ' ') << "exception: " << e.what() << endl; src += string(level, ' ') + "exception: " + e.what() + "\n";
try { try {
rethrow_if_nested(e); rethrow_if_nested(e);
} catch (const exception &e) { } catch (const exception &e) {
print_exception(e, level + 1); src = nested_exception_to_string(e, level + 1, src);
} catch (...) { } catch (...) {
} }
return src;
} }
// 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 (...) {
// }
// }
// File utils // File utils
ofstream file_create(const string &filename) ofstream file_create(const string &filename)
{ {
@ -198,7 +226,7 @@ namespace pEp {
} }
} }
void file_ensure_not_existing(string path) void file_ensure_not_existing(const string &path)
{ {
while (file_exists(path)) { while (file_exists(path)) {
file_delete(path); file_delete(path);

32
test/framework/utils.hh

@ -13,31 +13,37 @@
namespace pEp { namespace pEp {
namespace Test { namespace Test {
namespace Log { namespace Log {
void logH1(std::string msg); void log(const std::string &msg);
void logH2(std::string msg); void logH1(const std::string &msg);
} void logH2(const std::string &msg);
void lograw(const std::string &msg);
} // namespace Log
namespace Utils { namespace Utils {
// pEpEngine datatypes to string // pEpEngine datatypes to string
std::string to_string(::pEp_identity *ident, bool full = true, int indent = 0); std::string to_string(const ::pEp_identity *const ident, bool full = true, int indent = 0);
std::string to_string(::identity_list *idl, bool full = true, int indent = 0); std::string to_string(const ::identity_list *const idl, bool full = true, int indent = 0);
std::string to_string(::pEp_member *member, bool full = true, int indent = 0); std::string to_string(const ::pEp_member *const member, bool full = true, int indent = 0);
std::string to_string(::member_list *mbl, bool full = true, int indent = 0); std::string to_string(const ::member_list *const mbl, bool full = true, int indent = 0);
std::string to_string(::pEp_group *group, bool full = true, int indent = 0); std::string to_string(const ::pEp_group *const group, bool full = true, int indent = 0);
// C++/STL data types to string // C++/STL data types to string
template<typename T> template<typename T>
std::string to_string(std::vector<T> v); std::string to_string(const std::vector<T> &v);
// exception utils // exception utils
void print_exception(const std::exception& e, int level = 0); std::string nested_exception_to_string(
const std::exception &e,
int level = 0,
std::string src = "");
void print_exception(const std::exception &e, int level = 0);
// file utils // file utils
std::ofstream file_create(const std::string &filename); std::ofstream file_create(const std::string &filename);
bool file_exists(const std::string &filename); bool file_exists(const std::string &filename);
void file_delete(const std::string &filename); void file_delete(const std::string &filename);
void file_ensure_not_existing(std::string path); void file_ensure_not_existing(const std::string &path);
} } // namespace Utils
} // namespace Test } // namespace Test
} // namespace pEp } // namespace pEp
#include "utils.hxx" #include "utils.hxx"

Loading…
Cancel
Save