knowL: Knowledge Libraries
Loading...
Searching...
No Matches
Test.h
1#pragma once
2
3#include <QtTest/QtTest>
4
5#include "BigNumber.h"
6#include "Timestamp.h"
7#include "ReturnValue.h"
8#include "Uri.h"
9#include "Unit.h"
10#include "Value.h"
11
12namespace QTest
13{
14 inline bool qCompare(knowCore::Uri const &t1, const char* const &t2, const char *actual, const char *expected,
15 const char *file, int line)
16 {
17 return qCompare((QString)t1, QString::fromUtf8(t2), actual, expected, file, line);
18 }
19}
20
21namespace knowCore
22{
23 inline char *toString(const knowCore::Uri &t)
24 {
25 return QTest::toString((QString)t);
26 }
27 inline char *toString(const knowCore::Timestamp &t)
28 {
29 return QTest::toString(t.toEpoch<knowCore::NanoSeconds>().count());
30 }
31 inline char *toString(const knowCore::BigNumber &t)
32 {
33 return QTest::toString(t.toString());
34 }
35 inline char *toString(const knowCore::Value &t)
36 {
37 auto const [success, result, error] = t.printable();
38 if(success) {
39 return QTest::toString(result);
40 } else {
41 return QTest::toString(error);
42 }
43 }
44 template<typename _T_>
45 inline char *toString(const ReturnValue<_T_>& _t)
46 {
47 if(_t.success())
48 {
49 return QTest::toString(_t.value());
50 } else {
51 return toString(_t.message());
52 }
53 }
54 inline char *toString(const knowCore::Unit &t)
55 {
56 return t.isValid() ? QTest::toString(t.symbol()) : QTest::toString("invalid unit");
57 }
58}
59
60#define KNOWCORE_TEST_WAIT_FOR_ACT(_CONDITION_, _ACT_) \
61 for(int i = 0; i < 2000; ++i) \
62 { \
63 QThread::msleep(10); \
64 {_ACT_} \
65 auto ___expr___ = clog_eval_expr(_CONDITION_); \
66 if(___expr___.value()) break; \
67 if((i % 100) == 0) \
68 { \
69 KNOWCORE_LOG_INFO("Waiting for: {} since {}s", ___expr___, i/100); \
70 } \
71 }(void)0
72
73
74#define KNOWCORE_TEST_WAIT_FOR(_CONDITION_) \
75 KNOWCORE_TEST_WAIT_FOR_ACT(_CONDITION_, )
76
77
78#define KNOWCORE_TEST_VERIFY_SUCCESS_VOID(_EXPR_) \
79 do { \
80 auto const [__success__, __message__] = (_EXPR_); \
81 if (!QTest::qVerify(static_cast<bool>(__success__), #_EXPR_, qPrintable(__message__), \
82 __FILE__, __LINE__)) \
83 return; \
84 } while (false)
85
86
87#define KNOWCORE_TEST_VERIFY_FAILURE_VOID(_EXPR_) \
88 do { \
89 auto const [__success__, __message__] = (_EXPR_); \
90 if (!QTest::qVerify(static_cast<bool>(!__success__), #_EXPR_, \
91 qPrintable("Call succeeded, expected failure."), \
92 __FILE__, __LINE__)) \
93 return; \
94 } while (false)
95
96#define KNOWCORE_TEST_VERIFY_SUCCESS(_EXPR_) \
97 ([&]() { \
98 auto const [__success__, __result__, __message__] = (_EXPR_); \
99 QTest::qVerify(__success__, #_EXPR_, qPrintable(__message__), __FILE__, __LINE__); \
100 if(not __success__) \
101 { \
102 throw(std::runtime_error(__message__.toStdString())); \
103 } \
104 return __result__; \
105 })()
106
107#define KNOWCORE_TEST_VERIFY_FAILURE(_EXPR_) \
108 ([&]() { \
109 auto const [__success__, __result__, __message__] = (_EXPR_); \
110 QTest::qVerify(not __success__, #_EXPR_, \
111 qPrintable("Call succeeded, expected failure."), __FILE__, __LINE__); \
112 if(__success__) \
113 { \
114 throw(std::runtime_error(__message__.toStdString())); \
115 } \
116 return __result__; \
117 })()
118
119#define KNOWCORE_TEST_VERIFY_COMPARE(_EXPR_, _EXPECTED_) \
120 do { \
121 auto const [__success__, __result__, __message__] = (_EXPR_); \
122 if (!QTest::qVerify(static_cast<bool>(__success__), #_EXPR_, qPrintable(__message__), \
123 __FILE__, __LINE__)) \
124 return; \
125 if (!QTest::qCompare(__result__, _EXPECTED_, #_EXPR_, #_EXPECTED_, __FILE__, __LINE__)) \
126 return; \
127 } while (false)
Class that can contains large numeric value.
Definition BigNumber.h:46
Definition Timestamp.h:33
_unit_ toEpoch() const
Definition Timestamp.h:150
Definition Unit.h:16
QString symbol() const
Definition Unit.cpp:244
bool isValid() const
Definition Unit.cpp:279
Definition Uri.h:14
Definition Value.h:13