forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalsTest.java
More file actions
276 lines (228 loc) · 9.22 KB
/
Copy pathLexicalsTest.java
File metadata and controls
276 lines (228 loc) · 9.22 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (c) 2016-2020 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
* which accompanies this distribution, and is available at
* http://opensource.org/licenses/BSD-3-Clause
*
* Contributors:
* Chapman Flack
*/
package org.postgresql.pljava;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.InputMismatchException;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static
org.postgresql.pljava.sqlgen.Lexicals.ISO_AND_PG_IDENTIFIER_CAPTURING;
import static
org.postgresql.pljava.sqlgen.Lexicals.SEPARATOR;
import static
org.postgresql.pljava.sqlgen.Lexicals.PG_OPERATOR;
import static org.postgresql.pljava.sqlgen.Lexicals.identifierFrom;
import static org.postgresql.pljava.sqlgen.Lexicals.separator;
import org.postgresql.pljava.sqlgen.Lexicals.Identifier;
import org.postgresql.pljava.sqlgen.Lexicals.Identifier.Simple;
import org.postgresql.pljava.sqlgen.Lexicals.Identifier.Operator;
import org.postgresql.pljava.sqlgen.Lexicals.Identifier.Qualified;
public class LexicalsTest extends TestCase
{
public LexicalsTest(String name) { super(name); }
public void testSeparator() throws Exception
{
Pattern allTheRest = Pattern.compile(".*", Pattern.DOTALL);
Matcher m = SEPARATOR.matcher("no starting separator");
assertFalse("separator 0", separator(m, true));
m.usePattern(allTheRest).matches();
assertEquals("no starting separator", m.group(0));
m.reset();
assertFalse("separator 1", separator(m, false));
m.usePattern(allTheRest).matches();
assertEquals("no starting separator", m.group(0));
m.reset(" simple separator");
assertTrue("separator 2", separator(m, true));
m.usePattern(allTheRest).matches();
assertEquals("simple separator", m.group(0));
m.reset();
assertFalse("separator 3", separator(m, false));
m.usePattern(allTheRest).matches();
assertEquals("simple separator", m.group(0));
m.reset(" \n simple separator");
assertTrue("separator 4", separator(m, true));
m.usePattern(allTheRest).matches();
assertEquals("simple separator", m.group(0));
m.reset();
assertTrue("separator 5", separator(m, false));
m.usePattern(allTheRest).matches();
assertEquals("simple separator", m.group(0));
m.reset(" -- a simple comment\nsimple comment");
assertTrue("separator 6", separator(m, true));
m.usePattern(allTheRest).matches();
assertEquals("simple comment", m.group(0));
m.reset();
assertTrue("separator 7", separator(m, false));
m.usePattern(allTheRest).matches();
assertEquals("simple comment", m.group(0));
m.reset("/* a bracketed comment\n */ bracketed comment");
assertTrue("separator 8", separator(m, true));
m.usePattern(allTheRest).matches();
assertEquals("bracketed comment", m.group(0));
m.reset();
assertFalse("separator 9", separator(m, false));
m.usePattern(allTheRest).matches();
assertEquals("bracketed comment", m.group(0));
m.reset("/* a /* nested */ comment\n */ nested comment");
assertTrue("separator 10", separator(m, true));
m.usePattern(allTheRest).matches();
assertEquals("nested comment", m.group(0));
m.reset();
assertFalse("separator 11", separator(m, false));
m.usePattern(allTheRest).matches();
assertEquals("nested comment", m.group(0));
m.reset("/* an /* unclosed */ comment\n * / unclosed comment");
try
{
separator(m, true);
fail("unclosed comment not detected");
}
catch ( Exception ex )
{
assertTrue("separator 12", ex instanceof InputMismatchException);
}
m.reset("/* -- tricky \n */ nested comment");
assertTrue("separator 13", separator(m, true));
m.usePattern(allTheRest).matches();
assertEquals("nested comment", m.group(0));
m.reset();
assertFalse("separator 14", separator(m, false));
m.usePattern(allTheRest).matches();
assertEquals("nested comment", m.group(0));
m.reset("-- /* tricky \n */ nested comment");
assertTrue("separator 15", separator(m, true));
m.usePattern(allTheRest).matches();
assertEquals("*/ nested comment", m.group(0));
m.reset();
assertTrue("separator 16", separator(m, false));
m.usePattern(allTheRest).matches();
assertEquals("*/ nested comment", m.group(0));
}
public void testIdentifierFrom() throws Exception
{
Matcher m = ISO_AND_PG_IDENTIFIER_CAPTURING.matcher("anIdentifier");
assertTrue("i", m.matches());
assertEquals("anIdentifier", identifierFrom(m).nonFolded());
m.reset("\"an\"\"Identifier\"\"\"");
assertTrue("xd", m.matches());
assertEquals("an\"Identifier\"", identifierFrom(m).nonFolded());
m.reset("u&\"an\\0049dent\"\"if\\+000069er\"");
assertTrue("xui2", m.matches());
assertEquals("anIdent\"ifier", identifierFrom(m).nonFolded());
m.reset("u&\"an@@\"\"@0049dent@+000069fier\"\"\" uescape '@'");
assertTrue("xui3", m.matches());
assertEquals("an@\"Identifier\"", identifierFrom(m).nonFolded());
}
public void testIdentifierEquivalence() throws Exception
{
Identifier baß = Simple.from("baß", false);
Identifier Baß = Simple.from("Baß", false);
Identifier bass = Simple.from("bass", false);
Identifier BASS = Simple.from("BASS", false);
Identifier qbaß = Simple.from("baß", true);
Identifier qBaß = Simple.from("Baß", true);
Identifier qbass = Simple.from("bass", true);
Identifier qBASS = Simple.from("BASS", true);
Identifier sab = Simple.from("sopran alt baß", true);
Identifier SAB = Simple.from("Sopran Alt Baß", true);
/* DESERET SMALL LETTER OW */
Identifier ow = Simple.from("\uD801\uDC35", false);
/* DESERET CAPITAL LETTER OW */
Identifier OW = Simple.from("\uD801\uDC0D", false);
assertEquals("hash1", baß.hashCode(), Baß.hashCode());
assertEquals("hash2", baß.hashCode(), bass.hashCode());
assertEquals("hash3", baß.hashCode(), BASS.hashCode());
assertEquals("hash4", baß.hashCode(), qbaß.hashCode());
assertEquals("hash5", baß.hashCode(), qBaß.hashCode());
assertEquals("hash6", baß.hashCode(), qbass.hashCode());
assertEquals("hash7", baß.hashCode(), qBASS.hashCode());
assertEquals("hash8", ow.hashCode(), OW.hashCode());
assertEquals("eq1", baß, Baß);
assertEquals("eq2", baß, bass);
assertEquals("eq3", baß, BASS);
assertEquals("eq4", Baß, qbaß);
assertEquals("eq5", Baß, qBASS);
assertEquals("eq6", ow, OW);
assertThat("ne1", Baß, not(equalTo(qBaß)));
assertThat("ne2", Baß, not(equalTo(qbass)));
assertThat("ne3", sab, not(equalTo(SAB)));
}
public void testIdentifierSimpleFrom() throws Exception
{
Identifier.Simple s1 = Simple.fromJava("aB");
Identifier.Simple s2 = Simple.fromJava("\"ab\"");
Identifier.Simple s3 = Simple.fromJava("\"A\"\"b\"");
Identifier.Simple s4 = Simple.fromJava("A\"b");
Identifier.Simple s5 = Simple.fromCatalog("ab");
Identifier.Simple s6 = Simple.fromCatalog("AB");
Identifier.Simple s7 = Simple.fromCatalog("A\"b");
assertEquals("eq1", s1, s2);
assertEquals("eq2", s3, s4);
assertEquals("eq3", s1, s5);
assertEquals("eq4", s2, s5);
assertEquals("eq5", s1, s6);
assertEquals("eq6", s5, s6);
assertEquals("eq7", s3, s7);
assertThat("ne1", s2, not(equalTo(s6)));
assertEquals("deparse1", s1.toString(), "aB");
assertEquals("deparse2", s2.toString(), "\"ab\"");
assertEquals("deparse3", s3.toString(), "\"A\"\"b\"");
assertEquals("deparse4", s4.toString(), "\"A\"\"b\"");
assertEquals("deparse5", s5.toString(), "ab");
assertEquals("deparse6", s6.toString(), "\"AB\"");
assertEquals("deparse7", s7.toString(), "\"A\"\"b\"");
}
public void testOperatorPattern() throws Exception
{
Matcher m = PG_OPERATOR.matcher("+");
assertTrue("+", m.matches());
assertTrue("-", m.reset("-").matches());
assertFalse("--", m.reset("--").matches());
assertFalse("/-", m.reset("/-").matches());
assertTrue("@-", m.reset("@-").matches());
assertTrue("@_--", m.reset("@--").lookingAt());
assertEquals("eq1", m.group(), "@");
assertTrue("@/", m.reset("@/").lookingAt());
assertEquals("eq2", m.group(), "@/");
assertTrue("@_/*", m.reset("@/*").lookingAt());
assertEquals("eq3", m.group(), "@");
assertTrue("+_-", m.reset("+-").lookingAt());
assertEquals("eq4", m.group(), "+");
assertTrue("-_+", m.reset("-+").lookingAt());
assertEquals("eq5", m.group(), "-");
assertFalse("--+", m.reset("--+").lookingAt());
assertTrue("-_++", m.reset("-++").lookingAt());
assertEquals("eq6", m.group(), "-");
assertTrue("**_-++", m.reset("**-++").lookingAt());
assertEquals("eq7", m.group(), "**");
assertTrue("*!*-++", m.reset("*!*-++").lookingAt());
assertEquals("eq8", m.group(), "*!*-++");
}
public void testIdentifierOperatorFrom() throws Exception
{
Operator o1 = Operator.from("!@#%*");
Operator o2 = Operator.from("!@#%*");
assertEquals("eq1", o1, o2);
assertEquals("eq2", o1.toString(), "!@#%*");
Simple s1 = Simple.from("foo", false);
Qualified q1 = o1.withQualifier(null);
assertEquals("eq3", q1.toString(), o1.toString());
Qualified q2 = o1.withQualifier(s1);
assertEquals("eq4", q2.toString(), "OPERATOR(foo.!@#%*)");
Simple s2 = Simple.from("foo", true);
Qualified q3 = o1.withQualifier(s2);
assertEquals("eq5", q3.toString(), "OPERATOR(\"foo\".!@#%*)");
}
}