]> git.saurik.com Git - apt.git/blob - test/libapt/commandline_test.cc
Use _apt as our unprivileged user name
[apt.git] / test / libapt / commandline_test.cc
1 #include <config.h>
2
3 #include <apt-pkg/cmndline.h>
4 #include <apt-pkg/configuration.h>
5
6 #include <gtest/gtest.h>
7
8 class CLT: public CommandLine {
9 public:
10 std::string static AsString(const char * const * const argv,
11 unsigned int const argc) {
12 std::string const static conf = "Commandline::AsString";
13 _config->Clear(conf);
14 SaveInConfig(argc, argv);
15 return _config->Find(conf);
16 }
17 };
18
19 #define EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }
20
21 TEST(CommandLineTest,SaveInConfig)
22 {
23 EXPECT_CMD("apt-get install -sf",
24 "apt-get", "install", "-sf");
25 EXPECT_CMD("apt-cache -s apt -so Debug::test=Test",
26 "apt-cache", "-s", "apt", "-so", "Debug::test=Test");
27 EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"",
28 "apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test");
29 EXPECT_CMD("apt-cache -s apt --hallo test=1.0",
30 "apt-cache", "-s", "apt", "--hallo", "test=1.0");
31 }
32 TEST(CommandLineTest,Parsing)
33 {
34 CommandLine::Args Args[] = {
35 { 't', 0, "Test::Worked", 0 },
36 { 'z', "zero", "Test::Zero", 0 },
37 {0,0,0,0}
38 };
39 ::Configuration c;
40 CommandLine CmdL(Args, &c);
41
42 char const * argv[] = { "test", "--zero", "-t" };
43 CmdL.Parse(3 , argv);
44 EXPECT_TRUE(c.FindB("Test::Worked", false));
45 EXPECT_TRUE(c.FindB("Test::Zero", false));
46
47 c.Clear("Test");
48 EXPECT_FALSE(c.FindB("Test::Worked", false));
49 EXPECT_FALSE(c.FindB("Test::Zero", false));
50
51 c.Set("Test::Zero", true);
52 EXPECT_TRUE(c.FindB("Test::Zero", false));
53
54 char const * argv2[] = { "test", "--no-zero", "-t" };
55 CmdL.Parse(3 , argv2);
56 EXPECT_TRUE(c.FindB("Test::Worked", false));
57 EXPECT_FALSE(c.FindB("Test::Zero", false));
58 }
59
60 TEST(CommandLineTest, BoolParsing)
61 {
62 CommandLine::Args Args[] = {
63 { 't', 0, "Test::Worked", 0 },
64 {0,0,0,0}
65 };
66 ::Configuration c;
67 CommandLine CmdL(Args, &c);
68
69 // the commandline parser used to use strtol() on the argument
70 // to check if the argument is a boolean expression - that
71 // stopped after the "0". this test ensures that we always check
72 // that the entire string was consumed by strtol
73 {
74 char const * argv[] = { "show", "-t", "0ad" };
75 bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
76 EXPECT_TRUE(res);
77 ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad");
78 }
79
80 {
81 char const * argv[] = { "show", "-t", "0", "ad" };
82 bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
83 EXPECT_TRUE(res);
84 ASSERT_EQ(std::string(CmdL.FileList[0]), "ad");
85 }
86
87 }