home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,531 of 33,346   
   Casey Carter to DeMarcus   
   Re: Is this kind of static polymorphism    
   05 Sep 12 21:22:36   
   
   From: Casey_at_Carter_dot_net@dontspam.me.invalid   
      
   On 2012-09-05 17:02, DeMarcus wrote:   
   > What I want to do is an ultra-light (syntax-wise) yet powerful TDD   
   > framework. It looks like the following. It should compile but it's   
   > possible that the self-registering doesn't run perfectly due to the   
   > initialization order thing. This is just to show you the idea.   
   >   
   [snip code]   
   >   
   > Also another question I have is why I can't put the specialized   
   > runTests() function within the unnamed namespace as well? I get the   
   > following error from gcc 4.7.1.   
      
   runTests() must be defined in the namespace where it was declared,   
   i.e., the same namespace as MyTestFixture.   
      
   I don't see what benefit your approach has over plain-old polymorphism   
   like:   
      
   // ITestFixture.hpp   
   #pragma once   
   class ITestFixture   
   {   
   public:   
          static std::vector testFixtures;   
      
          virtual ~ITestFixture() {}   
          virtual void runAllTests() = 0;   
   };   
      
   // MyTestFixture.hpp   
   #pragma once   
   #include    
   #include    
   #include    
   #include    
   #include "ITestFixture.hpp"   
      
   class MyTestFixture : public ITestFixture   
   {   
   public:   
          MyTestFixture( const std::string& name )   
          {   
             // This is a self-registering object.   
             testFixtures.push_back( this );   
             std::cout << "Registering: " << name << std::endl;   
          }   
      
   protected:   
          void test( std::function function )   
          {   
             function();   
          }   
   };   
      
      
   // FreeFunctions1Test.cpp   
   // !!!  NOTE, it's a CPP file  !!!   
   //   
   // This file will be multiplied and specialized for all the   
   // different unit tests.   
   #include "MyTestFixture.hpp"   
      
   namespace   
   {   
   class FreeFunctionTestFixture : public MyTestFixture   
   {   
   public:   
          FreeFunctionTestFixture() : MyTestFixture( "Free Functions 1" )   
          {}   
      
   private:   
          void runAllTests()   
          {   
               test(   
         [&]   
                  {   
                     std::cout << "Test 1 for Free Functions 1" << std::endl;   
                  });   
      
               test(   
         [&]   
                  {   
                     std::cout << "Test 2 for Free Functions 1" << std::endl;   
                  });   
          }   
   };   
   FreeFunctionTestFixture tst;   
   }   
      
   // main.cpp   
   #include    
      
   #include "ITestFixture.hpp"   
      
   std::vector ITestFixture::testFixtures;   
      
   int main()   
   {   
          for( auto fixture : testFixtures )   
             fixture->runAllTests();   
      
          return 0;   
   }   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca