]>
git.saurik.com Git - apt.git/blob - test/libapt/commandline_test.cc
3 #include <apt-pkg/cmndline.h>
4 #include <apt-pkg/configuration.h>
6 #include <gtest/gtest.h>
8 class CLT
: public CommandLine
{
10 std::string
static AsString(const char * const * const argv
,
11 unsigned int const argc
) {
12 std::string
const static conf
= "Commandline::AsString";
14 SaveInConfig(argc
, argv
);
15 return _config
->Find(conf
);
19 #define EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }
21 TEST(CommandLineTest
,SaveInConfig
)
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");
32 TEST(CommandLineTest
,Parsing
)
34 CommandLine::Args Args
[] = {
35 { 't', 0, "Test::Worked", 0 },
36 { 'z', "zero", "Test::Zero", 0 },
40 CommandLine
CmdL(Args
, &c
);
42 char const * argv
[] = { "test", "--zero", "-t" };
44 EXPECT_TRUE(c
.FindB("Test::Worked", false));
45 EXPECT_TRUE(c
.FindB("Test::Zero", false));
48 EXPECT_FALSE(c
.FindB("Test::Worked", false));
49 EXPECT_FALSE(c
.FindB("Test::Zero", false));
51 c
.Set("Test::Zero", true);
52 EXPECT_TRUE(c
.FindB("Test::Zero", false));
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));
60 TEST(CommandLineTest
, BoolParsing
)
62 CommandLine::Args Args
[] = {
63 { 't', 0, "Test::Worked", 0 },
67 CommandLine
CmdL(Args
, &c
);
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
74 char const * argv
[] = { "show", "-t", "0ad" };
75 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
77 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "0ad");
81 char const * argv
[] = { "show", "-t", "0", "ad" };
82 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
84 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "ad");