summaryrefslogtreecommitdiff
path: root/test/libapt
diff options
context:
space:
mode:
authorJulian Andres Klode <julian.klode@canonical.com>2020-01-20 14:14:49 +0100
committerJulian Andres Klode <julian.klode@canonical.com>2020-02-03 12:55:54 +0100
commitfd43b1694f1382a3a47f5dc546ebe3d39fcd6e7d (patch)
treef8a54edcd442f81a69c9603f4c6bdfb722af95f0 /test/libapt
parentd9c4c1d88ae6c42fee0f2da3a5b9669187fc2fc5 (diff)
Implement short patterns (patterns starting with ~)
Also make pattern detector in cacheset and private's list accept such patterns. We probably should just try to parse and see if it is a (start of a) pattern.
Diffstat (limited to 'test/libapt')
-rw-r--r--test/libapt/pattern_test.cc66
1 files changed, 66 insertions, 0 deletions
diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc
index de2fbceb9..492a29eac 100644
--- a/test/libapt/pattern_test.cc
+++ b/test/libapt/pattern_test.cc
@@ -93,3 +93,69 @@ TEST(TreeParserTest, ParseWithManyArgsWithSpacesWithTrailingComma)
EXPECT_EQ(patternNode->term, "?hello");
EXPECT_EQ(2u, patternNode->arguments.size());
}
+
+// Helper
+static bool samePattern(const std::unique_ptr<PatternTreeParser::Node> &a, const std::unique_ptr<PatternTreeParser::Node> &b)
+{
+ auto pa = dynamic_cast<const PatternTreeParser::PatternNode *>(a.get());
+ auto pb = dynamic_cast<const PatternTreeParser::PatternNode *>(b.get());
+
+ if (pa && pb)
+ {
+ if (pa->term != pb->term || pa->haveArgumentList != pb->haveArgumentList || pa->arguments.size() != pb->arguments.size())
+ return false;
+
+ for (size_t i = 0; i < pa->arguments.size(); i++)
+ {
+ if (!samePattern(pa->arguments[i], pb->arguments[i]))
+ return false;
+ }
+ return true;
+ }
+
+ auto wa = dynamic_cast<const PatternTreeParser::WordNode *>(a.get());
+ auto wb = dynamic_cast<const PatternTreeParser::WordNode *>(b.get());
+ if (wa && wb)
+ return wa->word == wb->word && wa->quoted == wb->quoted;
+
+ return false;
+}
+
+#define EXPECT_PATTERN_EQ(shrt, lng) \
+ EXPECT_TRUE(samePattern(PatternTreeParser(shrt).parseTop(), PatternTreeParser(lng).parseTop()))
+#define EXPECT_PATTERN_EQ_ATOMIC(shrt, lng) \
+ EXPECT_TRUE(PatternTreeParser(shrt).parseTop()); \
+ caught = false; \
+ try \
+ { \
+ PatternTreeParser(shrt "XXX").parseTop(); \
+ } \
+ catch (PatternTreeParser::Error & e) \
+ { \
+ caught = true; \
+ }; \
+ EXPECT_TRUE(caught) << shrt "XXX should have thrown an exception"; \
+ EXPECT_PATTERN_EQ(shrt, lng)
+
+TEST(TreeParserTest, ParseShortPattern)
+{
+ bool caught;
+ EXPECT_PATTERN_EQ("~ramd64", "?architecture(amd64)");
+ EXPECT_PATTERN_EQ("~AanArchive", "?archive(anArchive)");
+ EXPECT_PATTERN_EQ_ATOMIC("~M", "?automatic");
+ EXPECT_PATTERN_EQ_ATOMIC("~b", "?broken");
+ EXPECT_PATTERN_EQ_ATOMIC("~c", "?config-files");
+ EXPECT_PATTERN_EQ_ATOMIC("~E", "?essential");
+ EXPECT_PATTERN_EQ_ATOMIC("~F", "?false");
+ EXPECT_PATTERN_EQ_ATOMIC("~g", "?garbage");
+ EXPECT_PATTERN_EQ_ATOMIC("~i", "?installed");
+ EXPECT_PATTERN_EQ("~napt", "?name(apt)");
+ EXPECT_PATTERN_EQ_ATOMIC("~o", "?obsolete");
+ EXPECT_PATTERN_EQ("~Obar", "?origin(bar)");
+ EXPECT_PATTERN_EQ("~sfoo", "?section(foo)");
+ EXPECT_PATTERN_EQ("~esourcename", "?source-package(sourcename)");
+ EXPECT_PATTERN_EQ_ATOMIC("~T", "?true");
+ EXPECT_PATTERN_EQ_ATOMIC("~U", "?upgradable");
+ EXPECT_PATTERN_EQ("~Vverstr", "?version(verstr)");
+ EXPECT_PATTERN_EQ_ATOMIC("~v", "?virtual");
+}