[cfe-dev] gtest and AST matchers

Manuel Klimek klimek at google.com
Mon Nov 18 05:37:05 PST 2013


On Mon, Nov 18, 2013 at 11:14 AM, Jesper Eskilson
<jesper.eskilson at iar.com>wrote:

>
> Hi,
>
> I'm trying to use gtest to write tests for stuff that goes on inside an
> AST matcher, but it seems like any gtest assertions made inside a AST
> matcher callback are silently ignored.
>
> For example, in the following program, the "FAIL" in the callback is
> silently ignored, but the TEST in the test-method succeeds.
>

Yep, this generally doesn't work - just set a variable from the callback,
and check that from the test...


>
> --
> #include "gtest/gtest.h"
> #include "clang/ASTMatchers/ASTMatchFinder.h"
> #include "clang/Frontend/FrontendActions.h"
> #include "clang/Tooling/Tooling.h"
>
> using namespace clang::ast_matchers;
> using namespace clang::tooling;
>
> class Callback : public MatchFinder::MatchCallback {
>   virtual void run(const MatchFinder::MatchResult &result) { FAIL(); }
> };
>
> TEST(suite, test) {
>   MatchFinder f;
>   f.addMatcher(expr(), new Callback);
>   runToolOnCode(newFrontendActionFactory(&f)->create(), "int x;");
>   FAIL() << "We should have failed in the callback!";
> }
>
> int main(int argc, char **argv) {
>   testing::InitGoogleTest(&argc, argv);
>   return RUN_ALL_TESTS();
> }
> --
>
> Is there a way around this? (Or should I be asking on the gtest-mailing
> list instead?)
>
> /Jesper
> --
> *Jesper Eskilson* /Development Engineer/
> IAR Systems AB
> Box 23051, Strandbodgatan 1
> SE-750 23 Uppsala, SWEDEN
> E-mail: jesper.eskilson at iar.com <mailto:jesper.eskilson at iar.com>
> Website: www.iar.com
> <http://www.iar.com> Twitter: www.twitter.com/iarsystems
> <http://www.twitter.com/iarsystems>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131118/8096ff82/attachment.html>


More information about the cfe-dev mailing list