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 #define CATCH_CONFIG_MAIN 1 #define CATCH_CONFIG_MAIN
2 #include "catch.hpp" 2 #include "catch.hpp"
3 3
  4 +#include <initializer_list>
  5 +
4 #include "../src/cxxopts.hpp" 6 #include "../src/cxxopts.hpp"
5 7
6 class Argv { 8 class Argv {
7 public: 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 auto ptr = std::unique_ptr<char[]>(new char[len]); 19 auto ptr = std::unique_ptr<char[]>(new char[len]);
15 20
16 - strcpy(ptr.get(), argv[i]); 21 + strcpy(ptr.get(), *iter);
17 m_args.push_back(std::move(ptr)); 22 m_args.push_back(std::move(ptr));
18 m_argv.get()[i] = m_args.back().get(); 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,10 +31,15 @@ class Argv {
23 return m_argv.get(); 31 return m_argv.get();
24 } 32 }
25 33
  34 + int argc() const {
  35 + return m_argc;
  36 + }
  37 +
26 private: 38 private:
27 39
28 std::vector<std::unique_ptr<char[]>> m_args; 40 std::vector<std::unique_ptr<char[]>> m_args;
29 std::unique_ptr<char*[]> m_argv; 41 std::unique_ptr<char*[]> m_argv;
  42 + int m_argc;
30 }; 43 };
31 44
32 TEST_CASE("Basic options", "[options]") 45 TEST_CASE("Basic options", "[options]")
@@ -40,7 +53,7 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;) @@ -40,7 +53,7 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
40 ("value", "an option with a value", cxxopts::value<std::string>()) 53 ("value", "an option with a value", cxxopts::value<std::string>())
41 ("a,av", "a short option with a value", cxxopts::value<std::string>()); 54 ("a,av", "a short option with a value", cxxopts::value<std::string>());
42 55
43 - const char* args[] = { 56 + Argv argv({
44 "tester", 57 "tester",
45 "--long", 58 "--long",
46 "-s", 59 "-s",
@@ -48,13 +61,10 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;) @@ -48,13 +61,10 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
48 "value", 61 "value",
49 "-a", 62 "-a",
50 "b" 63 "b"
51 - };  
52 -  
53 - int argc = sizeof(args) / sizeof(args[0]);  
54 -  
55 - Argv argv(argc, args); 64 + });
56 65
57 char** actual_argv = argv.argv(); 66 char** actual_argv = argv.argv();
  67 + auto argc = argv.argc();
58 68
59 options.parse(argc, actual_argv); 69 options.parse(argc, actual_argv);
60 70
@@ -65,3 +75,14 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;) @@ -65,3 +75,14 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
65 CHECK(options["value"].as<std::string>() == "value"); 75 CHECK(options["value"].as<std::string>() == "value");
66 CHECK(options["a"].as<std::string>() == "b"); 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 +}