Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/StrategyContext.cpp
blob: 638d93221824a0d50bbdb20e5828d9ac879f3590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#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