Commit 7b26c50e59d27603909248b678bf706d480770c6

Authored by Jarryd Beck
1 parent 728da0e6

add test with no options

Showing 1 changed file with 32 additions and 11 deletions
test/options.cpp
1 1 #define CATCH_CONFIG_MAIN
2 2 #include "catch.hpp"
3 3  
  4 +#include <initializer_list>
  5 +
4 6 #include "../src/cxxopts.hpp"
5 7  
6 8 class Argv {
7 9 public:
8 10  
9   - Argv(int n, const char** argv)
10   - : m_argv(new char*[n])
  11 + Argv(std::initializer_list<const char*> argv)
  12 + : m_argv(new char*[argv.size()])
  13 + , m_argc(argv.size())
11 14 {
12   - for (int i = 0; i != n; ++i) {
13   - auto len = strlen(argv[i]) + 1;
  15 + int i = 0;
  16 + auto iter = argv.begin();
  17 + while (iter != argv.end()) {
  18 + auto len = strlen(*iter) + 1;
14 19 auto ptr = std::unique_ptr<char[]>(new char[len]);
15 20  
16   - strcpy(ptr.get(), argv[i]);
  21 + strcpy(ptr.get(), *iter);
17 22 m_args.push_back(std::move(ptr));
18 23 m_argv.get()[i] = m_args.back().get();
  24 +
  25 + ++iter;
  26 + ++i;
19 27 }
20 28 }
21 29  
... ... @@ -23,10 +31,15 @@ class Argv {
23 31 return m_argv.get();
24 32 }
25 33  
  34 + int argc() const {
  35 + return m_argc;
  36 + }
  37 +
26 38 private:
27 39  
28 40 std::vector<std::unique_ptr<char[]>> m_args;
29 41 std::unique_ptr<char*[]> m_argv;
  42 + int m_argc;
30 43 };
31 44  
32 45 TEST_CASE("Basic options", "[options]")
... ... @@ -40,7 +53,7 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
40 53 ("value", "an option with a value", cxxopts::value<std::string>())
41 54 ("a,av", "a short option with a value", cxxopts::value<std::string>());
42 55  
43   - const char* args[] = {
  56 + Argv argv({
44 57 "tester",
45 58 "--long",
46 59 "-s",
... ... @@ -48,13 +61,10 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
48 61 "value",
49 62 "-a",
50 63 "b"
51   - };
52   -
53   - int argc = sizeof(args) / sizeof(args[0]);
54   -
55   - Argv argv(argc, args);
  64 + });
56 65  
57 66 char** actual_argv = argv.argv();
  67 + auto argc = argv.argc();
58 68  
59 69 options.parse(argc, actual_argv);
60 70  
... ... @@ -65,3 +75,14 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
65 75 CHECK(options["value"].as<std::string>() == "value");
66 76 CHECK(options["a"].as<std::string>() == "b");
67 77 }
  78 +
  79 +TEST_CASE("No positional", "[positional]")
  80 +{
  81 + cxxopts::Options options("test_positional", " - test no positional options");
  82 +
  83 + Argv argv({"tester", "a", "b", "def"});
  84 +
  85 + char** passed_argv = argv.argv();
  86 + auto argc = argv.argc();
  87 + options.parse(argc, passed_argv);
  88 +}
... ...