Add mock print implementation for unit tests

This commit is contained in:
Daniel Öster 2025-08-31 13:07:17 +03:00
parent 01aa926f26
commit 268910ef63
4 changed files with 40 additions and 25 deletions

View file

@ -1,12 +1,15 @@
#ifndef __LOGGING_H__
#define __LOGGING_H__
#include <Print.h>
#include <inttypes.h>
#include "../../../USER_SETTINGS.h"
#include "../../datalayer/datalayer.h"
#include "types.h"
#ifndef UNIT_TEST
// Real implementation for production
#include <Print.h>
class Logging : public Print {
void add_timestamp(size_t size);
@ -17,9 +20,7 @@ class Logging : public Print {
Logging() {}
};
extern Logging logging;
// Replace compile-time macros with runtime checks
// Production macros
#define DEBUG_PRINTF(fmt, ...) \
do { \
if (datalayer.system.info.web_logging_active || datalayer.system.info.usb_logging_active) { \
@ -33,4 +34,34 @@ extern Logging logging;
logging.println(str); \
} \
} while (0)
#else
// Mock implementation for tests
#include <cstdarg>
#include <cstdio>
class Logging {
public:
// Mock methods that do nothing
size_t write(const uint8_t* buffer, size_t size) { return size; }
size_t write(uint8_t) { return 0; }
static void printf(const char* fmt, ...) {
// Empty implementation - silence unused parameter warnings
(void)fmt;
}
static void println(const char* str) { (void)str; }
Logging() {}
};
// Test macros - empty implementations
#define DEBUG_PRINTF(fmt, ...) ((void)0)
#define DEBUG_PRINTLN(str) ((void)0)
#endif
extern Logging logging;
#endif // __LOGGING_H__

View file

@ -28,6 +28,7 @@ ExternalProject_Get_Property(gtest source_dir binary_dir)
# Create a libgtest target to be used as a dependency by test programs
add_library(libgtest IMPORTED STATIC GLOBAL)
add_dependencies(libgtest gtest)
add_definitions(-DUNIT_TEST)
# Set libgtest properties
if(WIN32)

View file

@ -1,3 +1,4 @@
#include "Logging.h"
#include "../../src/devboard/utils/logging.h"
// This creates the global instance that links against the real implementation
Logging logging;

View file

@ -1,22 +1,4 @@
#pragma once
#include <cstdarg>
#include <cstdio>
namespace test {
class Logging {
public:
static void printf(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
static void println(const char* message) { printf("%s\n", message); }
static void print(const char* message) { printf("%s", message); }
};
} // namespace test
using Logging = test::Logging;
extern Logging logging;
// Include the real logging header which will provide the mock implementation
#include "../../Software/src/devboard/utils/logging.h"