4

I am using Ceedling under Ubuntu 16.04 and Eclipse 4.7.2. So far everything works fine with the exception that I can't make _ExpectWithArray mocking functions to work.

For example I have the following function that I need to mock void TestFunc(uint8_t * data);. In my test file I have the following call uint8_t TEST_DATA[5] = { 0xFF, 0x00, 0xA0, 0x00, 0x09 }; TestFunc_ExpectWithArray(TEST_DATA, 5)

I also tried giving different values for param_depth but with no luck.

When I try to run the test it always fails with

implicit declaration of function ‘TestFunc_ExpectWithArray’ [-Wimplicit-function-declaration]

In my experience that always happens when the function to mock is not called with the right parameters and CMock fails to generate a mocked version. What am I doing wrong? Can someone give an example how to use _ExpectWithArray properly?

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177

2 Answers2

3

Add this line - :array for plugins in .yml -file

    :cmock:
    :mock_prefix: mock_
    :when_no_prototypes: :warn
    :enforce_strict_ordering: TRUE
    :plugins:
       - :array
       - :ignore
       - :callback

example use _ExpectWithArray

/test/test_example.c

    #include "unity.h"
    #include "temp.h"
    #include "mock_example.h"

    void setUp(void)
    {
    }

    void tearDown(void)
    {
    }

    void test_sendMesFirst(void)
    {
        uint8_t message[] = {"Hello"}, answerMessage[] = {"Hello"}, answerNum = 4;
        sendBytes_ExpectWithArray(answerMessage, sizeof(message), answerNum);
        sendMes(message, sizeof(message), answerNum);
    }  

/src/example.h

   #ifndef example_H
   #define example_H

   #include "stdint.h"

   void sendBytes(uint8_t *bytes, int size);

   #endif //

/src/temp.c

    #include "temp.h"
    #include "example.h"


    void sendMes(uint8_t *mes, int size, int num)
    {
        if(num < size)
            sendBytes(mes, num);
        else
            sendBytes(mes, size);
    }

/src/temp.h

    #ifndef temp_H
    #define temp_H

    #include "stdint.h"

    void sendMes(uint8_t *mes, int size, int num);        
    #endif
1

As an alternative, try using _StubWithCallback and implement your own array checking with a custom function.

// Can be any name. Same signature as void TestFunc(uint8_t * data);
void TestFunc_CustomMock(uint8_t* array) {
  // Loop through the array and ASSERT each element
  uint8_t TEST_DATA[5] = { 0xFF, 0x00, 0xA0, 0x00, 0x09 };

  for (uint8_t i = 0; i < 5; i++) {
    TEST_ASSERT_EQUAL(TEST_DATA[i], array[i]);
  }
  // Also possible to use this to check custom structs, etc. 

  // Can also return a value from this callback function, if the signature
  // says so
}

void test_MyFunc() {
  // Mock TestFunc(uint8_t * data)
  TestFunc_StubWithCallback((void *)&TestFunc_CustomMock);

  // Call TestFunc(uint8_t * data) which is located in MyFunc()
  MyFunc();

}
new_at_coding
  • 31
  • 1
  • 4