8000 Adding test code for Stream::readBytes(...) · arduino/ArduinoCore-API@a6d2f7f · GitHub
[go: up one dir, main page]

Skip to content

Commit a6d2f7f

Browse files
committed
Adding test code for Stream::readBytes(...)
1 parent da7100c commit a6d2f7f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ set(TEST_SRCS
5252
src/Stream/test_getTimeout.cpp
5353
src/Stream/test_parseFloat.cpp
5454
src/Stream/test_parseInt.cpp
55+
src/Stream/test_readBytes.cpp
5556
src/Stream/test_readString.cpp
5657
src/Stream/test_readStringUntil.cpp
5758
src/Stream/test_setTimeout.cpp

test/src/Stream/test_readBytes.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
* INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <StreamMock.h>
12+
13+
/**************************************************************************************
14+
* TEST CODE
15+
**************************************************************************************/
16+
17+
TEST_CASE ("Testing readBytes(char *buffer, size_t length)", "[Stream-readBytes-01]")
18+
{
19+
StreamMock mock;
20+
21+
WHEN ("the stream is empty")
22+
{
23+
char buf[32] = {0};
24+
REQUIRE(mock.readBytes(buf, sizeof(buf)) == 0);
25+
}
26+
WHEN ("the stream contains less data then we want to read")
27+
{
28+
mock << "some stream content";
29+
char buf[32] = {0};
30+
REQUIRE(mock.readBytes(buf, sizeof(buf)) == strlen("some stream content"));
31+
REQUIRE(strcmp(buf, "some stream content") == 0);
32+
REQUIRE(mock.readString() == arduino::String(""));
33+
}
34+
WHEN ("the stream contains more data then we want to read")
35+
{
36+
mock << "some stream content";
37+
char buf[5] = {0};
38+
REQUIRE(mock.readBytes(buf, sizeof(buf)) == 5);
39+
REQUIRE(strcmp(buf, "some ") == 0);
40+
REQUIRE(mock.readString() == arduino::String("stream content"));
41+
}
42+
}

0 commit comments

Comments
 (0)
0