File: OptionParserTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (213 lines) | stat: -rw-r--r-- 6,411 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "OptionParser.h"

#include <cstring>
#include <sstream>

#include <cppunit/extensions/HelperMacros.h>

#include "OptionHandlerImpl.h"
#include "Exception.h"
#include "util.h"
#include "Option.h"
#include "array_fun.h"
#include "prefs.h"
#include "help_tags.h"

namespace aria2 {

class OptionParserTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(OptionParserTest);
  CPPUNIT_TEST(testFindAll);
  CPPUNIT_TEST(testFindByNameSubstring);
  CPPUNIT_TEST(testFindByTag);
  CPPUNIT_TEST(testFind);
  CPPUNIT_TEST(testFindByShortName);
  CPPUNIT_TEST(testFindById);
  CPPUNIT_TEST(testParseDefaultValues);
  CPPUNIT_TEST(testParseArg);
  CPPUNIT_TEST(testParse);
  CPPUNIT_TEST(testParseKeyVals);
  CPPUNIT_TEST_SUITE_END();

private:
  std::shared_ptr<OptionParser> oparser_;

public:
  void setUp()
  {
    oparser_.reset(new OptionParser());

    OptionHandler* timeout(
        new DefaultOptionHandler(PREF_TIMEOUT, NO_DESCRIPTION, "ALPHA", "",
                                 OptionHandler::REQ_ARG, 'A'));
    timeout->addTag(TAG_BASIC);
    timeout->setEraseAfterParse(true);
    oparser_->addOptionHandler(timeout);

    OptionHandler* dir(new DefaultOptionHandler(PREF_DIR));
    dir->addTag(TAG_BASIC);
    dir->addTag(TAG_HTTP);
    dir->addTag(TAG_FILE);
    oparser_->addOptionHandler(dir);

    DefaultOptionHandler* daemon(
        new DefaultOptionHandler(PREF_DAEMON, NO_DESCRIPTION, "CHARLIE", "",
                                 OptionHandler::REQ_ARG, 'C'));
    daemon->hide();
    daemon->addTag(TAG_FILE);
    oparser_->addOptionHandler(daemon);

    OptionHandler* out(new UnitNumberOptionHandler(PREF_OUT, NO_DESCRIPTION,
                                                   "1M", -1, -1, 'D'));
    out->addTag(TAG_FILE);
    oparser_->addOptionHandler(out);
  }

  void tearDown() {}

  void testFindAll();
  void testFindByNameSubstring();
  void testFindByTag();
  void testFind();
  void testFindByShortName();
  void testFindById();
  void testParseDefaultValues();
  void testParseArg();
  void testParse();
  void testParseKeyVals();
};

CPPUNIT_TEST_SUITE_REGISTRATION(OptionParserTest);

void OptionParserTest::testFindAll()
{
  std::vector<const OptionHandler*> res = oparser_->findAll();
  CPPUNIT_ASSERT_EQUAL((size_t)3, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("timeout"), std::string(res[0]->getName()));
  CPPUNIT_ASSERT_EQUAL(std::string("dir"), std::string(res[1]->getName()));
  CPPUNIT_ASSERT_EQUAL(std::string("out"), std::string(res[2]->getName()));
}

void OptionParserTest::testFindByNameSubstring()
{
  std::vector<const OptionHandler*> res = oparser_->findByNameSubstring("i");
  CPPUNIT_ASSERT_EQUAL((size_t)2, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("timeout"), std::string(res[0]->getName()));
  CPPUNIT_ASSERT_EQUAL(std::string("dir"), std::string(res[1]->getName()));
}

void OptionParserTest::testFindByTag()
{
  std::vector<const OptionHandler*> res = oparser_->findByTag(TAG_FILE);
  CPPUNIT_ASSERT_EQUAL((size_t)2, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("dir"), std::string(res[0]->getName()));
  CPPUNIT_ASSERT_EQUAL(std::string("out"), std::string(res[1]->getName()));
}

