Пример использования ExpectWithArray в CMock
Я использую Ceedling под Ubuntu 16.04 и Eclipse 4.7.2. Пока что все работает нормально, за исключением того, что я не могу заставить работать функции-насмешки _ExpectWithArray.
Например, у меня есть следующая функция, которую мне нужно смоделировать void TestFunc(uint8_t * data);
, В моем тестовом файле у меня есть следующий вызов
uint8_t TEST_DATA[5] = { 0xFF, 0x00, 0xA0, 0x00, 0x09 };
TestFunc_ExpectWithArray(TEST_DATA, 5)
Я также попытался дать разные значения для param_depth
но без удачи.
Когда я пытаюсь запустить тест, всегда происходит сбой
implicit declaration of function ‘TestFunc_ExpectWithArray’ [-Wimplicit-function-declaration]
По моему опыту, это всегда происходит, когда функция mock не вызывается с правильными параметрами, и CMock не может генерировать mock-версию. Что я делаю неправильно? Может кто-нибудь привести пример, как правильно использовать _ExpectWithArray?
2 ответа
Добавьте эту строку -: массив для плагинов в.yml -file
:cmock:
:mock_prefix: mock_
:when_no_prototypes: :warn
:enforce_strict_ordering: TRUE
:plugins:
- :array
- :ignore
- :callback
пример использования _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
В качестве альтернативы попробуйте использовать _StubWithCallback
и реализовать собственную проверку массива с помощью пользовательской функции.
// 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();
}