Skip to content

Commit b6a55a8

Browse files
committed
Adding StreamMock class to test functions of Stream base class as well as test code for Stream::getTimeout()
1 parent f2dcfc4 commit b6a55a8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

Diff for: test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ set(TEST_SRCS
4747
src/Ringbuffer/test_peek.cpp
4848
src/Ringbuffer/test_read_char.cpp
4949
src/Ringbuffer/test_store_char.cpp
50+
src/Stream/test_getTimeout.cpp
5051
src/String/test_concat.cpp
5152
src/String/test_operators.cpp
5253
src/String/test_compareTo.cpp

Diff for: test/include/StreamMock.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*/
4+
5+
#ifndef STREAM_MOCK_H_
6+
#define STREAM_MOCK_H_
7+
8+
/**************************************************************************************
9+
* INCLUDE
10+
**************************************************************************************/
11+
12+
#include <sstream>
13+
14+
#include <Stream.h>
15+
16+
/**************************************************************************************
17+
* CLASS DECLARATION
18+
**************************************************************************************/
19+
20+
class StreamMock : public arduino::Stream
21+
{
22+
public:
23+
std::stringstream _ss;
24+
25+
virtual size_t write(uint8_t ch) override { _ss << static_cast<char>(ch); return 1; }
26+
virtual int available() override { return _ss.gcount(); }
27+
virtual int read() override
28+
{
29+
char ch;
30+
_ss >> ch;
31+
return ch;
32+
}
33+
virtual int peek() override
34+
{
35+
char ch = _ss.peek();
36+
return ch;
37+
}
38+
};
39+
40+
#endif /* STREAM_MOCK_H_ */

Diff for: test/src/Stream/test_getTimeout.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 ("Verifying if default timeout is returned correctly", "[Stream-getTimeout-01]")
18+
{
19+
StreamMock mock;
20+
REQUIRE(mock.getTimeout() == 1000);
21+
}

0 commit comments

Comments
 (0)