2023-08-24 16:33:15 +02:00
|
|
|
|
|
|
|
// Copyright Catch2 Authors
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE.txt or copy at
|
|
|
|
// https://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
|
2019-02-26 12:46:10 +01:00
|
|
|
// 301-Gen-MapTypeConversion.cpp
|
|
|
|
// Shows how to use map to modify generator's return type.
|
|
|
|
|
2020-06-23 18:30:48 +02:00
|
|
|
// Specifically we wrap a std::string returning generator with a generator
|
|
|
|
// that converts the strings using stoi, so the returned type is actually
|
|
|
|
// an int.
|
2019-02-26 12:46:10 +01:00
|
|
|
|
2020-01-21 14:46:07 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2020-03-30 10:34:21 +02:00
|
|
|
#include <catch2/generators/catch_generators_adapters.hpp>
|
2019-02-26 12:46:10 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
2020-02-25 12:39:40 +01:00
|
|
|
namespace {
|
|
|
|
|
2019-02-26 12:46:10 +01:00
|
|
|
// 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()) {
|
2020-02-14 10:32:30 +01:00
|
|
|
Catch::Generators::Detail::throw_generator_exception("Couldn't read a single line");
|
2019-02-26 12:46:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 23:38:21 +01:00
|
|
|
std::string const& get() const override;
|
2020-01-21 14:46:07 +01:00
|
|
|
|
2019-02-26 12:46:10 +01:00
|
|
|
bool next() override {
|
|
|
|
return !!std::getline(m_stream, m_line);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-29 23:38:21 +01:00
|
|
|
std::string const& LineGenerator::get() const {
|
|
|
|
return m_line;
|
|
|
|
}
|
|
|
|
|
2019-02-26 12:46:10 +01:00
|
|
|
// 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>(
|
2020-05-25 09:45:24 +02:00
|
|
|
new LineGenerator()
|
2019-02-26 12:46:10 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-25 12:39:40 +01:00
|
|
|
} // end anonymous namespace
|
2019-02-26 12:46:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
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")));
|
2020-01-21 14:46:07 +01:00
|
|
|
|
2019-02-26 12:46:10 +01:00
|
|
|
REQUIRE(num > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compiling and running this file will result in 4 successful assertions
|