xref: /glogg/tests/test_utils.h (revision 74d66bf473e291335504e45e0cf472565f7db196)
11ee847caSNicolas Bonnefon #ifndef TEST_UTILS_H
21ee847caSNicolas Bonnefon #define TEST_UTILS_H
31ee847caSNicolas Bonnefon 
41ee847caSNicolas Bonnefon #include "gmock/gmock.h"
51ee847caSNicolas Bonnefon 
61ee847caSNicolas Bonnefon #include <string>
71ee847caSNicolas Bonnefon #include <chrono>
81ee847caSNicolas Bonnefon struct TestTimer {
TestTimerTestTimer9*74d66bf4SNicolas Bonnefon     TestTimer()
10*74d66bf4SNicolas Bonnefon         : TestTimer(
11*74d66bf4SNicolas Bonnefon                 ::testing::UnitTest::GetInstance()->current_test_info()->test_case_name() ) {
12*74d66bf4SNicolas Bonnefon     text_ += std::string {"."} + std::string {::testing::UnitTest::GetInstance()->current_test_info()->name() };
131ee847caSNicolas Bonnefon     }
141ee847caSNicolas Bonnefon 
TestTimerTestTimer15*74d66bf4SNicolas Bonnefon     TestTimer(const std::string& text)
16*74d66bf4SNicolas Bonnefon         : Start { std::chrono::system_clock::now() }
171ee847caSNicolas Bonnefon         , text_ {text} {}
181ee847caSNicolas Bonnefon 
~TestTimerTestTimer19*74d66bf4SNicolas Bonnefon     virtual ~TestTimer() {
20*74d66bf4SNicolas Bonnefon         using namespace std;
211ee847caSNicolas Bonnefon         Stop = chrono::system_clock::now();
221ee847caSNicolas Bonnefon         Elapsed = chrono::duration_cast<chrono::microseconds>(Stop - Start);
231ee847caSNicolas Bonnefon         cout << endl << text_ << " elapsed time = "
241ee847caSNicolas Bonnefon             << Elapsed.count() * 0.001 << "ms" << endl;
251ee847caSNicolas Bonnefon     }
261ee847caSNicolas Bonnefon 
27*74d66bf4SNicolas Bonnefon     std::chrono::time_point<std::chrono::system_clock> Start;
28*74d66bf4SNicolas Bonnefon     std::chrono::time_point<std::chrono::system_clock> Stop;
29*74d66bf4SNicolas Bonnefon     std::chrono::microseconds Elapsed;
30*74d66bf4SNicolas Bonnefon     std::string text_;
31*74d66bf4SNicolas Bonnefon };
32*74d66bf4SNicolas Bonnefon 
331ee847caSNicolas Bonnefon class SafeQSignalSpy : public QSignalSpy {
341ee847caSNicolas Bonnefon   public:
351ee847caSNicolas Bonnefon     template <typename... Args>
SafeQSignalSpy(Args &&...args)361ee847caSNicolas Bonnefon     SafeQSignalSpy( Args&&... args )
371ee847caSNicolas Bonnefon         : QSignalSpy( std::forward<Args>(args)... ) {}
381ee847caSNicolas Bonnefon 
391ee847caSNicolas Bonnefon     bool safeWait( int timeout = 10000 ) {
401ee847caSNicolas Bonnefon         // If it has already been received
411ee847caSNicolas Bonnefon         bool result = count() > 0;
421ee847caSNicolas Bonnefon         if ( ! result ) {
431ee847caSNicolas Bonnefon             result = wait( timeout );
441ee847caSNicolas Bonnefon         }
451ee847caSNicolas Bonnefon         return result;
461ee847caSNicolas Bonnefon     }
471ee847caSNicolas Bonnefon };
481ee847caSNicolas Bonnefon 
491ee847caSNicolas Bonnefon #endif
50