|
Boost Users : |
Subject: Re: [Boost-users] [Test] How to Access Private Data from a Test
From: Jürgen Hunold (jhunold_at_[hidden])
Date: 2015-06-25 16:52:08
Hi Merrill,
Am Donnerstag, 25. Juni 2015, 14:37:30 schrieb Merrill Cornish:
> In the Bad-Old-Days when I had to roll my own, I would define my tests
> as methods of a special test class. If those tests needed to access
> private data in the classes under test, I would declare special test
> class as a friend of the class under test--one extra statement and
> everything worked.
In my opinion, a test should never access private data. But there are times
and legacy code where I had no other choice, too.
> How do I do that using Boost test (1.58.0) with the AUTO_TEST family of
> macros?
>
> In the labyrinth of macro expansions, I found a struct name with the
> argument name from BOOST_AUTO_TEST_CASE(), but a friend of that name
> doesn't work.
for BOOST_AUTO_TEST_CASE(foo_test)
use
friend struct ::foo_test;
Note the "::" for the global namespace though it might work without.
> The ideal case would be to declare the string name in
> BOOST_AUTO_TEST_SUITE() as a friend so one friend declaration does it
> all. Alternately, having to declare a separate friend for each
> BOOST_AUTO_TEST_CASE() would be tedious (especially for large classes),
> but it would work.
For BOOST_AUTO_TEST_SUITE(test_suite) use
friend struct test_suite::foo_test;
Rule of thumb: Each SUITE(bar) just add another namespace "bar" into the
chain.
> At the moment, I'm using the ole private=public trick:
>
> #define private public
> #include "header_under_test"
> #undef private
>
> However, I've already discovered one system header, <sstream>, that
> won't work with that kludge. There may be others.
Won't work with msvc because it mangles the access control specifiers into the
symbol name. No luck then...
> Is there another workaround for Boost Tests? I can't be the only one
> who has to access private data in tests.
Been there, done that. Just had to grep the examples out of one of my test
suites...
Yours,
Jürgen
-- * Dipl.-Math. Jürgen Hunold ! * voice: ++49 4257 300 ! Fährstraße 1 * fax : ++49 4257 300 ! 31609 Balge/Sebbenhausen * jhunold_at_gmx.eu ! Germany
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net