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

summaryrefslogtreecommitdiffhomepage
path: root/tests/Progmasoft/RunnerFixture.cpp
blob: 3a61d05c44ba3ef10b4afe3acabe790e8ad1c11a (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

// A test binary whose tests misbehave on purpose. The runner's integration
// tests start it and check what the runner made of each misbehaviour.

#include <Progmasoft/Catch3/Mock.hpp>
#include <Progmasoft/Catch3/Runner.hpp>
#include <Progmasoft/Catch3/TestMacros.hpp>
#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <optional>
#include <string>
#include <thread>

namespace
{
    /// The value of the environment variable `name`, if it is set.
    std::optional<std::string>
    Environment(const char *name)
    {
#if defined(_WIN32)
        char *value = nullptr;
        std::size_t length = 0;
        if (_dupenv_s(&value, &length, name) != 0 || value == nullptr)
        {
            return std::nullopt;
        }
        std::string result{ value };
        std::free(value);
        return result;
#else
        const char *value = std::getenv(name);
        return value == nullptr ? std::nullopt
                                : std::optional<std::string>{ value };
#endif
    }

    /// A directory the integration test gives every run of the fixture, to
    /// count attempts across worker processes.
    std::filesystem::path
    StateDirectory()
    {
        const auto directory = Environment("CATCH3_FIXTURE_STATE");
        return directory ? std::filesystem::path{ *directory }
                         : std::filesystem::path{};
    }

    /// How often `name` ran before, counted in a file; increments it.
    int
    CountRun(const std::string &name)
    {
        const auto file = StateDirectory() / (name + ".count");
        int count = 0;
        {
            std::ifstream input(file);
            input >> count;
        }
        std::ofstream output(file, std::ios::trunc);
        output << count + 1;
        return count;
    }

    bool polluted = false;
} // namespace

TEST_CASE("fixture passes", "[fixture][pass]")
{
    REQUIRE(1 + 1 == 2);
}

TEST_CASE("fixture prints", "[fixture][pass]")
{
    std::cout << "said through std::cout\n";
    std::cerr << "said through std::cerr\n";
    CHECK(true);
}

TEST_CASE("fixture fails", "[fixture][fail]")
{
    const int expected = 3;
    SECTION("in a section")
    {
        CHECK(1 + 1 == expected);
    }
}

TEST_CASE("fixture throws", "[fixture][fail]")
{
    throw std::runtime_error("broken on purpose");
}

TEST_CASE("fixture crashes", "[fixture][crash]")
{
    SECTION("deep down")
    {
        CHECK(true);
        std::abort();
    }
}

TEST_CASE("fixture hangs", "[fixture][hang][!timeout:1]")
{
    CHECK(true);
    std::this_thread::sleep_for(std::chrono::seconds(60));
}

TEST_CASE("fixture is flaky", "[fixture][flaky]")
{
    // Fails the first time it runs, passes afterwards.
    REQUIRE(CountRun("flaky") > 0);
}

TEST_CASE("fixture alternates", "[fixture][alternate]")
{
    // Fails on every even run: flaky on every run of the runner.
    REQUIRE(CountRun("alternates") % 2 == 1);
}

TEST_CASE("fixture innocent one", "[fixture][order]")
{
    CHECK(true);
}

TEST_CASE("fixture polluter", "[fixture][order]")
{
    polluted = true;
    CHECK(true);
}

TEST_CASE("fixture innocent two", "[fixture][order]")
{
    CHECK(true);
}

TEST_CASE("fixture victim", "[fixture][order]")
{
    REQUIRE_FALSE(polluted);
}

TEST_CASE("fixture base", "[fixture][deps]")
{
    FAIL("the base is broken");
}

TEST_CASE("fixture dependent", "[fixture][deps][!depends:fixture base]")
{
    CHECK(true);
}

TEST_CASE("fixture temporary directory", "[fixture][temp]")
{
    const auto directory = Progmasoft::Catch3::TestTemporaryDirectory();
    REQUIRE(std::filesystem::exists(directory));
    REQUIRE(std::filesystem::equivalent(std::filesystem::temp_directory_path(),
                                        directory));
    std::ofstream(directory / "scratch.txt") << "kept only on failure";
    CHECK_FALSE(Environment("CATCH3_FIXTURE_FAIL_TEMP").has_value());
}

TEST_CASE("fixture slow one", "[fixture][slow]")
{
    std::this_thread::sleep_for(std::chrono::milliseconds(300));
    CHECK(true);
}

TEST_CASE("fixture slow two", "[fixture][slow]")
{
    std::this_thread::sleep_for(std::chrono::milliseconds(300));
    CHECK(true);
}

TEST_CASE("fixture slow three", "[fixture][slow]")
{
    std::this_thread::sleep_for(std::chrono::milliseconds(300));
    CHECK(true);
}

TEST_CASE("fixture slow four", "[fixture][slow]")
{
    std::this_thread::sleep_for(std::chrono::milliseconds(300));
    CHECK(true);
}

TEST_CASE("fixture mock unexpected call", "[fixture][mock]")
{
    Progmasoft::Catch3::MockFunction<int(std::string)> get{ "Get" };
    CATCH3_EXPECT_CALL(get)("a").Returns(1);
    CHECK(get("a") == 1);
    CHECK(get("b") == 0);
}

TEST_CASE("fixture mock with no expectations", "[fixture][mock]")
{
    CATCH3_MOCK_FUNCTION(tick, void());
    tick();
}

TEST_CASE("fixture mock unmet expectation", "[fixture][mock]")
{
    CATCH3_MOCK_FUNCTION(tick, void());
    CATCH3_EXPECT_CALL(tick)().Times(2);
    tick();
}

TEST_CASE("fixture mock too many calls", "[fixture][mock]")
{
    CATCH3_MOCK_FUNCTION(tick, void());
    CATCH3_EXPECT_CALL(tick)();
    tick();
    tick();
}

TEST_CASE("fixture mock out of order", "[fixture][mock]")
{
    Progmasoft::Catch3::Sequence order;
    CATCH3_MOCK_FUNCTION(first, void());
    CATCH3_MOCK_FUNCTION(second, void());
    CATCH3_EXPECT_CALL(first)().InSequence(order);
    CATCH3_EXPECT_CALL(second)().InSequence(order);
    second();
}

TEST_CASE("fixture mock past its step", "[fixture][mock]")
{
    Progmasoft::Catch3::Sequence order;
    CATCH3_MOCK_FUNCTION(first, void());
    CATCH3_MOCK_FUNCTION(second, void());
    CATCH3_EXPECT_CALL(first)().AtLeast(1).InSequence(order);
    CATCH3_EXPECT_CALL(second)().InSequence(order);
    first();
    second();
    first();
}

TEST_CASE("fixture mock with nothing to return", "[fixture][mock]")
{
    CATCH3_MOCK_FUNCTION(name, std::string & ());
    CATCH3_EXPECT_CALL(name)();
    name();
    FAIL("the call above leaves the test case");
}

TEST_CASE("fixture mock left by a failure", "[fixture][mock]")
{
    CATCH3_MOCK_FUNCTION(tick, void());
    CATCH3_EXPECT_CALL(tick)();
    FAIL("the test's own failure");
}

TEST_CASE("fixture mock outlives the run", "[fixture][mock-exit]")
{
    static auto *mock = new Progmasoft::Catch3::MockFunction<void()>{ "late" };
    CATCH3_EXPECT_CALL (*mock)();
    std::atexit([] {
        delete mock;
    });
}

TEST_CASE("fixture skips", "[fixture][skip]")
{
    SKIP("not on this machine");
}