Ver código fonte

Test repeatable arguments.

Sam Jaffe 4 anos atrás
pai
commit
28c9b19de7
2 arquivos alterados com 43 adições e 0 exclusões
  1. 1 0
      include/program_args/utilities.h
  2. 42 0
      test/options_test.cpp

+ 1 - 0
include/program_args/utilities.h

@@ -75,6 +75,7 @@ template <> struct conversion_helper<int> {
 
 template <typename T>
 struct conversion_helper<std::vector<T>> : conversion_helper<T> {
+  using conversion_helper<T>::operator();
   std::vector<T> operator()(std::vector<std::string> const & data) const {
     std::vector<T> rval;
     for (auto & str : data) {

+ 42 - 0
test/options_test.cpp

@@ -9,7 +9,9 @@
 
 #include "xcode_gtest_helper.h"
 
+using testing::ElementsAre;
 using testing::Eq;
+using testing::IsEmpty;
 
 template <typename T, size_t N> static T parse(char const * const (&argv)[N]) {
   return T(N, argv);
@@ -122,3 +124,43 @@ TEST(LongOptionWithAbbrevTest, CanPutAbbrevArgAndValueInSameToken) {
   auto const options = parse<LongOptionWithAbbrevTest>({"", "-p443"});
   EXPECT_THAT(options.port, Eq(443));
 }
+
+struct LongOptionRepeatTest : program::Arguments<LongOptionRepeatTest> {
+  using program::Arguments<LongOptionRepeatTest>::Arguments;
+
+  std::vector<int> port = option("port");
+};
+
+TEST(LongOptionRepeatTest, DefaultIsEmpty) {
+  auto const options = parse<LongOptionRepeatTest>({""});
+  EXPECT_THAT(options.port, IsEmpty());
+}
+
+TEST(LongOptionRepeatTest, CanProvideArgument) {
+  auto const options = parse<LongOptionRepeatTest>({"", "--port", "443"});
+  EXPECT_THAT(options.port, ElementsAre(443));
+}
+
+TEST(LongOptionRepeatTest, RepeatingArgumentsAppends) {
+  auto const options =
+      parse<LongOptionRepeatTest>({"", "--port", "443", "--port", "8080"});
+  EXPECT_THAT(options.port, ElementsAre(443, 8080));
+}
+
+struct LongOptionRepeatWithDefaultTest
+    : program::Arguments<LongOptionRepeatWithDefaultTest> {
+  using program::Arguments<LongOptionRepeatWithDefaultTest>::Arguments;
+
+  std::vector<int> port = option("port") = std::vector{8080};
+};
+
+TEST(LongOptionRepeatWithDefaultTest, DefaultIsProvided) {
+  auto const options = parse<LongOptionRepeatWithDefaultTest>({""});
+  EXPECT_THAT(options.port, ElementsAre(8080));
+}
+
+TEST(LongOptionRepeatWithDefaultTest, ArgumentOverwritesDefault) {
+  auto const options =
+      parse<LongOptionRepeatWithDefaultTest>({"", "--port", "443"});
+  EXPECT_THAT(options.port, ElementsAre(443));
+}