Question

I'm working on a project where I've written almost all my test code in header files. I've done this primarily because I'm doing test driven development and this results in a large amount of complementary classes for each class I add: Interface, Test, Mock etc. I think I'd go crazy if I also had to deal with cpp versions of all these files...

I don't add "using namespace std" to the start of my headers because I've learned that this is a no, no. Anyway, lets say I currently initialise my Blob object at the start of a test, as follows:

Blob v =
    boost::assign::list_of(std::pair<std::string, Container >("Scotland",Container(boost::assign::list_of(1)(2)(3).convert_to_container<std::vector<int> >())))
    (std::pair<std::string, Container >("Sweden",Container()));

where Blob is typedefed somewhere as std::vector<std::pair<std::string, Container > >.

How can I make this prettier? The reason I'm using list_of is to make things more readable but in this case I think it makes it a lot more difficult to read. This is a lot better:

Blob v =
    list_of(pair<string, Container >("Scotland",Container(list_of(1)(2)(3).convert_to_container<vector<int> >())))
    (pair<string, Container >("Sweden",Container()));

but I can't do this in a header...

What might I do to solve this problem? I'm working with C++98.

UPDATE:

Just an idea. What if I renamed all my test headers to cpp files instead?

Was it helpful?

Solution

TDD requires short edit->compilation->run cycle times. Therefore you should write as much code as possible in cpp files to reduce compilation time. Nevertheless, you could solve your problem using a init function:

inline Blob InitBlob()
{
    using namespace boost;
    using namespace std;
    return assign::list_of(/*...*/);
}

Blob v = InitBlob();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top