Changeset 1374

Show
Ignore:
Timestamp:
08/10/08 10:11:13 (3 months ago)
Author:
jtv
Message:

Standard main() macro for tests, so later we can run all tests in one program.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r1372 r1374  
    22 include/pqxx/connection.hxx, src/connection.cxx: 
    33  - Doc/comment update 
     4 include/pqxx/util.hxx: 
     5  - Fixed broken is_null for C-style strings 
    46 test/test000.cxx, test/test001.cxx, test/test002.cxx, test/test004.cxx, 
    57 test/test005.cxx, test/test006.cxx, test/test007.cxx, test/test008.cxx, 
     
    1012  - New TestCase::run() so normal tests don't need pqxx::test::pqxxtest() 
    1113  - Rigged up test setup so it runs with nullconnection 
    12   - Consistently use TestCase for tests 
     14  - Standard main() macro for tests, so later we can do all tests in 1 program 
    1315  - Improved PQXX_CHECK_THROWS 
    1416  - More thorough testing of test helpers 
  • trunk/test/test_helpers.hxx

    r1372 r1374  
    2929  int line() const throw () { return m_line; } 
    3030}; 
    31  
    32  
    33 // Run a libpqxx test function. 
    34 template<typename TESTFUNC> 
    35 inline int pqxxtest(TESTFUNC &func) 
    36 { 
    37   try 
    38   { 
    39     func(); 
    40   } 
    41   catch (const test_failure &e) 
    42   { 
    43     PGSTD::cerr << "Test failure in " + e.file() + " line " +  
    44         to_string(e.line()) << ": " << e.what() << PGSTD::endl; 
    45     return 1; 
    46   } 
    47   catch (const PGSTD::bad_alloc &) 
    48   { 
    49     PGSTD::cerr << "Out of memory!" << PGSTD::endl; 
    50     return 50; 
    51   } 
    52   catch (const sql_error &e) 
    53   { 
    54     PGSTD::cerr << "SQL error: " << e.what() << PGSTD::endl 
    55          << "Query was: " << e.query() << PGSTD::endl; 
    56     return 1; 
    57   } 
    58   catch (const PGSTD::exception &e) 
    59   { 
    60     PGSTD::cerr << "Exception: " << e.what() << PGSTD::endl; 
    61     return 2; 
    62   } 
    63   catch (...) 
    64   { 
    65     PGSTD::cerr << "Unknown exception" << PGSTD::endl; 
    66     return 100; 
    67   } 
    68  
    69   return 0; 
    70 } // pqxxtest() 
    7131 
    7232 
     
    13999 
    140100  // Run test, catching errors & returning Unix-style success value 
    141   int run() { return pqxxtest(*this); } 
     101  int run() 
     102  { 
     103    try 
     104    { 
     105      (*this)(); 
     106    } 
     107    catch (const test_failure &e) 
     108    { 
     109      PGSTD::cerr << "Test failure in " + e.file() + " line " +  
     110          to_string(e.line()) << ": " << e.what() << PGSTD::endl; 
     111      return 1; 
     112    } 
     113    catch (const PGSTD::bad_alloc &) 
     114    { 
     115      PGSTD::cerr << "Out of memory!" << PGSTD::endl; 
     116      return 50; 
     117    } 
     118    catch (const sql_error &e) 
     119    { 
     120      PGSTD::cerr << "SQL error: " << e.what() << PGSTD::endl 
     121           << "Query was: " << e.query() << PGSTD::endl; 
     122      return 1; 
     123    } 
     124    catch (const PGSTD::exception &e) 
     125    { 
     126      PGSTD::cerr << "Exception: " << e.what() << PGSTD::endl; 
     127      return 2; 
     128    } 
     129    catch (...) 
     130    { 
     131      PGSTD::cerr << "Unknown exception" << PGSTD::endl; 
     132      return 100; 
     133    } 
     134 
     135    return 0; 
     136  } 
    142137 
    143138private: 
     
    146141  testfunc m_func; 
    147142}; 
     143 
     144 
     145// Register a function taking (connection_base &, transaction_base &) as a test. 
     146#define PQXX_REGISTER_TEST(function) \ 
     147        int main() \ 
     148        { \ 
     149          pqxx::test::TestCase<> test(#function, (function)); \ 
     150          return test.run(); \ 
     151        } 
     152 
     153 
     154// Register test function that doesn't access the database. 
     155#define PQXX_REGISTER_TEST_NODB(function) \ 
     156        int main() \ 
     157        { \ 
     158          pqxx::test::TestCase<nullconnection, nontransaction> \ 
     159                test(#function, (function)); \ 
     160          return test.run(); \ 
     161        } 
    148162 
    149163 
     
    298312} // namespace pqxx 
    299313 
     314 
  • trunk/test/unit/test_escape.cxx

    r1370 r1374  
    6363        "Connection and transaction quote differently."); 
    6464} 
     65 
     66 
     67void test_escaping(connection_base &c, transaction_base &t) 
     68{ 
     69  test_esc(c, t); 
     70  test_quote(c, t); 
     71} 
    6572} // namespace 
    6673 
    67 int main() 
    68 
    69   test::TestCase<> test1("test_esc", test_esc); 
    70   return test1.run(); 
    71 
    72  
     74PQXX_REGISTER_TEST(test_escaping) 
  • trunk/test/unit/test_float.cxx

    r1368 r1374  
    44using namespace pqxx; 
    55 
     6namespace 
     7{ 
    68template<typename T> T make_infinity() 
    79{ 
     
    1416} 
    1517 
    16 namespace 
    17 
    18 void infinity_test() 
     18void infinity_test(connection_base &, transaction_base &) 
    1919{ 
    2020  double inf = make_infinity<double>(); 
     
    2222  PQXX_CHECK_EQUAL(to_string(-inf), "-infinity", "Negative infinity is broken"); 
    2323} 
    24 } 
     24} // namespace 
    2525 
    26 int main() 
    27 
    28   return pqxx::test::pqxxtest(infinity_test); 
    29 
    30  
     26PQXX_REGISTER_TEST_NODB(infinity_test) 
  • trunk/test/unit/test_pipeline.cxx

    r1370 r1374  
    5151} // namespace 
    5252 
    53 int main() 
    54 
    55   test::TestCase<> test("pipeline_detach", test_pipeline_detach); 
    56   return test.run(); 
    57 
    58  
     53PQXX_REGISTER_TEST(test_pipeline_detach) 
  • trunk/test/unit/test_simultaneous_transactions.cxx

    r1370 r1374  
    1818} // namespace 
    1919 
    20 int main() 
    21 
    22   test::TestCase<> test("simultaneous_trans", test_simultaneous_transactions); 
    23   return test.run(); 
    24 
    25  
     20PQXX_REGISTER_TEST(test_simultaneous_transactions) 
  • trunk/test/unit/test_stateless_cursor.cxx

    r1371 r1374  
    8585} // namespace 
    8686 
    87 int main() 
    88 
    89   test::TestCase<> test("stateless_cursor", test_stateless_cursor); 
    90   return test.run(); 
    91 
    92  
     87PQXX_REGISTER_TEST(test_stateless_cursor) 
  • trunk/test/unit/test_test_helpers.cxx

    r1372 r1374  
    142142 
    143143 
    144 int main() 
    145 
    146   TestCase<nullconnection, nontransaction> test( 
    147         "test_test_helpers", 
    148         test_test_helpers); 
    149   return test.run(); 
    150 
    151  
     144PQXX_REGISTER_TEST_NODB(test_test_helpers)