Tests: Move util functions to utils.cc

LIB-12
heck 2021-04-18 22:34:01 +02:00
parent 082e886357
commit 50130c5caf
3 changed files with 55 additions and 46 deletions

View File

@ -4,6 +4,8 @@
#include "utils.hh"
#include <iostream>
#include <iostream>
#include <fstream>
#include <pEp/identity_list.h>
@ -18,7 +20,7 @@ namespace pEp {
namespace Log {
void logH1(string msg)
{
char decoration{'='};
char decoration{ '=' };
cout << endl
<< endl
<< std::string(30, decoration) << ' ' << msg << ' '
@ -27,7 +29,7 @@ namespace pEp {
void logH2(string msg)
{
char decoration{'-'};
char decoration{ '-' };
cout << endl
<< std::string(10, decoration) << ' ' << msg << ' '
<< std::string(10, decoration) << endl;
@ -35,9 +37,9 @@ namespace pEp {
} // namespace Log
namespace Utils {
string identity_to_string(::pEp_identity *ident, bool full, int indent)
{
stringstream builder;
if (ident != nullptr) {
if (full) {
@ -161,17 +163,48 @@ namespace pEp {
return builder.str();
}
void print_exception(const exception& e, int level)
void print_exception(const exception &e, int level)
{
cerr << string(level, ' ') << "exception: " << e.what() << endl;
try {
rethrow_if_nested(e);
} catch (const exception& e) {
} catch (const exception &e) {
print_exception(e, level + 1);
} catch (...) {
}
}
} // namespace Utils
// File utils
ofstream file_create(const string &filename)
{
ofstream outfile{ filename };
return outfile;
}
bool file_exists(const string &filename)
{
ifstream ifile(filename.c_str());
return (bool)ifile;
}
void file_delete(const string &filename)
{
int status = remove(filename.c_str());
if (status) {
runtime_error e{ string(
"file_delete(\"" + filename + "\") - " + strerror(errno)) };
throw(e);
}
}
void file_ensure_not_existing(string path)
{
while (file_exists(path)) {
file_delete(path);
}
}
} // namespace Utils
} // namespace Test
} // namespace pEp

View File

@ -17,16 +17,25 @@ namespace pEp {
void logH2(std::string msg);
}
namespace Utils {
// pEpEngine datatypes to string
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);
// C++/STL data types to string
template<typename T>
std::string vector_to_string(std::vector<T> v);
// exception utils
void print_exception(const std::exception& e, int level = 0);
// file utils
std::ofstream file_create(const std::string &filename);
bool file_exists(const std::string &filename);
void file_delete(const std::string &filename);
void file_ensure_not_existing(std::string path);
}
} // namespace Test
} // namespace pEp

View File

@ -1,51 +1,18 @@
#include "test_pEpSQLite.hh"
#include "../src/sqlite3.h"
#include "../src/pEpLog.hh"
#include "../src/pEpSQLite.hh"
#include "../src/pEpLog.hh"
#include "framework/utils.hh"
#include <exception>
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
using namespace pEp;
using namespace pEp::Test;
using namespace Test::Log;
ofstream file_create(const string& filename)
{
ofstream outfile{ filename };
return outfile;
}
bool file_exists(const string& filename)
{
ifstream ifile(filename.c_str());
return (bool)ifile;
}
void file_delete(const string& filename)
{
int status = remove(filename.c_str());
if (status) {
runtime_error e{ string("file_delete(\"" + filename + "\") - " + strerror(errno)) };
throw(e);
}
}
using namespace pEp::Test::Log;
using namespace pEp::Test::Utils;
namespace pEp {
namespace Test {
// Util
void ensure_file_not_existing(string path)
{
while (file_exists(path)) {
file_delete(path);
}
}
// --------------------------------- FIXTURES ---------------------------------
// filenames
string fixture_db_filename_new()
@ -82,7 +49,7 @@ namespace pEp {
// pEpLog("called");
string path = fixture_db_filename_new();
// cout << "fixture: \"" << path << "\" not existing" << endl;
ensure_file_not_existing(path);
file_ensure_not_existing(path);
return path;
}
@ -91,7 +58,7 @@ namespace pEp {
// pEpLog("called");
string path = "existing.db";
// cout << "fixture: \"" << path << "\" not existing" << endl;
ensure_file_not_existing(path);
file_ensure_not_existing(path);
return path;
}
@ -100,7 +67,7 @@ namespace pEp {
// pEpLog("called");
// cout << "creating corrupt db" << endl;
string path = fixture_db_filename_corrupt();
ensure_file_not_existing(path);
file_ensure_not_existing(path);
ofstream db_corrupt = file_create(path);
db_corrupt << "G4rbage" << endl;
db_corrupt.close();