diff --git a/include/CLI/TypeTools.hpp b/include/CLI/TypeTools.hpp index e91e3623..762d5f9b 100644 --- a/include/CLI/TypeTools.hpp +++ b/include/CLI/TypeTools.hpp @@ -124,20 +124,10 @@ namespace detail { } } - /// Vector - template::value, detail::enabler> = detail::dummy> - bool lexical_cast(std::string input, T& output) { - if(output.size() == input.size()) - output.resize(input.size()); - for(size_t i=0; i::value && !std::is_integral::value && !is_vector::value + enable_if_t::value && !std::is_integral::value , detail::enabler> = detail::dummy> bool lexical_cast(std::string input, T& output) { output = input; diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index 2553daa3..2e09e2a4 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include TEST(Split, SimpleByToken) { auto out = CLI::detail::split("one.two.three", '.'); @@ -268,3 +270,57 @@ TEST(SplitUp, Spaces) { std::vector result = CLI::detail::split_up(orig); EXPECT_EQ(oput, result); } + +TEST(Types, TypeName) { + std::string int_name = CLI::detail::type_name(); + EXPECT_EQ("INT", int_name); + + std::string int2_name = CLI::detail::type_name(); + EXPECT_EQ("INT", int2_name); + + std::string uint_name = CLI::detail::type_name(); + EXPECT_EQ("UINT", uint_name); + + std::string float_name = CLI::detail::type_name(); + EXPECT_EQ("FLOAT", float_name); + + std::string vector_name = CLI::detail::type_name>(); + EXPECT_EQ("VECTOR", vector_name); + + std::string text_name = CLI::detail::type_name(); + EXPECT_EQ("TEXT", text_name); + + std::string text2_name = CLI::detail::type_name(); + EXPECT_EQ("TEXT", text2_name); +} + +TEST(Types, LexicalCastInt) { + std::string input = "912"; + int x; + EXPECT_TRUE(CLI::detail::lexical_cast(input, x)); + EXPECT_EQ(912, x); + + unsigned char y; + std::string overflow_input = std::to_string(UINT64_MAX) + "0"; + EXPECT_FALSE(CLI::detail::lexical_cast(overflow_input, y)); + + std::string bad_input = "hello"; + EXPECT_FALSE(CLI::detail::lexical_cast(bad_input, y)); +} + +TEST(Types, LexicalCastDouble) { + + std::string input = "9.12"; + long double x; + EXPECT_TRUE(CLI::detail::lexical_cast(input, x)); + EXPECT_FLOAT_EQ(9.12, x); + + std::string bad_input = "hello"; + EXPECT_FALSE(CLI::detail::lexical_cast(bad_input, x)); + + std::string overflow_input = "1" + std::to_string(LDBL_MAX); + std::cout << "Before: " << overflow_input << std::endl; + EXPECT_FALSE(CLI::detail::lexical_cast(overflow_input, x)); + std::cout << "After: " << x << std::endl; + +}