Skip to content
Snippets Groups Projects
Commit d4019e42 authored by Martin Wölzer's avatar Martin Wölzer
Browse files

src/include/clapp/value.h: moved stringify() function from src/clapp/option_container.cpp

parent 6a885ada
No related branches found
No related tags found
No related merge requests found
Pipeline #552 failed with stages
in 7 hours, 29 minutes, and 29 seconds
...@@ -145,18 +145,6 @@ void clapp::parser::basic_option_container_t::validate_options() const { ...@@ -145,18 +145,6 @@ void clapp::parser::basic_option_container_t::validate_options() const {
} }
} }
namespace clapp {
static std::string stringify(
std::optional<std::vector<std::string>>& opt_str_vec) {
return std::accumulate(opt_str_vec.value().begin(),
opt_str_vec.value().end(), std::string{},
[](std::string& lhs, const std::string& rhs) {
return lhs.empty() ? (rhs) : (lhs + " " + rhs);
});
}
} // namespace clapp
void clapp::parser::basic_option_container_t::validate_options_xor( void clapp::parser::basic_option_container_t::validate_options_xor(
const types::variant_opt_conf_container_t* options, const types::variant_opt_conf_container_t* options,
const std::string& options_str, const std::string& options_str,
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <clapp/filesystem.h> #include <clapp/filesystem.h>
#include <functional> #include <functional>
#include <numeric>
#include <optional> #include <optional>
#include <string> #include <string>
#include <string_view> #include <string_view>
...@@ -106,6 +107,11 @@ class path_exists_t { ...@@ -106,6 +107,11 @@ class path_exists_t {
}; };
#endif #endif
inline std::string concat_str(const std::string &lhs, const std::string &rhs);
inline std::string stringify(
const std::optional<std::vector<std::string>> &opt_str_vec);
} // namespace value } // namespace value
} // namespace clapp } // namespace clapp
......
...@@ -204,4 +204,16 @@ clapp::fs::path clapp::value::convert_value<clapp::fs::path>( ...@@ -204,4 +204,16 @@ clapp::fs::path clapp::value::convert_value<clapp::fs::path>(
std::string_view param); std::string_view param);
#endif #endif
std::string clapp::value::concat_str(const std::string& lhs,
const std::string& rhs) {
return lhs.empty() ? (rhs) : (lhs + " " + rhs);
}
std::string clapp::value::stringify(
const std::optional<std::vector<std::string>>& opt_str_vec) {
return std::accumulate(opt_str_vec.value().begin(),
opt_str_vec.value().end(), std::string{},
concat_str);
}
#endif #endif
...@@ -501,3 +501,34 @@ TEST(value, foundFuncTReturnsExit) { ...@@ -501,3 +501,34 @@ TEST(value, foundFuncTReturnsExit) {
ASSERT_THAT(ret.value().get_exit_code(), testing::Eq(exit_code)); ASSERT_THAT(ret.value().get_exit_code(), testing::Eq(exit_code));
ASSERT_THAT(stringstr.str(), testing::StrEq("called func-name2")); ASSERT_THAT(stringstr.str(), testing::StrEq("called func-name2"));
} }
TEST(value, concatStr) {
ASSERT_THAT(clapp::value::concat_str("", "b"), testing::StrEq("b"));
ASSERT_THAT(clapp::value::concat_str("a", "b"), testing::StrEq("a b"));
ASSERT_THAT(clapp::value::concat_str("a", ""), testing::StrEq("a "));
}
TEST(value, stringifyNulloptThrows) {
const std::optional<std::vector<std::string>> opt_str_vec{std::nullopt};
ASSERT_THROW(clapp::value::stringify(opt_str_vec),
std::bad_optional_access);
}
TEST(value, stringifyEmptyVec) {
const std::optional<std::vector<std::string>> opt_str_vec{
std::vector<std::string>{}};
ASSERT_THAT(clapp::value::stringify(opt_str_vec), testing::StrEq(""));
}
TEST(value, stringifyVecWithOneElement) {
const std::optional<std::vector<std::string>> opt_str_vec{
std::vector<std::string>{"one"}};
ASSERT_THAT(clapp::value::stringify(opt_str_vec), testing::StrEq("one"));
}
TEST(value, stringifyVecWithTwoElements) {
const std::optional<std::vector<std::string>> opt_str_vec{
std::vector<std::string>{"one", "two"}};
ASSERT_THAT(clapp::value::stringify(opt_str_vec),
testing::StrEq("one two"));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment