|
4 | 4 | * SPDX-License-Identifier: MIT |
5 | 5 | */ |
6 | 6 |
|
7 | | -/************************************************************************************** |
8 | | - * INCLUDE |
9 | | - **************************************************************************************/ |
10 | | - |
11 | 7 | #include <catch.hpp> |
12 | | - |
13 | 8 | #include <StreamMock.h> |
14 | 9 | #include <Arduino_RPClite.h> |
15 | 10 |
|
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]); |
38 | 28 | } |
39 | 29 | } |
40 | 30 |
|
41 | | -TEST_CASE ("Testing find(const char *target, size_t length)", "[Stream-find-02]") |
| 31 | +TEST_CASE("RPCClient::call", "[RPCClient-01]") |
42 | 32 | { |
43 | 33 | StreamMock mock; |
| 34 | + SerialTransport transport(&mock); |
| 35 | + RPCClient rpc(transport); |
44 | 36 |
|
45 | | - WHEN ("'target' is contained in stream") |
| 37 | + WHEN("Make an RPC call") |
46 | 38 | { |
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); |
81 | 65 | } |
82 | 66 | } |
0 commit comments