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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author <a href="mailto:magnarn@yahoo-inc.com">Magnar Nedland</a>
*/
public class PredicateParserTest {
@Test
public void requireThatParseErrorThrowsException() {
try {
Predicate.fromString("a in b");
fail("Expected an exception");
} catch (IllegalArgumentException e) {
assertEquals("line 1:5 no viable alternative at input 'b'", e.getMessage());
}
}
@Test
public void requireThatLexerErrorThrowsException() {
try {
Predicate.fromString("a-b in [b]");
fail("Expected an exception");
} catch (IllegalArgumentException e) {
assertEquals("line 1:2 no viable alternative at character 'b'", e.getMessage());
}
}
@Test
public void requireThatSingleValueLeafNodesParse() {
assertParsesTo("a in [b]", "a in [b]");
assertParsesTo("0 in [1]", "0 in [1]");
assertParsesTo("in in [in]", "in in [in]");
assertParsesTo("and in [or]", "and in [or]");
assertParsesTo("not in [not]", "not in [not]");
assertParsesTo("'-234' in ['+200']", "'-234' in ['+200']");
assertParsesTo("string in ['!@#$%^&*()[]']", "'string' in ['!@#$%^&*()[]']");
assertParsesTo("a in [b]", "a in [b]");
assertParsesTo("string in ['foo\\\\_\"\\t\\n\\f\\rbar']",
"string in ['foo\\\\_\\x22\\t\\n\\f\\rbar']");
assertParsesTo("'\\xC3\\xB8' in [b]", "'ø' in [b]");
assertParsesTo("'\\xEF\\xBF\\xBD' in [b]", "'\\xf8' in [b]");
assertParsesTo("'\\xEF\\xBF\\xBD' in [b]", "'\\xef\\xbf\\xbd' in ['b']");
assertParsesTo("'\\xE4\\xB8\\x9C\\xE8\\xA5\\xBF' in ['\\xE8\\x87\\xAA\\xE8\\xA1\\x8C\\xE8\\xBD\\xA6']",
"'东西' in ['自行车']");
assertParsesTo("true in [false]", "true in [false]");
}
@Test
public void requireThatMultiValueLeafNodesParse() {
assertParsesTo("a in [b]", "a in [b]");
assertParsesTo("0 in [1]", "0 in [1]");
assertParsesTo("in in [and, in]", "in in [in, and]");
assertParsesTo("a in [b, c, d, e, f]", "'a' in ['b', 'c', 'd', 'e', 'f']");
}
@Test
public void requireThatBothSingleAndDoubleQuotesWork() {
assertParsesTo("a in [b]", "'a' in ['b']");
assertParsesTo("a in [b]", "\"a\" in [\"b\"]");
assertParsesTo("'a\\x27' in [b]", "'a\\'' in ['b']");
assertParsesTo("'a\"' in [b]", "\"a\\\"\" in [\"b\"]");
}
@Test
public void requireThatRangeLeafNodesParse() {
assertParsesTo("a in [0..100]", "a in [0..100]");
assertParsesTo("0 in [..100]", "0 in [..100]");
assertParsesTo("0 in [0..]", "0 in [0..]");
assertParsesTo("0 in [..]", "0 in [..]");
assertParsesTo("a in [-100..100]", "a in [-100..+100]");
assertParsesTo("a in [-9223372036854775808..9223372036854775807]",
"a in [-9223372036854775808..+9223372036854775807]");
}
@Test
public void requireThatRangePartitionsAreIgnored() {
assertParsesTo("a in [0..100]", "a in [0..100 (a=0-99,a=100+[..0])]");
assertParsesTo("a in [-100..0]", "a in [-100..0 (a=-0-99,a=-100+[..0])]");
assertParsesTo("a in [-9223372036854775808..0]", "a in [-9223372036854775808..0 (a=-0-9223372036854775808)]");
assertParsesTo("a in [2..8]", "a in [2..8 (a=0+[2..8])]");
assertParsesTo("a in [0..8]", "a in [0..8 (a=0+[..8])]");
assertParsesTo("a in [2..9]", "a in [2..9 (a=0+[2..])]");
}
@Test
public void requireThatNotInSetWorks() {
assertParsesTo("a not in [b]", "a not in [b]");
}
@Test
public void requireThatConjunctionWorks() {
assertParsesTo("a in [b] and c in [d]", "a in [b] and c in [d]");
assertParsesTo("a in [b] and c in [d] and e in [f]", "a in [b] and c in [d] and e in [f]");
}
@Test
public void requireThatDisjunctionWorks() {
assertParsesTo("a in [b] or c in [d]", "a in [b] or c in [d]");
assertParsesTo("a in [b] or c in [d] or e in [f]", "a in [b] or c in [d] or e in [f]");
}
@Test
public void requireThatParenthesesWorks() {
assertParsesTo("a in [b] or c in [d]",
"(a in [b]) or (c in [d])");
assertParsesTo("a in [b] or c in [d] or e in [f]",
"(((a in [b]) or c in [d]) or e in [f])");
assertParsesTo("(a in [b] and c in [d]) or e in [f]",
"a in [b] and c in [d] or e in [f]");
assertParsesTo("a in [b] and (c in [d] or e in [f])",
"a in [b] and (c in [d] or e in [f])");
assertParsesTo("a in [b] and (c in [d] or e in [f]) and g in [h]",
"a in [b] and (c in [d] or e in [f]) and g in [h]");
}
@Test
public void requireThatNotOutsideParenthesesWorks() {
assertParsesTo("a not in [b]", "not (a in [b])");
}
@Test
public void requireThatConjunctionsCanGetMoreThanTwoChildren() {
Predicate p = Predicate.fromString("a in [b] and c in [d] and e in [f] and g in [h]");
assertTrue(p instanceof Conjunction);
assertEquals(4, ((Conjunction)p).getOperands().size());
}
@Test
public void requireThatDisjunctionsCanGetMoreThanTwoChildren() {
Predicate p = Predicate.fromString("a in [b] or c in [d] or e in [f] or g in [h]");
assertTrue(p instanceof Disjunction);
assertEquals(4, ((Disjunction)p).getOperands().size());
}
@Test
public void requireThatBooleanCanBeParsed() {
assertParsesTo("true", "true");
assertParsesTo("false", "false");
assertParsesTo("true or false", "true or false");
assertParsesTo("false and true", "false and true");
}
private static void assertParsesTo(String expected, String predicate_str) {
assertEquals(expected, // TODO: Predicate.fromString(expected)
Predicate.fromString(predicate_str).toString());
}
}
|