Skip to content

Commit da10a50

Browse files
authored
Issue #99: Fields wildcard bug (#100)
* remove unnecessary COMMAND_FIELDS_MODE_WILDCARD * fix fields test case; add fields_wildcard test case * some improvements to FieldsSyntaxTests * add more FieldsSyntaxTests & improve naming of tests
1 parent a1e3d8c commit da10a50

4 files changed

Lines changed: 117 additions & 26 deletions

File tree

src/main/antlr4/imports/COMMAND_FIELDS_MODE.g4

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ COMMAND_FIELDS_MODE_BRACKET_R: ']' -> type(BRACKET_R), popMode, popMode;
6161

6262
// tokens
6363

64-
COMMAND_FIELDS_MODE_WILDCARD : '*';
6564
COMMAND_FIELDS_MODE_COMMA : ',' -> type(COMMA);
6665
COMMAND_FIELDS_MODE_MINUS : '-';
6766
COMMAND_FIELDS_MODE_PLUS : '+';

src/test/java/com/teragrep/pth_03/tests/FieldsSyntaxTests.java

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,59 +50,105 @@
5050

5151
import static org.junit.jupiter.api.Assertions.assertEquals;
5252

53+
import org.junit.jupiter.api.Assertions;
5354
import org.junit.jupiter.params.ParameterizedTest;
5455
import org.junit.jupiter.params.provider.ValueSource;
5556
import org.w3c.dom.NodeList;
5657

57-
public class FieldsSyntaxTests {
58+
public final class FieldsSyntaxTests {
5859
@ParameterizedTest(name = "{index} command=''{0}''")
5960
@ValueSource(strings = {
6061
"fields",
6162
"fields2",
63+
"fields_wildcard"
6264
})
63-
public void fieldsSyntaxParseTest(String arg) throws Exception {
64-
String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
65-
ParserSyntaxTestingUtility parserSyntaxTestingUtility
65+
void fieldsSyntaxParseTest(final String arg) throws Exception {
66+
final String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
67+
final ParserSyntaxTestingUtility parserSyntaxTestingUtility
6668
= new ParserSyntaxTestingUtility(fileName, false);
6769
parserSyntaxTestingUtility.syntaxParseTest(arg);
6870
}
6971
@ParameterizedTest
7072
@ValueSource(strings = {
7173
"fields",
74+
"fields2",
75+
"fields_wildcard"
7276
})
73-
void xpathTest1(String arg) throws Exception {
74-
ParserStructureTestingUtility pstu = new ParserStructureTestingUtility();
75-
String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
76-
String xpathExp = "/root/transformStatement/fieldsTransformation";
77+
void testFieldsTransformationExists(final String arg) throws Exception {
78+
final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility();
79+
final String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
80+
final String xpathExp = "/root/transformStatement/fieldsTransformation";
7781

78-
NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false);
82+
final NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false);
7983
// Check that 1 found
80-
assertEquals(1,nodesA.getLength());
84+
Assertions.assertEquals(1,nodesA.getLength());
8185
}
8286
@ParameterizedTest
8387
@ValueSource(strings = {
8488
"fields",
8589
})
86-
void xpathTest2(String arg) throws Exception {
87-
ParserStructureTestingUtility pstu = new ParserStructureTestingUtility();
88-
String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
89-
String xpathExp = "/root/transformStatement/fieldsTransformation/fieldListType/fieldType[1]/value";
90+
void testFieldsInPlusMode(final String arg) throws Exception {
91+
final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility();
92+
final String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
93+
final String xpathExp = "/root/transformStatement/fieldsTransformation/fieldListType/fieldType";
9094

91-
NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false);
92-
// Check that 1 found
93-
assertEquals(1,nodesA.getLength());
95+
final NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false);
96+
// Check that 3 found
97+
Assertions.assertEquals(3,nodesA.getLength());
98+
Assertions.assertEquals("foo_*",nodesA.item(0).getTextContent());
99+
Assertions.assertEquals("bar_*",nodesA.item(1).getTextContent());
100+
Assertions.assertEquals("eawg",nodesA.item(2).getTextContent());
101+
102+
final String xpathExpPlus = "/root/transformStatement/fieldsTransformation/value";
103+
104+
final NodeList nodesB = (NodeList) pstu.xpathQueryFile(fileName, xpathExpPlus, false);
105+
Assertions.assertEquals(2,nodesB.getLength());
106+
Assertions.assertEquals("fields",nodesB.item(0).getTextContent());
107+
Assertions.assertEquals("+",nodesB.item(1).getTextContent());
94108
}
95109
@ParameterizedTest
96110
@ValueSource(strings = {
97111
"fields2",
98112
})
99-
void xpathTest3(String arg) throws Exception {
100-
ParserStructureTestingUtility pstu = new ParserStructureTestingUtility();
101-
String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
102-
String xpathExp = "/root/transformStatement/fieldsTransformation/fieldListType/fieldType[1]/value";
113+
void testFieldsInMinusMode(final String arg) throws Exception {
114+
final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility();
115+
final String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
116+
final String xpathExp = "/root/transformStatement/fieldsTransformation/fieldListType/fieldType";
103117

104-
NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false);
105-
// Check that 1 found
106-
assertEquals(1,nodesA.getLength());
118+
final NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false);
119+
// Check that 2 found
120+
Assertions.assertEquals(2,nodesA.getLength());
121+
Assertions.assertEquals("host",nodesA.item(0).getTextContent());
122+
Assertions.assertEquals("ip",nodesA.item(1).getTextContent());
123+
124+
final String xpathExpPlus = "/root/transformStatement/fieldsTransformation/value";
125+
126+
final NodeList nodesB = (NodeList) pstu.xpathQueryFile(fileName, xpathExpPlus, false);
127+
Assertions.assertEquals(2,nodesB.getLength());
128+
Assertions.assertEquals("fields",nodesB.item(0).getTextContent());
129+
Assertions.assertEquals("-",nodesB.item(1).getTextContent());
130+
}
131+
132+
@ParameterizedTest
133+
@ValueSource(strings = {
134+
"fields_wildcard",
135+
})
136+
void testFieldsWithLonelyWildcard(final String arg) throws Exception {
137+
final ParserStructureTestingUtility pstu = new ParserStructureTestingUtility();
138+
final String fileName = "src/test/resources/antlr4/commands/fields/" + arg + ".txt";
139+
final String xpathExp = "/root/transformStatement/fieldsTransformation/fieldListType/fieldType";
140+
141+
final NodeList nodesA = (NodeList) pstu.xpathQueryFile(fileName, xpathExp, false);
142+
// Check that 3 found
143+
Assertions.assertEquals(3,nodesA.getLength());
144+
Assertions.assertEquals("*",nodesA.item(0).getTextContent());
145+
Assertions.assertEquals("*_foo",nodesA.item(1).getTextContent());
146+
Assertions.assertEquals("bar",nodesA.item(2).getTextContent());
147+
148+
final String xpathExpPlus = "/root/transformStatement/fieldsTransformation/value";
149+
150+
final NodeList nodesB = (NodeList) pstu.xpathQueryFile(fileName, xpathExpPlus, false);
151+
Assertions.assertEquals(1,nodesB.getLength());
152+
Assertions.assertEquals("fields",nodesB.item(0).getTextContent());
107153
}
108154
}

