You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was thinking this morning about unit testing a factory class. With a certain set of parameters I need to check that the emitted object is of the correct type. I think this will be similar to the CHECK_EXCEPTION macro.
I'm assuming that in your example TypeA and TypeB share some inheritance hierarchy, and that DoSomethingThatShouldReturnAnObjectOfTypeA has a co-variant return type. If I'm wrong I may not quite be understanding the use case.
In the past when I've encountered the need to check the type of an object, I just use dynamic_cast. This has always been good enough for my purposes. What do you propose the benefits of CHECK_TYPE would be over something like the below?
In my opinion, UnitTest++ is a bit poor in its assertions set. C# for example has plenty of useful Assert.*
It could be great to have some of them, like type match, greater than, lower than.
By the way I would love to have a force-fail assertion, like FAIL("Message"); (but it is off topic).
As for the issue the types do not have to be from a hierarchy. In my use
case it is a factory/parser taking a stream of bytes and spewing out blocks
of data in neat little bundles in a tag despatch fashion.
It is not the same functionality, it should interrupt the test execution so it should better be REQUIRE CHECK(false); but this way does not allow to specify a failure message, it will show "error: CHECK(false);" which is not that expressive...
If you are not sure about the return type, it should be converted to void* before but I am not sure about the result of dynamic_cast in this case : CHECK(dynamic_cast<type*>(reinterpret_cast<void*>(var)));
@killoctal If I recall correctly, @pjohnmeyer has toyed around with adding custom failure messages to assertions in the past; perhaps you or he could sketch out what that might look like without introducing new macros?
Force fail with a custom message is currently achievable using throw, assuming you're building UnitTest++ with exception support on and are throwing a std::exception. That said, force fail is off the topic of the thread so let's stay on the main event.
As for the issue the types do not have to be from a hierarchy. In my use
case it is a factory/parser taking a stream of bytes and spewing out blocks
of data in neat little bundles in a tag despatch fashion.
@grahamreeds Can you post a minimal example of what the target code and the proposed unit test would look like?
Pretty much as it is in my original post. The idea revolves around messages coming in from udp source and a parser creating a tag that is passed to the dispatcher.
Activity
pjohnmeyer commentedon Sep 6, 2016
I'm assuming that in your example
TypeAandTypeBshare some inheritance hierarchy, and thatDoSomethingThatShouldReturnAnObjectOfTypeAhas a co-variant return type. If I'm wrong I may not quite be understanding the use case.In the past when I've encountered the need to check the type of an object, I just use
dynamic_cast. This has always been good enough for my purposes. What do you propose the benefits ofCHECK_TYPEwould be over something like the below?killoctal commentedon Sep 13, 2016
In my opinion, UnitTest++ is a bit poor in its assertions set. C# for example has plenty of useful Assert.*
It could be great to have some of them, like type match, greater than, lower than.
By the way I would love to have a force-fail assertion, like
FAIL("Message");(but it is off topic).grahamreeds commentedon Sep 13, 2016
Force fail is CHECK(false);
As for the issue the types do not have to be from a hierarchy. In my use
case it is a factory/parser taking a stream of bytes and spewing out blocks
of data in neat little bundles in a tag despatch fashion.
Sent from my Nexus 6P
On 13 Sep 2016 9:53 a.m., "Gabriel Schlozer" notifications@github.com
wrote:
killoctal commentedon Sep 13, 2016
It is not the same functionality, it should interrupt the test execution so it should better be
REQUIRE CHECK(false);but this way does not allow to specify a failure message, it will show "error: CHECK(false);" which is not that expressive...If you are not sure about the return type, it should be converted to void* before but I am not sure about the result of dynamic_cast in this case :
CHECK(dynamic_cast<type*>(reinterpret_cast<void*>(var)));ohz10 commentedon Sep 29, 2016
@killoctal you say "poor", I say minimalist.
ohz10 commentedon Sep 29, 2016
@killoctal If I recall correctly, @pjohnmeyer has toyed around with adding custom failure messages to assertions in the past; perhaps you or he could sketch out what that might look like without introducing new macros?
pjohnmeyer commentedon Oct 4, 2016
Force fail with a custom message is currently achievable using
throw, assuming you're building UnitTest++ with exception support on and are throwing astd::exception. That said, force fail is off the topic of the thread so let's stay on the main event.@grahamreeds Can you post a minimal example of what the target code and the proposed unit test would look like?
grahamreeds commentedon Oct 5, 2016
Pretty much as it is in my original post. The idea revolves around messages coming in from udp source and a parser creating a tag that is passed to the dispatcher.
Actual code would look like
In my little toy project neither
message_aormessage_bare related via a hierarchy but still go through the same dispatch routine.pjohnmeyer commentedon Jan 13, 2017
If compile-time dispatch techniques are in play, can't the compiler just be the check here?
Or is that not what you meant?