Files
Library/tests/standalone/Log/ConsoleLogger.cpp
T
faumaray bd5488b15b Windows: qualify Configuration, Crypto and System
- Allowlist Configuration, Crypto and System for the Windows target.
- System: link winmm for the Win32 audio backend; include windows.h before
  mmsystem.h.
- Tests: add a whole-file // @requires-posix gate (CMake path and the VBS
  build.cpp) and mark the POSIX-only System suites; skip helper-based cases
  on Windows; make setenv/unsetenv and getpid portable.
- The POSIX process helper is no longer built on Windows.

Cross-built with x86_64-w64-mingw32 GCC 16.2: all modules and VoidTests.exe
link. Linux System+Testing suite still passes (477 tests, 474 passed).

Refs #65
2026-12-05 15:57:12 +03:00

106 lines
2.7 KiB
C++

#include <Void/Log/ConsoleLogger.hpp>
#include <Void/Log/Level.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Fixture.hpp>
#include <Void/Testing/Test.hpp>
#include <cstdlib>
#include <sstream>
#include <string>
namespace
{
// setenv/unsetenv are POSIX-only; _putenv_s is the Windows equivalent.
void set_env(const char* name, const char* value)
{
#ifdef _WIN32
_putenv_s(name, value);
#else
::setenv(name, value, 1);
#endif
}
void unset_env(const char* name)
{
#ifdef _WIN32
_putenv_s(name, "");
#else
::unsetenv(name);
#endif
}
} // namespace
class ConsoleLoggerTests : public Void::Testing::Fixture
{
public:
Void::Testing::Test writes_to_stream()
{
std::ostringstream out;
{
Void::Logging::ConsoleLogger log(out);
log.info("hello world");
}
const auto content = out.str();
Void::Testing::expect(content.find("hello world") !=
std::string::npos);
Void::Testing::expect(content.find("Info") != std::string::npos);
}
Void::Testing::Test colors_by_default_with_no_color_opt_out()
{
unset_env("NO_COLOR");
{
std::ostringstream out;
Void::Logging::ConsoleLogger log{out, Void::Logging::Mode::Immediate};
log.error("colored");
Void::Testing::expect(out.str().find("\x1b[") != std::string::npos);
}
set_env("NO_COLOR", "1");
{
std::ostringstream out;
Void::Logging::ConsoleLogger log{out, Void::Logging::Mode::Immediate};
log.error("plain");
Void::Testing::expect(out.str().find("\x1b[") == std::string::npos);
Void::Testing::expect(out.str().find("plain") != std::string::npos);
}
unset_env("NO_COLOR");
}
Void::Testing::Test level_filtering()
{
std::ostringstream out;
{
Void::Logging::ConsoleLogger log(out);
log.set_level(Void::Logging::Level::Error);
log.info("filtered out");
log.error("kept");
}
const auto content = out.str();
Void::Testing::expect(content.find("filtered out") ==
std::string::npos);
Void::Testing::expect(content.find("kept") != std::string::npos);
}
Void::Testing::Test multiple_levels_emit_in_order()
{
std::ostringstream out;
{
Void::Logging::ConsoleLogger log(out);
log.set_level(Void::Logging::Level::Trace);
log.trace("first");
log.warning("second");
log.fatal("third");
}
const auto content = out.str();
const auto first = content.find("first");
const auto second = content.find("second");
const auto third = content.find("third");
Void::Testing::expect(first != std::string::npos);
Void::Testing::expect(second != std::string::npos);
Void::Testing::expect(third != std::string::npos);
Void::Testing::expect(first < second);
Void::Testing::expect(second < third);
}
};