@@ -1589,6 +1589,28 @@ class cppfront
15891589 }
15901590 }
15911591
1592+ if (parser.test_names ().size () > 0 ) {
1593+ if (cpp1_filename.back () == ' h' ) {
1594+ errors.emplace_back (
1595+ source_position{},
1596+ " tests should never be on headers, move them to a cpp2 file."
1597+ );
1598+ }
1599+ printer.print_extra ( " \n //=== Cpp2 test registry for this TU =====================================\n\n " );
1600+ printer.print_extra (" namespace {\n " );
1601+ auto & tests = parser.test_names ();
1602+ int test_count = 0 ;
1603+ for (auto test : tests) {
1604+ std::string test_decl = " cpp2::testing::auto_reg t" ;
1605+ test_decl += std::to_string (test_count++);
1606+ test_decl += " (&" ;
1607+ test_decl += test;
1608+ test_decl += " );\n " ;
1609+ printer.print_extra (test_decl);
1610+ }
1611+ printer.print_extra (" }" );
1612+ }
1613+
15921614 if (cpp1_filename.back () == ' h' ) {
15931615 printer.print_extra ( " \n #endif" );
15941616 }
@@ -4694,18 +4716,35 @@ class cppfront
46944716 return ;
46954717 }
46964718
4697- if (
4698- is_main
4699- && n.parameters ->parameters .size () > 0
4700- )
4719+ if (is_main)
47014720 {
4702- printer.print_cpp2 (
4703- " (int const argc_, char** argv_)" ,
4704- n.parameters ->position ()
4705- );
4706- current_functions.back ().prolog .statements .push_back (
4707- " auto const args = cpp2::make_args(argc_, argv_); "
4708- );
4721+ // Should probably be "are tests enabled?", to be able to run tests
4722+ // from different TUs.
4723+ bool const has_tests = parser.test_names ().size () > 0 ;
4724+ if (n.parameters ->parameters .size () > 0 ) {
4725+ printer.print_cpp2 (
4726+ " (int const argc_, char** argv_)" ,
4727+ n.parameters ->position ()
4728+ );
4729+ if (has_tests) {
4730+ current_functions.back ().prolog .statements .push_back (
4731+ " cpp2::testing::session::the().run(argc_, argv_); "
4732+ );
4733+ }
4734+ current_functions.back ().prolog .statements .push_back (
4735+ " auto const args = cpp2::make_args(argc_, argv_); "
4736+ );
4737+ }else {
4738+ printer.print_cpp2 (
4739+ " ()" ,
4740+ n.parameters ->position ()
4741+ );
4742+ if (has_tests) {
4743+ current_functions.back ().prolog .statements .push_back (
4744+ " cpp2::testing::session::the().run(0, nullptr); "
4745+ );
4746+ }
4747+ }
47094748 }
47104749 else {
47114750 emit (*n.parameters , false , false , generating_postfix_inc_dec);
0 commit comments