Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions tests/NewParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

#include <complex>
#include <cstdint>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -645,3 +647,69 @@ TEST_CASE_METHOD(TApp, "vectorComplex", "[newparse]") {
CHECK(1.4 == vcomplex[1].real());
CHECK(-4.0 == vcomplex[1].imag());
}

#if (defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS == 1) || \
(!defined(CLI11_DISABLE_EXTRA_VALIDATORS) || CLI11_DISABLE_EXTRA_VALIDATORS == 0)

// enumeration with stream operator
#define COLOR_LIST \
X(Red) \
X(Blue) \
X(Green)

enum class Color {
#define X(name) name,
COLOR_LIST
#undef X
};

const std::unordered_map<std::string, Color> STR_TO_COLOR_MAP{
#define X(name) {#name, Color::name},
COLOR_LIST
#undef X
};

// std::map: GCC < 6.1 has no std::hash for enums
const std::map<Color, std::string> COLOR_TO_STR_MAP{
#define X(name) {Color::name, #name},
COLOR_LIST
#undef X
};

// this stream operator triggered the ConversionError in issue #1330
std::ostream &operator<<(std::ostream &os, Color c) {
auto it = COLOR_TO_STR_MAP.find(c);
if(it != COLOR_TO_STR_MAP.end())
return os << it->second;

return os << std::string("Unknown");
}

TEST_CASE_METHOD(TApp, "enumDefaultParse", "[newparse]") {
Color color{Color::Blue};
Color other_color{Color::Red};
app.add_option("--color", color)
->default_val(Color::Red)
->transform(CLI::CheckedTransformer(STR_TO_COLOR_MAP))
->force_callback();

args = {};
run();
CHECK(color == Color::Red);

args = {"--color", "Green"};
run();
CHECK(color == Color::Green);

app.add_option("--color2", other_color)
->default_str("Green")
->transform(CLI::CheckedTransformer(STR_TO_COLOR_MAP))
->force_callback();

args = {"--color", "Blue"};
run();
CHECK(color == Color::Blue);
CHECK(other_color == Color::Green);
}

#endif
Loading