commandline_test.cc 2.5 KB

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