On Fri, Jul 11, 2008 at 5:25 PM, balazs.dan <Balaz...@gmail.com> wrote:
>
> Hello Guys!
>
> As I see there is no support for exception tests.
> I know you dont like exceptions as your style guide says, but we
> mortal beings have to use it sometimes. :) Especially on windows where
> hard to avoid it.. I think it would be useful for many people.
Agreed.
> So is there any plan to add exception tester macros? Such as
> EXPECT_THROW, ASSERT_THROW, EXPECT_NO_THROW and so on. Of course, I
> can create a patch also. ;)
We don't have the time to do this now. It's great that you
volunteered! Before you actually write the code, would you please
describe the usage of the macros you want to add and give some
examples? Once we reach a consensus on the design, you can start to
work on the implementation and tests.
Also, would you please create an issue in the issue tracker for this?
> And I have a problem with exception handling in gtest!
> If a tested function throws one and you expect a boolean value, then
> gtest wont tell you where is the problem exactly. Here is an example:
> bool ThrowAnException()
> {
> throw 0;
> }
>
> TEST(Test, Exception)
> {
> EXPECT_TRUE(ThrowAnException());
> }
>
> Result is:
> [ RUN ] Test.Exception
> unknown file: Failure
> Exception thrown with code 0xe06d7363 in the test body.
> [ FAILED ] Test.Exception
>
> It could report the source if you surround the test code with a try
> catch block. As I see the best place for this would be in GTEST_ASSERT
> and GTEST_TEST_BOOLEAN macros. Because gtest must run with disabled
> exceptions this try catch block should be controlled by a define.
> What do you think?
Sounds good. I haven't had the time to think through this, but it may
be technically challenging or even impossible, as there are some other
constraints to be satisified:
- an assertion macro must behave like an atomic statement (e.g. you
can write code like "if (...) ASSERT_BLAH(...); else ...;").
- an assertion macro must be able to accept additional failure messages
via the << operator.
- an assertion macro must evaluate its arguments exactly once.
I'd encourage you to do some experiment to find out whether this is possible.
Thanks,
> Best regards,
> Balazs
>
--
Zhanyong
--
C. Keith Ray, IXP Coach, Industrial Logic, Inc.
http://industriallogic.com 866-540-8336 (toll free)
Groove with our Agile Greatest Hits: http://www.industriallogic.com/elearning/
http://homepage.mac.com/keithray/blog/index.html
I don't have a strong opinion on THROW vs THROWS. I can see merits either way.
We should allow a statement (not just an expression) as the first argument.
The user should also be able to describe additional properties the
exception value is expected to have, maybe using a predicate or
matcher (as used in our mocking framework). I like matchers better,
which means we may want to delay this work until our C++ mocking
framework is open-sourced in September. This would lead to something
like:
EXPECT_THROW(statement, FooException* e, e->HasBarProperty()); //
e->HasBarProperty() must be true.
EXPECT_THROW(statement, ..., true); // Any exception will do.
Since this is C++, we also need to consider memory management if the
exception value is a pointer type. This means (arggh) the user of the
macro needs to be able to specify how to delete/free the exception
value. We could provide a simplified version that always use delete
to reclaim the memory, but a general mechansim should also be provided
(like ASSERT_DEATH vs ASSERT_EXIT), e.g.
EXPECT_THROW_EX(statement, FooException* e, e->HasBarProperty(), delete e);
All things considered, this may not be as clean/declarative as one
might have hoped. I would recommend to take the time to figure out a
good design instead of rushing to the implementation.
Thanks,
>> _______________________________________________
>> gunit-users mailing list
>> gunit...@google.com
>> https://mailman.corp.google.com/mailman/listinfo/gunit-users
>
>
>
> --
> Keith Ray - A member of your friendly, neighborhood Testability Corps.
>
> http://big.corp.google.com/~mstriebeck/testmercenaries/
>
--
Zhanyong
While I expect a test that isn't explicitly documented to expect an
exception, to fail if an exception is thrown, sometimes I would want
to be more clear that something specifically doesn't throw an
exception by intention. ("test as spec")
I also note that boost has:
BOOST_WARN_NO_THROW
BOOST_CHECK_NO_THROW
BOOST_REQUIRE_NO_THROW
in its unit test support headers.
Good point. I was suggesting a 3-argument macro for the general case,
and agree that we should provide a 2-argument version for convenience.
Perhaps the general case that requires testing the property of the
exception is rare enough that we don't need to worry about it.
> try {
> DoSmthCrazy();
> FAIL();
> } catch (const std::exception& e) {
> EXPECT_EQ(e.what(), "crazy");
> }
Unrelated style nit: the two arguments to EXPECT_EQ should be
switched, by xUnit's convention. gtest's error messages are optimized
for that.
>> Since this is C++, we also need to consider memory management if the
>> exception value is a pointer type. This means (arggh) the user of the
>> macro needs to be able to specify how to delete/free the exception
>> value. We could provide a simplified version that always use delete
>> to reclaim the memory, but a general mechansim should also be provided
>> (like ASSERT_DEATH vs ASSERT_EXIT), e.g.
>>
>> EXPECT_THROW_EX(statement, FooException* e, e->HasBarProperty(), delete
>> e);
>
> You don't need to do that. In C++ exceptions are never thrown by pointer.
You can throw any copyable value, including a pointer. It is up to
the individual project to decide whether this is a good practice, but
C++ does not discriminate against that. In fact, this style is used
widely on Windows, e.g. in MFC.
>
> Roman.
--
Zhanyong
OK, so my experience is not typical. :)
I agree that our macros don't need to worry about exceptions thrown as
pointers. The user can always write his own try-catch if necessary.
> I think that the most common and most convenient EXPECT_THROW macro should
> take two arguments. You can always add _EX version with more arguments when
> you get a user request for it (or even better -- a patch). I would wait for
> someone to ask for such feature before adding it, because users might have
> some requirements that we are not aware of, therefore "preemptive
> implementation" may result in inconvenient interface for them.
>
> Note that this feature is absent from CppUnit and Boost.Test. IMHO, it
> indicates its usefulness from the user's perspective.
Agreed. Two-argument it is then.
Now, whoever wants to implement it, please make sure that it behaves
like a single statement and that we can stream additional messages to
it. See the implementation of death tests for an example.
Cheers,
> Roman.
--
Zhanyong
Thanks for the hard work!
2008/7/16 balazs.dan <Balaz...@gmail.com>:
>
> Sorry, a copy-paste error.. :)
>
> Correctly:
> will report:
> [ RUN ] Test.Exception2
> test.cc:123: Failure
> Expected exception: "long" not thrown from: ThrowAnException()
> additional message
> [ FAILED ] Test.Exception2
>
> Regards.
>
>
> On Jul 16, 9:51 pm, "balazs.dan" <Balazs....@gmail.com> wrote:
>> Hello Zhanyong,
>>
>> Sorry for the late answer.
>> I've finished my experiments and I was able to modify GTEST_ASSERT and
>> GTEST_TEST_BOOLEAN macros and still satisfy all 3 constraints while
>> reports more information about the source of the error!
COOOOOL! Looking forward to the patch.
>> Before my modification my test was reported this way:
>> TEST(Test, Exception)
>> {
>> EXPECT_TRUE(ThrowAnException()) << "additional message";
>>
>> }
>>
>> [ RUN ] Test.Exception
>> unknown file: Failure
>> Exception thrown with code 0xe06d7363 in the test body.
>> [ FAILED ] Test.Exception
>>
>> Now it says:
>> [ RUN ] Test.Exception
>> test.cc:113: Failure
>> Unhandled exception caught in: ThrowAnException()
>> [ FAILED ] Test.Exception
>>
>> Is this format acceptable for you?
Is it possible to include the exception code in the message as before?
"Unhandled exception caught in: statement" reads like that the
statement *caught* the exception. Perhaps "Unhandled exception thrown
by: statement"?
Did you forget the additional message?
Otherwise it looks great!
>> If yes, then I have to make some test test and i can submit the
>> patch..
>>
>> And I've created exception testing macros also!
You're on a roll! :)
>> TEST(Test, Exception2)
>> {
>> EXPECT_THROW(ThrowAnException(), long) << "additional message";
>>
>> }
>>
>> will report:
>> [ RUN ] Test.Exception2
>> test.cc:123: Failure
>> Expected exception: "long" not thrown from: ThrowAnException()
>> [ FAILED ] Test.Exception2
Does this work when the second argument is ...?
The double quotes around "long" make it look like a string. Someone
may think that the message means a string with the value "long" is
expected. Perhaps
Expected exception of type long: not thrown by ThrowAnException()
?
Best,
--
Zhanyong
I was just wondering. It will be great to have the additional message
in the output. If that cannot be done, well it's still an improvement.
>> Does this work when the second argument is ...?
>
> No. But why would be that good? :)
To assert that the code will throw something but you don't know or
don't care about its type?
I wonder why it doesn't work. If you expand EXPECT_THROW(statement,
ex) to something like:
...
try { statement; }
catch(ex) {
...
}
...
it shouldn't matter whether ex is ... or a type.
--
Zhanyong
You can nest two try-catch blocks:
try {
try { statement; } catch(ex) { ... }
} catch(...) { ... }
> 2. I'm using "catch (exceptiontype const&)".
I would think most exception objects are copyable, so
catch(exception_type) would be fine. In the rare case the exception
is not copyable, the user can always write
EXPECT_THROW(statement, const exception_type&);
Thanks,
--
Zhanyong
It's still useful: when it's false (the default), the user will get a
chance to attach a debugger (at least on Windows) to the process when
an exception is thrown. When it's true, gtest will handle the
exception and won't give the user a chance to debug.
I think the assertion macros should catch the exception only when the
flag is true. Is it possible?
> I've tried to check this flag in TEST macros,
> but it is not possible because you declared these flags in gtest-
> internal-inl.h and the comment says:
> "We don't want the users to modify these flags in the code, but want
> Google Test's own unit tests to be able to access them. Therefore we
> declare them here as opposed to in gtest.h."
>
> Well, I dont wanna modify it..
>
> Is there any other way to check this flag than
> GTEST_FLAG(catch_exceptions)?
You can add a read-only accessor bool GetCatchExceptionsFlag() to
include/gtest/internal/gtest-internal.h. Thanks,
> This question affects all my modifications and I need some help now..
>
> Thx,
> Balazs
>
--
Zhanyong
Most wonderful. Thank you!
We changed our dev guideline a bit. Now instead of attaching the
patch to the issue, we want to upload the patch to
http://codereview.appspot.com/ such that it can be easily reviewed. I
have done this for your other two patches - have you had the chance to
look at people's comments on them?
I can upload this patch again, but since it seems you'll be a frequent
contributor, it makes sense for you to learn to do it yourself. What
do you think? The instructions can be found on
http://code.google.com/p/rietveld/wiki/CodeReviewHelp. Thanks,
--
Zhanyong
Yes, if you don't mind. If you prefer, I can work on the existing
issues myself and submit a new patch later, but it will take a while.
To submit a new patch, go to http://codereview.appspot.com/2582, sign
in with your gmail account, and click on "Add Another Patch Set".
>
>> I can upload this patch again, but since it seems you'll be a frequent
>> contributor, it makes sense for you to learn to do it yourself. What
>> do you think? The instructions can be found onhttp://code.google.com/p/rietveld/wiki/CodeReviewHelp. Thanks,
>
> Please, upload this one now and next time I will try to do it myself.
I have uploaded it to http://codereview.appspot.com/2612. People,
please take a look.
>
> Thx,
> Balazs
>
--
Zhanyong
I'm new to this code review tool too, so I'm not sure. Did you see a
link "sign in" or "log in"? Did you sign in with your gmail account?
Thanks,
>
>
> Balazs
>
> On Jul 21, 9:02 pm, "Zhanyong Wan (λx.x x)" <w...@google.com> wrote:
>> On Fri, Jul 18, 2008 at 7:23 PM, balazs.dan <Balazs....@gmail.com> wrote:
>>
>> >> We changed our dev guideline a bit. Now instead of attaching the
>> >> patch to the issue, we want to upload the patch tohttp://codereview.appspot.com/suchthat it can be easily reviewed. I
>> >> have done this for your other two patches - have you had the chance to
>> >> look at people's comments on them?
>>
>> > Yes, I saw. Should I submit a new patch with those suggested
>> > modifications?
>>
>> Yes, if you don't mind. If you prefer, I can work on the existing
>> issues myself and submit a new patch later, but it will take a while.
>>
>> To submit a new patch, go tohttp://codereview.appspot.com/2582, sign
>> in with your gmail account, and click on "Add Another Patch Set".
>>
>>
>>
>> >> I can upload this patch again, but since it seems you'll be a frequent
>> >> contributor, it makes sense for you to learn to do it yourself. What
>> >> do you think? The instructions can be found onhttp://code.google.com/p/rietveld/wiki/CodeReviewHelp. Thanks,
>>
>> > Please, upload this one now and next time I will try to do it myself.
>>
>> I have uploaded it tohttp://codereview.appspot.com/2612. People,
>> please take a look.
>>
>>
>>
>> > Thx,
>> > Balazs
>>
>> --
>> Zhanyong
> _______________________________________________
> opensource-gtest mailing list
> opensour...@google.com
> https://mailman.corp.google.com/mailman/listinfo/opensource-gtest
>
--
Zhanyong
I'm not sure if only the original uploader can upload new versions of the patch.
Can you download upload.py and try
upload.py -i 1234
(where 1234 is the issue number) in your svn directory? Or you can
send the patch to me and I'll upload it.
Thanks,
--
Zhanyong
Sorry for the hassle. I didn't see any place where I can make you an
owner of the issue, so I added you as a reviewer. If you still cannot
upload, just send the patch to me and I'll upload it. Thank,
--
Zhanyong