| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent cf02040 commit 803c153
43 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,5 @@ | |||
| 1 | 1 | cmake_minimum_required(VERSION 3.27 FATAL_ERROR) | |
| 2 | 2 | ||
| 3 | - if(IWYU) | ||
| 4 | - find_program(iwyu_path NAMES include-what-you-use iwyu) | ||
| 5 | - endif(IWYU) | ||
| 6 | - | ||
| 7 | 3 | project(c++spec) | |
| 8 | 4 | ||
| 9 | 5 | set(CMAKE_COLOR_DIAGNOSTICS ON) | |
@@ -14,7 +10,7 @@ include(FetchContent) | |||
| 14 | 10 | ||
| 15 | 11 | FetchContent_Declare(argparse | |
| 16 | 12 | GIT_REPOSITORY https://github.com/p-ranav/argparse/ | |
| 17 | - GIT_TAG v2.9 | ||
| 13 | + GIT_TAG v3.0 | ||
| 18 | 14 | ) | |
| 19 | 15 | FetchContent_MakeAvailable(argparse) | |
| 20 | 16 | ||
@@ -43,23 +39,43 @@ target_precompile_headers(c++spec INTERFACE | |||
| 43 | 39 | ${c++spec_headers} | |
| 44 | 40 | ) | |
| 45 | 41 | ||
| 46 | - option(BUILD_TESTS "Build tests." ON) | ||
| 42 | + # HELPERS | ||
| 43 | + | ||
| 44 | + # Add spec | ||
| 45 | + function(add_spec source_file) | ||
| 46 | + cmake_path(GET source_file STEM spec_name) | ||
| 47 | + add_executable(${spec_name} ${source_file}) | ||
| 48 | + target_link_libraries(${spec_name} c++spec) | ||
| 49 | + | ||
| 50 | + set_target_properties(${spec_name} PROPERTIES | ||
| 51 | + CXX_STANDARD 20 | ||
| 52 | + CXX_STANDARD_REQUIRED YES | ||
| 53 | + ) | ||
| 54 | + add_test(NAME ${spec_name} COMMAND ${spec_name} --verbose) | ||
| 55 | + endfunction(add_spec) | ||
| 56 | + | ||
| 57 | + # Discover Specs | ||
| 58 | + function(discover_specs spec_folder) | ||
| 59 | + file(GLOB_RECURSE specs ${spec_folder}/*_spec.cpp) | ||
| 60 | + | ||
| 61 | + foreach(spec IN LISTS specs) | ||
| 62 | + add_spec(${spec}) | ||
| 63 | + endforeach() | ||
| 64 | + endfunction(discover_specs) | ||
| 65 | + | ||
| 66 | + option(BUILD_TESTS "Build tests" ON) | ||
| 47 | 67 | ||
| 48 | 68 | if(BUILD_TESTS) | |
| 49 | - # enable_testing() | ||
| 69 | + enable_testing() | ||
| 50 | 70 | ||
| 51 | 71 | # Tests | |
| 52 | - add_subdirectory(spec) | ||
| 72 | + discover_specs(spec) | ||
| 53 | 73 | endif(BUILD_TESTS) | |
| 54 | 74 | ||
| 55 | - option(BUILD_EXAMPLES "Build examples." ON) | ||
| 75 | + option(BUILD_EXAMPLES "Build examples" ON) | ||
| 56 | 76 | ||
| 57 | 77 | if(BUILD_EXAMPLES) | |
| 58 | 78 | add_subdirectory(examples) | |
| 59 | - | ||
| 60 | - if(iwyu_path) | ||
| 61 | - set_property(TARGET cppspec_sample PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path}) | ||
| 62 | - endif() | ||
| 63 | 79 | endif(BUILD_EXAMPLES) | |
| 64 | 80 | ||
| 65 | 81 | # ##### Documentation generation ####### | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,13 +192,13 @@ describe list_spec("A list spec", $ { | |||
| 192 | 192 | }); | |
| 193 | 193 | ||
| 194 | 194 | /* Here is the declaration of fabs description defined in an other file (fabs_spec.c in this sample)*/ | |
| 195 | - int main(){ | ||
| 196 | - return CppSpec::Runner() | ||
| 195 | + int main(int argc, char **argv){ | ||
| 196 | + return CppSpec::parse(argc, argv) | ||
| 197 | 197 | .add_spec(bool_spec) | |
| 198 | 198 | .add_spec(abs_spec) | |
| 199 | 199 | .add_spec(strcmp_spec) | |
| 200 | 200 | .add_spec(vector_spec) | |
| 201 | 201 | .add_spec(let_spec) | |
| 202 | 202 | .add_spec(list_spec) | |
| 203 | - .exec<CppSpec::Formatters::Verbose>() ? EXIT_SUCCESS : EXIT_FAILURE; | ||
| 204 | - } | ||
| 203 | + .exec() ? EXIT_SUCCESS : EXIT_FAILURE; | ||
| 204 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -255,7 +255,7 @@ auto to_include_matcher = describe("to contain", $ { | |||
| 255 | 255 | }); | |
| 256 | 256 | ||
| 257 | 257 | int main(int argc, char** argv) { | |
| 258 | - return CppSpec::Runner() | ||
| 258 | + return CppSpec::parse(argc, argv) | ||
| 259 | 259 | .add_spec(a_suite) | |
| 260 | 260 | .add_spec(suite_object) | |
| 261 | 261 | .add_spec(to_be_compare) | |
@@ -266,5 +266,5 @@ int main(int argc, char** argv) { | |||
| 266 | 266 | .add_spec(a_spec_before_each) | |
| 267 | 267 | .add_spec(a_spec_nesting) | |
| 268 | 268 | .add_spec(to_include_matcher) | |
| 269 | - .exec<CppSpec::Formatters::Verbose>() ? EXIT_SUCCESS : EXIT_FAILURE; | ||
| 269 | + .exec() ? EXIT_SUCCESS : EXIT_FAILURE; | ||
| 270 | 270 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | #include <argparse/argparse.hpp> | |
| 4 | 4 | #include <string_view> | |
| 5 | + #include "runner.hpp" | ||
| 5 | 6 | ||
| 6 | 7 | #include "formatters/progress.hpp" | |
| 7 | 8 | #include "formatters/tap.hpp" | |
@@ -22,14 +23,15 @@ constexpr std::string file_name(std::string_view path) { | |||
| 22 | 23 | ||
| 23 | 24 | struct RuntimeOpts { | |
| 24 | 25 | bool verbose = false; | |
| 25 | - std::unique_ptr<Formatters::BaseFormatter> formatter = nullptr; | ||
| 26 | + std::shared_ptr<Formatters::BaseFormatter> formatter = nullptr; | ||
| 26 | 27 | }; | |
| 27 | 28 | ||
| 28 | - inline RuntimeOpts parse(int argc, char** argv) { | ||
| 29 | - argparse::ArgumentParser program(file_name(__FILE__)); | ||
| 29 | + inline Runner parse(int argc, char** argv) { | ||
| 30 | + argparse::ArgumentParser program{file_name(argv[0])}; | ||
| 30 | 31 | ||
| 31 | 32 | program.add_argument("-f", "--format") | |
| 32 | 33 | .default_value(std::string{"p"}) | |
| 34 | + .choices("progress", "p", "tap", "t", "detail", "d") | ||
| 33 | 35 | .required() | |
| 34 | 36 | .help("set the output format"); | |
| 35 | 37 | ||
@@ -48,22 +50,18 @@ inline RuntimeOpts parse(int argc, char** argv) { | |||
| 48 | 50 | ||
| 49 | 51 | RuntimeOpts opts; | |
| 50 | 52 | ||
| 51 | - if (program["--verbose"] == true) { | ||
| 52 | - opts.verbose = true; | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | 53 | auto format_string = program.get<std::string>("--format"); | |
| 56 | - if (format_string == "p" || format_string == "progress") { | ||
| 54 | + if (format_string == "d" || format_string == "detail" || program["--verbose"] == true) { | ||
| 55 | + opts.formatter = std::make_unique<Formatters::Verbose>(); | ||
| 56 | + } else if (format_string == "p" || format_string == "progress") { | ||
| 57 | 57 | opts.formatter = std::make_unique<Formatters::Progress>(); | |
| 58 | 58 | } else if (format_string == "t" || format_string == "tap") { | |
| 59 | 59 | opts.formatter = std::make_unique<Formatters::TAP>(); | |
| 60 | - } else if (format_string == "d" || format_string == "detail") { | ||
| 61 | - opts.formatter = std::make_unique<Formatters::Verbose>(); | ||
| 62 | 60 | } else { | |
| 63 | 61 | std::cerr << "Unrecognized format type" << std::endl; | |
| 64 | 62 | std::exit(-1); | |
| 65 | 63 | } | |
| 66 | 64 | ||
| 67 | - return opts; | ||
| 65 | + return Runner{opts.formatter}; | ||
| 68 | 66 | } | |
| 69 | 67 | } // namespace CppSpec | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,11 +80,11 @@ class Child { | |||
| 80 | 80 | ||
| 81 | 81 | /** @brief Check to see if the Child has a parent. */ | |
| 82 | 82 | const bool has_parent() noexcept { return parent != nullptr; } | |
| 83 | - const bool has_parent() const noexcept { return parent != nullptr; } | ||
| 83 | + [[nodiscard]] const bool has_parent() const noexcept { return parent != nullptr; } | ||
| 84 | 84 | ||
| 85 | 85 | // TODO: Look in to making these references instead of pointer returns | |
| 86 | 86 | /** @brief Get the Child's parent. */ | |
| 87 | - Child *get_parent() const noexcept { return parent; } | ||
| 87 | + [[nodiscard]] Child *get_parent() const noexcept { return parent; } | ||
| 88 | 88 | ||
| 89 | 89 | template <class C> | |
| 90 | 90 | C get_parent_as() const noexcept { | |
@@ -103,23 +103,23 @@ class Child { | |||
| 103 | 103 | ||
| 104 | 104 | /*--------- Formatter helper functions -----------*/ | |
| 105 | 105 | // Check to see if the tree has a printer | |
| 106 | - const bool has_formatter() const noexcept; | ||
| 106 | + [[nodiscard]] bool has_formatter() const noexcept; | ||
| 107 | 107 | ||
| 108 | 108 | // Get the printer from the tree | |
| 109 | - Formatters::BaseFormatter &get_formatter() const noexcept; | ||
| 109 | + [[nodiscard]] Formatters::BaseFormatter &get_formatter() const noexcept; | ||
| 110 | 110 | ||
| 111 | - void set_formatter(const Formatters::BaseFormatter &formatter) { | ||
| 112 | - this->formatter = &const_cast<Formatters::BaseFormatter &>(formatter); | ||
| 111 | + void set_formatter(Formatters::BaseFormatter &formatter) { | ||
| 112 | + this->formatter = &formatter; | ||
| 113 | 113 | } | |
| 114 | 114 | ||
| 115 | 115 | /*--------- Primary member functions -------------*/ | |
| 116 | 116 | ||
| 117 | 117 | /** @brief Get the status of the object (success/failure) */ | |
| 118 | - const bool get_status() const noexcept { return this->status; } | ||
| 118 | + [[nodiscard]] bool get_status() const noexcept { return this->status; } | ||
| 119 | 119 | void failed() noexcept; // Report failure to the object. | |
| 120 | 120 | ||
| 121 | 121 | // Calculate the padding for printing this object | |
| 122 | - std::string padding() const noexcept; | ||
| 122 | + [[nodiscard]] std::string padding() const noexcept; | ||
| 123 | 123 | }; | |
| 124 | 124 | ||
| 125 | 125 | /*>>>>>>>>>>>>>>>>>>>> Child <<<<<<<<<<<<<<<<<<<<<<<<<*/ | |
@@ -133,7 +133,8 @@ class Child { | |||
| 133 | 133 | inline void Child::failed() noexcept { | |
| 134 | 134 | this->status = false; | |
| 135 | 135 | // propogates the failure up the tree | |
| 136 | - if (this->has_parent()) this->get_parent()->failed(); | ||
| 136 | + if (this->has_parent()) { this->get_parent()->failed(); | ||
| 137 | + } | ||
| 137 | 138 | } | |
| 138 | 139 | ||
| 139 | 140 | /** | |
@@ -144,15 +145,19 @@ inline std::string Child::padding() const noexcept { | |||
| 144 | 145 | return this->has_parent() ? this->get_parent()->padding() + " " : ""; | |
| 145 | 146 | } | |
| 146 | 147 | ||
| 147 | - inline const bool Child::has_formatter() const noexcept { | ||
| 148 | - if (this->formatter != nullptr) return true; | ||
| 149 | - if (!this->has_parent()) return false; // base case; | ||
| 148 | + inline bool Child::has_formatter() const noexcept { | ||
| 149 | + if (this->formatter != nullptr) { return true; | ||
| 150 | + } | ||
| 151 | + if (!this->has_parent()) { return false; // base case; | ||
| 152 | + } | ||
| 150 | 153 | return parent->has_formatter(); | |
| 151 | 154 | } | |
| 152 | 155 | ||
| 153 | 156 | inline Formatters::BaseFormatter &Child::get_formatter() const noexcept { | |
| 154 | - if (this->formatter) return *formatter; | ||
| 155 | - if (!this->has_parent()) std::terminate(); | ||
| 157 | + if (this->formatter != nullptr) { return *formatter; | ||
| 158 | + } | ||
| 159 | + if (!this->has_parent()) { std::terminate(); | ||
| 160 | + } | ||
| 156 | 161 | return parent->get_formatter(); | |
| 157 | 162 | } | |
| 158 | 163 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,6 @@ | |||
| 6 | 6 | ||
| 7 | 7 | #include "description.hpp" | |
| 8 | 8 | ||
| 9 | - | ||
| 10 | 9 | namespace CppSpec { | |
| 11 | 10 | ||
| 12 | 11 | /** | |
@@ -25,7 +24,7 @@ class ClassDescription : public Description { | |||
| 25 | 24 | using Block = std::function<void(ClassDescription<T> &)>; | |
| 26 | 25 | ||
| 27 | 26 | Block block; | |
| 28 | - std::string type = ""; | ||
| 27 | + std::string type; | ||
| 29 | 28 | ||
| 30 | 29 | public: | |
| 31 | 30 | const bool has_subject = true; | |
@@ -83,7 +82,7 @@ class ClassDescription : public Description { | |||
| 83 | 82 | /** @brief an alias for it */ | |
| 84 | 83 | Result specify(std::function<void(ItCD<T> &)> block) { return it(block); } | |
| 85 | 84 | ||
| 86 | - template <class U=std::nullptr_t> | ||
| 85 | + template <class U = std::nullptr_t> | ||
| 87 | 86 | Result context(std::string description, | |
| 88 | 87 | std::function<void(ClassDescription<T> &)> block); | |
| 89 | 88 | ||
@@ -100,7 +99,9 @@ class ClassDescription : public Description { | |||
| 100 | 99 | ||
| 101 | 100 | Result run(Formatters::BaseFormatter &printer) override; | |
| 102 | 101 | ||
| 103 | - std::string get_subject_type() const noexcept override { return type; } | ||
| 102 | + [[nodiscard]] std::string get_subject_type() const noexcept override { | ||
| 103 | + return type; | ||
| 104 | + } | ||
| 104 | 105 | }; | |
| 105 | 106 | ||
| 106 | 107 | template <class T> | |
@@ -244,11 +245,17 @@ Result ClassDescription<T>::it(std::function<void(ItCD<T> &)> block) { | |||
| 244 | 245 | ||
| 245 | 246 | template <class T> | |
| 246 | 247 | Result ClassDescription<T>::run(Formatters::BaseFormatter &printer) { | |
| 247 | - if (not this->has_formatter()) this->set_formatter(printer); | ||
| 248 | + if (not this->has_formatter()) { | ||
| 249 | + this->set_formatter(printer); | ||
| 250 | + } | ||
| 248 | 251 | printer.format(*this); | |
| 249 | 252 | this->block(*this); | |
| 250 | - for (const auto &a : after_alls) a(); | ||
| 251 | - if (this->get_parent() == nullptr) printer.flush(); | ||
| 253 | + for (const auto &a : after_alls) { | ||
| 254 | + a(); | ||
| 255 | + } | ||
| 256 | + if (this->get_parent() == nullptr) { | ||
| 257 | + printer.flush(); | ||
| 258 | + } | ||
| 252 | 259 | return this->get_status() ? Result::success() : Result::failure(); | |
| 253 | 260 | } | |
| 254 | 261 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,8 +6,9 @@ | |||
| 6 | 6 | #define CPPSPEC_HPP | |
| 7 | 7 | #pragma once | |
| 8 | 8 | ||
| 9 | - #include "runner.hpp" | ||
| 9 | + #include "argparse.hpp" | ||
| 10 | 10 | ||
| 11 | + #ifndef CPPSPEC_MACROLESS | ||
| 11 | 12 | /*>>>>>>>>>>>>>>>>>>>> MACROS <<<<<<<<<<<<<<<<<<<<<<*/ | |
| 12 | 13 | ||
| 13 | 14 | // For *some* reason, MSVC++ refuses to correctly deduce the types of | |
@@ -18,7 +19,9 @@ | |||
| 18 | 19 | ||
| 19 | 20 | #define it self.it | |
| 20 | 21 | #define specify it | |
| 21 | - #ifdef _MSC_VER // Apparently MSVC++ doesn't conform to C++14 14.2/4. Annoying. | ||
| 22 | + | ||
| 23 | + // Apparently MSVC++ doesn't conform to C++14 14.2/4. Annoying. | ||
| 24 | + #if defined(_MSC_VER) && !defined(__clang__) | ||
| 22 | 25 | #define context self.context | |
| 23 | 26 | #define expect self.expect | |
| 24 | 27 | #else | |
@@ -34,8 +37,15 @@ | |||
| 34 | 37 | #define before_each self.before_each | |
| 35 | 38 | #define after_all self.after_all | |
| 36 | 39 | #define after_each self.after_each | |
| 37 | - #define let(name, body) auto name = self.let(body); | ||
| 40 | + #define let(name, body) auto (name) = self.let(body); | ||
| 41 | + | ||
| 42 | + #define CPPSPEC_MAIN(spec) \ | ||
| 43 | + int main(int argc, char **argv) { \ | ||
| 44 | + return CppSpec::parse(argc, argv).add_spec(spec).exec() ? EXIT_SUCCESS \ | ||
| 45 | + : EXIT_FAILURE; \ | ||
| 46 | + } | ||
| 38 | 47 | ||
| 48 | + #endif | ||
| 39 | 49 | /*>>>>>>>>>>>>>>>>>>> TYPEDEFS <<<<<<<<<<<<<<<<<<<<<*/ | |
| 40 | 50 | ||
| 41 | 51 | using describe = CppSpec::Description; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments