Skip to content

Commit feaec3e

Browse files
committed
Added more unit-test
1 parent 446ab81 commit feaec3e

File tree

1 file changed

+76
-8
lines changed

1 file changed

+76
-8
lines changed

extras/test/src/RPC/test_RPCClient.cpp

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <StreamMock.h>
99
#include <Arduino_RPClite.h>
1010

11-
// MsgPack codes
11+
// Poor-man MsgPack encoder
1212
#define ARRAY_N(x) (0x90 + (x))
1313
#define STRING_N(x) (0xA0 + (x))
1414
#define NIL 0xc0
@@ -30,13 +30,12 @@ void compareArrays(const std::string & test, unsigned line, const T *expected, c
3030

3131
TEST_CASE("RPCClient::call", "[RPCClient-01]")
3232
{
33-
StreamMock mock;
34-
SerialTransport transport(&mock);
35-
RPCClient rpc(transport);
36-
3733
WHEN("Make an RPC call")
3834
{
39-
const unsigned char response[] = {
35+
StreamMock mock;
36+
SerialTransport transport(&mock);
37+
RPCClient rpc(transport);
38+
byte response[] = {
4039
ARRAY_N(4),
4140
RESP_TAG,
4241
0,
@@ -45,11 +44,11 @@ TEST_CASE("RPCClient::call", "[RPCClient-01]")
4544
};
4645
mock.push_array(response, sizeof(response));
4746

48-
float result;
47+
float result = 0.0f;
4948
bool ok = rpc.call("mult", result, 2.0, 3.0);
5049
REQUIRE(ok == true);
5150
REQUIRE(result == 6.0f);
52-
const unsigned char expected[] = {
51+
byte expected[] = {
5352
ARRAY_N(4),
5453
CALL_TAG,
5554
0, // msg_id
@@ -63,4 +62,73 @@ TEST_CASE("RPCClient::call", "[RPCClient-01]")
6362
REQUIRE(mock.pull_array(got, sizeof(expected)) == sizeof(expected));
6463
COMPARE_ARRAYS(expected, got);
6564
}
65+
66+
WHEN("Make an RPC call with an missing parameter, followed by a response without an error")
67+
{
68+
StreamMock mock;
69+
SerialTransport transport(&mock);
70+
RPCClient rpc(transport);
71+
{
72+
const unsigned char response[] = {
73+
ARRAY_N(4),
74+
RESP_TAG,
75+
1,
76+
ARRAY_N(2),
77+
1,
78+
STRING_N(17), 'm', 'i', 's', 's', 'i', 'n', 'g', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r',
79+
NIL,
80+
};
81+
mock.push_array(response, sizeof(response));
82+
83+
float result = 0.0f;
84+
bool ok = rpc.call("mult", result, 2.0);
85+
REQUIRE(ok == false);
86+
REQUIRE(result == 0.0f);
87+
const unsigned char expected[] = {
88+
ARRAY_N(4),
89+
CALL_TAG,
90+
1, // msg_id
91+
STRING_N(4), 'm', 'u', 'l', 't',
92+
ARRAY_N(1), // args
93+
FLOAT_32, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 2.0
94+
};
95+
REQUIRE(mock.pull_available() == sizeof(expected));
96+
unsigned char got[sizeof(expected)];
97+
REQUIRE(mock.pull_array(got, sizeof(expected)) == sizeof(expected));
98+
COMPARE_ARRAYS(expected, got);
99+
REQUIRE(rpc.lastError.code == 1);
100+
REQUIRE(rpc.lastError.traceback == "missing parameter");
101+
}
102+
// Make another call without error
103+
{
104+
byte response[] = {
105+
ARRAY_N(4),
106+
RESP_TAG,
107+
2,
108+
NIL,
109+
FLOAT_32, 0x40, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // 6.0
110+
};
111+
mock.push_array(response, sizeof(response));
112+
113+
float result = 0.0f;
114+
bool ok = rpc.call("mult", result, 2.0, 3.0);
115+
REQUIRE(ok == true);
116+
REQUIRE(result == 6.0f);
117+
byte expected[] = {
118+
ARRAY_N(4),
119+
CALL_TAG,
120+
2, // msg_id
121+
STRING_N(4), 'm', 'u', 'l', 't',
122+
ARRAY_N(2), // args
123+
FLOAT_32, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 2.0
124+
FLOAT_32, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // 3.0
125+
};
126+
REQUIRE(mock.pull_available() == sizeof(expected));
127+
unsigned char got[sizeof(expected)];
128+
REQUIRE(mock.pull_array(got, sizeof(expected)) == sizeof(expected));
129+
COMPARE_ARRAYS(expected, got);
130+
REQUIRE(rpc.lastError.code == 0);
131+
REQUIRE(rpc.lastError.traceback == "");
132+
}
133+
}
66134
}

0 commit comments

Comments
 (0)