Skip to content

Commit

Permalink
test: refactor API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Sep 5, 2023
1 parent 2ea146f commit dfc4533
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ add_test(i2cChunkedReceive_test)
add_test(i2cChunkedTransmit_test)
add_test(NoteBinaryReceive_test)
add_test(NoteBinaryTransmit_test)
add_test(NoteBinaryRequiredRxBuffer_test)
add_test(NoteBinaryRequiredRxMaxBuffer_test)
add_test(NoteBinaryReset_test)

if(NOTE_C_COVERAGE)
Expand Down
40 changes: 21 additions & 19 deletions test/src/NoteBinaryReceive_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

DEFINE_FFF_GLOBALS
FAKE_VALUE_FUNC(J *, NoteNewRequest, const char *)
FAKE_VALUE_FUNC(const char *, NoteBinaryRequiredRxBuffer, size_t *)
FAKE_VALUE_FUNC(const char *, NoteBinaryRequiredRxMaxBuffer, size_t *)
FAKE_VALUE_FUNC(J *, NoteRequestResponse, J *)
FAKE_VALUE_FUNC(const char *, NoteChunkedReceive, uint8_t *, size_t *, bool,
size_t, uint32_t *)
Expand All @@ -43,23 +43,25 @@ size_t rawMsgLen = strlen(rawMsg);
namespace
{

SCENARIO("NoteBinaryReceive")
SCENARIO("NoteBinaryReceive all")
{
RESET_FAKE(NoteNewRequest);
RESET_FAKE(NoteBinaryRequiredRxBuffer);
RESET_FAKE(NoteBinaryRequiredRxMaxBuffer);
RESET_FAKE(NoteRequestResponse);
RESET_FAKE(NoteChunkedReceive);
RESET_FAKE(NoteLockNote);
RESET_FAKE(NoteUnlockNote);

const size_t offset = 0;

NoteSetFnDefault(malloc, free, NULL, NULL);

// These fakes are the default. Tests below may override them to exercise
// different scenarios.
NoteNewRequest_fake.custom_fake = [](const char *req) -> J* {
return JCreateObject();
};
NoteBinaryRequiredRxBuffer_fake.custom_fake = [](size_t *size)
NoteBinaryRequiredRxMaxBuffer_fake.custom_fake = [](size_t *size)
-> const char * {
*size = bufLen;

Expand All @@ -76,29 +78,29 @@ SCENARIO("NoteBinaryReceive")
return rsp;
};

GIVEN("NoteBinaryRequiredRxBuffer fails") {
NoteBinaryRequiredRxBuffer_fake.custom_fake = [](size_t *size)
GIVEN("NoteBinaryRequiredRxMaxBuffer fails") {
NoteBinaryRequiredRxMaxBuffer_fake.custom_fake = [](size_t *size)
-> const char * {
*size = 0;

return NULL;
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
}

GIVEN("NoteBinaryRequiredRxBuffer indicates there's no binary data") {
NoteBinaryRequiredRxBuffer_fake.custom_fake = NULL;
NoteBinaryRequiredRxBuffer_fake.return_val = "some error";
GIVEN("NoteBinaryRequiredRxMaxBuffer indicates there's no binary data") {
NoteBinaryRequiredRxMaxBuffer_fake.custom_fake = NULL;
NoteBinaryRequiredRxMaxBuffer_fake.return_val = "some error";

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -107,15 +109,15 @@ SCENARIO("NoteBinaryReceive")
}

GIVEN("The receive buffer isn't big enough") {
NoteBinaryRequiredRxBuffer_fake.custom_fake = [](size_t *size)
NoteBinaryRequiredRxMaxBuffer_fake.custom_fake = [](size_t *size)
-> const char * {
*size = bufLen + 1;

return NULL;
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -128,7 +130,7 @@ SCENARIO("NoteBinaryReceive")
NoteNewRequest_fake.return_val = NULL;

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -146,7 +148,7 @@ SCENARIO("NoteBinaryReceive")
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -158,7 +160,7 @@ SCENARIO("NoteBinaryReceive")
NoteChunkedReceive_fake.return_val = "some error";

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -176,7 +178,7 @@ SCENARIO("NoteBinaryReceive")
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand Down Expand Up @@ -207,7 +209,7 @@ SCENARIO("NoteBinaryReceive")
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -217,7 +219,7 @@ SCENARIO("NoteBinaryReceive")

AND_GIVEN("The computed MD5 matches the status field") {
WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("No error is returned") {
CHECK(err == NULL);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* @file NoteBinaryRequiredRxBuffer_test.cpp
* @file NoteBinaryRequiredRxMaxBuffer_test.cpp
*
* Written by the Blues Inc. team.
*
Expand All @@ -26,7 +26,7 @@ const size_t cobsLen = 10;
namespace
{

SCENARIO("NoteBinaryRequiredRxBuffer")
SCENARIO("NoteBinaryRequiredRxMaxBuffer")
{
RESET_FAKE(NoteRequestResponse);

Expand All @@ -41,8 +41,8 @@ SCENARIO("NoteBinaryRequiredRxBuffer")
return NULL;
};

WHEN("NoteBinaryRequiredRxBuffer is called") {
const char *err = NoteBinaryRequiredRxBuffer(&size);
WHEN("NoteBinaryRequiredRxMaxBuffer is called") {
const char *err = NoteBinaryRequiredRxMaxBuffer(&size);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -59,8 +59,8 @@ SCENARIO("NoteBinaryRequiredRxBuffer")
return rsp;
};

WHEN("NoteBinaryRequiredRxBuffer is called") {
const char *err = NoteBinaryRequiredRxBuffer(&size);
WHEN("NoteBinaryRequiredRxMaxBuffer is called") {
const char *err = NoteBinaryRequiredRxMaxBuffer(&size);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -78,8 +78,8 @@ SCENARIO("NoteBinaryRequiredRxBuffer")
return rsp;
};

WHEN("NoteBinaryRequiredRxBuffer is called") {
const char *err = NoteBinaryRequiredRxBuffer(&size);
WHEN("NoteBinaryRequiredRxMaxBuffer is called") {
const char *err = NoteBinaryRequiredRxMaxBuffer(&size);

THEN("An error is not returned") {
CHECK(err == NULL);
Expand All @@ -101,8 +101,8 @@ SCENARIO("NoteBinaryRequiredRxBuffer")
return rsp;
};

WHEN("NoteBinaryRequiredRxBuffer is called") {
const char *err = NoteBinaryRequiredRxBuffer(&size);
WHEN("NoteBinaryRequiredRxMaxBuffer is called") {
const char *err = NoteBinaryRequiredRxMaxBuffer(&size);

THEN("An error is not returned") {
CHECK(err == NULL);
Expand Down

0 comments on commit dfc4533

Please sign in to comment.