Skip to content

Commit 7388814

Browse files
committed
add error response test
1 parent 14dd3ed commit 7388814

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/test_ParserVS2/test_ParserVS2.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,43 @@ void test_ok_readresponse() {
9292
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, parser.packet().data(), 2);
9393
}
9494

95+
void test_nok_readresponse() {
96+
const uint8_t stream[] = {
97+
0x41, // start byte
98+
0x06, // length
99+
0x03, // packet type (error)
100+
0x01, // flags: id + function code (0 + read)
101+
0x77, // address 1
102+
0x60, // address 2
103+
0x01, // payload length
104+
0x01, // payload
105+
0xe3 // cs
106+
};
107+
const std::size_t length = 9;
108+
const std::size_t packetLength = 7;
109+
const uint8_t data[1] = {0x01};
110+
111+
std::size_t bytesRead = 0;
112+
ParserResult result = ParserResult::ERROR;
113+
114+
while (bytesRead < length) {
115+
result = parser.parse(stream[bytesRead++]);
116+
if (result != ParserResult::CONTINUE) {
117+
break;
118+
}
119+
}
120+
121+
TEST_ASSERT_EQUAL(ParserResult::COMPLETE, result);
122+
TEST_ASSERT_EQUAL_UINT(length, bytesRead);
123+
TEST_ASSERT_EQUAL_UINT8(packetLength, parser.packet().length());
124+
TEST_ASSERT_EQUAL_UINT8(PacketType::ERROR, parser.packet().packetType());
125+
TEST_ASSERT_EQUAL_UINT8(0x00, parser.packet().id());
126+
TEST_ASSERT_EQUAL_UINT8(FunctionCode::READ, parser.packet().functionCode());
127+
TEST_ASSERT_EQUAL_UINT16(0x7760, parser.packet().address());
128+
TEST_ASSERT_EQUAL_UINT8(0x01, parser.packet().dataLength());
129+
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, parser.packet().data(), 1);
130+
}
131+
95132
void test_ok_writeresponse() {
96133
const uint8_t stream[] = {
97134
0x41, // start byte
@@ -287,6 +324,7 @@ int main() {
287324
UNITY_BEGIN();
288325
RUN_TEST(test_ok_request);
289326
RUN_TEST(test_ok_readresponse);
327+
RUN_TEST(test_nok_readresponse);
290328
RUN_TEST(test_ok_writeresponse);
291329
RUN_TEST(test_spuriousbytes);
292330
RUN_TEST(test_invalidLength);

0 commit comments

Comments
 (0)