1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00

Adding tests for Types, lexical casts, dropped useless and incorrect template

This commit is contained in:
Henry Fredrick Schreiner 2017-03-08 09:15:35 -05:00
parent e23f56551d
commit 228fd36cec
2 changed files with 57 additions and 11 deletions

View File

@ -124,20 +124,10 @@ namespace detail {
} }
} }
/// Vector
template<typename T,
enable_if_t<is_vector<T>::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<input.size(); i++)
output[i] = input[i];
return true;
}
/// String and similar /// String and similar
template<typename T, template<typename T,
enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value && !is_vector<T>::value enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value
, detail::enabler> = detail::dummy> , detail::enabler> = detail::dummy>
bool lexical_cast(std::string input, T& output) { bool lexical_cast(std::string input, T& output) {
output = input; output = input;

View File

@ -2,6 +2,8 @@
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
#include <stdint.h>
#include <string>
TEST(Split, SimpleByToken) { TEST(Split, SimpleByToken) {
auto out = CLI::detail::split("one.two.three", '.'); auto out = CLI::detail::split("one.two.three", '.');
@ -268,3 +270,57 @@ TEST(SplitUp, Spaces) {
std::vector<std::string> result = CLI::detail::split_up(orig); std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result); EXPECT_EQ(oput, result);
} }
TEST(Types, TypeName) {
std::string int_name = CLI::detail::type_name<int>();
EXPECT_EQ("INT", int_name);
std::string int2_name = CLI::detail::type_name<short>();
EXPECT_EQ("INT", int2_name);
std::string uint_name = CLI::detail::type_name<unsigned char>();
EXPECT_EQ("UINT", uint_name);
std::string float_name = CLI::detail::type_name<double>();
EXPECT_EQ("FLOAT", float_name);
std::string vector_name = CLI::detail::type_name<std::vector<int>>();
EXPECT_EQ("VECTOR", vector_name);
std::string text_name = CLI::detail::type_name<std::string>();
EXPECT_EQ("TEXT", text_name);
std::string text2_name = CLI::detail::type_name<char*>();
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;
}