Skip to content

Commit a9b37d5

Browse files
gabriele-tomassettiadamretter
authored andcommitted
[test] Add test for Count Expression
1 parent c7dcfec commit a9b37d5

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* eXist-db Open Source Native XML Database
3+
* Copyright (C) 2001 The eXist-db Authors
4+
*
5+
6+
* http://www.exist-db.org
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
package org.exist.xquery;
23+
24+
import antlr.RecognitionException;
25+
import antlr.TokenStreamException;
26+
import org.exist.xquery.parser.XQueryAST;
27+
import org.exist.xquery.parser.XQueryLexer;
28+
import org.exist.xquery.parser.XQueryParser;
29+
import org.exist.xquery.parser.XQueryTreeParser;
30+
import org.junit.jupiter.api.Test;
31+
32+
import java.io.StringReader;
33+
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.fail;
36+
37+
/**
38+
* @author <a href="mailto:[email protected]">Adam Retter</a>
39+
* @author <a href="mailto:[email protected]">Gabriele Tomassetti</a>
40+
*/
41+
public class CountExpressionTest {
42+
43+
@Test
44+
public void countTest() throws RecognitionException, XPathException, TokenStreamException {
45+
final String query = "xquery version \"3.1\";\n" +
46+
"for $p in $products\n" +
47+
"order by $p/sales descending\n" +
48+
"count $rank\n" +
49+
"where $rank <= 3\n" +
50+
"return\n" +
51+
" <product rank=\"{$rank}\">\n" +
52+
" {$p/name, $p/sales}\n" +
53+
" </product>";
54+
55+
// parse the query into the internal syntax tree
56+
final XQueryContext context = new XQueryContext();
57+
final XQueryLexer lexer = new XQueryLexer(context, new StringReader(query));
58+
final XQueryParser xparser = new XQueryParser(lexer);
59+
xparser.xpath();
60+
if (xparser.foundErrors()) {
61+
fail(xparser.getErrorMessage());
62+
return;
63+
}
64+
65+
final XQueryAST ast = (XQueryAST) xparser.getAST();
66+
67+
final XQueryTreeParser treeParser = new XQueryTreeParser(context);
68+
final PathExpr expr = new PathExpr(context);
69+
treeParser.xpath(ast, expr);
70+
if (treeParser.foundErrors()) {
71+
fail(treeParser.getErrorMessage());
72+
return;
73+
}
74+
75+
// count keyword
76+
assertEquals(143, ast.getNextSibling().getFirstChild().getNextSibling().getNextSibling().getType());
77+
// rank variable binding
78+
assertEquals(20, ast.getNextSibling().getFirstChild().getNextSibling().getNextSibling().getFirstChild().getType());
79+
}
80+
}

0 commit comments

Comments
 (0)