Salome HOME
5cf1089c8c8f7121f65d2e0cf1c6b42298ca9b1d
[modules/hydro.git] / src / HYDRO_tests / TestLib_Runner.cxx
1
2 /**
3   @file
4   \brief Implementation of the custom runner allowing start a subset of tests
5 */
6
7 #include <TestLib_Runner.h>
8 #include <cppunit/TestPath.h>
9 #include <cppunit/TestResult.h>
10 #include <algorithm>
11 #include <fstream>
12
13 /**
14   Conversion of the given string to lower-case
15   @param theStr the string to convert to lower-case
16   @return the converted string
17 */
18 std::string ToLower( const std::string& theStr )
19 {
20   std::string aStr = theStr;
21   std::transform( aStr.begin(), aStr.end(), aStr.begin(), ::tolower );
22   return aStr;
23 }
24
25 /**
26   Add a substring pattern to test runner
27   @param theSubstr the substring pattern to add
28 */
29 void TestLib_Runner::Add( const std::string& theSubstr )
30 {
31   mySubStrings.push_back( ToLower( theSubstr ) );
32 }
33
34 /**
35   Load the configuration file with substring patterns
36   @param theConfigFile the path to the configuration file
37   @return if the operation is successful
38 */
39 bool TestLib_Runner::Load( const std::string& theConfigFile )
40 {
41   std::ifstream anInput( theConfigFile.c_str() );
42   bool isOK = anInput.is_open();
43   if( !isOK )
44     return false;
45
46   std::string aLine;
47   while( std::getline( anInput, aLine ) )
48   {
49     int aPos = aLine.find( "//" );
50     if( aPos != std::string::npos )
51       aLine.erase( aPos );
52
53     //trim 
54     aLine.erase( 0, aLine.find_first_not_of( ' ' ) );
55     aLine.erase( aLine.find_last_not_of( ' ' ) + 1 );
56
57     if( !aLine.empty() )
58       Add( aLine );
59   }
60
61   return true;
62 }
63
64 /**
65   Determine if the given test name is allowed by patterns
66   @param theTestName the test name
67   @return true if the test name is allowed
68 */
69 bool TestLib_Runner::IsAllowed( const std::string& theTestName ) const
70 {
71   if( mySubStrings.empty() )
72     return true;
73
74   std::string aTestName = ToLower( theTestName );
75
76   std::list<std::string>::const_iterator anIt = mySubStrings.begin(), aLast = mySubStrings.end();
77   for( ; anIt!=aLast; ++anIt )
78     if( aTestName.find( *anIt ) != std::string::npos )
79       return true;
80
81   return false;
82 }
83
84
85 /**
86   Determine list of tests allowed by patterns from the given CPPUnit tests tree
87   @param theRoot the root of the CPPUnit tests tree
88   @param theAllowedTests the returned vector of allowed tests names
89 */
90 void TestLib_Runner::GetTests( CPPUNIT_NS::Test* theRoot, std::list<std::string>& theAllowedTests ) const
91 {
92   for( int i=0, n=theRoot->getChildTestCount(); i<n; i++ )
93   {
94     CPPUNIT_NS::Test* aTest = theRoot->getChildTestAt( i );
95     std::string aName = aTest->getName();
96     if( IsAllowed( aName ) )
97       theAllowedTests.push_back( aName );
98     else
99       GetTests( aTest, theAllowedTests );
100   }
101 }
102
103 /**
104   Run all necessary tests
105   @param theController the controller of the tests results
106 */
107 void TestLib_Runner::Run( CPPUNIT_NS::TestResult& theController )
108 {
109   std::list<std::string> aTests;
110   GetTests( m_suite, aTests );
111   std::list<std::string>::const_iterator anIt = aTests.begin(), aLast = aTests.end();
112   for( ; anIt!=aLast; ++anIt )
113   {
114     std::string aTestPath = *anIt;
115     CPPUNIT_NS::TestPath aPath = m_suite->resolveTestPath( aTestPath );
116     CPPUNIT_NS::Test* aTest = aPath.getChildTest();
117     theController.runTest( aTest );
118   }
119 }
120