<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5549969176545377403</id><updated>2011-07-07T15:49:58.024-07:00</updated><title type='text'>Musings on C++ Testing Tools</title><subtitle type='html'>Testing rocks.  That is, if you have the proper tools.

In this blog we share our thoughts on C++ testing tools and techniques.  In particular, we discuss the two open-source C++ testing tools we work on, &lt;a href="http://code.google.com/p/googletest/"&gt;Google Test&lt;/a&gt; and &lt;a href="http://code.google.com/p/googlemock/"&gt;Google Mock&lt;/a&gt;.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-2149650785201829795</id><published>2009-10-05T09:40:00.000-07:00</published><updated>2009-10-05T09:43:36.297-07:00</updated><title type='text'>Another Testing-on-the-Toilet on Matchers</title><content type='html'>Today we published another Testing-on-the-Toilet episode about matchers on the &lt;a href="http://googletesting.blogspot.com/"&gt;Google Testing Blog&lt;/a&gt;.  It shows how easy it is to define custom matchers that enhance your tests' readability.  &lt;a href="http://googletesting.blogspot.com/2009/10/tott-making-perfect-matcher.html"&gt;Check it out&lt;/a&gt;!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-2149650785201829795?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/2149650785201829795/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/10/another-testing-on-toilet-on-matchers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/2149650785201829795'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/2149650785201829795'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/10/another-testing-on-toilet-on-matchers.html' title='Another Testing-on-the-Toilet on Matchers'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-5064322402758979181</id><published>2009-09-30T16:32:00.000-07:00</published><updated>2009-09-30T16:40:04.049-07:00</updated><title type='text'>Testing-on-the-Toilet Featuring Google Mock Matchers</title><content type='html'>As some of you know, Google publishes a "&lt;a href="http://googletesting.blogspot.com/search/label/TotT"&gt;testing on the toilet&lt;/a&gt;" series on its &lt;a href="http://googletesting.blogspot.com/"&gt;testing blog&lt;/a&gt; from time to time.  These are basically posts available in nicely-formatted PDF that you can print and hang up in the toilet at your workplace.  Each episode talks about a particular testing technique or trick.&lt;br /&gt;&lt;br /&gt;This week, a "testing on the toilet" episode about matchers in Google Mock was published.  You can read it and download the PDF &lt;a href="http://googletesting.blogspot.com/2009/09/tott-literate-testing-with-matchers.html"&gt;here&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-5064322402758979181?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/5064322402758979181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/09/testing-on-toilet-featuring-google-mock.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/5064322402758979181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/5064322402758979181'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/09/testing-on-toilet-featuring-google-mock.html' title='Testing-on-the-Toilet Featuring Google Mock Matchers'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-3994691646899339881</id><published>2009-06-16T11:49:00.001-07:00</published><updated>2009-06-16T11:49:37.286-07:00</updated><title type='text'>Google Mock Internals: Converting Matchers Safely</title><content type='html'>After spending the last 4 weeks on writing code, I&amp;#39;m finally ready to get back to blogging.  In my &lt;a href="http://cpptesting.blogspot.com/2009/05/google-mock-internals-how-not-to.html"&gt;last post&lt;/a&gt;, I introduced the basic typing rules for Google Mock&amp;#39;s matchers.  At the end of the post, the reader is left with this question:&lt;br&gt;  &lt;br&gt;When we use a composite matcher &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Not(m)&lt;/span&gt; in a context where a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt; is expected, what type should we require &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;m&lt;/span&gt; to have?&lt;br&gt;  &lt;br&gt;As we have seen, it is too restrictive to demand &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;m&lt;/span&gt; being a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt;, as it prevents you from using &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Not(an_int_matcher)&lt;/span&gt; as a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;short&amp;gt;&lt;/span&gt;, even if you can convert the &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;short&lt;/span&gt; argument to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;int&lt;/span&gt; before giving it to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;an_int_matcher&lt;/span&gt;.&lt;br&gt;  &lt;br&gt;To design the typing rule here in a non-restrictive yet safe way, we need to distinguish between two kinds of conversions between monomorphic matchers: &lt;i&gt;safe&lt;/i&gt; conversions and &lt;i&gt;unsafe&lt;/i&gt; conversions:&lt;br&gt;&lt;br&gt;As an example of a safe conversion, you can use a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;int&amp;gt;&lt;/span&gt; (let&amp;#39;s call it &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;m&lt;/span&gt;) as a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;const int&amp;amp;&amp;gt;&lt;/span&gt;: you just pass the argument (typed as &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;const int&amp;amp;&lt;/span&gt;) to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;m&lt;/span&gt;, which takes an &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;int&lt;/span&gt;.  No information will be lost in the process.  The converse is not true: if you use a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;const int&amp;amp;&amp;gt;&lt;/span&gt; as a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;int&amp;gt;&lt;/span&gt;, you may get a wrong result as the &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;const int&amp;amp;&amp;gt;&lt;/span&gt; may be interested in the address of the argument (as in the example of &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Ref(x)&lt;/span&gt;).&lt;br&gt;  &lt;br&gt;But what dos it &lt;i&gt;exactly&lt;/i&gt; mean for a conversion to be &lt;b&gt;safe&lt;/b&gt;?&lt;br&gt;&lt;br&gt;In general, if type &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;A&lt;/span&gt; can be implicitly converted to type &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;B&lt;/span&gt;, we can safely convert a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;B&amp;gt;&lt;/span&gt; to a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;A&amp;gt;&lt;/span&gt; (i.e. &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&lt;/span&gt; is contravariant): just keep a copy of the original &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;B&amp;gt;&lt;/span&gt;, convert the argument from type &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;A&lt;/span&gt; to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;B&lt;/span&gt;, and then pass it to the underlying &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;B&amp;gt;&lt;/span&gt;.  There are two exceptions to this rule:&lt;br&gt;  &lt;ol&gt;&lt;li&gt;When &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;B&lt;/span&gt; is a reference and &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;A&lt;/span&gt; is not, the conversion is unsafe as the underlying &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;B&amp;gt;&lt;/span&gt; may be interested in the argument&amp;#39;s address, which is not preserved in the conversion from &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;A&lt;/span&gt; to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;B&lt;/span&gt;.&lt;/li&gt;  &lt;li&gt;&lt;i&gt;Lossy&lt;/i&gt; conversions between built-in numeric types are implicit in C++.  That is, as far as the compiler is concerned, you can cast a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;double&lt;/span&gt; to an &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;int&lt;/span&gt; implicitly.  Therefore, for conversions between built-in numeric types, we impose the additional constraint that they must not be lossy (e.g. converting from &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;int&lt;/span&gt; to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;long&lt;/span&gt; is fine, but vice versa is not).&lt;/li&gt;  &lt;/ol&gt;If it&amp;#39;s still unclear to you why #2 is necessary, consider this example:&lt;br&gt;&lt;br&gt;&lt;div style="margin-left: 40px;"&gt; &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;int&amp;gt; equals_5 = Eq(5);&lt;/span&gt;&lt;br style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;  &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt; Matcher&amp;lt;double&amp;gt; double_equals_5 = SafeMatcherCast&amp;lt;double&amp;gt;(equals_5);&lt;/span&gt;&lt;br style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;  &lt;/div&gt;&lt;br&gt;If you use &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;double_equals_5&lt;/span&gt; to match &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;5.2&lt;/span&gt;, you&amp;#39;ll get &amp;quot;yes&amp;quot;, even though &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;5.2&lt;/span&gt; isn&amp;#39;t equal to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;5&lt;/span&gt;.&lt;br&gt;  &lt;br&gt;Before we end this post, let recap the basic typing rules for matchers:&lt;br&gt;&lt;ul&gt;&lt;li&gt;Matchers are statically typed.&lt;/li&gt;&lt;li&gt;A monomorphic matcher that matches an &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;int&lt;/span&gt; value has type &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;int&amp;gt;&lt;/span&gt;.&lt;/li&gt;  &lt;li&gt;&lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;int&amp;gt;&lt;/span&gt; and &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;const int&amp;amp;&amp;gt;&lt;/span&gt; are different types: a matcher of the latter type can see the address of its argument, while a matcher of the former type can&amp;#39;t.&lt;/li&gt;  &lt;li&gt;A polymorphic matcher &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;m&lt;/span&gt; has a type that can be implicitly converted to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt; if &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;m&lt;/span&gt; can be used to match a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;T&lt;/span&gt; value.&lt;/li&gt;  &lt;li&gt;A &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;B&amp;gt;&lt;/span&gt; can be safely converted to a &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;Matcher&amp;lt;A&amp;gt;&lt;/span&gt; as long as &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;A&lt;/span&gt; can be implicitly converted to &lt;span style="color: rgb(51, 51, 255); font-family: courier new,monospace;"&gt;B&lt;/span&gt;, the conversion is not from a non-reference to a reference, and it&amp;#39;s not a lossy numeric conversion.&lt;/li&gt;  &lt;/ul&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-3994691646899339881?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/3994691646899339881/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/06/google-mock-internals-converting.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/3994691646899339881'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/3994691646899339881'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/06/google-mock-internals-converting.html' title='Google Mock Internals: Converting Matchers Safely'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-42174464080372642</id><published>2009-05-19T00:22:00.000-07:00</published><updated>2009-05-19T00:54:49.457-07:00</updated><title type='text'>Google Mock Internals: How Not to Compare Apples to Oranges</title><content type='html'>Earlier, we wrote about how matchers can help you &lt;a href="http://cpptesting.blogspot.com/2009/04/readable-test-code-and-readable-test.html"&gt;validate values&lt;/a&gt; in tests and how you can easily &lt;a href="http://cpptesting.blogspot.com/2009/04/create-your-own-matchers-with-ease.html"&gt;define your own matchers&lt;/a&gt;.  The latter post also shows that some matchers can match more than one type of values.  We can them &lt;b&gt;polymorphic matchers&lt;/b&gt;.  Naturally, matchers that accept only one type of values are called &lt;b&gt;monomorphic matchers&lt;/b&gt;.&lt;br&gt; &lt;br&gt;Since C++ is a statically-typed language, matchers in &lt;a href="http://code.google.com/p/googlemock/"&gt;Google Mock&lt;/a&gt; are statically typed.  Namely, a monomorphic matcher that validates a value of type &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;T&lt;/span&gt; is implemented as an object of class &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt;.  This lets the compiler catch the user error of giving the matcher a value with the wrong type.  Failing early is far better than returning a bogus answer!&lt;br&gt; &lt;br&gt;In Google Mock, the context where a matcher is used determines what type of value it receives, and hence determines the matcher&amp;#39;s type. For example, if your mock object has a method &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Foo()&lt;/span&gt; that takes an &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;int&lt;/span&gt; argument, you can write&lt;br&gt; &lt;br&gt; &lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;EXPECT_CALL(mock, Foo(an_int_matcher));&lt;/span&gt;&lt;br style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt; &lt;/div&gt;&lt;br&gt;to verify that &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Foo()&lt;/span&gt; will be called with an argument that matches &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;an_int_matcher&lt;/span&gt;.  Here, &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;an_int_matcher&lt;/span&gt; is an expression of type &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;int&amp;gt;&lt;/span&gt;, and the compiler enforces that.&lt;br&gt; &lt;br&gt;So far it has been simple.  It gets more interesting when the argument is passed by reference, in which case the argument type becomes &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;const T&amp;amp;&lt;/span&gt; instead of &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;T&lt;/span&gt; (let&amp;#39;s ignore the possibility of &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;T&amp;amp;&lt;/span&gt; for now) .  What type of matcher should we use to match such an argument?&lt;br&gt; &lt;br&gt;The answer is &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;const T&amp;amp;&amp;gt;&lt;/span&gt;.  This type is considered &lt;i&gt;not equivalent &lt;/i&gt;to &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt;, so you cannot use the two interchangeably. Such distinction is necessary for catching user errors (again).  For example, the standard &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Ref(x)&lt;/span&gt; matcher matches a reference value that references &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;x&lt;/span&gt;.  Its type is &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;const T&amp;amp;&amp;gt;&lt;/span&gt; where &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;T&lt;/span&gt; is &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;x&lt;/span&gt;&amp;#39;s type.  If you say&lt;br&gt; &lt;br&gt;&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;EXPECT_CALL(mock, Foo(Ref(an_int_variable)));&lt;/span&gt;&lt;br&gt;&lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;&lt;/span&gt;&lt;/div&gt; &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;&lt;br&gt;&lt;/span&gt;where &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Foo()&lt;/span&gt; takes an &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;int &lt;/span&gt;argument, you will get a compiler error - a good thing since &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Ref(an_int_variable)&lt;/span&gt; doesn&amp;#39;t make sense for an &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;int&lt;/span&gt; value.&lt;br&gt; &lt;br&gt;Yet often we don&amp;#39;t care about if a value is passed by value or by reference.  After all, you cannot tell them apart by looking at the call site.  This, is when a polymorphic matcher is useful.&lt;br&gt;&lt;br&gt;The type of a polymorphic matcher is neither &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt; nor &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;const T&amp;amp;&amp;gt;&lt;/span&gt;.  Instead, it&amp;#39;s a type that can be &lt;i&gt;implicitly converted&lt;/i&gt; to &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;A&amp;gt;&lt;/span&gt;, where &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;A&lt;/span&gt; is the argument type.  &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;A&lt;/span&gt; can be either a reference or a non-reference.&lt;br&gt; &lt;br&gt;A frequently-used polymorphic matcher is &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Eq(x)&lt;/span&gt;, which matches any value equal to &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;x&lt;/span&gt;.  For instance, &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Eq(5)&lt;/span&gt; can be used as &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;int&amp;gt;&lt;/span&gt;, &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;const int&amp;amp;&amp;gt;&lt;/span&gt;, &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;short&amp;gt;&lt;/span&gt;, etc, depending on the context.&lt;br&gt; &lt;br&gt;In &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;EXPECT_THAT(value, matcher)&lt;/span&gt;, the context doesn&amp;#39;t make it clear whether &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;matcher&lt;/span&gt; should b a &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt; or a &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;const T&amp;amp;&amp;gt;&lt;/span&gt;, where &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;T&lt;/span&gt; is the type of &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;value&lt;/span&gt;.  To be safe, we treat &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;matcher&lt;/span&gt; as a &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;const T&amp;amp;&amp;gt;&lt;/span&gt; here.  The reason is two-fold: &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;T&lt;/span&gt; may not be copyable (in which case &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt; won&amp;#39;t compile as it requires passing &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;T&lt;/span&gt; by value), and &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;matcher&lt;/span&gt; may be interested in the address of &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;value&lt;/span&gt; (as in &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Ref(x)&lt;/span&gt;).&lt;br&gt; &lt;br&gt;It gets even more interesting when we consider the typing rule for composite matchers like &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Not(m)&lt;/span&gt;.  Suppose we are using &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Not(m)&lt;/span&gt; where a &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt;  is expected.  Since we need to pass the value (with type &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;T&lt;/span&gt;) to the underlying matcher &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;m&lt;/span&gt;, an obvious idea is to require that &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;m&lt;/span&gt; has type &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;T&amp;gt;&lt;/span&gt; as well.  While this is safe, it is also overly restrictive.  In particular, it doesn&amp;#39;t allow us to use &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Not(an_int_matcher)&lt;/span&gt; as a &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;Matcher&amp;lt;short&amp;gt;&lt;/span&gt;, even though we could convert the &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;short&lt;/span&gt; argument to &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;int&lt;/span&gt; and then feed it to &lt;span style="font-family: courier new,monospace; color: rgb(51, 51, 255);"&gt;an_int_matcher&lt;/span&gt;.&lt;br&gt; &lt;br&gt;In the next post, we will reveal how we define composite matchers in a safe yet non-restrictive way, and how we solve the problem of safely converting between matcher types in general.&lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-42174464080372642?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/42174464080372642/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/05/google-mock-internals-how-not-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/42174464080372642'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/42174464080372642'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/05/google-mock-internals-how-not-to.html' title='Google Mock Internals: How Not to Compare Apples to Oranges'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-928651044300591224</id><published>2009-04-17T22:44:00.001-07:00</published><updated>2009-04-17T22:44:12.837-07:00</updated><title type='text'>Create Your Own Matchers with Ease</title><content type='html'>&lt;div class="gmail_quote"&gt;&lt;div class="gmail_quote"&gt;Earlier I showed how you can use Google Mock matchers to &lt;a href="http://cpptesting.blogspot.com/2009/04/readable-test-code-and-readable-test.html" target="_blank"&gt;make both your test code and your test output readable&lt;/a&gt;.  But what if the matcher you need isn&amp;#39;t provided by Google Mock?&lt;br&gt;  &lt;br&gt;Don&amp;#39;t fret -- you can roll your own matchers easily, either by building on existing ones or writing them from scratch. For the former, Google Mock gives you several &lt;a href="http://code.google.com/p/googlemock/wiki/CheatSheet#Composite_Matchers" target="_blank"&gt;composite matchers&lt;/a&gt;.  For the latter, you can use some simple macros.&lt;/div&gt; &lt;div class="gmail_quote"&gt;&lt;br&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;&lt;b&gt;Writing a Composite Matcher&lt;/b&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;&lt;br&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;The simplest composite matcher is &lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;&lt;b&gt;Not&lt;/b&gt;(&lt;i&gt;matcher&lt;/i&gt;)&lt;/span&gt;, which negates the given matcher, as you probably have guessed.&lt;br&gt;  &lt;br&gt;Here&amp;#39;s an example: &lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;ContainsRegex(&lt;i&gt;s&lt;/i&gt;)&lt;/span&gt; is a matcher verifying that a string contains regular expression &lt;i style="font-family:courier new,monospace"&gt;&lt;span style="color:rgb(51, 51, 255)"&gt;s&lt;/span&gt;&lt;/i&gt; as a substring.  To verify that a string does &lt;i&gt;not&lt;/i&gt; contain the regular expression, write:&lt;br&gt;  &lt;div style="margin-left:40px"&gt;&lt;font class="Apple-style-span" color="#3333FF" face="&amp;#39;courier new&amp;#39;" size="3"&gt;&lt;span class="Apple-style-span" style="font-size: 12px;"&gt;&lt;b&gt;&lt;br&gt;&lt;/b&gt;&lt;/span&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style="margin-left:40px"&gt; &lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;EXPECT_THAT(GetUserResponse(), &lt;b&gt;Not&lt;/b&gt;(ContainsRegex(&amp;quot;Acho+!&amp;quot;)));&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;&lt;br&gt;&lt;/div&gt;which may produce a message like:&lt;br&gt;  &lt;div style="margin-left:40px"&gt;&lt;font class="Apple-style-span" color="#FF0000" face="&amp;#39;courier new&amp;#39;" size="3"&gt;&lt;span class="Apple-style-span" style="font-size: 12px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style="margin-left:40px"&gt; &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;Value of: GetUserResponse()&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;&lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;Expected: &lt;b&gt;doesn&amp;#39;t contain regular expression &amp;quot;Acho+!&amp;quot;&lt;/b&gt;&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;  &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;  Actual: &amp;quot;Ah! Achooo!&amp;quot;&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&lt;br&gt;&lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;&lt;b&gt;AnyOf&lt;/b&gt;(&lt;i&gt;matcher_1&lt;/i&gt;, ..., &lt;i&gt;matcher_k&lt;/i&gt;)&lt;/span&gt; is another composite matcher you can use.  It asserts that at least one of the &lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;i&gt;k&lt;/i&gt;&lt;/font&gt;&lt;/font&gt; matchers matches the value.  Therefore&lt;br&gt;  &lt;div style="margin-left:40px"&gt;&lt;font class="Apple-style-span" color="#3333FF" face="&amp;#39;courier new&amp;#39;" size="3"&gt;&lt;span class="Apple-style-span" style="font-size: 12px;"&gt;&lt;b&gt;&lt;br&gt;&lt;/b&gt;&lt;/span&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style="margin-left:40px"&gt; &lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;EXPECT_THAT(s, &lt;b&gt;AnyOf&lt;/b&gt;(StartsWith(&amp;quot;Wow&amp;quot;), ContainsRegex(&amp;quot;Acho+!&amp;quot;)));&lt;/span&gt;&lt;br&gt;  &lt;/div&gt;&lt;div class="gmail_quote"&gt;&lt;br&gt;&lt;/div&gt;may print:&lt;br&gt; &lt;div style="margin-left:40px"&gt;&lt;font class="Apple-style-span" color="#FF0000" face="&amp;#39;courier new&amp;#39;" size="3"&gt;&lt;span class="Apple-style-span" style="font-size: 12px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style="margin-left:40px"&gt; &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;Value of: s&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt; &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;Expected: &lt;b&gt;(starts with &amp;quot;Wow&amp;quot;) or (contains regular expression &amp;quot;Acho+!&amp;quot;)&lt;/b&gt;&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;   &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;  Actual: &amp;quot;Aha!&amp;quot;&lt;/span&gt;&lt;br&gt; &lt;/div&gt; &lt;br&gt;Note how the matcher descriptions are combined.  There is also &lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; color: rgb(51, 51, 255); "&gt;&lt;b&gt;AllOf&lt;/b&gt;(&lt;i&gt;matcher_1&lt;/i&gt;, ..., &lt;i&gt;matcher_k&lt;/i&gt;)&lt;/span&gt; for asserting that all &lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;i&gt;k&lt;/i&gt;&lt;/font&gt;&lt;/font&gt; matchers match the value.&lt;/div&gt; &lt;div class="gmail_quote"&gt;&lt;br&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;&lt;b&gt;Writing a Matcher from Scratch&lt;/b&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;&lt;br&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;Defining matchers from scratch is easy too.  Just use the &lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;&lt;b&gt;MATCHER&lt;/b&gt;()&lt;/span&gt; or &lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;&lt;b&gt;MATCHER_P&lt;/b&gt;&lt;i&gt;n&lt;/i&gt;()&lt;/span&gt; macro.  Here are some examples:&lt;b&gt;&lt;br&gt; &lt;/b&gt;&lt;b&gt; &lt;/b&gt;&lt;br&gt; To define a &lt;b&gt;simple matcher&lt;/b&gt; to verify that a number is even, just write:&lt;br&gt; &lt;br&gt; &lt;div style="margin-left:40px"&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;MATCHER&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;(&lt;b&gt;IsEven&lt;/b&gt;, &amp;quot;&amp;quot;) { return (&lt;/font&gt;&lt;b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;arg&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt; % 2) == 0; }&lt;/font&gt;&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(0, 0, 153)"&gt;  &lt;/div&gt; &lt;span style="font-family:courier new,monospace;color:rgb(0, 0, 153)"&gt;&lt;br&gt; &lt;/span&gt;Ignore the &lt;font class="Apple-style-span" color="#3333FF"&gt;&amp;quot;&amp;quot;&lt;/font&gt; in the argument list for now.  As you probably have guessed, the matcher tests whether the argument (referred to by &lt;b&gt;&lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;arg&lt;/span&gt;&lt;/b&gt;) is divisible by 2 -- you can put any code you want inside the &lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;{}&lt;/font&gt;&lt;/font&gt; to verify the argument.  With that, you can write:&lt;br&gt;  &lt;br&gt; &lt;div style="margin-left:40px"&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;/span&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;EXPECT_THAT&lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;(Foo(5), &lt;/font&gt;&lt;b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;IsEven&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;());&lt;/font&gt;&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(0, 0, 153)"&gt;  &lt;/div&gt;   &lt;br&gt; and it may print:&lt;br&gt; &lt;br&gt;  &lt;div style="margin-left:40px"&gt;&lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;Value of: Foo(5)&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;&lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt; Expected: &lt;b&gt;is even&lt;/b&gt;&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;  &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;   Actual: 9&lt;/span&gt;&lt;br style="color:rgb(0, 153, 0)"&gt;&lt;/div&gt;     &lt;br&gt; The matcher description &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;&amp;quot;is even&amp;quot;&lt;/span&gt; is &lt;i&gt;automatically calculated&lt;/i&gt; from the matcher&amp;#39;s name.  As long as you pick a good name for your matcher, you get the readable description for free.&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;br&gt; &lt;/span&gt;&lt;/b&gt;&lt;br&gt; A matcher can have parameters (as in the example of &lt;span style="font-family:courier new,monospace;color:rgb(51, 51, 255)"&gt;&lt;b&gt;ContainsRegex&lt;/b&gt;(&amp;quot;...&amp;quot;)&lt;/span&gt;).  Such &lt;b&gt;parameterized matchers&lt;/b&gt; can be defined using &lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;b&gt;MATCHER_P&lt;/b&gt;&lt;/font&gt;&lt;/font&gt; or &lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;b&gt;MATCHER_P&lt;/b&gt;&lt;i&gt;n&lt;/i&gt;&lt;/font&gt;&lt;/font&gt;, where &lt;i&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;n&lt;/font&gt;&lt;/font&gt;&lt;/i&gt; is the number of parameters:&lt;br&gt;   &lt;br&gt; &lt;div style="margin-left:40px"&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;b&gt;&lt;span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;MATCHER_P&lt;/font&gt;&lt;/span&gt;&lt;/b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;(Contains, &lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;element&lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;, &amp;quot;&amp;quot;) {&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt; &lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;   return std::find(&lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;arg&lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;.begin(), &lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;arg&lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;.end(), &lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;element&lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;) != &lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;arg&lt;/font&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;.end();&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt; &lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;}&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;...&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt; &lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;EXPECT_THAT&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;(prime_set, Not(Contains(12)));&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;EXPECT_THAT&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;(user_list, Contains(my_user_id));&lt;/font&gt;&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(0, 0, 153)"&gt;  &lt;/div&gt;         &lt;br&gt; may print:&lt;br&gt; &lt;br&gt;  &lt;div style="margin-left:40px"&gt;&lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;Value of: prime_set&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;&lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt; Expected: not (contains 12)&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;  &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;   Actual: { 2, 3, 5, 7, 12 }&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;  &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt; Value of: user_list&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;&lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt; Expected: contains &amp;quot;wan&amp;quot;&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;  &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;   Actual: { &amp;quot;joe&amp;quot;, &amp;quot;jon&amp;quot;, &amp;quot;nat&amp;quot; }&lt;/span&gt;&lt;br&gt;&lt;/div&gt;           &lt;br&gt; Note that &lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;Contains()&lt;/font&gt;&lt;/font&gt; is polymorphic: it was first used to match a&lt;span style="font-family:courier new,monospace"&gt; &lt;font class="Apple-style-span" color="#3333FF"&gt;set&amp;lt;int&amp;gt;&lt;/font&gt;&lt;/span&gt;, and then used to match a &lt;span style="font-family:courier new,monospace"&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;list&amp;lt;string&amp;gt;&lt;/font&gt;&lt;/span&gt;.  The description &lt;span style="font-family:courier new,monospace"&gt;&lt;font class="Apple-style-span" color="#FF0000"&gt;&amp;#39;contains &amp;quot;wan&amp;quot;&amp;#39;&lt;/font&gt; &lt;/span&gt;is &lt;i&gt;automatically generated&lt;/i&gt; from the matcher name &lt;span style="font-style:italic"&gt;and&lt;/span&gt; its parameter -- you don&amp;#39;t need to worry about a thing.&lt;/div&gt; &lt;div class="gmail_quote"&gt;&lt;br&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;Also note that you don&amp;#39;t need to write the type of the value being matched and the parameter types: the macro &lt;i&gt;automatically infers&lt;/i&gt; all of them for you too.&lt;br&gt; &lt;/span&gt; &lt;/b&gt;&lt;br&gt;What if you aren&amp;#39;t happy with the auto-generated description?  You can override it.  Just write a &lt;i&gt;description pattern&lt;/i&gt; instead of &lt;font class="Apple-style-span" color="#3333FF"&gt;&amp;quot;&amp;quot;&lt;/font&gt; inside the &lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;MATCHER*()&lt;/font&gt;&lt;/font&gt; macro:&lt;br&gt;  &lt;br&gt; &lt;div style="margin-left:40px"&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;MATCHER_P2&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;(InClosedRange, low, high, &lt;/font&gt;&lt;b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&amp;quot;is in range [%(low)s, %(high)s]&amp;quot;&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;) {&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt; &lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;   return low &amp;lt;= arg &amp;amp;&amp;amp; arg &amp;lt;= high;&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;}&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt; &lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;...&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;EXPECT_THAT(my_age, InClosedRange(adult_min, can_withdraw_401k));&lt;/font&gt;&lt;/span&gt;&lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;br style="font-family: &amp;#39;courier new&amp;#39;, monospace; "&gt; &lt;/font&gt; &lt;/div&gt;          &lt;br&gt; may print:&lt;br&gt; &lt;br&gt;  &lt;div style="margin-left:40px"&gt;&lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;Value of: my_age&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;&lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt; Expected: &lt;b&gt;is in range [18, 60]&lt;/b&gt;&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;  &lt;span style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;   Actual: 71&lt;/span&gt;&lt;br style="font-family:courier new,monospace;color:rgb(255, 0, 0)"&gt;&lt;/div&gt;    &lt;br&gt;Note how you can refer to the matcher parameters in the description pattern.&lt;/div&gt;&lt;div class="gmail_quote"&gt;&lt;br&gt;&lt;/div&gt;&lt;div class="gmail_quote"&gt;Interested?  You can learn more about the usage of &lt;font class="Apple-style-span" color="#3333FF"&gt;&lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;, monospace"&gt;MATCHER*&lt;/font&gt;&lt;/font&gt; &lt;a href="http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Matchers_Quickly"&gt;here&lt;/a&gt;.&lt;br&gt; &lt;/div&gt;&lt;/div&gt;&lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-928651044300591224?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/928651044300591224/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/04/create-your-own-matchers-with-ease.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/928651044300591224'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/928651044300591224'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/04/create-your-own-matchers-with-ease.html' title='Create Your Own Matchers with Ease'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-923247096748034817</id><published>2009-04-09T15:52:00.000-07:00</published><updated>2009-04-10T11:58:31.808-07:00</updated><title type='text'>Readable Test Code and Readable Test Output - Get Both Using Matchers</title><content type='html'>&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;Who doesn't like readable code?  The benefit is obvious: bugs are harder to hide in it, and you can change it more easily when the requirement changes.&lt;br /&gt;&lt;br /&gt;Test code is code too.  Thus it also benefits from being readable.  In fact, I'd argue that  &lt;span style="font-weight: bold;"&gt;readability is even more important for test code, as you have to be able to trust it&lt;/span&gt; (remember that most tests don't have their own tests).&lt;br /&gt;&lt;br /&gt;If you have used a mocking framework before, you are probably familiar with the concept of a &lt;span style="font-style: italic;"&gt;matcher&lt;/span&gt;: you give it a value, and it tells you whether the value has a certain property -- if yes, we say that it &lt;i&gt;matches&lt;/i&gt; the value.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For example, in &lt;a href="http://code.google.com/p/googlemock/"&gt;Google Mock&lt;/a&gt;, &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;a href="http://code.google.com/p/googlemock/wiki/CheatSheet#String_Matchers"&gt;ContainsRegex("Ahcho+!")&lt;/a&gt;&lt;/span&gt;&lt;/span&gt; is a matcher that matches any string that has the regular expression &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;"Ahcho+!"&lt;/span&gt;&lt;/span&gt; in it.  For instance, it matches &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;"Ahchoo!"&lt;/span&gt;&lt;/span&gt; and &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;"Ahchoooo!  Sorry."&lt;/span&gt;&lt;/span&gt;, but not &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;"Aha!"&lt;/span&gt;&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;What's this to do with test readability, anyway?  It turns out that matchers lend themselves easily to a style of writing assertions that resembles natural languages (and thus is easy for human to understand).   For example, Google Mock supports the following assertion syntax, borrowed from the &lt;a href="http://code.google.com/p/hamcrest/"&gt;Hamcrest&lt;/a&gt; project:&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;blockquote&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;a href="http://code.google.com/p/googlemock/wiki/CookBook#Using_Matchers_in_Google_Test_Assertions"&gt;&lt;b&gt;EXPECT_THAT&lt;/b&gt;&lt;/a&gt;(&lt;i&gt;value&lt;/i&gt;, &lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;i&gt;matcher&lt;/i&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;The assertion succeeds if the given value matches the given matcher.  Therefore,&lt;/div&gt;&lt;div&gt;  &lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;b&gt;EXPECT_THAT&lt;/b&gt;(my_string, ContainsRegex("ab*"));&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;verifies that &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;my_string&lt;/span&gt;&lt;/span&gt; contains regular expression &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;"ab*"&lt;/span&gt;&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Now, pretend the punctuations aren't in the above C++ statement and read it:&lt;br /&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;i&gt;"Expect that my string contains regex ab*".&lt;/i&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;/blockquote&gt;Does that sound like English?&lt;br /&gt;&lt;br /&gt;That's not all.  When an &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;EXPECT_THAT&lt;/span&gt;&lt;/span&gt; assertion fails, it prints an informative message that includes the expression being validated, its value, and the property we expect it to have -- thanks to a matcher's ability to describe itself in human-friendly language.  Therefore, not only is the test code readable, &lt;b&gt;the test output it generates is readable too.&lt;/b&gt;  For example,&lt;br /&gt;&lt;blockquote&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;b&gt;EXPECT_THAT&lt;/b&gt;(GetUserList(), Contains(admin_id));&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;might produce:&lt;br /&gt;&lt;blockquote&gt;&lt;span class="Apple-style-span" style="color: rgb(255, 0, 0);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Value of: GetUserList()&lt;br /&gt;Expected: contains "root"&lt;br /&gt;Actual: { "peter", "paul", "mary" }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;This message contains relevant information to help you diagnose the problem, often without having to use a debugger.  To get the same effect without using a matcher, you'd have to write something like:&lt;br /&gt;&lt;blockquote&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;std::vector&amp;lt;std::string&gt; users = GetUserList();&lt;br /&gt;&lt;b&gt;EXPECT_TRUE&lt;/b&gt;(VectorContains(users, admin_id))&lt;br /&gt;&amp;lt;&amp;lt; " GetUserList() returns " &amp;lt;&amp;lt; users&lt;br /&gt;&amp;lt;&amp;lt; " and admin_id is " &amp;lt;&amp;lt; admin_id;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;where &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;/span&gt; is the syntax Google Test uses for appending custom messages to an assertion failure.  Clearly this is more tedious to write and harder to read than the one-liner using &lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;b&gt;EXPECT_THAT&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Google Mock provides dozens of matchers for you to use out-of-box.  Here's &lt;a href="http://code.google.com/p/googlemock/wiki/CheatSheet#Matchers"&gt;a list of them and their usage&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So matchers are nice.  But what if you cannot find a matcher for the property you want to test?  You can either combine existing matchers, or define your own from scratch.  Both are quite easy to do in Google Mock.  I'll show you how in another post.  Stay tuned.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-923247096748034817?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/923247096748034817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/04/readable-test-code-and-readable-test.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/923247096748034817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/923247096748034817'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/04/readable-test-code-and-readable-test.html' title='Readable Test Code and Readable Test Output - Get Both Using Matchers'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-4231367069110362877</id><published>2009-04-03T15:45:00.000-07:00</published><updated>2009-04-03T16:13:24.562-07:00</updated><title type='text'>What's New in Google Mock 1.1.0</title><content type='html'>Three months after we released Google C++ Mocking Framework 1.0.0, we were ready to unveil a new version.  Google Mock 1.1.0 is available for &lt;a href="http://code.google.com/p/googlemock/downloads/list"&gt;download&lt;/a&gt; now.  It contains several new features we are excited about.&lt;br /&gt;&lt;br /&gt;The previous version of Google Mock requires Google Test (as a reminder, a mocking framework is not a testing framework - it &lt;span style="font-style: italic;"&gt;works with&lt;/span&gt; the latter to help you write better tests).  We were aware that such requirement posed difficulty for some people, who cannot (yet) switch away from the testing framework they have already been using.  &lt;span style="font-weight: bold;"&gt;This version of Google Mock can be used with a non-Google-Test testing framework. &lt;/span&gt; It still works with Google Test out-of-box, but you can easily &lt;a href="http://code.google.com/p/googlemock/wiki/ForDummies#Using_Google_Mock_with_Any_Testing_Framework"&gt;configure&lt;/a&gt; it to use a different testing framework now.&lt;br /&gt;&lt;br /&gt;Two other features make Google Mock much easier to extend than before.  If you have used any mocking framework, you know that a &lt;a href="http://code.google.com/p/googlemock/wiki/ForDummies#Matchers:_What_Arguments_Do_We_Expect?"&gt;matcher&lt;/a&gt; is good for validating a value and printing informative diagnostics when the validation fails.  Google Mock provides many built-in matchers for the common scenarios, but there will always be a time when you need to write your own matcher.  There were several ways to do that in the past, but some &lt;a href="http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Matchers_Quickly"&gt;new&lt;/a&gt; &lt;a href="http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Parameterized_Matchers_Quickly"&gt;macros&lt;/a&gt; just made the task &lt;span style="font-style: italic;"&gt;an order of magnitude easier&lt;/span&gt;.  I will cover this more in another post.&lt;br /&gt;&lt;br /&gt;The second extensibility improvement is for &lt;a href="http://code.google.com/p/googlemock/wiki/ForDummies#Actions:_What_Should_It_Do?"&gt;actions&lt;/a&gt;, which you use to specify what to do when a mock object's member function gets called.  Similarly, despite the rich set of built-in actions, sometimes a user will need to define his own.   The &lt;a href="http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Actions_Quickly"&gt;ACTION&lt;/a&gt; and &lt;a href="http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Parameterized_Actions_Quickly"&gt;ACTION_P*&lt;/a&gt; macros make it trivial.&lt;br /&gt;&lt;br /&gt;There are some minor improvements too, like new built-in matchers and actions, and better diagnostics, but I'll stop here for now.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-4231367069110362877?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/4231367069110362877/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/04/whats-new-in-google-mock-110.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/4231367069110362877'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/4231367069110362877'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/04/whats-new-in-google-mock-110.html' title='What&apos;s New in Google Mock 1.1.0'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-4871425799202009887</id><published>2009-04-03T15:07:00.000-07:00</published><updated>2009-04-03T15:43:13.248-07:00</updated><title type='text'>What's New in Google Test 1.3.0</title><content type='html'>A couple of weeks ago we released &lt;a href="http://code.google.com/p/googletest/downloads/list"&gt;version 1.3.0&lt;/a&gt; of Google C++ Testing Framework (or Google Test for short).  So what's new compared with the previous version, 1.2.1?&lt;br /&gt;&lt;br /&gt;This is a major feature release.  The most noteworthy things we added are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;We have brought &lt;a href="http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Death_Tests"&gt;death tests&lt;/a&gt; to more operating systems.  &lt;span style="font-weight: bold;"&gt;In addition to Linux, you can write death tests on Windows, Cygwin, and Mac OS X now.&lt;/span&gt;  In case you don't know yet, a death test makes sure that your code &lt;span style="font-style: italic;"&gt;does crash&lt;/span&gt; under certain conditions.  Odd this may sound, it is useful for verifying that you have the right assertions in your production code to catch anomalies.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;If you cannot switch from your current testing framework to Google Test yet, we now allow you to &lt;span style="font-weight: bold;"&gt;&lt;a href="http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Letting_Another_Testing_Framework_Drive"&gt;use Google Test's assertions (including death tests) with your existing framework&lt;/a&gt;.&lt;/span&gt;  You can then try out Google Test in small steps and decide whether you want to switch later.&lt;/li&gt;&lt;li&gt;Google Test allows you to mark a test function as "&lt;a href="http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Temporarily_Disabling_Tests"&gt;disabled&lt;/a&gt;."  Disabled tests will be compiled but skipped at run time (handy when you want to fix the bug later).  Now, you can use the &lt;a href="http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Temporarily_Enabling_Disabled_Tests"&gt;&lt;span style="font-weight: bold;font-family:courier new;" &gt;--gtest_also_run_disabled_tests&lt;/span&gt;&lt;/a&gt; command-line flag to run such tests without editing and recompiling the source code.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;The &lt;span style="font-weight: bold;font-family:courier new;" &gt;--help&lt;/span&gt; flag is added for &lt;a href="http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Running_Test_Programs:_Advanced_Options"&gt;printing the usage&lt;/a&gt; of common Google Test command-line flags, saving you a trip to the manual.&lt;/li&gt;&lt;li&gt;You can &lt;a href="http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Running_Test_Programs:_Advanced_Options"&gt;read and change the state of Google Test's flags&lt;/a&gt; in tests now.  This gives you more control on the behavior of Google Test.&lt;/li&gt;&lt;li&gt;To make it easy to use Google Test for a quick job, we provide a script to &lt;a href="http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Fusing_Google_Test_Source_Files"&gt;pack all Google Test source code&lt;/a&gt; into two files: &lt;span style="font-weight: bold;font-family:courier new;" &gt;gtest.h&lt;/span&gt; and &lt;span style="font-weight: bold;font-family:courier new;" &gt;gtest-all.cc&lt;/span&gt;.  Just copy the two files to any machine you need to run tests on, and start hacking!&lt;/li&gt;&lt;/ul&gt;Check it out.  We'd like to hear what you think of it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-4871425799202009887?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/4871425799202009887/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/04/whats-new-in-google-test-130.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/4871425799202009887'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/4871425799202009887'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/04/whats-new-in-google-test-130.html' title='What&apos;s New in Google Test 1.3.0'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5549969176545377403.post-8165637732832030020</id><published>2009-04-03T13:04:00.000-07:00</published><updated>2009-04-05T12:47:22.820-07:00</updated><title type='text'>Why So Many C++ Testing Frameworks Out There?</title><content type='html'>These days, it seems that everyone is rolling his own C++ testing framework, if he hasn't done so already.  Wikipedia has a &lt;a href="http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B"&gt;partial list&lt;/a&gt; of such frameworks.  This is interesting, as in many other OOP languages there are only one or two major players.  For example, most Java people seem to be happy with either &lt;a href="http://junit.org/"&gt;JUnit&lt;/a&gt; or &lt;a href="http://testng.org/"&gt;TestNG&lt;/a&gt;.  Are C++ programmers the &lt;a href="http://en.wikipedia.org/wiki/Do_it_yourself"&gt;DIY&lt;/a&gt; kind?&lt;br /&gt;&lt;br /&gt;Since we started to work on &lt;a href="http://code.google.com/p/googletest/"&gt;Google Test&lt;/a&gt;, and especially after we &lt;a href="http://googletesting.blogspot.com/2008/07/announcing-new-google-c-testing.html"&gt;open-sourced it&lt;/a&gt;, many people have asked us "why are you doing it?"  The short answer, is that we were frustrated by the existing frameworks for one reason or another.  This doesn't mean that they are all poorly designed or implemented.  Rather, many of them have great ideas and tricks that we learned from.  However, as a whole they don't fit our need.&lt;br /&gt;&lt;br /&gt;Unlike Java, which has the famous slogan "Write once, run anywhere," C++ code is being written in a much more diverse environment.  Due to the complexity of the language and the need to do low-level tasks, compatibility between different C++ compilers and even different versions of the same compiler is poor.  There is a C++ Standard, but it's not well supported by compiler vendors.  For many tasks you have to rely on unportable extensions or platform-specific functionality.  This makes it hard to write a reasonably complex system that can be built using many different compilers and works on many platforms.&lt;br /&gt;&lt;br /&gt;To make things more complicated, most C++ compilers allow you to turn off some standard language features in return for better performance.  Don't like using exceptions?  You can turn it off.  Think dynamic cast is bad?  You can disable &lt;a href="http://en.wikipedia.org/wiki/RTTI"&gt;Run-Time Type Identification&lt;/a&gt;, the feature behind dynamic cast and run-time access to type information.  If you do any of these, however, code using these features will stop to compile and is useless to you.  Many testing frameworks rely on exceptions.  They are automatically out of the question for us since we turn off exceptions in many projects.&lt;br /&gt;&lt;br /&gt;Why don't people just write a portable framework, then?  You may ask.  Indeed, that's a top design goal for Google Test, as we need to use it on various platforms with different configurations.  And authors of some other frameworks have tried this too.  However, this comes with a cost.  Cross-platform C++ development requires much more effort: you need to test your code with different operating systems, different compilers, different versions of them, and different compiler flags (combine these factors and the task soon gets daunting); some platforms may not let you do certain things and you have to find a workaround there and guard the code with conditional compilation; different versions of compilers have different bugs and you may have to revise your code to bypass them all; etc.  In the end, it's hard unless you are happy with a bare-bone system.&lt;br /&gt;&lt;br /&gt;So, I think a major reason that we have many C++ testing frameworks is that &lt;span style="font-weight: bold;"&gt;C++ is different in different environments and it's hard to be portable&lt;/span&gt;.  John's framework may not suit Bill's environment, even if it solves John's problems perfectly.&lt;br /&gt;&lt;br /&gt;Another reason is that &lt;span style="font-weight: bold;"&gt;some limitation of C++ itself makes it impossible to implement certain features really well&lt;/span&gt;.  One notable example is that C++ is a statically-typed language and doesn't support reflection.  Most Java testing frameworks use reflection to automatically discover tests you've written such that you don't have to register them one-by-one.  This is a good thing as manually registering tests is tedious and you can easily write a test and forget to register it.  Since C++ has no reflection, we have to do it differently.  Unfortunately there is no single best option.  Some framework requires you to register tests by hand, some use scripts to parse your source code to discover tests, and some use macros to automate the registration.  We prefer the last approach and think it works for most people, but some people don't think so.  Also, there are differently ways to devise the macros and they involve different trade-offs, so the result is not clearly cut.&lt;br /&gt;&lt;br /&gt;Finally, I believe a good framework should have a good extension story.  Let's face it: you cannot be all things to all people, no matter what.  Instead of bloating the framework with rarely used features, we should provide good out-of-box solutions for maybe 95% of the use cases, and leave the rest to extensions.  If I can easily extend a framework to solve a particular problem of mine, I will feel less motivated to write my own thing.  &lt;span style="font-weight: bold;"&gt;Unfortunately, many framework authors don't seem to see the importance of extensibility.&lt;/span&gt;  I think that mindset contributed to the plethora of frameworks we see today.  In Google Test and Google Mock we make it &lt;a href="http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Matchers_Quickly"&gt;easy to define your own assertions&lt;/a&gt; (or matchers in mocking frameworks' terms) that can be used exactly the same way as the built-in assertions, and we are working on publishing an event listener API such that people can write plug-ins.  We hope people will use these features to extend Google Test/Mock for their own need and contribute back extensions that might be generally useful.  We shall talk more about them later.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5549969176545377403-8165637732832030020?l=cpptesting.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpptesting.blogspot.com/feeds/8165637732832030020/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cpptesting.blogspot.com/2009/04/why-so-many-c-testing-frameworks-out.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/8165637732832030020'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5549969176545377403/posts/default/8165637732832030020'/><link rel='alternate' type='text/html' href='http://cpptesting.blogspot.com/2009/04/why-so-many-c-testing-frameworks-out.html' title='Why So Many C++ Testing Frameworks Out There?'/><author><name>Zhanyong Wan</name><uri>http://www.blogger.com/profile/15394645596185604184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://3.bp.blogspot.com/_SlvwY__jXX4/SeFYYtY9BvI/AAAAAAAAAAM/4PRZh9mvv2Q/S220/zhanyong.png'/></author><thr:total>5</thr:total></entry></feed>
