Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
301-Gen-MapTypeConversion.cpp
Go to the documentation of this file.
1// 301-Gen-MapTypeConversion.cpp
2// Shows how to use map to modify generator's return type.
3
4// TODO
5
6#include <catch2/catch.hpp>
7
8#include <string>
9#include <sstream>
10
11// Returns a line from a stream. You could have it e.g. read lines from
12// a file, but to avoid problems with paths in examples, we will use
13// a fixed stringstream.
14class LineGenerator : public Catch::Generators::IGenerator<std::string> {
15 std::string m_line;
16 std::stringstream m_stream;
17public:
19 m_stream.str("1\n2\n3\n4\n");
20 if (!next()) {
21 throw Catch::GeneratorException("Couldn't read a single line");
22 }
23 }
24
25 std::string const& get() const override {
26 return m_line;
27 }
28
29 bool next() override {
30 return !!std::getline(m_stream, m_line);
31 }
32};
33
34// This helper function provides a nicer UX when instantiating the generator
35// Notice that it returns an instance of GeneratorWrapper<std::string>, which
36// is a value-wrapper around std::unique_ptr<IGenerator<std::string>>.
44
45
46
47TEST_CASE("filter can convert types inside the generator expression", "[example][generator]") {
48 auto num = GENERATE(map<int>([](std::string const& line) { return std::stoi(line); },
49 lines("fake-file")));
50
51 REQUIRE(num > 0);
52}
53
54// Compiling and running this file will result in 4 successful assertions
Catch::Generators::GeneratorWrapper< std::string > lines(std::string)
#define GENERATE(...)
std::string const & get() const override
#define TEST_CASE(...)
Definition catch.hpp:222
#define REQUIRE(...)
Definition catch.hpp:185