From ef166277eaaed68d07d9708f7ca471ba3135ab59 Mon Sep 17 00:00:00 2001 From: Mohammed Alyousef Date: Tue, 15 Sep 2020 22:18:45 +0300 Subject: [PATCH] support testing --- CHANGELOG.md | 3 ++- README.md | 6 +++++- include/cmake.hpp | 7 +++++++ include/literals.h | 5 +++++ src/cmake.cpp | 13 +++++++++++++ src/gen.cpp | 17 ++++++++++++++++- 6 files changed, 48 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2425f52..6572203 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,4 +5,5 @@ - Add support for find_package components. - Add options. - Support aliases. -- Support interface libs (header-only libs). \ No newline at end of file +- Support interface libs (header-only libs). +- Support testing. \ No newline at end of file diff --git a/README.md b/README.md index e5613b8..4b7ecf4 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,11 @@ alias = "" # optional features = [] # optional defines = [] # optional link-libs = [] # optional + +[[test]] # optional +name = "test1" # required +command = "cmkr" # required +arguments = [] # optional ``` The cmkr executable can be run from the command-line: @@ -129,4 +134,3 @@ Header only library. ## Roadmap - Support more cmake fields. - Support conditional cmake args somehow! -- Support ctest. diff --git a/include/cmake.hpp b/include/cmake.hpp index 677b3eb..3a705b7 100644 --- a/include/cmake.hpp +++ b/include/cmake.hpp @@ -30,6 +30,12 @@ struct Bin { std::string alias; }; +struct Test { + std::string name; + std::string cmd; + std::vector args; +}; + struct CMake { std::string cmake_version = "3.14"; std::string bin_dir = "bin"; @@ -46,6 +52,7 @@ struct CMake { std::vector packages; std::map> contents; std::vector binaries; + std::vector tests; CMake(const std::string &path, bool build); }; diff --git a/include/literals.h b/include/literals.h index 8f2f85d..5c6bb32 100644 --- a/include/literals.h +++ b/include/literals.h @@ -39,4 +39,9 @@ include-dirs = ["include"] # features = [] # defines = [] # link-libs = [] + +# [[test]]] +# name = "test1" +# command = "%s" +# arguments = [] )lit"; diff --git a/src/cmake.cpp b/src/cmake.cpp index f36f8db..397f1e9 100644 --- a/src/cmake.cpp +++ b/src/cmake.cpp @@ -152,6 +152,19 @@ CMake::CMake(const std::string &path, bool build) { binaries.push_back(b); } } + + if (toml.contains("test")) { + const auto &ts = toml::find(toml, "test").as_array(); + for (const auto &t : ts) { + Test test; + test.name = toml::find(t, "name").as_string(); + test.cmd = toml::find(t, "type").as_string(); + if (t.contains("arguments")) { + test.args = detail::to_string_vec(toml::find(t, "arguments").as_array()); + } + tests.push_back(test); + } + } } } } // namespace cmkr::cmake diff --git a/src/gen.cpp b/src/gen.cpp index 2be9aed..20cb66a 100644 --- a/src/gen.cpp +++ b/src/gen.cpp @@ -46,7 +46,8 @@ int generate_project(const char *str) { fs::create_directory("include"); const auto dir_name = fs::current_path().stem().string(); std::string mainbuf; - const auto tomlbuf = detail::format(cmake_toml, dir_name.c_str(), dir_name.c_str(), str); + const auto tomlbuf = + detail::format(cmake_toml, dir_name.c_str(), dir_name.c_str(), str, dir_name.c_str()); if (!strcmp(str, "exe")) { mainbuf = detail::format(hello_world, "main"); } else if (!strcmp(str, "static") || !strcmp(str, "shared") || !strcmp(str, "lib")) { @@ -247,6 +248,20 @@ int generate_cmake(const char *path) { } } + if (!cmake.tests.empty()) { + ss << "include(CTest)\n" + << "enable_testing()\n\n"; + for (const auto &test : cmake.tests) { + ss << "add_test(NAME " << test.name << " COMMAND " << test.cmd; + if (!test.args.empty()) { + for (const auto &arg : test.args) { + ss << " " << arg; + } + } + ss << ")\n\n"; + } + } + ss << "\n\n"; // printf("%s\n", ss.str().c_str());