mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-03 01:39:30 +02:00
Add mock print implementation for unit tests
This commit is contained in:
parent
01aa926f26
commit
268910ef63
4 changed files with 40 additions and 25 deletions
|
@ -1,12 +1,15 @@
|
||||||
#ifndef __LOGGING_H__
|
#ifndef __LOGGING_H__
|
||||||
#define __LOGGING_H__
|
#define __LOGGING_H__
|
||||||
|
|
||||||
#include <Print.h>
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include "../../../USER_SETTINGS.h"
|
#include "../../../USER_SETTINGS.h"
|
||||||
#include "../../datalayer/datalayer.h"
|
#include "../../datalayer/datalayer.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
#ifndef UNIT_TEST
|
||||||
|
// Real implementation for production
|
||||||
|
#include <Print.h>
|
||||||
|
|
||||||
class Logging : public Print {
|
class Logging : public Print {
|
||||||
void add_timestamp(size_t size);
|
void add_timestamp(size_t size);
|
||||||
|
|
||||||
|
@ -17,9 +20,7 @@ class Logging : public Print {
|
||||||
Logging() {}
|
Logging() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Logging logging;
|
// Production macros
|
||||||
|
|
||||||
// Replace compile-time macros with runtime checks
|
|
||||||
#define DEBUG_PRINTF(fmt, ...) \
|
#define DEBUG_PRINTF(fmt, ...) \
|
||||||
do { \
|
do { \
|
||||||
if (datalayer.system.info.web_logging_active || datalayer.system.info.usb_logging_active) { \
|
if (datalayer.system.info.web_logging_active || datalayer.system.info.usb_logging_active) { \
|
||||||
|
@ -33,4 +34,34 @@ extern Logging logging;
|
||||||
logging.println(str); \
|
logging.println(str); \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} 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__
|
#endif // __LOGGING_H__
|
||||||
|
|
|
@ -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
|
# Create a libgtest target to be used as a dependency by test programs
|
||||||
add_library(libgtest IMPORTED STATIC GLOBAL)
|
add_library(libgtest IMPORTED STATIC GLOBAL)
|
||||||
add_dependencies(libgtest gtest)
|
add_dependencies(libgtest gtest)
|
||||||
|
add_definitions(-DUNIT_TEST)
|
||||||
|
|
||||||
# Set libgtest properties
|
# Set libgtest properties
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
|
|
|
@ -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;
|
Logging logging;
|
||||||
|
|
|
@ -1,22 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdarg>
|
// Include the real logging header which will provide the mock implementation
|
||||||
#include <cstdio>
|
#include "../../Software/src/devboard/utils/logging.h"
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue