forked from catchorg/Catch2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example of using GENERATE(table())
There are some examples on issue catchorg#850 of using this feature, but they are not easily found from the documentation. Adding them here as an example makes them more findable and ensures they keep working if the API changes.
- Loading branch information
Richard Ash
committed
Jun 19, 2020
1 parent
e815acd
commit 309ffea
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 302-Gen-Table.cpp | ||
// Shows how to use table to run a test many times with different inputs. Lifted from examples on | ||
// issue #850. | ||
|
||
// TODO | ||
|
||
#include <catch2/catch.hpp> | ||
|
||
#include <string> | ||
|
||
class TestSubject | ||
{ | ||
public: | ||
// this is the method we are going to test. It returns the length of the input string. | ||
size_t GetLength(const std::string & input) | ||
{ | ||
return input.size(); | ||
} | ||
}; | ||
|
||
|
||
TEST_CASE("Table allows pre-computed test inputs and outputs", "[example][generator]") { | ||
// do setup here as normal | ||
TestSubject subj; | ||
|
||
SECTION("This section is run for each row in the table") { | ||
// C++17 alternative for the first three lines of code below using structured bindings: | ||
// auto [test_input, expected] = GENERATE( table<std::string, size_t>({ | ||
std::string test_input; | ||
size_t expected_output; | ||
std::tie(test_input, expected_output) = GENERATE( table<std::string, size_t>({ | ||
/* In this case one of the parameters to our test case is the expected output, but this is | ||
* not required. There could be multiple expected values in the table, which can have any | ||
* (fixed) number of columns. | ||
*/ | ||
{"one", 3}, | ||
{"two", 3}, | ||
{"three", 5}, | ||
{"four", 4} | ||
})); | ||
|
||
// run the test | ||
auto result = subj.GetLength(test_input); | ||
// capture the input data to go with the outputs. | ||
CAPTURE(test_input); | ||
// check it matches the pre-calculated data | ||
REQUIRE(result == expected_output); | ||
} // end section | ||
} | ||
|
||
// Compiling and running this file will result in 4 successful assertions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters