0

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?

10
  • 3
    can you explain why TDD development leads to all the code in header file ? Commented Apr 25, 2013 at 10:38
  • @Pradheep Its more convenient for me at least to not have to jump around between Foo.h, Foo.cpp, TestFoo.h, TestFoo.cpp, MockFoo.h, MockFoo.cpp and IFoo.h. Instead, I just have the .h versions. I also have to create all these files and put them in the correct folders. Commented Apr 25, 2013 at 10:40
  • well putting more code on the cpp file would mean more time for compilation and harder to debug the issues as everything will be same header file.Its not the convenience of the user that is at stake here but lesser build time and better modularity Commented Apr 25, 2013 at 11:04
  • You've learned that using namespace std; is a no no, but putting everything in the header files is a yes yes? Commented Apr 25, 2013 at 11:06
  • Nothing keeps you from having the definitions of the Interface, Mock, and Test in the same header as the class. The same way you can put the implementation into the same cpp file. Commented Apr 25, 2013 at 11:08

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.