martinmoene / lest

A modern, C++11-native, single-file header-only, tiny framework for unit-tests, TDD and BDD (includes C++98 variant)
Boost Software License 1.0
390 stars 45 forks source link

A question regarding accessing private or protected variable #45

Closed mizhang closed 7 years ago

mizhang commented 7 years ago

I am new to this test framework. Wondering what is the standard way to access private or protected variables or functions to test in this framework. Didn't find useful results form google, so asking it here.

Thanks a lot!

martinmoene commented 7 years ago

lest has no support for directly accessing private or protected members.

You may test :

  1. indirectly via the public interface
  2. by extracting more involved code into it's own class with a public interface
  3. via a derived class (for protected members), struct TestClass : TestedClass { using protected_function; };
  4. via injecting the likes of friend class Tester;
  5. ...

Refrain from doing

#define protected public
#define private public
#include "foo.hpp"

If foo.hpp happens to #include stuff, especially something from std it can easily backfire.

Note: Information obtained from a thread of ACCU.org's accu-general mailing list.

mizhang commented 7 years ago

Thanks!!  Miya

On Wed, Jun 7, 2017 4:41 PM, Martin Moene notifications@github.com wrote: lest has no support for directly accessing private or protected members.

You may test :

  1. indirectly via the public interface
  2. by extracting more involved code into it's own class with a public interface
  3. via a derived class (for protected members), struct TestClass : TestedClass { using protected_function; };
  4. via injecting the likes of friend class Tester;
  5. ...

Refrain from doing

define protected public

define private public

include "foo.hpp"

If foo.hpp happens to #include stuff, especially something from std it can easily backfire.

Note: Information obtained from a thread of ACCU.org's accu-general mailing list.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.