xref: /glogg/tests/test_utils.h (revision 1ee847ca425151b00d956eaea3ee654b7e55afc9)
1 #ifndef TEST_UTILS_H
2 #define TEST_UTILS_H
3 
4 #include "gmock/gmock.h"
5 
6 #include <string>
7 #include <chrono>
8 struct TestTimer {
9     TestTimer();
10     TestTimer(const std::string& text);
11     virtual ~TestTimer();
12     std::chrono::time_point<std::chrono::system_clock> Start;
13     std::chrono::time_point<std::chrono::system_clock> Stop;
14     std::chrono::microseconds Elapsed;
15     std::string text_;
16 };
17 
18 using namespace std;
19 
20 TestTimer::TestTimer()
21     : TestTimer( ::testing::UnitTest::GetInstance()->current_test_info()->test_case_name() ) {
22     text_ += string {"."} + string {::testing::UnitTest::GetInstance()->current_test_info()->name()};
23 }
24 
25 TestTimer::TestTimer(const string& text)
26     : Start { chrono::system_clock::now() }
27     , text_ {text} {}
28 
29 TestTimer::~TestTimer() {
30     Stop = chrono::system_clock::now();
31     Elapsed = chrono::duration_cast<chrono::microseconds>(Stop - Start);
32     cout << endl << text_ << " elapsed time = "
33         << Elapsed.count() * 0.001 << "ms" << endl;
34 }
35 
36 class SafeQSignalSpy : public QSignalSpy {
37   public:
38     template <typename... Args>
39     SafeQSignalSpy( Args&&... args )
40         : QSignalSpy( std::forward<Args>(args)... ) {}
41 
42     bool safeWait( int timeout = 10000 ) {
43         // If it has already been received
44         bool result = count() > 0;
45         if ( ! result ) {
46             result = wait( timeout );
47         }
48         return result;
49     }
50 };
51 
52 #endif
53