Re: c++ class design: where to put debug purpose utility class?

Discussion in 'C++' started by Phlip, Jun 27, 2008.

  1. Phlip

    Phlip Guest

    wrote:

    > Let's say I have the following class:
    >
    > class foo {
    > public:
    > foo();
    > void addItem(int item);
    >
    > private:
    > int* items;
    >
    > };
    >
    > I want to write a unit test program to test foo::addItem(), to do that
    > the test program needs to know the contents of foo::items.


    "Testing privates" is a FAQ in the unit testing communities.

    The best answers is: If you invented your class using Test Driven Development,
    then foo would expose enough effects of its private member - slightly more
    effects than its production clients need - that tests can indirectly assess the
    performance of the private.

    This strategy leverages the purpose of privacy - you can migrate int * items to
    some other system (such as std::vector<int> items!), without changing any tests.

    More below.

    > So I can add one more public function to class foo:
    > int* getItems();
    >
    > But suppose in my case, the client code of class foo does not need
    > getItems() at all, this function serves only for test in this case.
    >
    > So I am considering to have a separate class fooTest and this class is
    > a friend class of class foo so that it has an API to retrieve all
    > private data of foo.
    >
    > I believe this works, but I am wondering if there is better solution.
    > Basically my goals are:
    > 1. I do not want the class to be bloated with a lot of API which only
    > serve for test purpose
    > 2. I want the function class to be independent from test class


    To add unit tests to pre-existing, Read /Working Effectively with Legacy Code/
    by Mike Feathers. He will probably not mention the most savage hack possible in C++:

    #define private public

    If you cleanly recompile all your modules in that mode (such as modules going
    into a special "test mode" binaries folder), you can safely write unit tests
    that see any private. Make sure not to put that line above any #include that
    pulls in something you can't recompile in that mode, such as a standard library
    module.

    --
    Phlip
    Phlip, Jun 27, 2008
    #1
    1. Advertising

  2. Phlip

    James Kanze Guest

    Re: c++ class design: where to put debug purpose utility class?

    On Jun 27, 4:31 pm, Phlip <> wrote:
    > wrote:
    > > Let's say I have the following class:


    [...]
    > "Testing privates" is a FAQ in the unit testing communities.


    > The best answers is: If you invented your class using Test
    > Driven Development, then foo would expose enough effects of
    > its private member - slightly more effects than its production
    > clients need - that tests can indirectly assess the
    > performance of the private.


    Actually, I think that test driven development would have a
    slightly negative effect here. Greg made the real point: a
    function has post-conditions. If you can't see them, then they
    don't matter, and so aren't real post-conditions. And if you
    can see them, you verify that they work.

    But this concept really comes out of programming by contract,
    rather than test driven design (which can easily be used to
    avoid specifying the necessary contracts).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 27, 2008
    #2
    1. Advertising

  3. Phlip

    phlip Guest

    Re: c++ class design: where to put debug purpose utility class?

    James Kanze wrote:

    > But this concept really comes out of programming by contract,
    > rather than test driven design (which can easily be used to
    > avoid specifying the necessary contracts).


    What an unsupportable, straw-person argument.

    People doing TDD are forbidden to use any other techniques they have ever
    learned, folks!
    phlip, Jul 1, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGlmZmlkZW50?=

    "Page" class and utility class

    =?Utf-8?B?RGlmZmlkZW50?=, Jan 11, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    341
    =?UTF-8?B?IkFuZGVycyBOb3LDpXMgW01DQURdIg==?=
    Jan 11, 2005
  2. Gabriel Rossetti
    Replies:
    3
    Views:
    533
    Jerry Hill
    Apr 25, 2008
  3. Replies:
    8
    Views:
    384
    Greg Herlihy
    Jun 28, 2008
  4. Phlip
    Replies:
    36
    Views:
    859
    phlip
    Jul 2, 2008
  5. Garg
    Replies:
    0
    Views:
    305
Loading...

Share This Page