C++Spec 1.0.0
BDD testing for C++
Loading...
Searching...
No Matches
runner.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <list>
5#include <utility>
6
7#include "description.hpp"
9#include "result.hpp"
10
11namespace CppSpec {
12
16class Runner {
17 std::list<Description*> specs;
18 std::list<std::shared_ptr<Formatters::BaseFormatter>> formatters;
19
20 public:
21 template <typename... Formatters>
22 explicit Runner(Formatters&&... formatters) : formatters{std::forward<Formatters>(formatters)...} {}
23
24 explicit Runner(std::list<std::shared_ptr<Formatters::BaseFormatter>>&& formatters)
25 : formatters{std::move(formatters)} {}
26
33 Runner& add_spec(Description& spec) {
34 specs.push_back(&spec);
35 return *this;
36 }
37
38 template <typename... Specs>
39 Runner& add_specs(Specs&... specs) {
40 (add_spec(specs), ...); // Fold expression to add all specs
41 return *this;
42 }
43
44 Result run(std::source_location location = std::source_location::current()) {
45 bool success = true;
46 for (Description* spec : specs) {
47 spec->timed_run();
48 success &= !spec->get_result().is_failure();
49 }
50 for (auto& formatter : formatters) {
51 for (Description* spec : specs) {
52 formatter->format(static_cast<Runnable&>(*spec));
53 }
54 }
55 return success ? Result::success(location) : Result::failure(location);
56 }
57
58 Result exec() { return run(); }
59};
60
61} // namespace CppSpec
Definition description.hpp:20
A collection of Descriptions that are run in sequence.
Definition runner.hpp:16
Runner & add_spec(Description &spec)
Add a Description object.
Definition runner.hpp:33
Defines the Description class and associated functions.