Sequoia
Loading...
Searching...
No Matches
TestCreator.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2021. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
15
19
20#include <array>
21#include <vector>
22
23namespace sequoia::testing
24{
25 [[nodiscard]]
26 bool handle_as_ref(std::string_view type);
27
29 {
30 std::string species, symbol;
31
32 [[nodiscard]]
33 friend bool operator==(const template_spec&, const template_spec&) noexcept = default;
34 };
35
36 using template_data = std::vector<template_spec>;
37
38 [[nodiscard]]
39 std::string to_string(const template_data& data);
40
41 [[nodiscard]]
42 template_data generate_template_data(std::string_view str);
43
44 [[nodiscard]]
45 template_spec generate_template_spec(std::string_view str);
46
47 void cmake_nascent_tests(const project_paths& projPaths, std::ostream& stream);
48
49 enum class nascent_test_flavour { standard, framework_diagnostics };
50
52 {
53 public:
54 enum class gen_source_option {no, yes};
55
56 nascent_test_base(project_paths paths, std::string copyright, indentation codeIndent, std::ostream& stream)
57 : m_Paths{std::move(paths)}
58 , m_Copyright{std::move(copyright)}
59 , m_CodeIndent{codeIndent}
60 , m_Stream{&stream}
61 , m_ProjectNamespace{back(m_Paths.source().project()).string()}
62 {}
63
64 [[nodiscard]]
65 nascent_test_flavour flavour() const noexcept { return m_Flavour; }
66
67 void flavour(nascent_test_flavour f) { m_Flavour = f; }
68
69 [[nodiscard]]
70 const std::string& suite() const noexcept { return m_Suite; }
71
72 void suite(std::string name) { m_Suite = std::move(name); }
73
74 [[nodiscard]]
75 const std::filesystem::path& header() const noexcept { return m_Header; }
76
77 void header(std::filesystem::path h) { m_Header = std::move(h); }
78
79 [[nodiscard]]
80 const std::string& test_type() const noexcept { return m_TestType; }
81
82 void test_type(std::string type) { m_TestType = std::move(type); }
83
84 [[nodiscard]]
85 const std::string& forename() const noexcept { return m_Forename; }
86
87 void forename(std::string name) { m_Forename = std::move(name); }
88
89 [[nodiscard]]
90 const std::string& surname() const noexcept { return m_Surname; }
91
92 void surname(std::string name) { m_Surname = std::move(name); }
93
94 void generate_source_files(gen_source_option opt)
95 {
96 m_SourceOption = opt;
97 }
98
99 [[nodiscard]]
100 const std::filesystem::path& host_dir() const noexcept { return m_HostDir; }
101
102 [[nodiscard]]
103 const std::filesystem::path& header_path() const noexcept { return m_HeaderPath; }
104
105 [[nodiscard]]
106 friend bool operator==(const nascent_test_base&, const nascent_test_base&) noexcept = default;
107
108 [[nodiscard]]
109 static std::vector<std::string> framework_diagnostics_stubs();
110 protected:
111 nascent_test_base(const nascent_test_base&) = default;
112 nascent_test_base(nascent_test_base&&) noexcept = default;
113 nascent_test_base& operator=(const nascent_test_base&) = default;
114 nascent_test_base& operator=(nascent_test_base&&) noexcept = default;
115
116 ~nascent_test_base() = default;
117
118 [[nodiscard]]
119 const project_paths& paths() const noexcept
120 {
121 return m_Paths;
122 }
123
124 [[nodiscard]]
125 std::filesystem::path build_source_path(const std::filesystem::path& filename) const;
126
127 template<invocable_r<std::filesystem::path, std::filesystem::path> WhenAbsent,std::invocable<std::string&> FileTransformer>
128 void finalize(WhenAbsent fn,
129 const std::vector<std::string>& stubs,
130 const std::vector<std::string>& constructors,
131 std::string_view nameStub,
132 FileTransformer transformer);
133
134 [[nodiscard]]
135 const std::string& camel_name() const noexcept { return m_CamelName; }
136
137 void camel_name(std::string name);
138
139 void set_cpp(const std::filesystem::path& headerPath, std::string_view nameSpace);
140
141 [[nodiscard]]
142 const indentation& code_indent() const noexcept { return m_CodeIndent; }
143
144 [[nodiscard]]
145 const std::string& copyright() const noexcept { return m_Copyright; }
146
147 [[nodiscard]]
148 const std::string& project_namespace() const noexcept { return m_ProjectNamespace; }
149
150 [[nodiscard]]
151 std::ostream& stream() noexcept { return *m_Stream; }
152
153 void finalize_suite(std::string_view fallbackIngredient);
154
155 void make_common_replacements(std::string& text) const;
156 private:
157 constexpr static std::array<std::string_view, 3> st_HeaderExtensions{".hpp", ".h", ".hxx"};
158
159 project_paths m_Paths;
160 std::string m_Copyright{};
161 indentation m_CodeIndent{" "};
162 std::ostream* m_Stream;
163
164 nascent_test_flavour m_Flavour{nascent_test_flavour::standard};
165 std::string
166 m_Suite{},
167 m_TestType{},
168 m_Forename{},
169 m_Surname{},
170 m_CamelName{},
171 m_ProjectNamespace{};
172 std::filesystem::path m_Header{}, m_HostDir{}, m_HeaderPath{};
173 gen_source_option m_SourceOption{};
174
175 void on_source_path_error() const;
176
177 void finalize_header(const std::filesystem::path& sourcePath);
178
179 template<std::invocable<std::string&> FileTransformer>
180 [[nodiscard]]
181 std::string create_file(std::string_view inputNameStub, std::string_view nameEnding, FileTransformer transformer) const;
182
183 };
184
186 {
187 public:
188 using nascent_test_base::nascent_test_base;
189
190 void qualified_name(std::string name) { m_QualifiedName = std::move(name); }
191
192 void add_equivalent_type(std::string name) { m_EquivalentTypes.emplace_back(std::move(name)); }
193
194 void source_dir(std::filesystem::path dir) { m_SourceDir = std::move(dir); }
195
196 void finalize();
197
198 [[nodiscard]]
199 std::vector<std::string> constructors() const;
200
201 [[nodiscard]]
202 friend bool operator==(const nascent_semantics_test&, const nascent_semantics_test&) noexcept = default;
203
204 [[nodiscard]]
205 static std::vector<std::string> stubs();
206 private:
207 std::string m_QualifiedName{};
208
209 template_data m_TemplateData{};
210
211 std::vector<std::string> m_EquivalentTypes{};
212
213 std::filesystem::path m_SourceDir{};
214
215 void transform_file(std::string& text) const;
216
217 void set_header_text(std::string& text, std::string_view copyright, std::string_view nameSpace) const;
218
219 [[nodiscard]]
220 std::filesystem::path when_header_absent(const std::filesystem::path& filename, const std::string& nameSpace);
221 };
222
224 {
225 public:
226 using nascent_test_base::nascent_test_base;
227
228 [[nodiscard]]
229 static std::vector<std::string> stubs();
230
231 void finalize();
232
233 [[nodiscard]]
234 std::vector<std::string> constructors() const;
235 private:
236 void transform_file(std::string& text) const;
237 };
238
240 {
241 public:
242 using nascent_test_base::nascent_test_base;
243
244 void finalize();
245
246 [[nodiscard]]
247 std::vector<std::string> constructors() const;
248
249 [[nodiscard]]
250 friend bool operator==(const nascent_behavioural_test&, const nascent_behavioural_test&) noexcept = default;
251
252 [[nodiscard]]
253 static std::vector<std::string> stubs();
254
255 void set_namespace(std::string n) { m_Namespace = std::move(n); }
256 private:
257 void transform_file(std::string& text) const;
258
259 std::string m_Namespace;
260
261 [[nodiscard]]
262 std::filesystem::path when_header_absent(const std::filesystem::path& filename);
263 };
264
265
267 using nascent_test_vessel = typename nascent_test_factory::vessel;
268}
Factory implementation(s)
Extensions to the std::filesystem library.
A collection of functions for formatting test output.
File paths pertaining to a sequoia project.
Type-safe mechanism for indentations.
Definition: Indent.hpp:22
Generic factory with statically defined products.
Definition: Factory.hpp:68
Definition: Suite.hpp:46
Definition: TestCreator.hpp:224
Definition: TestCreator.hpp:240
Definition: TestCreator.hpp:186
Definition: TestCreator.hpp:52
Paths used by the project.
Definition: ProjectPaths.hpp:469
Definition: TestCreator.hpp:29