trompeloeil

FORK: Header only C++14 mocking framework
git clone https://git.neptards.moe/u3shit/trompeloeil.git
Log | Files | Refs | README

README.md (5834B)


      1 # *Trompeloeil*
      2 
      3 ![trompeloeil logo](trompeloeil-logo.png)
      4 
      5 <!-- markdownlint-disable no-space-in-links -->
      6 ![CI](https://github.com/rollbear/trompeloeil/workflows/CI/badge.svg)
      7 [![codecov](https://codecov.io/gh/rollbear/trompeloeil/branch/master/graph/badge.svg?token=PCUO4knwdU)](https://codecov.io/gh/rollbear/trompeloeil)
      8 
      9 Get: [![Conan](https://img.shields.io/badge/on-conan-blue.svg)](https://conan.io/center/trompeloeil)
     10 
     11 > *trompe l'oeil* noun    (Concise Encyclopedia)
     12 > Style of representation in which a painted object is intended
     13 > to deceive the viewer into believing it is the object itself...
     14 
     15 # What is it?
     16 
     17 A thread-safe header-only mocking framework for C++11/14 using the Boost Software License 1.0
     18 
     19 # Documentation
     20 
     21 - [Integrating with unit test frame works](docs/CookBook.md/#unit_test_frameworks)
     22 - [Introduction](https://playfulprogramming.blogspot.com/2014/12/introducing-trompeloeil-c-mocking.html)
     23 - [How to contribute](#contribute)
     24 - [Compiler compatibility](#compilers)
     25 - [External tools](#tools)
     26 - [Presentation videos](#videos)
     27 - [Trompeloeil on CppCast](http://cppcast.com/2017/02/bjorn-fahller/)
     28 - [Cheat Sheet (2*A4)](docs/trompeloeil_cheat_sheet.pdf)
     29 - [Cook Book](docs/CookBook.md)
     30 - [FAQ](docs/FAQ.md)
     31 - [Backward compatibility with earlier versions of C++](docs/Backward.md)
     32 - [Platform and library support for Trompeloeil](docs/PlatformsAndLibraries.md)
     33 - [Reference](docs/reference.md)
     34 
     35 Also, follow up with the post on
     36 [sequencing](
     37   https://playfulprogramming.blogspot.se/2015/01/sequence-control-with-trompeloeil-c.html
     38 ) for examples on how to restrict or relax allowed sequences of matching calls.
     39 
     40 # Teaser
     41 
     42 ```Cpp
     43 #include <trompeloeil.hpp>
     44 
     45 class Interface
     46 {
     47 public:
     48   virtual ~Interface() = default;
     49   virtual bool foo(int, std::string& s) = 0;
     50   virtual bool bar(int) = 0;
     51   virtual bool bar(std::string) = 0;
     52 };
     53 
     54 void interface_func(Interface*); // function to test
     55 
     56 class Mock : public Interface
     57 {
     58 public:
     59   MAKE_MOCK2(foo, bool(int, std::string&),override);
     60   MAKE_MOCK1(bar, bool(int),override);
     61   MAKE_MOCK1(bar, bool(std::string),override);
     62   MAKE_MOCK0(baz, void()); // not from Interface
     63 };
     64 
     65 TEST(exercise_interface_func)
     66 {
     67   using trompeloeil::_;  // wild card for matching any value
     68   using trompeloeil::gt; // greater-than match
     69 
     70   Mock m;
     71 
     72   trompeloeil::sequence seq1, seq2;  // control order of matching calls
     73 
     74   int local_var = 0;
     75 
     76   REQUIRE_CALL(m, bar(ANY(int)))     // expect call to m.bar(int)
     77     .LR_SIDE_EFFECT(local_var = _1)  // set captured variable to value of param
     78     .RETURN(_1 > 0)                  // return value depending on param value
     79     .IN_SEQUENCE(seq1)               // must be first match for seq1
     80     .TIMES(AT_LEAST(1));             // can be called several times
     81 
     82   FORBID_CALL(m, bar(0));            // but m.bar(0) is not allowed
     83 
     84   REQUIRE_CALL(m, bar("word"))       // expect one call to m.bar(std::string)
     85     .RETURN(true)
     86     .IN_SEQUENCE(seq2);              // must be first match for seq2
     87 
     88   REQUIRE_CALL(m, foo(gt(2), _))     // expect call to foo(int,std::string&)
     89     .WITH(_2 == "")                  // with int > 2 and empty string
     90     .IN_SEQUENCE(seq1, seq2)         // last for both seq1 and seq2
     91     .SIDE_EFFECT(_2 = "cat")         // and set param string to "cat"
     92     .RETURN(true);
     93 
     94   interface_func(&m);
     95 
     96   // all the above expectations must be fulfilled here
     97 }
     98 ```
     99 
    100 # <A name="contribute"/> How to contribute
    101 
    102 Contributions are most welcome. For new functionality, please file an issue as
    103 an enhancement request first, to get a discussion going about how to best
    104 implement it. Also for bugfixes, it is good to file an issue, so that others can
    105 see what the problem is and when it's solved. Internal changes are normally not
    106 mentioned in the ChangeLog - it should typically reflect what a user can see
    107 (however, performance improvements and silencing warnings are visible for
    108 users.) Feel free to add your name to the copyright blurb.
    109 
    110 |Change                       | PR to          |
    111 |-----------------------------|----------------|
    112 |Documentation                |master branch   |
    113 |Trivial bugfixes             |master branch   |
    114 |Non trivial bugfixes         |develop branch  |
    115 |Simple new functionality     |develop branch  |
    116 |Non-trivial new functionality|new topic branch|
    117 
    118 # <A name="compilers"/> Compiler compatibility
    119 
    120 Trompeloeil is known to work with:
    121 
    122 - GCC [4.8.4](docs/Backward.md#gxx48x_limitations)+, 4.9.3+, 5, 6, 7, 8, 9, 10, 11
    123 - Clang 3.5, 3.6, 3.7, 3.8, 3.9, 4, 5, 6, 7, 8, 9, 10, 11, 12
    124 - Visual Studio 2015, 2017, 2019
    125 
    126 Latest patch level releases are assumed in the versions listed above.
    127 
    128 Further details on C++11 support, platform and library limitations, may
    129 be found in
    130 
    131 - [Backward compatibility with earlier versions of C++](docs/Backward.md)
    132 - [Platform and library support for Trompeloeil](docs/PlatformsAndLibraries.md)
    133 
    134 # <A name="tools"/> External Tools
    135 
    136 - [ReSharperC++](https://www.jetbrains.com/resharper-cpp/) extension to
    137   [VisualStudio](https://visualstudio.microsoft.com/) has a mock generator for
    138   *Trompeloeil* since [2016.2](https://blog.jetbrains.com/rscpp/2016/09/14/whats-new-in-resharper-c-2016-2/)
    139   
    140 # <A name="videos"/> Videos
    141 
    142 - Intro presentation from Stockholm C++ UG [(YouTube 34m)](https://www.youtube.com/watch?v=mPYNsARvTDk) [(Slides)](https://speakerdeck.com/rollbear/mocking-modern-c-plus-plus-with-trompeloeil)
    143 - Presentation from NDC{Oslo} [(YouTube 52m)](https://www.youtube.com/watch?v=vvQ-kK4coYM&t=1122s) [(Slides)](https://speakerdeck.com/rollbear/ndc-oslo-using-trompeloeil-a-mocking-framework-for-modern-c-plus-plus)
    144 - Detailed presentation from ACCU 2017 [(YouTube 1h25m)](https://www.youtube.com/watch?v=HCh6cs9nXt0) [(Slides with extra material)](https://speakerdeck.com/rollbear/using-trompeloeil-a-mocking-framework-for-modern-c-plus-plus)