Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Add another example for generators

Commit
5bf6e473810d0df443024a3cafea5ab301e00e91
Author
Martin Hořeňovský ·
Parents
29b3b7ae6b28

Files · Download .tar.gz

Changes

 docs/list-of-examples.md               |  1 +
 examples/301-Gen-MapTypeConversion.cpp | 54 ++++++++++++++++++++++++++++++++++
 examples/CMakeLists.txt                |  1 +
 3 files changed, 56 insertions(+)

diff --git a/docs/list-of-examples.md b/docs/list-of-examples.md
index c1aa78a1..677dec1e 100644
--- a/docs/list-of-examples.md
+++ b/docs/list-of-examples.md
@@ -15,6 +15,7 @@
 - Listener: [Listeners](../examples/210-Evt-EventListeners.cpp)
 - Configuration: [Provide your own output streams](../examples/231-Cfg-OutputStreams.cpp)
 - Generators: [Create your own generator](../examples/300-Gen-OwnGenerator.cpp)
+- Generators: [Use map to convert types in GENERATE expression](../examples/301-Gen-MapTypeConversion.cpp)
 - Generators: [Use variables in generator expressions](../examples/310-Gen-VariablesInGenerators.cpp)
 
 
diff --git a/examples/301-Gen-MapTypeConversion.cpp b/examples/301-Gen-MapTypeConversion.cpp
new file mode 100644
index 00000000..b6377e99
--- /dev/null
+++ b/examples/301-Gen-MapTypeConversion.cpp
@@ -0,0 +1,54 @@
+// 301-Gen-MapTypeConversion.cpp
+// Shows how to use map to modify generator's return type.
+
+// TODO
+
+#include <catch2/catch.hpp>
+
+#include <string>
+#include <sstream>
+
+// Returns a line from a stream. You could have it e.g. read lines from
+// a file, but to avoid problems with paths in examples, we will use
+// a fixed stringstream.
+class LineGenerator : public Catch::Generators::IGenerator<std::string> {
+    std::string m_line;
+    std::stringstream m_stream;
+public:
+    LineGenerator() {
+        m_stream.str("1\n2\n3\n4\n");
+        if (!next()) {
+            throw Catch::GeneratorException("Couldn't read a single line");
+        }
+    }
+
+    std::string const& get() const override {
+        return m_line;
+    }
+    
+    bool next() override {
+        return !!std::getline(m_stream, m_line);
+    }
+};
+
+// This helper function provides a nicer UX when instantiating the generator
+// Notice that it returns an instance of GeneratorWrapper<std::string>, which
+// is a value-wrapper around std::unique_ptr<IGenerator<std::string>>.
+Catch::Generators::GeneratorWrapper<std::string> lines(std::string /* ignored for example */) {
+    return Catch::Generators::GeneratorWrapper<std::string>(
+        std::unique_ptr<Catch::Generators::IGenerator<std::string>>(
+            new LineGenerator()
+        )
+    );
+}
+
+
+
+TEST_CASE("filter can convert types inside the generator expression", "[example][generator]") {
+    auto num = GENERATE(map<int>([](std::string const& line) { return std::stoi(line); },
+                                 lines("fake-file")));
+                                 
+    REQUIRE(num > 0);
+}
+
+// Compiling and running this file will result in 4 successful assertions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 0eea900d..dff5c742 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -45,6 +45,7 @@ set( SOURCES_IDIOMATIC_TESTS
     120-Bdd-ScenarioGivenWhenThen.cpp
     210-Evt-EventListeners.cpp
     300-Gen-OwnGenerator.cpp
+    301-Gen-MapTypeConversion.cpp
     310-Gen-VariablesInGenerators.cpp
 )