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
Björn Fahller 06ee88b1ae Add detail::make_tuple 5 years ago
compilation_errors Silence warnings and simplify return_wrong_pointer_constness test 7 years ago
docs Merge branch 'master' into develop 5 years ago
include Add detail::make_tuple 5 years ago
test s/std::make_unique/detail::make_unique 5 years ago
test_package Update Conan packaging and add to Travis 6 years ago
.gitattributes Add gitattributes for line ending normalization 6 years ago
.gitignore Add CLion .idea to .gitignore 6 years ago
.travis.yml Merge branch 'master' into develop 5 years ago
CMakeLists.txt Merge branch 'master' into develop 5 years ago
ChangeLog Update ChangeLog for issue 129 5 years ago
LICENSE_1_0.txt Initial commit 11 years ago
Makefile.travis More compilers and flags on Travis.ci 9 years ago
README.md Prepared for v38 5 years ago
_config.yml Fix for Visual Studio 2017 compiler crash -- issue #29 (#38) 9 years ago
appveyor.yml Add MSVC 16 to appveyor builds 6 years ago
check_errors.sh Fix check_errors.sh and detect failure in Travis 7 years ago
conanfile.py Prepared for v38 5 years ago
trompeloeil-config.cmake versioned cmake package for trompeloeil - yay! 8 years ago
trompeloeil-logo.png French grammar 10 years ago
trompeloeil-logo.svg French grammar 10 years ago

README.md

Trompeloeil

trompeloeil logo

CI: Build Status AppVeyor Build Status Coverage Status

conan.io Download

HitCount

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 thread-safe header-only mocking framework for C++11/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.

Teaser

#include <trompeloeil.hpp>

class Interface
{
public:
  virtual ~Interface() = default;
  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&),override);
  MAKE_MOCK1(bar, bool(int),override);
  MAKE_MOCK1(bar, bool(std::string),override);
  MAKE_MOCK0(baz, void()); // not from Interface
};

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
}

How to contribute

Contributions are most welcome. For new functionality, please file an issue as an enhancement request first, to get a discussion going about how to best implement it. Also for bugfixes, it is good to file an issue, so that others can see what the problem is and when it's solved. Internal changes are normally not mentioned in the ChangeLog - it should typically reflect what a user can see (however, performance improvements and silencing warnings are visible for users.) Feel free to add your name to the copyright blurb.

Change PR to
Documentation master branch
Trivial bugfixes master branch
Non trivial bugfixes develop branch
Simple new functionality develop branch
Non-trivial new functionality new topic branch

Compiler compatibility

Trompeloeil is known to work with:

  • GCC 4.8.4+, 4.9.3+, 5, 6, 7, 8, 9
  • Clang 3.5, 3.6, 3.7, 3.8, 3.9, 4, 5, 6, 7, 8, 9
  • Visual Studio 2015, 2017, 2019

Latest patch level releases are assumed in the versions listed above.

Further details on C++11 support, platform and library limitations, may be found in

Videos