catchorg / Catch2

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
https://discord.gg/4CWS9zD
Boost Software License 1.0
18.65k stars 3.05k forks source link

How GENERATE internally works #2896

Closed getsoubl closed 1 month ago

getsoubl commented 2 months ago

I am reading about catch2 framework. I am trying to understand how internally the GENERATE macro works For example having the following section TEST_CASE("Foo") { auto i = GENERATE(1,2,3); std::cout << i << std::endl; } How is it called 3 times? Checking the internal implementation of makeGenerators is not clear to me . My understanding that this GENERATE returns a IteratorGenerator . But how the next is called ?

horenmar commented 2 months ago

It is called 3 times, because it has 3 elements. Why do you need to the details?

getsoubl commented 2 months ago

Hello , Just for educational reasons, I am wondering how does it work

horenmar commented 1 month ago

Right so the basic answer is that first time the GENERATE macro is executed, it creates an object that lives outside of the TEST_CASE, using the arguments provided by the user to the GENERATE macro. Every subsequent case, it uses some internal metadata generated by the macro to find the previously created object.

After the test case is finished executing*, the generator object is queried on whether it has more elements, if yes, the generator is moved forward and test case is executed again. If not, then it is done.