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