void OptionParserTest::testFind()
{
  const OptionHandler* dir = oparser_->find(PREF_DIR);
  CPPUNIT_ASSERT(dir);
  CPPUNIT_ASSERT_EQUAL(std::string("dir"), std::string(dir->getName()));

  const OptionHandler* daemon = oparser_->find(PREF_DAEMON);
  CPPUNIT_ASSERT(!daemon);

  const OptionHandler* log = oparser_->find(PREF_LOG);
  CPPUNIT_ASSERT(!log);
}

void OptionParserTest::testFindByShortName()
{
  const OptionHandler* timeout = oparser_->findByShortName('A');
  CPPUNIT_ASSERT(timeout);
  CPPUNIT_ASSERT_EQUAL(std::string("timeout"), std::string(timeout->getName()));

  CPPUNIT_ASSERT(!oparser_->findByShortName('C'));
}

void OptionParserTest::testFindById()
{
  const OptionHandler* timeout = oparser_->findById(PREF_TIMEOUT->i);
  CPPUNIT_ASSERT(timeout);
  CPPUNIT_ASSERT_EQUAL(std::string("timeout"), std::string(timeout->getName()));

  CPPUNIT_ASSERT(!oparser_->findById(9999));
}

void OptionParserTest::testParseDefaultValues()
{
  Option option;
  oparser_->parseDefaultValues(option);
  CPPUNIT_ASSERT_EQUAL(std::string("ALPHA"), option.get(PREF_TIMEOUT));
  CPPUNIT_ASSERT_EQUAL(std::string("1048576"), option.get(PREF_OUT));
  CPPUNIT_ASSERT_EQUAL(std::string("CHARLIE"), option.get(PREF_DAEMON));
  CPPUNIT_ASSERT(!option.defined(PREF_DIR));
}

void OptionParserTest::testParseArg()
{
  Option option;
  char prog[7];
  strncpy(prog, "aria2c", sizeof(prog));

  char optionTimeout[3];
  strncpy(optionTimeout, "-A", sizeof(optionTimeout));
  char argTimeout[6];
  strncpy(argTimeout, "ALPHA", sizeof(argTimeout));
  char optionDir[8];
  strncpy(optionDir, "--dir", sizeof(optionDir));
  char argDir[6];
  strncpy(argDir, "BRAVO", sizeof(argDir));

  char nonopt1[8];
  strncpy(nonopt1, "nonopt1", sizeof(nonopt1));
  char nonopt2[8];
  strncpy(nonopt2, "nonopt2", sizeof(nonopt2));

  char* argv[] = {prog,   optionTimeout, argTimeout, optionDir,
                  argDir, nonopt1,       nonopt2};
  int argc = arraySize(argv);

  std::stringstream s;
  std::vector<std::string> nonopts;

  oparser_->parseArg(s, nonopts, argc, argv);

  CPPUNIT_ASSERT_EQUAL(std::string("timeout=ALPHA\n"
                                   "dir=BRAVO\n"),
                       s.str());

  CPPUNIT_ASSERT_EQUAL((size_t)2, nonopts.size());
  CPPUNIT_ASSERT_EQUAL(std::string("nonopt1"), nonopts[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("nonopt2"), nonopts[1]);

  CPPUNIT_ASSERT_EQUAL(std::string("*****"), std::string(argTimeout));
}

void OptionParserTest::testParse()
{
  Option option;
  std::istringstream in("timeout=Hello\n"
                        "UNKNOWN=x\n"
                        "\n"
                        "dir=World");
  oparser_->parse(option, in);
  CPPUNIT_ASSERT_EQUAL(std::string("Hello"), option.get(PREF_TIMEOUT));
  CPPUNIT_ASSERT_EQUAL(std::string("World"), option.get(PREF_DIR));
}

void OptionParserTest::testParseKeyVals()
{
  Option option;
  KeyVals kv;
  kv.push_back(std::make_pair("timeout", "Hello"));
  kv.push_back(std::make_pair("UNKNOWN", "x"));
  kv.push_back(std::make_pair("dir", "World"));
  oparser_->parse(option, kv);
  CPPUNIT_ASSERT_EQUAL(std::string("Hello"), option.get(PREF_TIMEOUT));
  CPPUNIT_ASSERT_EQUAL(std::string("World"), option.get(PREF_DIR));
}

} // namespace aria2