Git Product home page Git Product logo

umock-c's Introduction

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

umock_c

umock_c is a C mocking library.

Setup

Build

  • Clone umock_c by:
git clone --recursive https://github.com/Azure/umock-c.git
  • Create a cmake folder under the root of umock-c

  • Switch to the cmake folder and run

cmake ..

If you would like to use installed (by CMake) versions of packages already on your machine:

cmake -Duse_installed=ON ../
  • Build the code for your platform (msbuild for Windows, make for Linux, etc.) by executing in the cmake folder:
cmake --build .

To install umock_c:

cmake -Duse_installed=ON ../

On Linux:

sudo make install

On Windows:

msbuild /m INSTALL.vcxproj

This requires that ctest and testrunnerswitcher are both installed (through CMake) on your machine.

Building tests

In order to build the tests use the run_unittests cmake option:

cmake .. -Drun_unittests:bool=ON

Example

Ever wanted to write something like this in C as a test?

TEST_FUNCTION(my_first_test)
{
    // arrange
    STRICT_EXPECTED_CALL(test_dependency_1_arg(42))
        .SetReturn(44)
        .IgnoreAllArguments();

    // act
    int result = function_under_test();

    // assert
    ASSERT_ARE_EQUAL(int, 44, result);
    ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}

umock_c has way more features than this simple example!

