hello guys,
I’m trying to unittest some class that has parametter std::string. I’m including <string> inside header file, thats working inside unit testing area.
But i cannot compile this code for uploading into arduino. Compiler does not know . I also cannot use the String class from arduino because while compiling in testing files there is not connection to Arduino.h
Can you help me? What library should I use that is compilable in both enviroments?
with thanks
rytir.ch
native – for “pure” C++ code, that works everywhere and can be easily tested
native_arduino – for code that can be mocked by ArduinoFake, and thus tested as well
nodemcu (could be called just arduino) – for the specific code that cannot be tested. And by “cannot” I mean: would be hard to test or would have to be tested on the device anyway, so I’m not really that interested.
I’m trying to have as much as I can in the native section, and keep arduino–specific code small, simple and isolated.
Remember, u have to separate the code by having it in different “libraries” (folders in lib dir) and “front controllers” (apps in src), due to limitation of LDF.
well thank you but unfortunetly if i use ArduinoFake and its String, I cannot than use this code for different chip that is not arduino supported. Is there any way how to achive this?
It makes code a bit more complicated, but I’d argue this is the use–case polymorphism was born for.
class String {
virtual const char* toCString() = 0;
// etc.
}
class ArduinoString: public String { /* Arduino's String implementation */ }
class StdString: public String { /* std::string impl */ }
class StringFactory {} // abstract factory pattern
// somewhere in the code
String theString = stringFactory.create("foo bar baz"); // can be ArduinoString or StdString
Yea, I know that look. I probably have a tendency to overengineer stuff xd And I’m an OOP fanboy. Two of the things, I started to notice, that are disliked in C++/mC world ;p
Or, you can just use C–strings everywhere (yep, sucks).
Or… are you sure std::string does not work in Arduino–env? I think it actually worked for me (although it is discouraged for sure). I will test that later.