Skip to content
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

Some issues when using String type #23

Open
aliaksandr-panasiuk opened this issue Sep 1, 2021 · 0 comments
Open

Some issues when using String type #23

aliaksandr-panasiuk opened this issue Sep 1, 2021 · 0 comments

Comments

@aliaksandr-panasiuk
Copy link

aliaksandr-panasiuk commented Sep 1, 2021

I've faced with a strange behavior when String type is used

a test contains the following

When(OverloadedMethod(ArduinoFake(Serial), println, size_t(const String &s))).AlwaysReturn();
String msg("test");
Serial.println(msg);
Verify(OverloadedMethod(ArduinoFake(Serial), println, size_t(const String &s)).Using("test")).Once();
// [FAILED]

it fails with a message ...'ArduinoFake(Serial).println(1)...
"println(1)." <-- why '1' ?

BUT the following code works OK

When(OverloadedMethod(ArduinoFake(Serial), println, size_t(const char *))).AlwaysReturn();
Serial.println("test");
Verify(OverloadedMethod(ArduinoFake(Serial), println, size_t(const char *)).Using("test")).Once();
// [PASSED]

One more example which doesn't work

const char * msg = String("test").c_str();
Serial.println(msg);
Verify(OverloadedMethod(ArduinoFake(Serial), println, size_t(const char *)).Using("test")).Once();
// [FAILED]
// it prints 'ArduinoFake(Serial).println()' like println was called without any value at all

BUT this one works OK

const char * msg = "test";
Serial.println(msg);
Verify(OverloadedMethod(ArduinoFake(Serial), println, size_t(const char *)).Using("test")).Once();
// [PASSED]

[UPDATED]
as I see, one of possible workaround for this is to modify the 'proxy' methods, and always create a copy of a String without calling free() method. E.g.

size_t Print::println(const String &s)
{
    return ArduinoFakeInstance(Print, this)->println(s);
}

replace to

size_t Print::println(const String &s)
{
    char * str = (char*)malloc(s.length() * sizeof(char));
    strcpy(str, s.c_str());
    // do not call free(str).
    return ArduinoFakeInstance(Print, this)->println(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants