REQUIRE_THROWS on constructor causes error

298 views
Skip to first unread message

Torsten Anders

unread,
Jan 5, 2014, 8:15:16 PM1/5/14
to catch...@googlegroups.com
Dear all,

I just started using CATCH and I like it (easy to start, nicely readable test cases, small addition to project).

However, I ran into some difficulty when checking whether the construction of some object fails with an expected exception. 

For some project I implemented an easy workaround to have named and optional arguments for certain class constructors (I have a large number of arguments which all need to be optional): they simple expect a std::map (type called Args) containing my arguments.I tested that the following lines work.

MyClass x = {Args {{"expected_arg", "value"}}}; // works fine
MyClass y = {Args {{"buggy_arg", "value"}}}; // throws expected exception

However, I fail to write a CATCH test for the second line.

Here is something I tried:

REQUIRE_THROWS( MyClass {Args {{"buggy_arg", "value"}}}; )


This causes a compiler errors: "Use of undeclared identifier REQUIRE_THROWS". Note that this happens in a file where CATCH was used successfully before -- this error is seemingly caused somehow by the argument to the macro REQUIRE_THROWS. I tried a few variants of the above line, but could not get around that error.

BTW: The same error happens for the following line which simplifies the case above.  

REQUIRE_THROWS( std::vector<std::string> {"this", "is", "a", "test"}; );

Does CATCH perhaps not yet support C++11 initializer lists in its assertion macros?

Can you perhaps suggest any alternative way to do the test above? Thank you very much!

Kind regards,
Torsten

PS: I am new to C++ (used other languages before), so apologies in case there is some stupid misunderstanding of the language. 

--

Torsten Anders

http://www.torsten-anders.de




Torsten Anders

unread,
Jan 5, 2014, 8:18:35 PM1/5/14
to catch...@googlegroups.com
For completeness, I am using CATCH v1.0 build 23 with clang++ 500.2.79 (Xcode 5.0.2) on OS X 10.8.5. 

Torsten

Torsten Anders

unread,
Jan 5, 2014, 8:28:09 PM1/5/14
to catch...@googlegroups.com
Apologies for the noise -- I finally found an easy way around notating the following:

Args myArgs {{"buggy_arg", "value"}};

REQUIRE_THROWS( MyClass {myArgs} );

Sorry and good night,
Torsten


On Monday, January 6, 2014 1:15:16 AM UTC, Torsten Anders wrote:

Phil Nash

unread,
Jan 6, 2014, 2:26:18 AM1/6/14
to catch...@googlegroups.com
The problem is just the old one with macros and commas. Macros don't consider {'s (or ['s) as special characters, so see the first comma as the end of the first argument to the macro.

Your workaround works by avoiding the issue. Another workaround is just to place the whole expression in parentheses:

    REQUIRE_NOTHROW( ( std::vector<std::string> {"this", "is", "a", "test"} ) );

or, in this case, you can do just the initialiser list if you prefer:

    REQUIRE_NOTHROW( std::vector<std::string> ( {"this""is""a""test"} ) );

HTH,

[)o
IhIL..

Torsten Anders

unread,
Jan 6, 2014, 6:05:09 AM1/6/14
to catch...@googlegroups.com
I see, thanks! So far I only programmed macros in Lisp, need to read about them in C++...

Best,
Torsten

Phil Nash

unread,
Jan 10, 2014, 12:22:19 PM1/10/14
to catch...@googlegroups.com
I'm afraid you'll be very disappointed with C/C++ macros ;-)

Ugur Murat Erdem

unread,
Jan 30, 2014, 11:04:26 AM1/30/14
to catch...@googlegroups.com
Hi Phil,

When I try your suggestions:

   
    REQUIRE_NOTHROW
( std::vector<std::string> ( {"this""is""a""test"} ) );


I get the following error with gcc 4.3.4 on Linux:

error: expected primary-expression before ‘(’ token
error: expected primary-expression before ‘{’ token

Am I doing something wrong?

Murat
Reply all
Reply to author
Forward
0 new messages