Commit 5c0d6617d59c841dba6f00d1ab9d2e71bc52b11b

Authored by Henry Fredrick Schreiner
Committed by Henry Schreiner
1 parent 11444a4e

Adding optional test

tests/CMakeLists.txt
... ... @@ -10,6 +10,7 @@ set(CLI_TESTS
10 10 SubcommandTest
11 11 HelpTest
12 12 NewParseTest
  13 + OptionalTest
13 14 )
14 15  
15 16 set(CLI_MULTIONLY_TESTS
... ...
tests/OptionalTest.cpp 0 → 100644
  1 +#include <cstdlib>
  2 +#include <iostream>
  3 +
  4 +#ifdef __has_include
  5 +#if __has_include(<optional>)
  6 +#include <optional>
  7 +#define have_optional 1
  8 +using std::experimental::optional;
  9 +#elif __has_include(<experimental/optional>)
  10 +#include <experimental/optional>
  11 +#define have_optional 1
  12 +using std::optional;
  13 +#else
  14 +#define have_optional 0
  15 +#endif
  16 +#endif
  17 +
  18 +#if have_optional
  19 +
  20 +template <typename T> std::istream &operator>>(std::istream &in, optional<T> &val) {
  21 + T v;
  22 + in >> v;
  23 + val = v;
  24 + return in;
  25 +}
  26 +
  27 +#include "app_helper.hpp"
  28 +
  29 +TEST_F(TApp, OptionalTest) {
  30 + optional<int> opt;
  31 + app.add_option("-c,--count", opt);
  32 + run();
  33 + EXPECT_FALSE(opt);
  34 +
  35 + app.reset();
  36 + args = {"-c", "1"};
  37 + run();
  38 + EXPECT_TRUE(opt);
  39 + EXPECT_EQ(*opt, 1);
  40 +
  41 + app.reset();
  42 + args = {"--count", "3"};
  43 + run();
  44 + EXPECT_TRUE(opt);
  45 + EXPECT_EQ(*opt, 3);
  46 +}
  47 +
  48 +#endif
... ...