]> git.saurik.com Git - apt.git/blame - test/libapt/commandline_test.cc
Use _apt as our unprivileged user name
[apt.git] / test / libapt / commandline_test.cc
CommitLineData
453b82a3
DK
1#include <config.h>
2
ae2be086 3#include <apt-pkg/cmndline.h>
453b82a3 4#include <apt-pkg/configuration.h>
ae2be086 5
f00832cc
DK
6#include <gtest/gtest.h>
7
8class 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
21TEST(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}
32TEST(CommandLineTest,Parsing)
ae2be086
DH
33{
34 CommandLine::Args Args[] = {
35 { 't', 0, "Test::Worked", 0 },
36 { 'z', "zero", "Test::Zero", 0 },
37 {0,0,0,0}
38 };
f00832cc
DK
39 ::Configuration c;
40 CommandLine CmdL(Args, &c);
7a6d9076 41
ae2be086
DH
42 char const * argv[] = { "test", "--zero", "-t" };
43 CmdL.Parse(3 , argv);
f00832cc
DK
44 EXPECT_TRUE(c.FindB("Test::Worked", false));
45 EXPECT_TRUE(c.FindB("Test::Zero", false));
ae2be086 46
f00832cc
DK
47 c.Clear("Test");
48 EXPECT_FALSE(c.FindB("Test::Worked", false));
49 EXPECT_FALSE(c.FindB("Test::Zero", false));
7a6d9076 50
f00832cc
DK
51 c.Set("Test::Zero", true);
52 EXPECT_TRUE(c.FindB("Test::Zero", false));
7a6d9076
DK
53
54 char const * argv2[] = { "test", "--no-zero", "-t" };
55 CmdL.Parse(3 , argv2);
f00832cc
DK
56 EXPECT_TRUE(c.FindB("Test::Worked", false));
57 EXPECT_FALSE(c.FindB("Test::Zero", false));
ae2be086 58}
08be0ca3
MV
59
60TEST(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}