Header only C++14 mocking framework
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Go to file
Bjorn Fahller 6c58b3cbe5 Updated ChangeLog for v10 10 years ago
compilation_errors fix expected compilation error test for g++ 10 years ago
docs Document using matchers with pointers 10 years ago
.travis.yml re-enabled travis 'before_script' 10 years ago
ChangeLog Updated ChangeLog for v10 10 years ago
LICENSE_1_0.txt Initial commit 11 years ago
Makefile.travis Attempt to trig travis-ci builds 10 years ago
README.md Added license blurb to README.md 10 years ago
check_errors.sh Improved compilation error test program 10 years ago
compiling_tests.cpp Make operator* work with custom matchers for pointer matching 10 years ago
trompeloeil-logo.png Fixed spelling in logo. Ouch 11 years ago
trompeloeil.hpp Use () init in matchers to avoid initializer_list confusion 10 years ago

README.md

Trompeloeil

trompeloeil logo Build Status Coverage Status

trompe l'oeil noun (Concise Encyclopedia)
Style of representation in which a painted object is intended to deceive the viewer into believing it is the object itself...

What is it?

A header only mocking framework for C++14 using the Boost Software License 1.0

Documentation

Also, follow up with the post on sequencing for examples on how to restrict or relax allowed sequences of matching calls.

If you want to contribute, read ACCU overload 125 to learn the internals.

Teaser

#include <trompeloeil.hpp>

class Interface
{
public:
  virtual bool foo(int, std::string& s) = 0;
  virtual bool bar(int) = 0;
  virtual bool bar(std::string) = 0;
};

void interface_func(Interface*); // function to test

class Mock : public Interface
{
public:
  MAKE_MOCK2(foo, bool(int, std::string&));
  MAKE_MOCK1(bar, bool(int));
  MAKE_MOCK1(bar, bool(std::string));
};

TEST(exercise_interface_func)
{
  using trompeloeil::_;  // wild card for matching any value
  using trompeloeil::gt; // greater-than match

  Mock m;

  trompeloeil::sequence seq1, seq2;  // control order of matching calls

  int local_var = 0;

  REQUIRE_CALL(m, bar(ANY(int)))     // expect call to m.bar(int)
    .LR_SIDE_EFFECT(local_var = _1)  // set captured variable to value of param
    .RETURN(_1 > 0)                  // return value depending on param value
    .IN_SEQUENCE(seq1)               // must be first match for seq1
    .TIMES(AT_LEAST(1));             // can be called several times

  FORBID_CALL(m, bar(0));            // but m.bar(0) is not allowed

  REQUIRE_CALL(m, bar("word"))       // expect one call to m.bar(std::string)
    .RETURN(true)
    .IN_SEQUENCE(seq2);              // must be first match for seq2

  REQUIRE_CALL(m, foo(gt(2), _))     // expect call to foo(int,std::string&)
    .WITH(_2 == "")                  // with int > 2 and empty string
    .IN_SEQUENCE(seq1, seq2)         // last for both seq1 and seq2
    .SIDE_EFFECT(_2 = "cat")         // and set param string to "cat"
    .RETURN(true);

  interface_func(&m);

  // all the above expectations must be fulfilled here
}

Compiler compatibility

Trompeloeil is known to work with:

  • GCC 4.9, 5.1, 5.2
  • Clang 3.5, 3.6, 3.7
  • VisualStudio 2015