Skip to content

Commit 1fd155c

Browse files
committed
[WIP] argparse
1 parent bd8b362 commit 1fd155c

File tree

11 files changed

+206
-123
lines changed

11 files changed

+206
-123
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"configurations": [
44
{
55
"type": "lldb",
6+
"preLaunchTask": "CMake: build",
67
"request": "launch",
78
"name": "Launch",
8-
"program": "${workspaceFolder}/build/spec/spec_runner.exe",
9+
"program": "${workspaceFolder}/build/examples/sample/jasmine_intro.exe",
910
"args": [],
1011
"cwd": "${workspaceFolder}"
1112
},

CMakeLists.txt

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cmake_minimum_required(VERSION 3.27 FATAL_ERROR)
2+
23
if(IWYU)
34
find_program(iwyu_path NAMES include-what-you-use iwyu)
45
endif(IWYU)
@@ -11,31 +12,30 @@ cmake_policy(SET CMP0135 NEW)
1112

1213
include(FetchContent)
1314

14-
# FetchContent_Declare(argparse
15-
# GIT_REPOSITORY https://github.com/p-ranav/argparse/
16-
# GIT_TAG v2.9
17-
# )
18-
# FetchContent_MakeAvailable(argparse)
15+
FetchContent_Declare(argparse
16+
GIT_REPOSITORY https://github.com/p-ranav/argparse/
17+
GIT_TAG v2.9
18+
)
19+
FetchContent_MakeAvailable(argparse)
1920

2021
FetchContent_Declare(cxx-prettyprint
2122
GIT_REPOSITORY https://github.com/louisdx/cxx-prettyprint
22-
GIT_TAG master
23+
GIT_TAG master
2324
)
2425
FetchContent_MakeAvailable(cxx-prettyprint)
2526

2627
add_library(cxx-prettyprint INTERFACE)
2728
target_sources(cxx-prettyprint INTERFACE ${cxx-prettyprint_SOURCE_DIR}/prettyprint.hpp)
2829
target_include_directories(cxx-prettyprint INTERFACE ${cxx-prettyprint_SOURCE_DIR})
2930

30-
3131
add_library(c++spec INTERFACE)
3232
target_include_directories(c++spec INTERFACE
3333
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
3434
$<INSTALL_INTERFACE:include/cppspec/> # <prefix>/include/cppspec
3535
)
3636
target_link_libraries(c++spec INTERFACE
3737
cxx-prettyprint
38-
# argparse
38+
argparse
3939
)
4040

4141
FILE(GLOB_RECURSE c++spec_headers ${CMAKE_CURRENT_LIST_DIR}/include/*.hpp)
@@ -62,47 +62,46 @@ if(BUILD_EXAMPLES)
6262
endif()
6363
endif(BUILD_EXAMPLES)
6464

65-
6665
# ##### Documentation generation #######
6766
# check if Doxygen is installed
6867
find_package(Doxygen
6968
OPTIONAL_COMPONENTS dot mscgen dia
7069
)
7170

7271
if(DOXYGEN_FOUND)
73-
if(NOT ${DOXYGEN_HAVE_DOT})
74-
message(
75-
"Can't find GraphViz DOT tool for generating images."
76-
"Make sure it's on your PATH or install GraphViz")
77-
endif()
78-
79-
FetchContent_Declare(doxygen-awesome-css
80-
URL https://github.com/jothepro/doxygen-awesome-css/archive/refs/tags/v2.2.1.tar.gz
72+
if(NOT ${DOXYGEN_HAVE_DOT})
73+
message(
74+
"Can't find GraphViz DOT tool for generating images."
75+
"Make sure it's on your PATH or install GraphViz")
76+
endif()
77+
78+
FetchContent_Declare(doxygen-awesome-css
79+
URL https://github.com/jothepro/doxygen-awesome-css/archive/refs/tags/v2.2.1.tar.gz
8180
URL_HASH MD5=340d3a206794ac01a91791c2a513991f
82-
)
83-
FetchContent_MakeAvailable(doxygen-awesome-css)
81+
)
82+
FetchContent_MakeAvailable(doxygen-awesome-css)
8483

85-
set(DOXYGEN_PROJECT_NAME "C++Spec")
86-
set(DOXYGEN_PROJECT_BRIEF "BDD testing for C++")
87-
set(DOXYGEN_RECURSIVE YES)
88-
set(DOXYGEN_EXAMPLE_RECURSIVE YES)
89-
set(DOXYGEN_EXCLUDE_PATTERNS "*/cxx-prettyprint/*")
90-
set(DOXYGEN_NUM_PROC_THREADS ${HOST_NUM_CORES})
84+
set(DOXYGEN_PROJECT_NAME "C++Spec")
85+
set(DOXYGEN_PROJECT_BRIEF "BDD testing for C++")
86+
set(DOXYGEN_RECURSIVE YES)
87+
set(DOXYGEN_EXAMPLE_RECURSIVE YES)
88+
set(DOXYGEN_EXCLUDE_PATTERNS "*/cxx-prettyprint/*")
89+
set(DOXYGEN_NUM_PROC_THREADS ${HOST_NUM_CORES})
9190

92-
# From doxygen-awesome
93-
set(DOXYGEN_GENERATE_TREEVIEW YES)
94-
set(DOXYGEN_DISABLE_INDEX NO)
95-
set(DOXYGEN_FULL_SIDEBAR NO)
96-
set(DOXYGEN_HTML_COLORSTYLE LIGHT)
97-
set(DOXYGEN_HTML_EXTRA_STYLESHEET "${doxygen-awesome-css_SOURCE_DIR}/doxygen-awesome.css")
91+
# From doxygen-awesome
92+
set(DOXYGEN_GENERATE_TREEVIEW YES)
93+
set(DOXYGEN_DISABLE_INDEX NO)
94+
set(DOXYGEN_FULL_SIDEBAR NO)
95+
set(DOXYGEN_HTML_COLORSTYLE LIGHT)
96+
set(DOXYGEN_HTML_EXTRA_STYLESHEET "${doxygen-awesome-css_SOURCE_DIR}/doxygen-awesome.css")
9897

99-
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE README.md)
98+
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE README.md)
10099

101-
file(GLOB markdown_SOURCES *.md)
100+
file(GLOB markdown_SOURCES *.md)
102101

103-
doxygen_add_docs(doxygen ${markdown_SOURCES} include)
102+
doxygen_add_docs(doxygen ${markdown_SOURCES} include)
104103
else(DOXYGEN_FOUND)
105-
message(WARNING
106-
"Doxygen needs to be installed to generate documentation."
107-
"Please install from https://github.com/doxygen/doxygen/releases")
104+
message(WARNING
105+
"Doxygen needs to be installed to generate documentation."
106+
"Please install from https://github.com/doxygen/doxygen/releases")
108107
endif(DOXYGEN_FOUND)

examples/sample/example_spec.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cstring>
44
#include <list>
55
#include "cppspec.hpp"
6+
#include "formatters/verbose.hpp"
67

78
describe bool_spec("Some Tests", $ {
89
context("true is", _ {
@@ -192,12 +193,12 @@ describe list_spec("A list spec", $ {
192193

193194
/* Here is the declaration of fabs description defined in an other file (fabs_spec.c in this sample)*/
194195
int main(){
195-
return CppSpec::Runner(CppSpec::Formatters::verbose)
196+
return CppSpec::Runner()
196197
.add_spec(bool_spec)
197198
.add_spec(abs_spec)
198199
.add_spec(strcmp_spec)
199200
.add_spec(vector_spec)
200201
.add_spec(let_spec)
201202
.add_spec(list_spec)
202-
.exec() ? EXIT_SUCCESS : EXIT_FAILURE;
203+
.exec<CppSpec::Formatters::Verbose>() ? EXIT_SUCCESS : EXIT_FAILURE;
203204
}

examples/sample/jasmine_intro.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2016 Katherine Whitlock
22

33
#include "cppspec.hpp"
4+
#include "formatters/verbose.hpp"
45

56
describe a_suite("A suite", $ {
67
it("contains a spec with an expectation", _ {
@@ -244,7 +245,7 @@ describe a_spec_nesting("A spec", $ {
244245

245246
// TODO: pending specs
246247

247-
describe to_include_matcher("to contain", $ {
248+
auto to_include_matcher = describe("to contain", $ {
248249
let(foo, ([]() -> std::vector<int> { return {1,2,3,4}; }));
249250

250251
it("matches arrays with some of the values", _ {
@@ -253,8 +254,8 @@ describe to_include_matcher("to contain", $ {
253254
});
254255
});
255256

256-
int main() {
257-
return CppSpec::Runner(CppSpec::Formatters::verbose)
257+
int main(int argc, char** argv) {
258+
return CppSpec::Runner()
258259
.add_spec(a_suite)
259260
.add_spec(suite_object)
260261
.add_spec(to_be_compare)
@@ -265,5 +266,5 @@ int main() {
265266
.add_spec(a_spec_before_each)
266267
.add_spec(a_spec_nesting)
267268
.add_spec(to_include_matcher)
268-
.exec() ? EXIT_SUCCESS : EXIT_FAILURE;
269+
.exec<CppSpec::Formatters::Verbose>() ? EXIT_SUCCESS : EXIT_FAILURE;
269270
}

include/argparse.hpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#include <argparse/argparse.hpp>
4+
#include <string_view>
5+
6+
#include "argparse.hpp"
7+
#include "formatters/formatters_base.hpp"
8+
9+
namespace CppSpec {
10+
constexpr std::string file_name(std::string_view path) {
11+
std::string_view file = path;
12+
for (size_t i = 0; i < path.size(); ++i) {
13+
if (path[i] == '/') {
14+
file = &path[i + 1];
15+
}
16+
}
17+
return std::string{file};
18+
}
19+
20+
enum class Formatter {
21+
Progress,
22+
TAP,
23+
Detail
24+
};
25+
26+
struct RuntimeOpts {
27+
bool verbose = false;
28+
Formatter format = Formatter::Progress;
29+
};
30+
31+
inline RuntimeOpts parse(int argc, char** argv) {
32+
argparse::ArgumentParser program(file_name(__FILE__));
33+
34+
program.add_argument("-f", "--format")
35+
.default_value(std::string{"p"})
36+
.required()
37+
.help("set the output format");
38+
39+
program.add_argument("--verbose")
40+
.help("increase output verbosity")
41+
.default_value(false)
42+
.implicit_value(true);
43+
44+
try {
45+
program.parse_args(argc, argv);
46+
} catch (const std::runtime_error& err) {
47+
std::cerr << err.what() << std::endl;
48+
std::cerr << program;
49+
std::exit(1);
50+
}
51+
52+
RuntimeOpts opts;
53+
54+
if (program["--verbose"] == true) {
55+
opts.verbose = true;
56+
}
57+
58+
auto format_string = program.get<std::string>("--format");
59+
if (format_string == "p" || format_string == "progress") {
60+
opts.format = Formatter::Progress;
61+
} else if (format_string == "t" || format_string == "tap") {
62+
opts.format = Formatter::TAP;
63+
} else if (format_string == "d" || format_string == "detail") {
64+
opts.format = Formatter::Detail;
65+
}
66+
}
67+
} // namespace CppSpec

include/cppspec.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#define CPPSPEC_HPP
77
#pragma once
88

9-
#ifndef CPPSPEC_DEBUG
10-
#define CPPSPEC_DEBUG false
11-
#endif
129
#include "runner.hpp"
1310

1411
/*>>>>>>>>>>>>>>>>>>>> MACROS <<<<<<<<<<<<<<<<<<<<<<*/
@@ -41,7 +38,7 @@
4138

4239
/*>>>>>>>>>>>>>>>>>>> TYPEDEFS <<<<<<<<<<<<<<<<<<<<<*/
4340

44-
typedef CppSpec::Description describe;
41+
using describe = CppSpec::Description;
4542

4643
template <class T>
4744
using describe_a = CppSpec::ClassDescription<T>;

include/description.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#define CPPSPEC_DESCRIPTION_HPP
77
#pragma once
88

9-
#include <string>
109
#include <deque>
11-
#include <queue>
1210
#include <forward_list>
13-
#include "it.hpp"
11+
#include <queue>
12+
#include <string>
13+
1414
#include "class_description.hpp"
15+
#include "it.hpp"
1516

1617
namespace CppSpec {
1718

@@ -45,21 +46,20 @@ class Description : public Runnable {
4546
: description(description) {}
4647

4748
Description(const Child &parent, std::string description,
48-
Block block) noexcept : Runnable(parent),
49-
block(block),
50-
description(description) {}
49+
Block block) noexcept
50+
: Runnable(parent), block(block), description(description) {}
5151

5252
void exec_before_eaches();
5353
void exec_after_eaches();
5454

5555
public:
5656
// Copy constructor
5757
Description(const Description &copy) = default;
58+
Description(Description &&copy) = default;
5859

5960
// Primary constructor. Entry of all specs.
6061
Description(std::string description, Block block) noexcept
61-
: block(block),
62-
description(description) {}
62+
: block(block), description(description) {}
6363

6464
/********* Specify/It *********/
6565

@@ -68,7 +68,7 @@ class Description : public Runnable {
6868

6969
/********* Context ***********/
7070

71-
template <class T=std::nullptr_t>
71+
template <class T = std::nullptr_t>
7272
Result context(std::string name, Block body);
7373

7474
template <class T>
@@ -103,6 +103,9 @@ class Description : public Runnable {
103103
/********* Run *********/
104104

105105
Result run(Formatters::BaseFormatter &printer) override;
106+
// std::function<int(int, char **)>
107+
template <typename Formatter>
108+
inline auto as_main();
106109
};
107110

108111
using Context = Description;

include/formatters/term_colors.hpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
#ifndef CPPSPEC_TERM_COLORS_HPP
33
#define CPPSPEC_TERM_COLORS_HPP
44
#pragma once
5+
#include <string_view>
56

67
// the following are Unix/BASH ONLY terminal color codes.
7-
#define RESET std::string("\033[0m")
8-
#define BLACK std::string("\033[30m") /* Black */
9-
#define RED std::string("\033[31m") /* Red */
10-
#define GREEN std::string("\033[32m") /* Green */
11-
#define YELLOW std::string("\033[33m") /* Yellow */
12-
#define BLUE std::string("\033[34m") /* Blue */
13-
#define MAGENTA std::string("\033[35m") /* Magenta */
14-
#define CYAN std::string("\033[36m") /* Cyan */
15-
#define WHITE std::string("\033[37m") /* White */
16-
#define BOLDBLACK std::string("\033[1m\033[30m") /* Bold Black */
17-
#define BOLDRED std::string("\033[1m\033[31m") /* Bold Red */
18-
#define BOLDGREEN std::string("\033[1m\033[32m") /* Bold Green */
19-
#define BOLDYELLOW std::string("\033[1m\033[33m") /* Bold Yellow */
20-
#define BOLDBLUE std::string("\033[1m\033[34m") /* Bold Blue */
21-
#define BOLDMAGENTA std::string("\033[1m\033[35m") /* Bold Magenta */
22-
#define BOLDCYAN std::string("\033[1m\033[36m") /* Bold Cyan */
23-
#define BOLDWHITE std::string("\033[1m\033[37m") /* Bold White */
8+
constexpr std::string_view RESET("\033[0m");
9+
constexpr std::string_view BLACK("\033[30m"); /* Black */
10+
constexpr std::string_view RED("\033[31m"); /* Red */
11+
constexpr std::string_view GREEN("\033[32m"); /* Green */
12+
constexpr std::string_view YELLOW("\033[33m"); /* Yellow */
13+
constexpr std::string_view BLUE("\033[34m"); /* Blue */
14+
constexpr std::string_view MAGENTA("\033[35m"); /* Magenta */
15+
constexpr std::string_view CYAN("\033[36m"); /* Cyan */
16+
constexpr std::string_view WHITE("\033[37m"); /* White */
17+
constexpr std::string_view BOLDBLACK("\033[1m\033[30m"); /* Bold Black */
18+
constexpr std::string_view BOLDRED("\033[1m\033[31m"); /* Bold Red */
19+
constexpr std::string_view BOLDGREEN("\033[1m\033[32m"); /* Bold Green */
20+
constexpr std::string_view BOLDYELLOW("\033[1m\033[33m"); /* Bold Yellow */
21+
constexpr std::string_view BOLDBLUE("\033[1m\033[34m"); /* Bold Blue */
22+
constexpr std::string_view BOLDMAGENTA("\033[1m\033[35m"); /* Bold Magenta */
23+
constexpr std::string_view BOLDCYAN("\033[1m\033[36m"); /* Bold Cyan */
24+
constexpr std::string_view BOLDWHITE("\033[1m\033[37m"); /* Bold White */
2425

25-
#endif //CPPSPEC_TERM_COLORS_HPP
26+
#endif // CPPSPEC_TERM_COLORS_HPP

0 commit comments

Comments
 (0)