Specs
Beautiful C++ Test Framework
Loading...
Searching...
No Matches
SpecGroup.h
Go to the documentation of this file.
1#pragma once
2
3#include <Specs/API.h>
4#include <string_format.h>
5
6#include <string>
7#include <string_view>
8
9#include "SpecComponent.h"
10#include "SpecDocumented.h"
12
13namespace SpecsCpp {
14
15 class SpecGroup : public ISpecGroup, public SpecComponent, public SpecDocumented {
16 std::string _fullDescription;
17
18 std::vector<ISpecGroup*> _childGroups;
19 std::vector<ISpec*> _specs;
20 std::vector<ISpecSetup*> _setups;
21 std::vector<ISpecTeardown*> _teardowns;
22 std::vector<ISpecSetup*> _oneTimeSetups;
23 std::vector<ISpecTeardown*> _oneTimeTeardowns;
24
25 std::unique_ptr<SpecVariableCollection> _variables;
26
27 void merge_child_groups(ISpecGroup* other) { add_group(other); }
28 void merge_tests(ISpec* other) { add_test(other); }
29 void merge_setups(ISpecSetup* other) { add_setup(other); }
30 void merge_teardowns(ISpecTeardown* other) { add_teardown(other); }
31 void merge_one_time_setups(ISpecSetup* other) { add_one_time_setup(other); }
32 void merge_one_time_teardowns(ISpecTeardown* other) { add_one_time_teardown(other); }
33
34 FunctionPointer<void(ISpecGroup*)> _merge_child_groups_fn =
35 function_pointer(this, &SpecGroup::merge_child_groups);
36 FunctionPointer<void(ISpec*)> _merge_tests_fn =
37 function_pointer(this, &SpecGroup::merge_tests);
38 FunctionPointer<void(ISpecSetup*)> _merge_setups_fn =
39 function_pointer(this, &SpecGroup::merge_setups);
40 FunctionPointer<void(ISpecTeardown*)> _merge_teardowns_fn =
41 function_pointer(this, &SpecGroup::merge_teardowns);
42 FunctionPointer<void(ISpecSetup*)> _merge_one_time_setups_fn =
43 function_pointer(this, &SpecGroup::merge_one_time_setups);
44 FunctionPointer<void(ISpecTeardown*)> _merge_one_time_teardowns_fn =
45 function_pointer(this, &SpecGroup::merge_one_time_teardowns);
46
47 public:
48 SpecGroup(ISpecGroup* parent = nullptr, std::string_view description = "")
51 _variables(std::make_unique<SpecVariableCollection>(parent)) {
52 if (parent == nullptr || strlen(parent->full_description()) == 0)
53 _fullDescription = description;
54 else
55 _fullDescription =
56 string_format("{} > {}", parent->full_description(), description);
57 }
58
59 SpecGroup(std::string_view description) : SpecGroup(nullptr, description) {}
60
61 void foreach_group(ForEachGroupFn* callback) const override {
62 for (auto* group : _childGroups) callback->invoke(group);
63 }
64
65 void foreach_test(ForEachSpecFn* callback) const override {
66 for (auto* spec : _specs) callback->invoke(spec);
67 }
68
69 void foreach_setup(ForEachSetupFn* callback) const override {
70 for (auto* setup : _setups) callback->invoke(setup);
71 }
72
73 void foreach_teardown(ForEachTeardownFn* callback) const override {
74 for (auto* teardown : _teardowns) callback->invoke(teardown);
75 }
76
77 void foreach_one_time_setup(ForEachSetupFn* callback) const override {
78 for (auto* setup : _oneTimeSetups) callback->invoke(setup);
79 }
80
81 void foreach_one_time_teardown(ForEachTeardownFn* callback) const override {
82 for (auto* teardown : _oneTimeTeardowns) callback->invoke(teardown);
83 }
84
85 void add_group(ISpecGroup* group) override { _childGroups.push_back(group); }
86 void add_test(ISpec* spec) override { _specs.push_back(spec); }
87 void add_setup(ISpecSetup* setup) override { _setups.push_back(setup); }
88 void add_teardown(ISpecTeardown* teardown) override { _teardowns.push_back(teardown); }
89 void add_one_time_setup(ISpecSetup* setup) override { _oneTimeSetups.push_back(setup); }
90 void add_one_time_teardown(ISpecTeardown* teardown) override {
91 _oneTimeTeardowns.push_back(teardown);
92 }
93
94 const char* full_description() const override { return _fullDescription.c_str(); }
95
96 void merge(ISpecGroup* other) override {
97 other->foreach_group(&_merge_child_groups_fn);
98 other->foreach_test(&_merge_tests_fn);
99 other->foreach_setup(&_merge_setups_fn);
100 other->foreach_teardown(&_merge_teardowns_fn);
101 other->foreach_one_time_setup(&_merge_one_time_setups_fn);
102 other->foreach_one_time_teardown(&_merge_one_time_teardowns_fn);
103 }
104
105 ISpecVariableCollection* variables() const override { return _variables.get(); }
106 };
107}
ISpecGroup * group() const override
const char * description() const override
void add_teardown(ISpecTeardown *teardown) override
Definition SpecGroup.h:88
void merge(ISpecGroup *other) override
Definition SpecGroup.h:96
void foreach_test(ForEachSpecFn *callback) const override
Definition SpecGroup.h:65
void foreach_group(ForEachGroupFn *callback) const override
Definition SpecGroup.h:61
void add_group(ISpecGroup *group) override
Definition SpecGroup.h:85
SpecGroup(ISpecGroup *parent=nullptr, std::string_view description="")
Definition SpecGroup.h:48
ISpecVariableCollection * variables() const override
Definition SpecGroup.h:105
void foreach_one_time_teardown(ForEachTeardownFn *callback) const override
Definition SpecGroup.h:81
const char * full_description() const override
Definition SpecGroup.h:94
void add_setup(ISpecSetup *setup) override
Definition SpecGroup.h:87
void foreach_teardown(ForEachTeardownFn *callback) const override
Definition SpecGroup.h:73
void add_test(ISpec *spec) override
Definition SpecGroup.h:86
void add_one_time_teardown(ISpecTeardown *teardown) override
Definition SpecGroup.h:90
void foreach_setup(ForEachSetupFn *callback) const override
Definition SpecGroup.h:69
void add_one_time_setup(ISpecSetup *setup) override
Definition SpecGroup.h:89
SpecGroup(std::string_view description)
Definition SpecGroup.h:59
void foreach_one_time_setup(ForEachSetupFn *callback) const override
Definition SpecGroup.h:77
Definition API.h:3
SpecComponentType
Definition API.h:128
IFunctionPointer< void(ISpecSetup *)> ForEachSetupFn
Definition API.h:318
virtual void foreach_one_time_teardown(ForEachTeardownFn *) const =0
IFunctionPointer< void(ISpecGroup *)> ForEachGroupFn
Definition API.h:316
virtual void foreach_group(ForEachGroupFn *) const =0
virtual void foreach_one_time_setup(ForEachSetupFn *) const =0
virtual void foreach_teardown(ForEachTeardownFn *) const =0
IFunctionPointer< void(ISpecTeardown *)> ForEachTeardownFn
Definition API.h:319
virtual void foreach_setup(ForEachSetupFn *) const =0
IFunctionPointer< void(ISpec *)> ForEachSpecFn
Definition API.h:317
virtual void foreach_test(ForEachSpecFn *) const =0
virtual IVoidPointer * get(const char *name) const =0