Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

develroot / src / Progmasoft / StrategyContext.cpp

3.2 KiB · Raw · Log

// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <Progmasoft/Catch3/Strategy/Context.hpp>
#include <string>
#include <utility>

namespace Progmasoft::Catch3
{

    namespace
    {

        thread_local TrialRecord *currentRecord = nullptr;
        thread_local const std::string *defaultPropertyName = nullptr;

    } // namespace

    namespace Detail
    {

        TrialRecord *
        CurrentTrialRecord() noexcept
        {
            return currentRecord;
        }

        TrialRecordScope::TrialRecordScope(TrialRecord &record) noexcept
            : previous_(currentRecord)
        {
            currentRecord = &record;
        }

        TrialRecordScope::~TrialRecordScope()
        {
            currentRecord = previous_;
        }

        void
        RecordPropertyFailure(std::string_view expression,
                              std::string_view file,
                              std::size_t line)
        {
            if (currentRecord == nullptr)
            {
                return;
            }
            // Only the first failure of a trial counts: it is the one the
            // body stopped at.
            if (!currentRecord->Failure.has_value())
            {
                currentRecord->Failure = "CATCH3_PROPERTY_ASSERT( "
                                         + std::string(expression) + " ) at "
                                         + std::string(file) + ":"
                                         + std::to_string(line);
            }
        }

        void
        RecordPropertyFailureMessage(std::string message)
        {
            if (currentRecord != nullptr && !currentRecord->Failure.has_value())
            {
                currentRecord->Failure = std::move(message);
            }
        }

        std::string
        DefaultPropertyName()
        {
            return defaultPropertyName != nullptr ? *defaultPropertyName
                                                  : std::string();
        }

        PropertyNameScope::PropertyNameScope(std::string name)
            : name_(std::move(name))
            , previous_(defaultPropertyName)
        {
            defaultPropertyName = &name_;
        }

        PropertyNameScope::~PropertyNameScope()
        {
            defaultPropertyName = previous_;
        }

    } // namespace Detail

    void
    Note(std::string text)
    {
        if (currentRecord != nullptr)
        {
            currentRecord->Notes.push_back(std::move(text));
        }
    }

    void
    Classify(bool condition, std::string label)
    {
        if (currentRecord != nullptr && condition)
        {
            currentRecord->Labels.push_back(std::move(label));
        }
    }

    void
    Cover(double requiredPercentage, bool condition, std::string label)
    {
        if (currentRecord != nullptr)
        {
            currentRecord->Covers.push_back(
                { std::move(label), requiredPercentage, condition });
        }
    }

    void
    Target(double score, std::string label)
    {
        if (currentRecord != nullptr)
        {
            currentRecord->Targets.push_back({ std::move(label), score });
        }
    }

} // namespace Progmasoft::Catch3