Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
misc tests and fixing error::Error
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Sep 15, 2014
1 parent d70f9fe commit 0a8ce73
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 11 deletions.
14 changes: 12 additions & 2 deletions include/qtaround/error.hpp
Expand Up @@ -14,19 +14,29 @@

namespace qtaround { namespace error {

template <typename T>
QString dump(T && t)
{
QString s;
QDebug d(&s);
d << t;
return s;
}

class Error : public std::exception
{
public:
Error(QVariantMap const &from) : m(from) {}
virtual ~Error() noexcept(true) {}
virtual const char* what() const noexcept(true)
{
QString s;
QDebug(&s) << m;
if (s.isEmpty())
s = dump(m);
return s.toUtf8();
}

QVariantMap m;
mutable QString s;
};

static inline QDebug operator << (QDebug dst, error::Error const &src)
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Expand Up @@ -4,7 +4,7 @@ include_directories(${TUT_INCLUDES})
find_package(Qt5Core REQUIRED)
set(CMAKE_AUTOMOC TRUE)

set(UNIT_TESTS os dbus)
set(UNIT_TESTS os dbus misc)
set(TESTS_DIR /opt/tests/qtaround)

MACRO(UNIT_TEST _name)
Expand Down
131 changes: 131 additions & 0 deletions tests/misc.cpp
@@ -0,0 +1,131 @@
#include <qtaround/util.hpp>
#include <qtaround/os.hpp>
#include <tut/tut.hpp>
#include "tests_common.hpp"
#include <qtaround/util.hpp>

#include <QDebug>
#include <QVariant>

namespace os = qtaround::os;
namespace error = qtaround::error;
namespace util = qtaround::util;

namespace tut
{

struct misc_test
{
virtual ~misc_test()
{
}
};

typedef test_group<misc_test> tf;
typedef tf::object object;
tf vault_misc_test("misc");

enum test_ids {
tid_visit = 1
, tid_zip
, tid_parsebytes
, tid_error
};

template<> template<>
void object::test<tid_visit>()
{
QVariantMap src = {
{"b1", true},
{"m1", map({{"s1", "Str 1"}})},
{"l1", list({false, "List str"})}
};
QStringList expected = {"///"
, "QString//QString/b1/bool/true"
, "QString//QString/l1"
, "QString//QString/m1"
, "QString/l1/int/0/bool/false"
, "QString/l1/int/1/QString/List str"
, "QString/m1/QString/s1/QString/Str 1"};

QStringList dst;
auto fn = [&dst](QVariant const &ctx, QVariant const &key
, QVariant const &data) {
auto k = str(key);
QStringList v;
v << ctx.typeName() << str(ctx) << key.typeName() << k;
if (!(hasType(data, QMetaType::QVariantMap)
|| hasType(data, QMetaType::QVariantList)))
v << data.typeName() << str(data);
dst << v.join("/");
return k;
};
util::visit(fn, src, QVariant());
dst.sort();
expected.sort();
ensure_eq("Visited items", dst, expected);
}

template<> template<>
void object::test<tid_zip>()
{
QStringList l1, l2;
l1 << "A";
l2 << "a";
auto res = util::zip(l1, l2);
ensure_eq("Single item", res.size(), 1);
l1 << "B";
res = util::zip(l1, l2);
ensure_eq("2 items, 2nd empty", res.size(), 2);
l2 << "b";
res = util::zip(l1, l2);
ensure_eq("2 items", res.size(), 2);
l2 << "c";
res = util::zip(l1, l2);
ensure_eq("2 items, w/o 3rd", res.size(), 2);

}

template<> template<>
void object::test<tid_parsebytes>()
{
double res = util::parseBytes("10");
ensure_eq("plain bytes", res, 10);
res = util::parseBytes("10K");
ensure_eq("10K", res, 1024 * 10);
res = util::parseBytes("20Kb");
ensure_eq("10Kb", res, 1024 * 20);
res = util::parseBytes("30kib");
ensure_eq("30Kb", res, 1024 * 30);
res = util::parseBytes(" 40 kb ");
ensure_eq("40K", res, 1024 * 40);

ensure_throws<error::Error>("10 k b", &util::parseBytes, " 10 k b ", "b", 1024);

res = util::parseBytes("50mb");
ensure_eq("50mb", res, 1024 * 1024 * 50);

res = util::parseBytes("60Mib", "b", 1000);
ensure_eq("60Mib", res, 1000 * 1000 * 60);

res = util::parseBytes("70mb", "mb");
ensure_eq("70mb", res, 70);

res = util::parseBytes("1024kb", "mb");
ensure_eq("1mb", res, 1);

res = util::parseBytes("1024kb", "GB");
ensure_eq("1024kb=xGb", res, 1. / 1024.);
}

template<> template<>
void object::test<tid_error>()
{
try {
error::raise({{"a", 1}, {"b", 2}});
} catch (error::Error const &e) {
ensure_ne(AT, S_(e.what()), "");
}
}

}
6 changes: 6 additions & 0 deletions tests/os.cpp
Expand Up @@ -6,6 +6,7 @@

namespace os = qtaround::os;
namespace error = qtaround::error;
using error::dump;

namespace {

Expand Down Expand Up @@ -331,7 +332,12 @@ void object::test<tid_mountpoint>()
{
auto home = os::home();
auto mp = os::mountpoint(home);
ensure_ne("There should be some mountpoint", mp.size(), 0);
ensure(AT, os::path::isDescendent(home, mp));
auto s = os::stat(home, {{"fields", "m"}});
auto mp2 = s["mount_point"];
if (mp != "?")
ensure_eq("Should be the same mountpoint if it is not ?", mp2, mp);
}

template<> template<>
Expand Down
9 changes: 1 addition & 8 deletions tests/tests_common.hpp
Expand Up @@ -46,14 +46,7 @@ QStringList strings(QString const &s, A &&...args)
return QStringList(s) + strings(std::forward<A>(args)...);
}

template <typename T>
QString dump(T && t)
{
QString s;
QDebug d(&s);
d << t;
return s;
}
#define S_(...) strings(__VA_ARGS__).join(' ').toStdString()

}

Expand Down

0 comments on commit 0a8ce73

Please sign in to comment.