C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
it_base.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <algorithm>
5#include <functional>
6#include <numeric>
7#include <source_location>
8#include <string>
9#include <utility>
10
11#include "let.hpp"
12#include "runnable.hpp"
13#include "util.hpp"
14
15namespace CppSpec {
16
17template <typename T>
19template <Util::is_functional T>
20class ExpectationFunc;
21
32class ItBase : public Runnable {
34 std::string description;
35 std::list<Result> results; // The results of the `it` statement
36
37 public:
38 ItBase() = delete; // Don't allow a default constructor
39
44 explicit ItBase(std::source_location location) noexcept : Runnable(location) {}
45
51 explicit ItBase(std::source_location location, const char* description) noexcept
52 : Runnable(location), description(description) {}
53
58 bool needs_description() noexcept { return description.empty(); }
59
64 [[nodiscard]] std::string get_description() const noexcept { return description; }
65
70 ItBase& set_description(std::string_view description) noexcept {
71 this->description = description;
72 return *this;
73 }
74
91 template <Util::is_not_functional T>
92 ExpectationValue<T> expect(T value, std::source_location location = std::source_location::current());
93
103 template <Util::is_functional T>
104 ExpectationFunc<T> expect(T block, std::source_location location = std::source_location::current());
105
115 template <typename T>
116 ExpectationValue<std::initializer_list<T>> expect(std::initializer_list<T> init_list,
117 std::source_location location = std::source_location::current());
118
128 template <typename T>
129 ExpectationValue<T> expect(Let<T>& let, std::source_location location = std::source_location::current());
130
131 void add_result(const Result& result) { results.push_back(result); }
132 std::list<Result>& get_results() noexcept { return results; }
133 [[nodiscard]] const std::list<Result>& get_results() const noexcept { return results; }
134 void clear_results() noexcept { results.clear(); }
135
136 [[nodiscard]] Result get_result() const override {
137 auto default_result = Result::success(this->get_location());
138 if (results.empty()) {
139 return default_result;
140 }
141
142 return std::accumulate(results.begin(), results.end(), default_result, &Result::reduce);
143 }
144};
145
146} // namespace CppSpec
Definition expectation.hpp:479
Definition expectation.hpp:438
bool needs_description() noexcept
Get whether the object needs a description string.
Definition it_base.hpp:58
ItBase & set_description(std::string_view description) noexcept
Set the description string.
Definition it_base.hpp:70
std::string get_description() const noexcept
Get the description string for the it statement.
Definition it_base.hpp:64
ExpectationValue< T > expect(T value, std::source_location location=std::source_location::current())
The expect object generator for objects and LiteralTypes.
Definition it.hpp:129
ItBase(std::source_location location, const char *description) noexcept
Create an BaseIt with an explicit description.
Definition it_base.hpp:51
ItBase(std::source_location location) noexcept
Create an BaseIt without an explicit description.
Definition it_base.hpp:44
Definition result.hpp:13
Utility functions and classes.