/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "fixture.h"
#include "errortypes.h"
#include "helpers.h"
#include "library.h"
#include "options.h"
#include "redirect.h"
#include "timer.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include "xml.h"
/**
* TestRegistry
**/
namespace {
struct CompareFixtures {
bool operator()(const TestInstance* lhs, const TestInstance* rhs) const {
return lhs->classname < rhs->classname;
}
};
}
using TestSet = std::set;
namespace {
class TestRegistry {
TestSet _tests;
public:
static TestRegistry &theInstance() {
static TestRegistry testreg;
return testreg;
}
void addTest(TestInstance *t) {
_tests.insert(t);
}
const TestSet &tests() const {
return _tests;
}
};
}
TestInstance::TestInstance(const char * _name)
: classname(_name)
{
TestRegistry::theInstance().addTest(this);
}
/**
* TestFixture
**/
std::ostringstream TestFixture::errmsg;
unsigned int TestFixture::countTests;
std::size_t TestFixture::fails_counter = 0;
std::size_t TestFixture::todos_counter = 0;
std::size_t TestFixture::succeeded_todos_counter = 0;
TestFixture::TestFixture(const char * const _name)
: classname(_name)
{}
TestFixture::~TestFixture() = default;
bool TestFixture::prepareTest(const char testname[])
{
mTemplateFormat.clear();
mTemplateLocation.clear();
prepareTestInternal();
// Check if tests should be executed
if (!testsToRun.empty()) {
const bool match = testsToRun.count(testname);
if ((match && exclude_tests) || (!match && !exclude_tests))
return false;
}
// Tests will be executed - prepare them
mTestname = testname;
++countTests;
std::string fullTestName = classname + "::" + mTestname;
if (quiet_tests) {
std::putchar('.'); // Use putchar to write through redirection of std::cout/cerr
std::fflush(stdout);
} else {
std::cout