Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attaching extra contextual information to REQUIRE() macro. #601

Open
fake-name opened this issue Mar 2, 2016 · 4 comments
Open

Attaching extra contextual information to REQUIRE() macro. #601

fake-name opened this issue Mar 2, 2016 · 4 comments

Comments

@fake-name
Copy link

Is there any (easy) way to attach contextual information to a REQUIRE macro?

I'd like to be able to do something like:

REQUIRE(expected_prog.size() > 0, "Program loaded from disk is empty! Either it's missing, or the file is damaged.");

Where the string "Program loaded from disk is empty! Either it's missing, or the file is damaged." is additionally printed to the output if the macro fails.

INFO() kind of does this, but the "INFO" stack is not cleared by a REQUIRE macro, so if I have a number of tests in the setup of a complex object, and the last one fails, it dumps all the INFO() messages to the console.

I can force scoping by putting each INFO()/REQUIRE() pair in a separate set of blocks, but when my setup procedure can involve 10+ calls that must be executed in sequence, it quickly gets hideous.


Before someone derails with comments about overly complex setup stuff, this is the setup procedure for a specialized piece of embedded hardware. Yes, it is that complicated, and I'm testing and exercising the low-level API. It's unavoidable.

@lightmare
Copy link
Contributor

You can write your own macro that wraps the other two in a block:

#define IREQUIRE(cond, msg) do { INFO(msg); REQUIRE(cond); } while(0)

IREQUIRE(expected_prog.size() > 0, "Program ...");

@fake-name
Copy link
Author

That works, but this seems like something that should be part of the core library. Python asserts work that way, though I don't know any other languages with this feature off the top of my head. C asserts don't do this, but then C asserts are a lot lower level.

Incidentally, why the do{}while(0)? Scoping with plain brackets seems to work fine.

@lightmare
Copy link
Contributor

That's a common idiom to swallow the semicolon.

@fake-name
Copy link
Author

Ah, I see, it prevents the macro from expanding into two statements. Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants