summaryrefslogtreecommitdiff
path: root/test/plugins/css/CSSParserTest.cpp
blob: 20d0836cd01896230d7d932248b9548025c0b17b (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <gtest/gtest.h>

#include <sstream>

#include <plugins/css/CSSParser.hpp>

namespace ousia {
namespace parser {
namespace css {
TEST(CSSParser, testParseSelectors)
{
	// create a string describing a SelectorTree as input.
	std::stringstream input;
	input << "A>B,A B:r, C#a A[bla=\"blub\"], A::g(4,2,3)";
	/* This should describe the tree:
	 * root_____
	 * | \      \
	 * A  C#a  A::g(4,2,3)
	 * |\    \
	 * B B::r A[bla="blub"]
	 */

	// initialize an empty parser context.
	StandaloneParserContext ctx;

	// parse the input.
	CSSParser instance;
	Rooted<SelectorNode> root = instance.parse(input, ctx).cast<SelectorNode>();

	// we expect three children of the root node overall.
	ASSERT_EQ(3, root->getEdges().size());
	// get all "A" children, which should be two.
	std::vector<Rooted<SelectorNode>> children = root->getChildren("A");
	ASSERT_EQ(2, children.size());
	// assert A
	Rooted<SelectorNode> A = children[0];
	ASSERT_EQ("A", A->getName());
	{
		PseudoSelector select{"true", false};
		ASSERT_EQ(select, A->getPseudoSelector());
	}
	ASSERT_EQ(2, A->getEdges().size());
	ASSERT_FALSE(A->isAccepting());
	{
		// assert A > B
		std::vector<Rooted<SelectorNode>> Achildren =
		    A->getChildren(SelectionOperator::DIRECT_DESCENDANT, "B");
		ASSERT_EQ(1, Achildren.size());
		Rooted<SelectorNode> B = Achildren[0];
		ASSERT_EQ("B", B->getName());
		{
			PseudoSelector select{"true", false};
			ASSERT_EQ(select, B->getPseudoSelector());
		}
		ASSERT_EQ(0, B->getEdges().size());
		ASSERT_TRUE(B->isAccepting());
		// assert A B:r
		Achildren = A->getChildren(SelectionOperator::DESCENDANT, "B");
		ASSERT_EQ(1, Achildren.size());
		Rooted<SelectorNode> Br = Achildren[0];
		ASSERT_EQ("B", Br->getName());
		{
			PseudoSelector select{"r", false};
			ASSERT_EQ(select, Br->getPseudoSelector());
		}
		ASSERT_EQ(0, Br->getEdges().size());
		ASSERT_TRUE(Br->isAccepting());
	}
	// assert C#a
	children = root->getChildren("C");
	ASSERT_EQ(1, children.size());
	Rooted<SelectorNode> C = children[0];
	ASSERT_EQ("C", C->getName());
	{
		PseudoSelector select{"has_id", {"a"}, false};
		ASSERT_EQ(select, C->getPseudoSelector());
	}
	ASSERT_EQ(1, C->getEdges().size());
	ASSERT_FALSE(C->isAccepting());
	{
		// assert C#a A[bla=\"blub\"]
		std::vector<Rooted<SelectorNode>> Cchildren =
		    C->getChildren(SelectionOperator::DESCENDANT, "A");
		ASSERT_EQ(1, Cchildren.size());
		Rooted<SelectorNode> A = Cchildren[0];
		ASSERT_EQ("A", A->getName());
		{
			PseudoSelector select{"has_value", {"bla", "blub"}, false};
			ASSERT_EQ(select, A->getPseudoSelector());
		}
		ASSERT_EQ(0, A->getEdges().size());
		ASSERT_TRUE(A->isAccepting());
	}
	// assert A::g(4,2,3)
	children = root->getChildren("A");
	ASSERT_EQ(2, children.size());
	Rooted<SelectorNode> Ag = children[1];
	ASSERT_EQ("A", Ag->getName());
	{
		PseudoSelector select{"g", {"4", "2", "3"}, true};
		ASSERT_EQ(select, Ag->getPseudoSelector());
	}
	ASSERT_EQ(0, Ag->getEdges().size());
	ASSERT_TRUE(Ag->isAccepting());
}
}
}
}