Welcome to mirror list, hosted at ThFree Co, Russian Federation.

testingmain.cpp « testing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 331af3cd0b9764a85dd40fc4b5e7eb059d714ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "testregister.hpp"
#include "testing.hpp"
#include "../base/logging.hpp"
#include "../std/algorithm.hpp"
#include "../std/iostream.hpp"
#include "../std/string.hpp"
#include "../std/vector.hpp"
#include "../std/target_os.hpp"

#ifdef OMIM_UNIT_TEST_WITH_QT_EVENT_LOOP
  #ifdef OMIM_OS_MAC // on Mac OS X native run loop works only for QApplication :(
    #include <QApplication>
    #define QAPP QApplication
  #else
    #include <QCoreApplication>
    #define QAPP QCoreApplication
  #endif
#endif

static bool g_bLastTestOK = true;

int main(int argc, char * argv[])
{
#ifdef OMIM_UNIT_TEST_WITH_QT_EVENT_LOOP
  QAPP theApp(argc, argv);
  UNUSED_VALUE(theApp);
#else
  UNUSED_VALUE(argc);
  UNUSED_VALUE(argv);
#endif

  my::g_LogLevel = LINFO;

  vector<string> testNames;
  vector<bool> testResults;
  int numFailedTests = 0;

  for (TestRegister * pTest = TestRegister::FirstRegister(); pTest; pTest = pTest->m_pNext)
  {
    string fileName(pTest->m_FileName);
    string testName(pTest->m_TestName);
    // Retrieve fine file name
    {
      int iFirstSlash = static_cast<int>(fileName.size()) - 1;
      while (iFirstSlash >= 0 && fileName[iFirstSlash] != '\\'  && fileName[iFirstSlash] != '/')
        --iFirstSlash;
      if (iFirstSlash >= 0)
        fileName.erase(0, iFirstSlash + 1);
    }

    testNames.push_back(fileName + "::" + testName);
    testResults.push_back(true);
  }

  int iTest = 0;
  for (TestRegister * pTest = TestRegister::FirstRegister(); pTest; ++iTest, pTest = pTest->m_pNext)
  {
    cerr << "Running " << testNames[iTest] << endl << flush;
    if (!g_bLastTestOK)
    {
      // Somewhere else global variables have been reset.
      cerr << "\n\nSOMETHING IS REALLY WRONG IN THE UNIT TEST FRAMEWORK!!!" << endl;
      return 5;
    }
    try
    {
      // Run the test.
      pTest->m_Fn();

      if (g_bLastTestOK)
      {
        cerr << "OK" << endl;
      }
      else
      {
        // You can set Break here if test failed,
        // but it is already set in OnTestFail - to fail immediately.
        testResults[iTest] = false;
        ++numFailedTests;
      }

    } catch (TestFailureException const & )
    {
      testResults[iTest] = false;
      ++numFailedTests;
    } catch (std::exception const & ex)
    {
      cerr << "FAILED" << endl << "<<<Exception thrown [" << ex.what() << "].>>>" << endl;
      testResults[iTest] = false;
      ++numFailedTests;
    } catch (...)
    {
      cerr << "FAILED" << endl << "<<<Unknown exception thrown.>>>" << endl;
      testResults[iTest] = false;
      ++numFailedTests;
    }
    g_bLastTestOK = true;
  }

  if (numFailedTests == 0)
  {
    cerr << endl << "All tests passed." << endl << flush;
    return 0;
  }
  else
  {
    cerr << endl << numFailedTests << " tests failed:" << endl;
    for (size_t i = 0; i < testNames.size(); ++i)
    {
      if (!testResults[i])
        cerr << testNames[i] << endl;
    }
    cerr << "Some tests FAILED." << endl << flush;
    return 1;
  }
}