summaryrefslogtreecommitdiff
path: root/test/libapt/pattern_test.cc
blob: de2fbceb92cc8a700c47c6ae84b5074ee4733429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * cachefilter-patterns.h - Pattern parser and additional patterns as matchers
 *
 * Copyright (c) 2019 Canonical Ltd
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include <config.h>
#include <apt-pkg/cachefilter-patterns.h>
#include <apt-pkg/cachefilter.h>

#include <gtest/gtest.h>

using namespace APT::Internal;

TEST(TreeParserTest, ParseWord)
{
   auto node = PatternTreeParser("word").parseTop();
   auto wordNode = dynamic_cast<PatternTreeParser::WordNode *>(node.get());

   EXPECT_EQ(node.get(), wordNode);
   EXPECT_EQ(wordNode->word, "word");
}

TEST(TreeParserTest, ParseQuotedWord)
{
   auto node = PatternTreeParser("\"a word\"").parseTop();
   auto wordNode = dynamic_cast<PatternTreeParser::WordNode *>(node.get());

   EXPECT_EQ(node.get(), wordNode);
   EXPECT_EQ(wordNode->word, "a word");
}

TEST(TreeParserTest, ParsePattern)
{
   auto node = PatternTreeParser("?hello").parseTop();
   auto patternNode = dynamic_cast<PatternTreeParser::PatternNode *>(node.get());

   EXPECT_EQ(node.get(), patternNode);
   EXPECT_EQ(patternNode->term, "?hello");
   EXPECT_TRUE(patternNode->arguments.empty());
   EXPECT_FALSE(patternNode->haveArgumentList);
}

TEST(TreeParserTest, ParseWithEmptyArgs)
{
   auto node = PatternTreeParser("?hello()").parseTop();
   auto patternNode = dynamic_cast<PatternTreeParser::PatternNode *>(node.get());

   EXPECT_EQ(node.get(), patternNode);
   EXPECT_EQ(patternNode->term, "?hello");
   EXPECT_TRUE(patternNode->arguments.empty());
   EXPECT_TRUE(patternNode->haveArgumentList);
}

TEST(TreeParserTest, ParseWithOneArgs)
{
   auto node = PatternTreeParser("?hello(foo)").parseTop();
   auto patternNode = dynamic_cast<PatternTreeParser::PatternNode *>(node.get());

   EXPECT_EQ(node.get(), patternNode);
   EXPECT_EQ(patternNode->term, "?hello");
   EXPECT_EQ(1u, patternNode->arguments.size());
}

TEST(TreeParserTest, ParseWithManyArgs)
{
   auto node = PatternTreeParser("?hello(foo,bar)").parseTop();
   auto patternNode = dynamic_cast<PatternTreeParser::PatternNode *>(node.get());

   EXPECT_EQ(node.get(), patternNode);
   EXPECT_EQ(patternNode->term, "?hello");
   EXPECT_EQ(2u, patternNode->arguments.size());
}

TEST(TreeParserTest, ParseWithManyArgsWithSpaces)
{
   auto node = PatternTreeParser("?hello (foo, bar)").parseTop();
   auto patternNode = dynamic_cast<PatternTreeParser::PatternNode *>(node.get());

   EXPECT_EQ(node.get(), patternNode);
   EXPECT_EQ(patternNode->term, "?hello");
   EXPECT_EQ(2u, patternNode->arguments.size());
}

TEST(TreeParserTest, ParseWithManyArgsWithSpacesWithTrailingComma)
{
   auto node = PatternTreeParser("?hello (foo, bar,)").parseTop();
   auto patternNode = dynamic_cast<PatternTreeParser::PatternNode *>(node.get());

   EXPECT_EQ(node.get(), patternNode);
   EXPECT_EQ(patternNode->term, "?hello");
   EXPECT_EQ(2u, patternNode->arguments.size());
}