What's the best way to write a re-usable chunk of code that carries
out some actions and performs some asserts/expects as it goes?
I'm imagining something like this:
void Foo(some parameters here)
{
...
EXPECT_EQ(something, x) << "bad x";
EXPECT_EQ(something, y) << "bad y";
ASSERT_GT(a, b) << "bad a,b";
}
something that creates ASSERT_FOO/EXPECT_FOO from Foo
TEST(x)
{
ASSERT_FOO(some parameters here);
EXPECT_FOO(some other parameters here);
}
Where the final output on failure includes references to both the line
in Foo itself that failed and the calling ASSERT_FOO line.
While it's possible to create something that does something similar
with *_PRED_FORMAT*, it doesn't look like I can use ASSERT_* or
EXPECT_* in the predicate routine, which is less convenient and makes
the predicate routine less readable.
I also had a look at using matchers from gmock (and ASSERT_THAT), but
those don't really seem suitable either since there isn't a value to
match against.
> Assertions in Sub-routines<http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Usin...>section
On Oct 15, 9:37 am, Vlad Losev <vl...@google.com> wrote:
> We don't have exactly what you are looking but please look at the "Using
> in GoogleTestAdvancedGuide wiki page.
I have. But use of SCOPED_TRACE with subroutines leads to fugly code
(because to be useful it has to be applied at the callsite, which
requires arbitrary blocks around each call), and it doesn't support
multiple arguments. (See http://code.google.com/p/googletest/issues/detail?id=207.)
On Wed, Oct 14, 2009 at 5:51 PM, Miral <go...@mirality.co.nz> wrote:
>
> On Oct 15, 9:37 am, Vlad Losev <vl...@google.com> wrote:
>> We don't have exactly what you are looking but please look at the "Using
>> Assertions in Sub-routines<http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Usin...>section
>> in GoogleTestAdvancedGuide wiki page.
>
> I have. But use of SCOPED_TRACE with subroutines leads to fugly code
> (because to be useful it has to be applied at the callsite, which
> requires arbitrary blocks around each call), and it doesn't support
> multiple arguments. (See http://code.google.com/p/googletest/issues/detail?id=207.)
For multiple arguments, write:
SCOPED_TRACE(Message() << "foo = " << foo);
> I've built something using EXPECT_PRED_FORMAT* for now, which is
> working ok. (Basically it creates a testing::Message object at the
> start and manually streams expectation failures to it as they happen,
> and returns it all at the end.) This isn't bad, but it's not
> composable.
I like using SCOPED_TRACE better. Perhaps you can try Vlad's
suggestion. Perhaps we should even make Vlad's macro part of gtest.
--
Zhanyong