Skip to content

Commit 94bb1bc

Browse files
committed
Implementation of mock and first RPC unit-test
1 parent 819ab72 commit 94bb1bc

File tree

4 files changed

+102
-128
lines changed

4 files changed

+102
-128
lines changed

extras/test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ set(TEST_TARGET_SRCS
8484
src/itoa.cpp
8585
src/MillisFake.cpp
8686
src/PrintMock.cpp
87-
src/StreamMock.cpp
8887
${TEST_SRCS}
8988
${TEST_DUT_SRCS}
9089
)

extras/test/include/StreamMock.h

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,71 @@
1212
**************************************************************************************/
1313

1414
#include <deque>
15-
1615
#include <api/Stream.h>
1716

18-
/**************************************************************************************
19-
* CLASS DECLARATION
20-
**************************************************************************************/
21-
2217
class StreamMock : public arduino::Stream
2318
{
2419
public:
20+
void operator << (char const * str) {
21+
for (size_t c = 0; c < strlen(str); c++) {
22+
_readStream.push_back(str[c]);
23+
}
24+
}
25+
26+
void push_array(const unsigned char *str, size_t len) {
27+
for (size_t c = 0; c < len; c++) {
28+
_readStream.push_back(static_cast<char>(str[c]));
29+
}
30+
}
2531

26-
void operator << (char const * str);
32+
int pull_available() {
33+
return _writeStream.size();
34+
}
2735

28-
virtual size_t write(uint8_t ch) override;
29-
virtual int available() override;
30-
virtual int read() override;
31-
virtual int peek() override;
36+
int pull_array(unsigned char *str, size_t len) {
37+
if (_writeStream.size() < len) {
38+
return 0; // Not enough data to read
39+
}
40+
for (size_t c = 0; c < len; c++) {
41+
str[c] = _writeStream.front();
42+
_writeStream.pop_front();
43+
}
44+
return len;
45+
}
3246

3347
private:
34-
std::deque<char> _stream;
48+
std::deque<unsigned char> _readStream;
49+
std::deque<unsigned char> _writeStream;
50+
51+
public:
52+
// --------------------------------
53+
// Implementation of Stream methods
54+
// --------------------------------
55+
56+
virtual size_t write(uint8_t ch) override {
57+
_writeStream.push_back(static_cast<char>(ch));
58+
return 1;
59+
}
60+
61+
virtual int available() override {
62+
return _readStream.size();
63+
}
64+
65+
virtual int read() override {
66+
if (available() == 0) {
67+
return -1;
68+
}
69+
unsigned char c = _readStream.at(0);
70+
_readStream.pop_front();
71+
return c;
72+
}
3573

74+
virtual int peek() override {
75+
if (available() == 0) {
76+
return -1;
77+
}
78+
return _readStream.at(0);
79+
}
3680
};
3781

3882
#endif /* STREAM_MOCK_H_ */

extras/test/src/RPC/test_RPCClient.cpp

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,63 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
/**************************************************************************************
8-
* INCLUDE
9-
**************************************************************************************/
10-
117
#include <catch.hpp>
12-
138
#include <StreamMock.h>
149
#include <Arduino_RPClite.h>
1510

