-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathMockStream.h
48 lines (34 loc) · 934 Bytes
/
MockStream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include <Stream.h>
class MockStream : public Stream {
private:
const char *readBuffer;
uint16_t readBufferLength;
uint16_t readPosition;
char writeBuffer[1000] = {};
uint16_t writePosition;
public:
MockStream(const char *readBuffer, int bufferLength = -1)
: readBuffer(readBuffer),
readBufferLength(bufferLength == -1 ? strlen(readBuffer) : bufferLength),
readPosition(0),
writePosition(0){};
~MockStream() {}
size_t write(uint8_t byte) {
if (writePosition >= 1000) {
return 0;
}
writeBuffer[writePosition++] = byte;
return 1;
};
int peek() { return readBuffer[readPosition]; };
int read() {
if (readPosition >= readBufferLength) {
return -1;
}
return readBuffer[readPosition++];
};
int available() { return readBufferLength - readPosition; };
void flush(){};
char *response() { return writeBuffer; }
};