Unit testing arduino code with std::string

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.

#include <string>

class USBreceiver
    bool messageIsComplete();
    void addData(std::string data);
    std::string getData();


For testing I’m usign googletest in native enviroment

platform = atmelavr
board = uno
framework = arduino
test_ignore = test_desktop

platform = native
test_framework = googletest
build_type = debug
debug_test = test_desktop
debug_init_break = tbreak loop
test_ignore = test_HW

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

Use this GitHub - FabioBatSilva/ArduinoFake: Arduino mocking made easy – it is very limited, but I tested it covers the basics.

I’ve actually created 3 envs:

  1. native – for “pure” C++ code, that works everywhere and can be easily tested
  2. native_arduino – for code that can be mocked by ArduinoFake, and thus tested as well
  3. 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?

You can always add another layer of abstraction :wink:

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.