16-
/**************************************************************************************
17-
* TEST CODE
18-
**************************************************************************************/
19-
20-
TEST_CASE ("Test RPCClient call", "[RPCClient-01]")
21-
{
22-
StreamMock mock;
23-
24-
WHEN ("'target' is contained in stream")
25-
{
26-
mock << "This is a test string";
27-
28-
29-
REQUIRE(mock.find("test") == true);
30-
REQUIRE(mock.readString() == arduino::String(" string"));
31-
}
32-
WHEN ("'target' is not contained in stream")
33-
{
34-
mock << "This is a string";
35-
36-
REQUIRE(mock.find("test") == false);
37-
REQUIRE(mock.readString() == arduino::String(""));
11+
// MsgPack codes
12+
#define ARRAY_N(x) (0x90 + (x))
13+
#define STRING_N(x) (0xA0 + (x))
14+
#define NIL 0xc0
15+
#define FLOAT_32 0xcb
16+
#define CALL_TAG 0x00
17+
#define RESP_TAG 0x01
18+
19+
#define COMPARE_ARRAYS(expected, got) compareArrays(Catch::getResultCapture().getCurrentTestName(), __LINE__, expected, got, sizeof(expected), sizeof(got))
20+
template <typename T>
21+
void compareArrays(const std::string & test, unsigned line, const T *expected, const T *got, size_t expectedSize, size_t gotSize) {
22+
INFO("Test case [" << test << "] failed at line " << line); // Reported only if REQUIRE fails
23+
INFO("Array size: expected: " << expectedSize << ", got: " << gotSize);
24+
REQUIRE(expectedSize == gotSize);
25+
for (size_t i = 0; i < expectedSize; ++i) {
26+
INFO("Index " << i << ": expected " << static_cast<int>(expected[i]) << ", got " << static_cast<int>(got[i]));
27+
REQUIRE(expected[i] == got[i]);
3828
}
3929
}
4030

41-
TEST_CASE ("Testing find(const char *target, size_t length)", "[Stream-find-02]")
31+
TEST_CASE("RPCClient::call", "[RPCClient-01]")
4232
{
4333
StreamMock mock;
34+
SerialTransport transport(&mock);
35+
RPCClient rpc(transport);
4436

45-
WHEN ("'target' is contained in stream")
37+
WHEN("Make an RPC call")
4638
{
47-
mock << "This is a test string";
48-
49-
/* 'length' should actually be '4' or strlen("test"). I'd rather
50-
* think this API should not be exposed at all.
51-
*/
52-
REQUIRE(mock.find("test", 3) == true);
53-
REQUIRE(mock.readString() == arduino::String("t string"));
54-
}
55-
WHEN ("'target' is not contained in stream")
56-
{
57-
mock << "This is a string";
58-
59-
REQUIRE(mock.find("test", 3) == false);
60-
REQUIRE(mock.readString() == arduino::String(""));
61-
}
62-
}
63-
64-
TEST_CASE ("Testing find(char target)", "[Stream-find-03]")
65-
{
66-
StreamMock mock;
67-
68-
WHEN ("'target' is contained in stream")
69-
{
70-
mock << "This is a test string";
71-
72-
REQUIRE(mock.find('t') == true);
73-
REQUIRE(mock.readString() == arduino::String("est string"));
74-
}
75-
WHEN ("'target' is not contained in stream")
76-
{
77-
mock << "This is a string";
78-
79-
REQUIRE(mock.find('!') == false);
80-
REQUIRE(mock.readString() == arduino::String(""));
39+
const unsigned char response[] = {
40+
ARRAY_N(4),
41+
RESP_TAG,
42+
0,
43+
NIL,
44+
FLOAT_32, 0x40, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // 6.0
45+
};
46+
mock.push_array(response, sizeof(response));
47+
48+
float result;
49+
bool ok = rpc.call("mult", result, 2.0, 3.0);
50+
REQUIRE(ok == true);
51+
REQUIRE(result == 6.0f);
52+
const unsigned char expected[] = {
53+
ARRAY_N(4),
54+
CALL_TAG,
55+
0, // msg_id
56+
STRING_N(4), 'm', 'u', 'l', 't',
57+
ARRAY_N(2), // args
58+
FLOAT_32, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 2.0
59+
FLOAT_32, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // 3.0
60+
};
61+
REQUIRE(mock.pull_available() == sizeof(expected));
62+
unsigned char got[sizeof(expected)];
63+
REQUIRE(mock.pull_array(got, sizeof(expected)) == sizeof(expected));
64+
COMPARE_ARRAYS(expected, got);
8165
}
8266
}

extras/test/src/StreamMock.cpp

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)