Skip to content
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
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

[env:native]
platform = native
build_flags = -std=gnu++17
build_flags = -std=gnu++17 -Wa,-mbig-obj
test_build_src = yes
;build_type = debug
;debug_test = *
3 changes: 2 additions & 1 deletion src/ArduinoFake.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ class ArduinoFakeContext

void reset(void)
{
ArduinoFakeInstances* newInstances = new ArduinoFakeInstances();
if (this->Instances) {
delete this->Instances;
}
this->Instances = new ArduinoFakeInstances();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this necessary ?

Copy link
Author

@arek-jamanskiy arek-jamanskiy Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is necessary to fix unit test ArduinoContextTest::test_reset. It failed on my pc all the time no matter what.
It failed because CRT, used in g++ compiler shipped with msys64 installed on Windows, reuses very same memory address (value of this->Instances) when re-creating object just after deleting it. So I created new object first and then deleted the old one

this->Instances = newInstances;

this->Mocks->Function.Reset();
this->Mocks->Stream.Reset();
Expand Down
29 changes: 29 additions & 0 deletions src/PrintFake.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
#include "ArduinoFake.h"
#include "PrintFake.h"
#include <stdarg.h>

size_t Print::write(const uint8_t *buffer, size_t size)
{
return ArduinoFakeInstance(Print, this)->write(buffer, size);
}


size_t Print::printf(const char *format, ...) {
va_list arg;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a call to the fake instance not a concrete implementation.

This might help : https://github.com/FabioBatSilva/ArduinoFake/blob/master/CONTRIBUTING.md

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried - maybe not very hard - but mocking method with ... did not work for me at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I will try one more time. thanks for fast feedback!

va_start(arg, format);

char loc_buf[64];
char *temp = loc_buf;
int len = vsnprintf(temp, sizeof(loc_buf), format, arg);
if (len < 0) {
va_end(arg);
return 0;
}
if (len >= (int)sizeof(loc_buf)) { // comparison of same sign type for the compiler
temp = (char *)malloc(len + 1);
if (temp == NULL) {
va_end(arg);
return 0;
}
len = vsnprintf(temp, len + 1, format, arg);
}
va_end(arg);
len = write((uint8_t *)temp, len);
if (temp != loc_buf) {
free(temp);
}
return len;
}

size_t Print::print(const __FlashStringHelper *ifsh)
{
return ArduinoFakeInstance(Print, this)->print(ifsh);
Expand Down
3 changes: 3 additions & 0 deletions src/PrintFake.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ struct PrintFake
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
virtual size_t write(uint8_t) = 0;

virtual size_t printf(const char *format, ...) = 0;


virtual size_t print(const __FlashStringHelper *) = 0;
virtual size_t print(const String &) = 0;
virtual size_t print(const char[]) = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/arduino/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Print
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *)buffer, size);
}


size_t printf(const char *format, ...) __attribute__((format(printf, 2, 3)));

size_t print(const __FlashStringHelper *);
size_t print(const String &);
Expand Down
4 changes: 4 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void setUp(void)
{
ArduinoFakeReset();
}
void tearDown(void)
{

}

int main(int argc, char **argv)
{
Expand Down
19 changes: 18 additions & 1 deletion test/test_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,27 @@ namespace PrintTest
Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(unsigned long, int)).Using(unsigned_long_var, DEC)).Once();
}


void test_printf(void)
{
const char * format = "format %d %s";
int int_var = 123;
const char * char_array_var = "char_array_var";
const char * expected_output = "format 123 char_array_var";

When(OverloadedMethod(ArduinoFake(Print), write, size_t(const uint8_t *, size_t ))).AlwaysReturn(strlen(expected_output));
Print* print = ArduinoFakeMock(Print);

size_t result = print->printf(format, int_var, char_array_var);
TEST_ASSERT_EQUAL(strlen(expected_output), result);

}

void run_tests()
{
RUN_TEST(PrintTest::test_print_variables);
RUN_TEST(PrintTest::test_println_variables);
RUN_TEST(PrintTest::test_print_variables);
RUN_TEST(PrintTest::test_printf);
}
}

Expand Down