commandline_test.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. }