Request to add ASSERT_SUBSTR to gtest.h

1,780 views
Skip to first unread message

Michael Kebe

unread,
Mar 21, 2012, 4:46:53 AM3/21/12
to Google C++ Testing Framework
There is no easy way to test that a string contains a substring.

I found ::testing::IsSubstring, but it should not directly called
(comment in gtest.h).

What do you think about adding two macros like this:

#define ASSERT_SUBSTR(needle, haystack)\
ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)

#define ASSERT_NSUBSTR(needle, haystack)\
ASSERT_PRED_FORMAT2(::testing::IsNotSubstring, needle, haystack)

Greetings
Michael

Dirk Meister

unread,
Mar 21, 2012, 5:11:55 AM3/21/12
to Michael Kebe, Google C++ Testing Framework
There are tons of interesting matchers in the companion project gmock:
http://code.google.com/p/googlemock/wiki/V1_6_CheatSheet#String_Matchers

Within others there is the HasSubstr(string) matcher.
The usage is than EXPECT_THAT(some_string, HasSubstr("foo"))

Even without using the mocking part of gmock, the matchers are worth a
look for gtest users.

Best regards,
Dirk

Michael Kebe

unread,
Mar 21, 2012, 6:31:55 AM3/21/12
to Dirk Meister, Google C++ Testing Framework
Great! Thanks!

Michael

Reply all
Reply to author
Forward
0 new messages