Dependencies

  • umock_c uses ctest as test runner (https://github.com/Azure/azure-ctest.git). ctest is a C test runner that can be run on many platforms as it does not make use of compiler/platform specific code and thus it is easily portable.
  • umock_c uses cmake (https://cmake.org/) to generate build files.
  • umock_c uses testrunnerswitcher to allow switching between ctest and CppUnitTest for Windows.

Documentation

Complete documentation is available here.

umock-c's People

Contributors

ackpaul avatar amar-sagare avatar andrew-buckley avatar anporumb avatar aribeironovaes avatar arsing avatar damonbarry avatar dandrestor avatar danewalton avatar darobs avatar dcristoloveanu avatar ewertons avatar ipg2013 avatar jebrando avatar jetstreamroysprowl avatar jspaith avatar jtanner-msft avatar m-iceberg avatar mamokarz avatar marcschier avatar mattdurak avatar microsoft-github-policy-service[bot] avatar mrohera avatar parth21999 avatar sagareamar avatar sahithkallakunta avatar saidfathelbab avatar ttins 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

Watchers

 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

umock-c's Issues

umock_c should have a way to inspect calls made.

Assume there's 2 modules, A and B. A calls B's B_do_something_async providing some callback and context.

In real world, B would call back.

In tests, there's no (easy) way to make the callback.

umock_c needs to improve on that.

Best Regards,
Andrei Porumb

WTB IGNORED_STRUCT_ARG

There is an IGNORED_PTR_ARG (nice) and IGNORED_NUM_ARG.

However, if there's a function that takes as parameter a struct (not a pointer to struct, but the struct itself) then umock_c does not offer a convenient way of saying "ignore this parameter that is a struct".

Random thoughts: {0} is always a valid initializer for a struct. Maybe a IGNORED_STRUCT_ARG(TYPE) would be needed to help the PP (but TYPE is already known, so magic might be needed).

Best Regards,
Andrei Porumb

MOCKABLE_FUNCTION ->header ->code

Right now the behavior or MOCKABLE_FUNCTION is that between #define ENABLE_MOCKS / #undef ENABLE_MOCKS will generate code.

This improvement wants to have MOCKABLE_FUNCTION generate SEPARATE "proper declarations for a header" and "proper definitions for a source file", possibly based on some more #defines, suggestions would be along the lines of ENABLE_HEADER_MOCKS / ENABLE_SOURCE_MOCKS.

This will greatly reduce compilation time, as the "source file" would only need be compiled once (and then used as object). This would also allow use of precompiled headers (maybe), but would greatly reduce our huge compilation times as we'd have a c_util_mocks library that would be compiled/linked once and re-used everywhere where any component of it would be used.

Best Regards,
Andrei Porumb

umock_c needs to have a CaptureParameter

would be nice if umock would have a CaptureParameter_parameterName modifier.

When this is defined:

MOCKABLE_FUNCTION(, void, someAPI, int, a, int, b))

The test might just want to capture the value of parameter "a" or "b" for further usage (for example when the parameter is a callback function...)

This is how the code would look like:

int captured_a;
STRICT_EXPECTED_CALL(someAPI(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
.CaptureParameter_a(&captured_a, sizeof(captured_a)); /*maybe sizeof(captured_a) would not be needed...

// go on and use captured_a for other test purposes

Best Regards,
Andrei Porumb

let's have #include "umock_end_mocks.h" and #include "umock_begin_mocks.h"

Those 2 headers would contain

#define ENABLE_MOCKS

respectively
#undef ENABLE_MOCKS
#include "umock_c/umock_c_prod.h"

and should have a mechanism where

  1. "umock_begin_mocks.h" after "umock_begin_mocks.h" is error
  2. "umock_end_mocks.h" without "umock_begin_mocks.h" is error
  3. "umock_end_mocks.h" after "umock_end_mocks.h" is error

WTB better validation than ValidateArgumentBuffer

  1. ValidateArgumentBuffer does not have a "a name" (doesn't expand to ValidateArgumentBuffer_result where "result" is the name of the parameter)

  2. Would be nice to have ValidateArgumentBuffer understand structures. ValidateArgumentBuffer_result_some_struct_field(&is_true, sizeof(bool)) would make a KILLING feature!

  3. At least ValidateArgumentBuffer should have a way to validate from offset 3, 4 bytes. That would allow validating arguments that are struct fields... in an archaic way, but workable. Maybe this is the easiest to implement proposal :)

The current workaround is to have a "hook_" and register it with REGISTER_GLOBAL_MOCK_HOOK, some global variables and validate inside the hook_ function that the globals match the actual parameters. So maybe thsi suggestion should not be exactly top of the priority list.

Best Regards,
Andrei Porumb

Access violation using with mstest

I am trying to use umock-c with mstest build-in visual studio 2017.
Here my code draft:

#define ENABLE_MOCKS

#include <umock_c/umock_c.h>

...

 MOCKABLE_FUNCTION(, int, test_dependency_1_arg, int, arg1);

...

  TEST_METHOD(TestTest)
  {

      // arrange
    STRICT_EXPECTED_CALL(test_dependency_1_arg(42))
        .SetReturn(44)
        .IgnoreAllArguments();

   // test_dependency_1_arg(42);

    //Assert::AreEqual(umock_c_get_expected_calls(), umock_c_get_actual_calls());

}
...

After running TestTest case i got message exception with code C0000005 in STRICT_EXPECTED_CALL line, or in test_dependency_1_arg(42) line (if comment STRICT_EXPECTED_CALL line).
Why?

IGNORED_ARG_NOT_NULL

Would be nice to have IGNORED_ARG, but fail if the argument is NULL. Only possible to do now with mock hook that explicitly checks.

Actual calls do not look nice

When using .ValidateArgumentBuffer(...) this is what is on the screen when it happen to fail validation:

Expected: [bsdl_metadata_write_async(0000000000000001,[0x00 0x50 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00],0000000000000000,0000000000000000)] 

Actual:   [bsdl_metadata_write_async(0000000000000001,000002630F9BFEB0,00007FF72BA8B980,000002630F9D3080)]                                                                  

Note how the expected is nicely showing the buffer as an array, while the Actual call shows a pointer.

Proposal is to have Actual call show an array too, because that gives some insight of "what doesn't match".

Best Regards,
Andrei Porumb

Support Calloc()?

Is there anyway to get umock to support calloc in the same way it supports the other 3 C mem functions? I've tried adding these lines of code:
umockalloc.h
void* umockalloc_calloc(size_t nmemb, size_t size);
umockalloc.c
void* umockalloc_calloc(size_t nmemb, size_t size)
{
/* Codes_SRS_UMOCKALLOC_01_003: [ umockalloc_calloc shall call calloc, while passing number of members and size argument to calloc. ] /
/
Codes_SRS_UMOCKALLOC_01_004: [ umockalloc_calloc shall return the result of calloc. ]*/
return calloc(nmemb, size);
}

Getting linker error and build failures. Is there other dependencies I need to add to any other corresponding files? I'm trying to trace through the directory structure looking for all malloc references, but I'm not seeing anything, is this currently even possible? If not tag this as an enhancement.

WTB .CaptureArgumentValueAlways or some other modifier

At the moment, .CaptureArgumetnValue will capture argument values when the mockable function will match its expected call.

If it doesn't match, then no argument capture is performed.

I believe arguments are interesting to be captured also when the actual call does not match the expected call. Maybe a different modifier?

Best Regards,
Andrei Porumb

umock_c_get_expected_calls and umock_c_get_actual_calls should return some "collection" of calls

umock_c_get_actual_calls should return some "query-able" collection of calls.

That way, in the scenario of "was function X called?" (notice: the scenario is missing any sort of ordering or other functions being called) there would be some answer.

Right now, the only verification of umock_c is "did all functions match in order?" (by means of ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); that actually compares 2 strings).

Having a collection of calls exposed would mean the user can better decide when "my test failed". Sometimes all the user cares is "was this callback called with these parameters?" disregarding any other function calls.

Mock function generation filters

Would be very nice to have the ability to specify which functions should be mocked so that less code is generated. Less code == less time spent preprocessing and compiling.

Building umock fails when using `use_installed_dependencies=on`

Building umock fails when using use_installed_dependencies=on because it cannot find the include directory for Macro Utils. (In my environment, Macro Utils was itself compiled with use_installed_dependencies=on.)

-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: /home/dan/tc-iot-terminal-agent/toolchain/robustel/r3010/arm-sam9x25-linux-gnueabi/bin/arm-sam9x25-linux-gnueabi-gcc
-- Check for working C compiler: /home/dan/tc-iot-terminal-agent/toolchain/robustel/r3010/arm-sam9x25-linux-gnueabi/bin/arm-sam9x25-linux-gnueabi-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /home/dan/tc-iot-terminal-agent/toolchain/robustel/r3010/arm-sam9x25-linux-gnueabi/bin/arm-sam9x25-linux-gnueabi-g++
-- Check for working CXX compiler: /home/dan/tc-iot-terminal-agent/toolchain/robustel/r3010/arm-sam9x25-linux-gnueabi/bin/arm-sam9x25-linux-gnueabi-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for include file stdint.h
-- Looking for include file stdint.h - found
-- Looking for include file stdbool.h
-- Looking for include file stdbool.h - found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dan/tc-iot-terminal-agent/build_cmake/build/umock-c
Scanning dependencies of target umock_c
[ 12%] Building C object CMakeFiles/umock_c.dir/src/umock_c_negative_tests.c.o
[ 12%] Building C object CMakeFiles/umock_c.dir/src/umock_c.c.o
[ 18%] Building C object CMakeFiles/umock_c.dir/src/umockcall.c.o
[ 25%] Building C object CMakeFiles/umock_c.dir/src/umockalloc.c.o
[ 31%] Building C object CMakeFiles/umock_c.dir/src/umockcallrecorder.c.o
[ 37%] Building C object CMakeFiles/umock_c.dir/src/umocktypes.c.o
[ 43%] Building C object CMakeFiles/umock_c.dir/src/umocktypename.c.o
[ 50%] Building C object CMakeFiles/umock_c.dir/src/umocktypes_bool.c.o
In file included from /home/dan/tc-iot-terminal-agent/vendor/umock-c/src/umock_c.c:4:0:
/home/dan/tc-iot-terminal-agent/vendor/umock-c/inc/umock_c/umock_c.h:7:43: fatal error: azure_macro_utils/macro_utils.h: No such file or directory
 #include "azure_macro_utils/macro_utils.h"
                                           ^
compilation terminated.
CMakeFiles/umock_c.dir/build.make:62: recipe for target 'CMakeFiles/umock_c.dir/src/umock_c.c.o' failed
make[2]: *** [CMakeFiles/umock_c.dir/src/umock_c.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....
In file included from /home/dan/tc-iot-terminal-agent/vendor/umock-c/src/umock_c_negative_tests.c:6:0:
/home/dan/tc-iot-terminal-agent/vendor/umock-c/inc/umock_c/umock_c.h:7:43: fatal error: azure_macro_utils/macro_utils.h: No such file or directory
 #include "azure_macro_utils/macro_utils.h"
                                           ^
compilation terminated.
CMakeFiles/umock_c.dir/build.make:86: recipe for target 'CMakeFiles/umock_c.dir/src/umock_c_negative_tests.c.o' failed
make[2]: *** [CMakeFiles/umock_c.dir/src/umock_c_negative_tests.c.o] Error 1
/home/dan/tc-iot-terminal-agent/vendor/umock-c/src/umocktypes_bool.c:7:43: fatal error: azure_macro_utils/macro_utils.h: No such file or directory
 #include "azure_macro_utils/macro_utils.h"
                                           ^
compilation terminated.
CMakeFiles/umock_c.dir/build.make:230: recipe for target 'CMakeFiles/umock_c.dir/src/umocktypes_bool.c.o' failed
make[2]: *** [CMakeFiles/umock_c.dir/src/umocktypes_bool.c.o] Error 1
CMakeFiles/Makefile2:291: recipe for target 'CMakeFiles/umock_c.dir/all' failed
make[1]: *** [CMakeFiles/umock_c.dir/all] Error 2
Makefile:128: recipe for target 'all' failed
make: *** [all] Error 2

STRICT_EXPECTED_CALL should also take function pointers

The following code is valid:

MOCKABLE_FUNCTION(WINAPI, BOOL, mocked_PathFileExistsA, LPCSTR, pszPath);
...
STRICT_EXPECTED_CALL(mocked_PathFileExistsA(TEST_FOLDER));

The following code is NOT valid:

MOCKABLE_FUNCTION(WINAPI, BOOL, mocked_PathFileExistsA, LPCSTR, pszPath);
...
static BOOL(WINAPI *pfPathFileExistsA)(LPCSTR path) = mocked_PathFileExistsA;
STRICT_EXPECTED_CALL(pfPathFileExistsA(TEST_FOLDER));

even when pfPathFileExists is a pointer to mocked_PathFileExistsA, the code should compile...

Best Regards,
Andrei Porumb

REGISTER_UMOCK_VALUE_TYPE does not work.

static char* umockvalue_stringify_BSDL_INDEX_INVARIANT(const BSDL_INDEX_INVARIANT* value)
{
    return sprintf_char(PRI_BSDL_INDEX_INVARIANT, BSDL_INDEX_INVARIANT_VALUES(*value));
}

static int umockvalue_are_equal_BSDL_INDEX_INVARIANT(const BSDL_INDEX_INVARIANT* left, const BSDL_INDEX_INVARIANT* right)
{
    return (
        (left->xlog_epoch == right->xlog_epoch) &&
        (left->xtail_gen_id == right->xtail_gen_id)
    );
}

int umockvalue_copy_BSDL_INDEX_INVARIANT(BSDL_INDEX_INVARIANT* destination, const BSDL_INDEX_INVARIANT* source)
{
    *destination = *source;
}

static void umockvalue_free_BSDL_INDEX_INVARIANT(BSDL_INDEX_INVARIANT* value)
{
    (void)value;
}
[...]

REGISTER_UMOCK_VALUE_TYPE(BSDL_INDEX_INVARIANT, umockvalue_stringify_BSDL_INDEX_INVARIANT, are_equal_func_BSDL_INDEX_INVARIANT, copy_func_BSDL_INDEX_INVARIANT, free_func_BSDL_INDEX_INVARIANT);

does not compile, says:
warning C4210: nonstandard extension used: function given file scope

Best Regards,
Andrei Porumb

umock_c_negative_tests_fail_call should have a return value

Usually code goes like the following:

    for (size_t i = 0; i < umock_c_negative_tests_call_count(); i++)
    {
        if(i==4) continue;
        umock_c_negative_tests_reset();
        umock_c_negative_tests_fail_call(i);
        ///act something

The suggestion is to have
umock_c_negative_tests_fail_call(i) return a value. The value is true if the function is fail-able (that is: it is NOT void and has a .SetFailReturn value explicitly set). Otherwise it returns false.

If that would exist, then the above loop turns into:

    for (size_t i = 0; i < umock_c_negative_tests_call_count(); i++)
    {
        umock_c_negative_tests_reset();
        if(!umock_c_negative_tests_fail_call(i)) continue;
        ///act something

Note the missing of if that excludes the non-failable function (which can get quite heavy).

Best Regards,
Andrei Porumb

WTB MOCKABLE_FUNCTION_WITH_RETURNS

Right now, there's a way to describe a mock by means of MOCKABLE_FUNCTION(...)
And a separate way to describe the behavior of the mock "when it succeeds/when it fails" by means of REGISTER_GLOBAL_MOCK_RETURNS(...)

Problem is, in every single unittest the REGISTER_GLOBAL_MOCK_RETURNS(...) needs to be done. This is repetitive, prone to human error ("i forgot to write it!").

Having a single new macro MOCKABLE_FUNCTION_WITH_RETURNS which would combine the two previous macros in the header would be quite awesome because it would bring consistency across unittests, and faster dev time, and would ensure that a said developer doesn't debug through negative tests because he forgot to add the global returns :)

Best Regards,
Andrei porumb

umock either has some look-ahead or it doesn't have or both at the same time

The following sort of self-contained code has admirable behavior at runtime:

#define ENABLE_MOCKS
#include "umock_c/umock_c_prod.h"
MOCKABLE_FUNCTION(, void, sum, int, a, int, b);
MOCKABLE_FUNCTION(, void, whatIsDouble, int, a, int*, b);
#undef ENABLE_MOCKS

static void code_under_test1(void)
{
    sum(2, 4);
    sum(3, 5);
}

static void code_under_test2(void)
{
    int nothing;
    whatIsDouble(2, &nothing);
    whatIsDouble(3, &nothing);
}

TEST_FUNCTION(umock_test_code_under_test1)
{
    umock_c_reset_all_calls();
    
    STRICT_EXPECTED_CALL(sum(2, 4));
    code_under_test1();
    STRICT_EXPECTED_CALL(sum(3, 5));
    ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}

TEST_FUNCTION(umock_test_code_under_test2)
{
    umock_c_reset_all_calls();

    STRICT_EXPECTED_CALL(whatIsDouble(2, IGNORED_PTR_ARG));
    code_under_test2();
    STRICT_EXPECTED_CALL(whatIsDouble(3, IGNORED_PTR_ARG));
    ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}

Note the extreme similarities between the 2 tests, the only difference being the second test uses a pointer instead of the int used in the first test.

Both tests (umock_test_code_under_test1 and umock_test_code_under_test2) are poorly written and have a STRICT_EXPECTED_CALL after the code under test.

This is the expected behavior for umock_test_code_under_test1 : after code_under_test1 is called, there's going to be an actual call (sum(3, 5);) that is "extra" (actual). After STRICT_EXPECTED_CALL(sum(3, 5)); there is another "extra" (expected) call. They should not be matched because the expected call was not existing at the time when the actual call happened.

However, umock_c will happily match them and in the case of umock_test_code_under_test1 the test will happily pass.

image

umock_test_code_under_test2 produces the expected behavior (that is, the test will fail). However, even when the test fails (as expected) the message on the screen doesn't really help with understanding "why" it failed, because it looks very "should pass":
image
"well - if the second argument is ignored, how come the calls didn't match?".

Overall the expectation is that both tests should fail. And when they fail - the message on the screen should indicate better "why they failed".

WTB STRICT_EXPECTED_CALL with format string

At the moment the output of the

ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());

looks like this on my screen:

Assert failed in line 11488 test setup MM3E12 Expected: [CONSTBUFFER_DecRef(0000000000000000)][bsdl_address_dec_ref(0000000000000000)][CONSTBUFFER_DecRef(0000000000000000)][bsdl_address_dec_ref(0000000000000000)][gballoc_free(0000000000000000)][gballoc_free(0000000000000000)], Actual: [bsdl_address_dec_ref(00000287FC45D3D0)][CONSTBUFFER_DecRef(00000287FC45F870)][bsdl_address_dec_ref(00000287FC45D5B0)][CONSTBUFFER_DecRef(00000287FC45F870)][gballoc_free(00000287FC4804D0)][gballoc_free(00000287FC45DD30)]

Which often at debug time leaves me wondering "sooo... exactly which of my 100 CONSTBUFFER_DecRefs were not matched... first... While it is discoverable which one it is, I would like to propose to change the signature of the STRICT_EXPECTED_CALL macro to include a format_string and a list of values, just like "printf" has.

Old code:

STRICT_EXPECTED_CALL(CONSTBUFFER_DecRef(IGNORED_PTR_ARG));

=>

Expected: [CONSTBUFFER_DecRef(0000000000000000)]...

New code:

STRICT_EXPECTED_CALL(CONSTBUFFER_DecRef(IGNORED_PTR_ARG), "expected in line=%d", __LINE__);

=>

[CONSTBUFFER_DecRef(0000000000000000)]("expected in line=11459")... //note the string "as printed by printf"

I believe this would allow the user to easily and quickly discover "which one" of the expected calls was not matched.

Best Regards,
Andrei Porumb

Need a FAIL_FAST option for halting a test when an unexpected call occurs.

When any expected call fails, the return values of the remaining calls fall back to their default state (or 0 if not set). Which can be misleading when a successful return code is a zero (i.e. the system assigned default).

This can manifest itself in a seemingly unpredictable way, making tracing the "real" issue difficult (to say the least). For example, when call "n", the unexpected call, ultimately causes the framework to crash because call "n+7" now returns a NULL pointer. Therefore, we have almost no way to know which call is actually failing, because we are seeing all the calls "0 - (n+6)" have all returned "success" values then it crashes...

WTB global REGISTER_UMOCK_ALIAS_TYPE

For every used header, for every used type in unittests, the following needs to be done:

REGISTER_UMOCK_ALIAS_TYPE(some_type, some_defined_type).

In most cases the some_defined_type can only be 1. For example:

REGISTER_UMOCK_ALIAS_TYPE(BSDL_FILE_LL_HANDLE, void*);

(in this case, the type is void* because nobody should care what's behind it).

The proposal is to have in the header that initially defines some_type some method that is equivalent to REGISTER_UMOCK_ALIAS_TYPE(BSDL_FILE_LL_HANDLE, void*);

Best Regards,
Andrei Porumb

Add umock_c_windows_types.h

It would be nice to have the heavily used windows types out of the box in umock-c.
I.e.: ULONG, LONG, HRESULT, etc.

Then they could be registered for usage with one call, reducing the amount of duplicated code.
umocktypes_windows_register_types

MOCKABLE_FUNCTION vs MOCK_FUNCTION_WITH_CODE

MOCKABLE_FUNCTION is not the same as MOCK_FUNCTION_WITH_CODE.

A MOCK_FUNCTION_WITH_CODE will be called in the context of negative tests. a MOCKABLE_FUNCTION will not be called. Note: the MO_FUNCTION_WITH_CODE will return the proper (FAIL) return value, but the code will be executed.

The workaround is to use only MOCKABLE_FUNCTIONs and have a REGISTER_GLOBAL_MOCK_HOOK. The hook is not called in the context of negative tests.

Best Regards,
Andrei Porumb

WTB MOCKABLE_FUNCTION_WITH_RETURNS

Right now, there's a way to describe a mock by means of MOCKABLE_FUNCTION(...)
And a separate way to describe the behavior of the mock "when it succeeds/when it fails" by means of REGISTER_GLOBAL_MOCK_RETURNS(...)

Problem is, in every single unittest the REGISTER_GLOBAL_MOCK_RETURNS(...) needs to be done. This is repetitive, prone to human error ("i forgot to write it!").

Having a single new macro MOCKABLE_FUNCTION_WITH_RETURNS which would combine the two previous macros in the header would be quite awesome because it would bring consistency across unittests, and faster dev time, and would ensure that a said developer doesn't debug through negative tests because he forgot to add the global returns :)

Best Regards,
Andrei porumb

Make umock thread safe

Call recorder is not thread safe, but it would be nice to be thread safe in case of multi-threaded tests that want to ignore certain mocked calls in the call recorder.

.CopyOutArgument_ should not call a hooked function

When

STRICT_EXPECTED_CALL(function(args...))
    .CopyOutArgument_gaga(&source, sizeof(source));

is used AND function has a hook registered the hook WILL get called. This should not happen, and CopyOutArgument should behave like .SetReturn() and not invoke the hooked function.

WTB MOCKABLE_FUNCTION_RETURNS

At the moment there's no way to specify in the header "what is the fail/success return value" for a MOCKABLE_FUNCTION.

It can be done in the unittests by means of REGISTER_GLOBAL_MOCK_RETURNS.

And for every header, every time, in every unittest that so happens to #include header, REGISTER_GLOBAL_MOCK_RETURNS needs to be defined.

The effects of REGISTER_GLOBAL_MOCK_RETURNS should be uniquely define-able in the header, overwritable in unittests (should the writer of the unittests so need it).

Best Regards,
Andrei Porumb

Register types treats "const char *" and "char const *" differently

Would be nice if either of these mockable functions worked "out of the box"

When setup like this:

#include "umock_c/umocktypes_charptr.h"

TEST_SUITE_INITIALIZE(suite_init)
{
    // ...
    result = umocktypes_charptr_register_types();
    ASSERT_ARE_EQUAL(int, 0, result, "umocktypes_charptr_register_types");
    // ...
}

TEST_FUNCTION(test1)
{
    STRICT_EXPECTED_CALL(mocked_popen("foo", "bar"));
    mocked_popen("foo", "bar");
    ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}

This works:

MOCKABLE_FUNCTION(, FILE*, mocked_popen,
    const char*, command,
    const char*, mode);

This falls back to void*:

MOCKABLE_FUNCTION(, FILE*, mocked_popen,
    char const*, command,
    char const*, mode);

Expected: [mocked_popen(00007FF6BFF3D9EC,00007FF6BFF3D9AC)], Actual: [mocked_popen(00007FF6BFF3DA3C,00007FF6BFF3D9FC)]

Maybe a nice improvement to umocktypename_normalize

WTB umock_c regions

At the moment umock_c mechanism of expected/actual call matching is enables globally.

There's a mechanism to reset all the calls (umock_c_reset_all_calls()). But that is rather brutal.

I propose to have (in addition) umock_c_disable() and umock_c_enable().

umock_c_disable() would simply not do any matching of actual calls to expected calls.

umock_c_enable() would (re-)start matching actual calls to expected call.

Best Regards,
Andrei Porumb

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.