src/test/resources/antlr4/commands/fields/fields.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@
4343
* Teragrep, the applicable Commercial License may apply to this file if you as
4444
* a licensee so wish it.
4545
*/ -->
46-
| fields +foo_*, bar_* eawg
46+
| fields + foo_*, bar_* eawg
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!-- /*
2+
* Teragrep Data Processing Language Parser Library PTH-03
3+
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
17+
*
18+
*
19+
* Additional permission under GNU Affero General Public License version 3
20+
* section 7
21+
*
22+
* If you modify this Program, or any covered work, by linking or combining it
23+
* with other code, such other code is not for that reason alone subject to any
24+
* of the requirements of the GNU Affero GPL version 3 as long as this Program
25+
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
26+
* modifications.
27+
*
28+
* Supplemented terms under GNU Affero General Public License version 3
29+
* section 7
30+
*
31+
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
32+
* versions must be marked as "Modified version of" The Program.
33+
*
34+
* Names of the licensors and authors may not be used for publicity purposes.
35+
*
36+
* No rights are granted for use of trade names, trademarks, or service marks
37+
* which are in The Program if any.
38+
*
39+
* Licensee must indemnify licensors and authors for any liability that these
40+
* contractual assumptions impose on licensors and authors.
41+
*
42+
* To the extent this program is licensed as part of the Commercial versions of
43+
* Teragrep, the applicable Commercial License may apply to this file if you as
44+
* a licensee so wish it.
45+
*/ -->
46+
| fields * *_foo bar

0 commit comments

Comments
 (0)