mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-20 12:26:10 +01:00
e1e6872c4c
This is both a really big and a really small commit. It is small in that it only contains renaming, moving and modification of include directives caused by this. It is really big in the obvious way of touching something like 200 files. The new rules for naming files is simple: headers use the `.hpp` extension. The rules for physical file layout is still kinda in progress, but the basics are also simple: * Significant parts of functionality get their own subfolder * Benchmarking is in `catch2/benchmark` * Matchers are in `catch2/matchers` * Generators are in `catch2/generators` * Reporters are in `catch2/reporters` * Baseline testing facilities are in `catch2/` * Various top level folders also contain `internal` subfolder, with files that users probably do not want to include directly, at least not until they have to write something like their own reporter. * The exact files in these subfolders is likely to change later on Note that while some includes were cleaned up in this commit, it is only the low hanging fruit and further cleanup using automatic tooling will happen later. Also note that various include guards, copyright notices and file headers will also be standardized later, rather than in this commit.
58 lines
1.7 KiB
C++
58 lines
1.7 KiB
C++
// 301-Gen-MapTypeConversion.cpp
|
|
// Shows how to use map to modify generator's return type.
|
|
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/generators/catch_generators_adapters.hpp>
|
|
|
|
#include <string>
|
|
#include <sstream>
|
|
|
|
namespace {
|
|
|
|
// 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()) {
|
|
Catch::Generators::Detail::throw_generator_exception("Couldn't read a single line");
|
|
}
|
|
}
|
|
|
|
std::string const& get() const override;
|
|
|
|
bool next() override {
|
|
return !!std::getline(m_stream, m_line);
|
|
}
|
|
};
|
|
|
|
std::string const& LineGenerator::get() const {
|
|
return 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::make_unique<LineGenerator>()
|
|
);
|
|
}
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
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
|