catchorg / catchorg/Catch2

Reporting of conditional sections?

Open
#714 0 comments 1 reaction 0 assignees View on GitHub
Discussion Possible bug
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

After hearing how catch implements its unique flow control (finding sections on the first pass) I got to wondering what the behavior would be if you had a section that was only entered conditionally. In the code snippet below I have three declarations of the same variable run_section_c, and depending on which one you have uncommented you will get different and interesting behavior.

If the first declaration (the global one) is used, and everything runs as it would if the c section was not conditional. If the second or third declarations are used, the 0 section will execute three times, but on the third time none of the inner sections are executed.

I'm not sure if there is any situation where it makes sense to use a conditional section deliberately, and the behavior that it will not execute despite triggering the closing section to run an extra time could lead to the mistaken impression that a CHECK or REQUIRE is passing when it is not actually being executed. Maybe any new sections discovered at the same nesting level after the first iteration through an enclosing section should trigger an error? If there is a legitimate circumstance under which to use this I would be very interested to hear it though.

```
#include

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

bool run_section_c = false;

TEST_CASE("test some stuff") {
std::cout << "start of test case" << std::endl;
// bool run_section_c = false;
SECTION("0") {
std::cout << "0" << std::endl;

// bool run_section_c = false;
SECTION("a") {
std::cout << "a" << std::endl;
}
SECTION("b") {
std::cout << "b" << std::endl;
run_section_c = true;
}
if(run_section_c) {
SECTION("c") {
std::cout << "c" << std::endl;
}
}
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.