GithubHelp home page GithubHelp logo

jgonzalezdr / cppumockgen Goto Github PK

View Code? Open in Web Editor NEW
41.0 3.0 12.0 969 KB

Mock generator for CppUTest/CppUMock

License: BSD 3-Clause "New" or "Revised" License

CMake 4.64% C++ 93.82% PowerShell 1.00% C 0.52% Makefile 0.01% Shell 0.01%
testing cpp unit-testing unit-test mock mocking cpputest

cppumockgen's People

Contributors

jgonzalezdr avatar stefaneicher avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cppumockgen's Issues

Functions with variable number of parameters not generated correctly.

In the follow code, the auto generated .cpp code doesn't include the ellipses from the function prototype, causing the code to not compile with a "no matching function defined" error

h/Log.h:

class CLog
{
	static void Error (int32_t Module, const char_t *pMessage, ...)	
};

mock/Log.CPP:

void Log::Error(int32_t Module, const char_t * pMessage)       // missing **, ...**
{
	mock().actualCall("Log::Error").withIntParameter("Module", Module).withConstPointerParameter("pMessage", pMessage);
}

libclang.dll dependency

libclang.dll is missing from the zipped / install packages, thus making those unusable if no libclang is already installed in the host.

Could the executable file be statically linked or libclang.dll distributed with the executable file?

Volatile function parameters

Is there any way to support volatile variables with the expectations, g++ complains about it. I'm testing embedded code and have several functions like the following:
void word_write(volatile unsigned short *port_ptr, unsigned short value);

Thanks
Daniel

Eliminate expect code duplication

Could you change the expect generated from:

namespace expect {
    namespace namespace1$ {
        namespace class6$ {
            MockExpectedCall& method1(CppUMockGen::Parameter<const namespace1::class6*> __object__, CppUMockGen::Parameter<const char*> p1, CppUMockGen::Parameter<int> p2)
            {
                bool __ignoreOtherParams__ = false;
                MockExpectedCall& __expectedCall__ = mock().expectOneCall("namespace1::class6::method1");
                mock().setData("namespace1::class6::method1__object___isIgnored", __object__.isIgnored());
                if (!__object__.isIgnored()) { __expectedCall__.onObject(const_cast<namespace1::class6*>(__object__.getValue())); }
                if (p1.isIgnored()) { __ignoreOtherParams__ = true; }
                else { __expectedCall__.withStringParameter("p1", p1.getValue()); }
                if (p2.isIgnored()) { __ignoreOtherParams__ = true; }
                else { __expectedCall__.withIntParameter("p2", p2.getValue()); }
                if (__ignoreOtherParams__) { __expectedCall__.ignoreOtherParameters(); }
                return __expectedCall__;
            }
        }
    }
}

To the following:

namespace expect {
    namespace namespace1$ {
        namespace class6$ {
            MockExpectedCall& method1(CppUMockGen::Parameter<const namespace1::class6*> __object__, CppUMockGen::Parameter<const char*> p1, CppUMockGen::Parameter<int> p2)
            {
                return method1(1, __object, p1, p2);
            }
        }
    }
}

To take advantage of the code already generated for the "numCalls".

IgnoreParameter not working?

First off, thank you for this wonderful tool. It is excellent and will save us a lot of time creating and maintaining mocks!

Sometimes the TESTs we write have no control over the mock's object instance. Only the unit under test does. So there is a need to ignore the class's object instance check. However, I am seeing a problem attempting to skip the object check. For example:

`
// class6.h
namespace namespace1
{
class class6
{
public:
void method1(const char* p1, int p2);
};
}

// class6.cpp
/*
#include
#include "class6.h"
namespace namespace1
{
void class6::method1(const char* p1, int p2)
{
if ((strcmp(p1, "hello") == 0) && (p2 == 42))
{
// do something
}
}
}*/

// class6_mock.cpp
/*

  • This file has been auto-generated by CppUTestMock v0.4.
  • Contents will NOT be preserved if it is regenerated!!!
  • Generation options: -x
    /
    //#include "class6.h"
    #include <CppUTestExt/MockSupport.h>
    void namespace1::class6::method1(const char
    p1, int p2)
    {
    mock().actualCall("namespace1::class6::method1").onObject(this).withStringParameter("p1", p1).withIntParameter("p2", p2);
    }

// class6_expect.hpp
/*

  • This file has been auto-generated by CppUTestMock v0.4.
  • Contents will NOT be preserved if it is regenerated!!!
  • Generation options: -x
    /
    #include <CppUMockGen.hpp>
    //#include "class6.h"
    #include <CppUTestExt/MockSupport.h>
    namespace expect {
    namespace namespace1$ {
    namespace class6$ {
    MockExpectedCall& method1(CppUMockGen::Parameter<const namespace1::class6
    > object, CppUMockGen::Parameter<const char*> p1, CppUMockGen::Parameter p2, bool return);
    MockExpectedCall& method1(unsigned int numCalls, CppUMockGen::Parameter<const namespace1::class6*> object, CppUMockGen::Parameter<const char*> p1, CppUMockGen::Parameter p2, bool return);
    }
    }
    }

// class6_expect.cpp
/*

  • This file has been auto-generated by CppUTestMock v0.4.
  • Contents will NOT be preserved if it is regenerated!!!
  • Generation options: -x
    /
    //#include "class6_expect.hpp"
    namespace expect {
    namespace namespace1$ {
    namespace class6$ {
    MockExpectedCall& method1(CppUMockGen::Parameter<const namespace1::class6
    > object, CppUMockGen::Parameter<const char*> p1, CppUMockGen::Parameter p2)
    {
    bool ignoreOtherParams = false;
    MockExpectedCall& expectedCall = mock().expectOneCall("namespace1::class6::method1");
    if (!object.isIgnored()) { expectedCall.onObject(const_castnamespace1::class6*(object.getValue())); }
    if (p1.isIgnored()) { ignoreOtherParams = true; }
    else { expectedCall.withStringParameter("p1", p1.getValue()); }
    if (p2.isIgnored()) { ignoreOtherParams = true; }
    else { expectedCall.withIntParameter("p2", p2.getValue()); }
    if (ignoreOtherParams) { expectedCall.ignoreOtherParameters(); }
    return expectedCall;
    }
    MockExpectedCall& method1(unsigned int numCalls, CppUMockGen::Parameter<const namespace1::class6*> object, CppUMockGen::Parameter<const char*> p1, CppUMockGen::Parameter p2)
    {
    bool ignoreOtherParams = false;
    MockExpectedCall& expectedCall = mock().expectNCalls(numCalls, "namespace1::class6::method1");
    if (!object.isIgnored()) { expectedCall.onObject(const_castnamespace1::class6*(object.getValue())); }
    if (p1.isIgnored()) { ignoreOtherParams = true; }
    else { expectedCall.withStringParameter("p1", p1.getValue()); }
    if (p2.isIgnored()) { ignoreOtherParams = true; }
    else { expectedCall.withIntParameter("p2", p2.getValue()); }
    if (ignoreOtherParams) { expectedCall.ignoreOtherParameters(); }
    return expectedCall;
    }
    }
    }
    }

void unit_under_test(const char* p1, int p2)
{
namespace1::class6 class6_object_instance;
class6_object_instance.method1(p1, p2);
}

TEST_GROUP(class6)
{
void setup() {}
void teardown() { mock().clear(); }
};

TEST(class6, method1_check_object)
{
namespace1::class6 class6_mock;

expect::namespace1$::class6$::method1(&class6_mock, "hello", 42);
class6_mock.method1("hello", 42);
mock().checkExpectations();

}

TEST(class6, method1_ignore_object)
{
// The unit under test controls the lifecycle of the mock object so ignore the object parameter!
expect::namespace1$::class6$::method1(CppUMockGen::IgnoreParameter::YES, "hello", 42);
unit_under_test("hello", 42); // <-- EXCEPTION here!
mock().checkExpectations();
}

/*
Failure in TEST(class6, method1_ignore_object)
MockFailure: Function called on an unexpected object: namespace1::class6::method1
Actual object for call has address: <007FE96B>
EXPECTED calls that DID NOT happen related to function: namespace1::class6::method1
namespace1::class6::method1 -> const char* p1: , int p2: <42>
ACTUAL calls that DID happen related to function: namespace1::class6::method1

..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 24 ms)
*/
`

I used your example in the documentation page, "namespace1::class6::method", to generate the class6 mock.

Problem with include file

Hi
First of all, thank you for your effort.
I noted that the application is not able to manage a function definition in another included header file.

//file1.h
#include file2.h

//file2.h
void foo(int bar);

Run-> CppUMockGen.exe -i file1.h -m
it return an error.

Regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.