Skip to content

Add String std::string wrapper and some methods to Serial mock. #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions include/arduino-mock/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,43 @@ class ArduinoMock {
ArduinoMock* arduinoMockInstance();
void releaseArduinoMock();

namespace {
std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}
}

class String : public std::string {
public:
String() {}
String(const String& string) : String(string.c_str()) {}
String(const std::string& string) : std::string(string) {}
String(const char* string) : std::string(string) {}

String substring(int start) {
return substr(start);
}
String substring(int start, int end) {
return substr(start, end - start);
}

String remove(int start) {
return erase(start);
}

void replace(const String& from, const String& to) {
ReplaceAll(*this, from, to);
}

int indexOf(const char needle) {
return find(needle);
}
};


#endif // ARDUINO_H
4 changes: 4 additions & 0 deletions include/arduino-mock/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class SerialMock {

MOCK_METHOD0(available, uint8_t());
MOCK_METHOD0(read, uint8_t());
MOCK_METHOD1(readStringUntil, String(const char));
MOCK_METHOD0(peek, uint8_t());

MOCK_METHOD0(flush, void());

Expand Down Expand Up @@ -79,6 +81,8 @@ class Serial_ {

uint8_t available();
uint8_t read();
uint8_t peek();
String readStringUntil(const char terminator);

static void flush();

Expand Down
8 changes: 8 additions & 0 deletions src/Serial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,13 @@ uint8_t Serial_::read() {
return gSerialMock->read();
}

uint8_t Serial_::peek() {
return gSerialMock->peek();
}

String Serial_::readStringUntil(const char term) {
return gSerialMock->readStringUntil(term);
}

// Preinstantiate Objects
Serial_ Serial;