Clarify documentation about nested generators

Closes #1947
This commit is contained in:
Martin Hořeňovský 2020-06-13 11:11:19 +02:00
parent 0614a4acb3
commit fb08596b1b
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -12,17 +12,36 @@ are run once per each value in a generator.
This is best explained with an example: This is best explained with an example:
```cpp ```cpp
TEST_CASE("Generators") { TEST_CASE("Generators") {
auto i = GENERATE(1, 2, 3); auto i = GENERATE(1, 2);
SECTION("one") { SECTION("one") {
auto j = GENERATE( -3, -2, -1 ); auto j = GENERATE(-3, -2);
REQUIRE(j < i); REQUIRE(j < i);
} }
SECTION("two") {
auto k = GENERATE(4, 5, 6);
REQUIRE(j != k);
}
} }
``` ```
The assertion in this test case will be run 9 times, because there The `SECTION` "one" will be run 4 (2*2) times, because the outer
are 3 possible values for `i` (1, 2, and 3) and there are 3 possible generator has 2 elements in it, and the inner generator also has 2
values for `j` (-3, -2, and -1). elements in it. The `SECTION` "two" will be run 6 (2*3) times. The
sections will be run in order "one", "one", "two", "two", "two", "one",
...
It is also possible to have multiple generators at the same level of
nesting. The result is the same as when generators are inside nested
sections, that is, the result will be a cartesian product of all
elements. This means that in the snippet below, the test case will be
run 6 (2*3) times.
```cpp
TEST_CASE("Generators") {
auto i = GENERATE(1, 2);
auto j = GENERATE(3, 4, 5);
}
```
There are 2 parts to generators in Catch2, the `GENERATE` macro together There are 2 parts to generators in Catch2, the `GENERATE` macro together