C++Spec 1.0.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
86 template <Util::is_not_functional T>
87 ExpectationValue<T> expect(T value, std::source_location location = std::source_location::current());
88
98 template <Util::is_functional T>
99 ExpectationFunc<T> expect(T block, std::source_location location = std::source_location::current());
100
110 template <typename T>
111 ExpectationValue<std::initializer_list<T>> expect(std::initializer_list<T> init_list,
112 std::source_location location = std::source_location::current());
113
123 template <typename T>
124 ExpectationValue<T> expect(Let<T>& let, std::source_location location = std::source_location::current());
125
132 ExpectationValue<std::string> expect(const char* string,
133 std::source_location location = std::source_location::current());
134
135 void add_result(const Result& result) { results.push_back(result); }
136 std::list<Result>& get_results() noexcept { return results; }
137 [[nodiscard]] const std::list<Result>& get_results() const noexcept { return results; }
138 void clear_results() noexcept { results.clear(); }
139
140 [[nodiscard]] Result get_result() const override {
141 auto default_result = Result::success(this->get_location());
142 if (results.empty()) {
143 return default_result;
144 }
145
146 return std::accumulate(results.begin(), results.end(), default_result, &Result::reduce);
147 }
148};
149
150} // namespace CppSpec
Definition expectation.hpp:438
Definition expectation.hpp:397
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
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
A container that memoizes the result of a block in `it's.
Definition let.hpp:34
Definition result.hpp:13
Utility functions and classes.