1

I am working on a c++ unit tester(mostly as practice) and had some questions about my implementation. I wanted to have the ability to overload my custom assertions so I decided to implement them as functions that I wrapped in a namespace.

My current implementation is as follows:

Tester.h
#include <string>

#define INFO __FILE__, __LINE__

namespace Tester
{
    void Assert(char const* input, char const* file_path, int line_number, std::string error_message);
    ...more overloaded Asserts and some Log functions...
}

And when I call the function:

#include "Tester.h"
...code...
    Tester::Assert(false, INFO, "Some message");
...code...

This works but I´m not sure that the 'INFO' macro is good practice. I welcome all suggestions and pointers about this implementation, and feel free to tell me if it makes no sense and I should be doing something completely different ;)

prazuber
  • 1,352
  • 10
  • 26

1 Answers1

0

INFO as a macro seems perfectly fine. An alternative, which I prefer is to have a macro

#define TEST_ASSERT(condition, message) Tester::Asssert(condition, __FILE__, __LINE__, message)

or something of that sort.

If you have different number of arguments, something like this may work:

#define TEST_ASSERT(condition, ...) Tester::Asssert(condition, __FILE__, __LINE__, __VA_ARGS__)

Assuming your compiler is new enough, that is.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • It's a pretty standard way of doing this, and yes, it makes it quite neat. [And, when you use it for non-test ASSERT, you can #define it to nothing, and still get rid off all your asserts - for example for production builds] – Mats Petersson Dec 23 '12 at 23:04
  • Thanks for the quick reply. That actually looks much better, I really want to get rid of that 'INFO', but then I have to define a new ASSERT macro for every overloaded instance of Assert. For instance: Tester::Assert(false, INFO, "Some message"); Tester::Assert(false, INFO, "Some message", "Error severity") These two could not use the same macro, since they take in a variable amount of parameters. Perhaps I should not be using overloaded Asserts? -Freddie – user1925608 Dec 23 '12 at 23:09
  • I've updated with a variadic macro that takes any number of arguments (including none). – Mats Petersson Dec 23 '12 at 23:12
  • Wow, thanks so much for that, its exactly what I was looking for! I had heard about __VA_ARGS__ but some forums discouraged its use, something about it not being standard if I recall correctly. But it works fine for me(visual studio 2010 compiler) -Freddie – user1925608 Dec 23 '12 at 23:21
  • @user1925608 As of the current ISO/IEC 14882:2011 standard variadic macros are compliant. See this [SO link](http://stackoverflow.com/questions/4786649/are-variadic-macros-nonstandard) (accepted answer is outdated). – IInspectable Dec 23 '12 at 23:37
  • Yeah, modern versions of gcc and MS compilers work fine. I thik VS 2005, perhaps also 2007, doesn't, but no one really cares about that these days... gcc has had it since at least 2002 or longer. – Mats Petersson Dec 23 '12 at 23:46
  • @Mats Variadic macros are available since VS 2005 and GCC 3.0. But don't fool yourself into thinking that noone cares about old compilers. You'd be surprised to find out how many projects still employ Visual C++ 6. – IInspectable Dec 24 '12 at 00:23
  • Yes, I'm aware that some people use ancient compilers/tools/operating systems (in fact I work for a company doing virtualization, and one key use-case is Windows XP, which won't run on off-the shelf computers these days, so they run XP in a VM...) – Mats Petersson Dec 24 '12 at 00:27