Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/jdk/jshell/EditorTestBase.java
40931 views
1
/*
2
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.util.concurrent.ExecutorService;
25
import java.util.concurrent.Executors;
26
import java.util.function.Consumer;
27
28
import org.testng.annotations.Test;
29
30
import static org.testng.Assert.assertEquals;
31
import static org.testng.Assert.assertTrue;
32
33
public abstract class EditorTestBase extends ReplToolTesting {
34
35
private static ExecutorService executor;
36
37
public abstract void writeSource(String s);
38
public abstract String getSource();
39
public abstract void accept();
40
public abstract void exit();
41
public abstract void cancel();
42
public abstract void shutdownEditor();
43
44
public void testEditor(ReplTest... tests) {
45
testEditor(false, new String[]{"--no-startup"}, tests);
46
}
47
48
public void testEditor(boolean defaultStartup, String[] args, ReplTest... tests) {
49
test(defaultStartup, args, tests);
50
}
51
52
abstract void assertEdit(boolean after, String cmd,
53
Consumer<String> checkInput, Consumer<String> checkOutput, Action action);
54
55
void assertEditInput(boolean after, String cmd, Consumer<String> checkInput, Action action) {
56
assertEdit(after, cmd, checkInput, s -> {}, action);
57
}
58
59
void assertEditOutput(boolean after, String cmd, Consumer<String> checkOutput, Action action) {
60
assertEdit(after, cmd, s -> {}, checkOutput, action);
61
}
62
63
void assertEditInput(boolean after, String cmd, String input, Action action) {
64
assertEditInput(after, cmd, s -> assertEquals(s, input, "Input"), action);
65
}
66
67
void assertEditOutput(boolean after, String cmd, String output, Action action) {
68
assertEditOutput(after, cmd, s -> assertEquals(s.trim(), output.trim(), "command"), action);
69
}
70
71
@Test
72
public void testEditNegative() {
73
for (String edit : new String[] {"/ed", "/edit"}) {
74
test(new String[]{"--no-startup"},
75
a -> assertCommandOutputStartsWith(a, edit + " 1",
76
"| No snippet with ID: 1"),
77
a -> assertCommandOutputStartsWith(a, edit + " unknown",
78
"| No such snippet: unknown")
79
);
80
}
81
}
82
83
@Test
84
public void testDoNothing() {
85
testEditor(
86
a -> assertVariable(a, "int", "a", "0", "0"),
87
a -> assertEditOutput(a, "/ed 1", "", this::exit),
88
a -> assertCommandCheckOutput(a, "/v", assertVariables())
89
);
90
}
91
92
@Test
93
public void testEditVariable1() {
94
testEditor(
95
a -> assertVariable(a, "int", "a", "0", "0"),
96
a -> assertEditOutput(a, "/ed 1", "a ==> 10", () -> {
97
writeSource("\n\n\nint a = 10;\n\n\n");
98
exit();
99
loadVariable(true, "int", "a", "10", "10");
100
}),
101
a -> assertEditOutput(a, "/ed 1", "a ==> 15", () -> {
102
writeSource("int a = 15;");
103
exit();
104
loadVariable(true, "int", "a", "15", "15");
105
}),
106
a -> assertCommandCheckOutput(a, "/v", assertVariables())
107
);
108
}
109
110
@Test
111
public void testEditVariable2() {
112
testEditor(
113
a -> assertVariable(a, "int", "a", "0", "0"),
114
a -> assertEditOutput(a, "/ed 1", "b ==> 10", () -> {
115
writeSource("int b = 10;");
116
exit();
117
loadVariable(true, "int", "b", "10", "10");
118
}),
119
a -> assertEditOutput(a, "/ed 1", "a ==> 15", () -> {
120
writeSource("int a = 15;");
121
exit();
122
loadVariable(true, "int", "a", "15", "15");
123
}),
124
a -> assertCommandCheckOutput(a, "/v", assertVariables())
125
);
126
}
127
128
@Test
129
public void testWriteVariables() {
130
testEditor(
131
a -> assertEditOutput(a, "/edit",
132
"x ==> 1\n" +
133
"y ==> 2\n" +
134
"z ==> 3",
135
() -> {
136
writeSource(
137
"var x = 1;\n" +
138
"var y = 2;\n" +
139
"var z = 3;\n");
140
exit();
141
}),
142
a -> assertCommand(a, "z", "z ==> 3")
143
);
144
}
145
146
public void testEditClass1() {
147
testEditor(
148
a -> assertClass(a, "class A {}", "class", "A"),
149
a -> assertEditOutput(a, "/ed 1", "", () -> {
150
writeSource("\n\n\nclass A {}\n\n\n");
151
exit();
152
loadClass(true, "class A {}", "class", "A");
153
}),
154
a -> assertEditOutput(a, "/ed 1",
155
"| replaced enum A", () -> {
156
writeSource("enum A {}");
157
exit();
158
loadClass(true, "enum A {}", "enum", "A");
159
}),
160
a -> assertCommandCheckOutput(a, "/types", assertClasses())
161
);
162
}
163
164
@Test
165
public void testEditClass2() {
166
testEditor(
167
a -> assertClass(a, "class A {}", "class", "A"),
168
a -> assertEditOutput(a, "/ed 1", "| created class B", () -> {
169
writeSource("class B { }");
170
exit();
171
loadClass(true, "class B {}", "class", "B");
172
}),
173
a -> assertEditOutput(a, "/ed 1",
174
"| replaced enum A", () -> {
175
writeSource("enum A {}");
176
exit();
177
loadClass(true, "enum A {}", "enum", "A");
178
}),
179
a -> assertCommandCheckOutput(a, "/types", assertClasses())
180
);
181
}
182
183
public void testEditMethod1() {
184
testEditor(
185
a -> assertMethod(a, "void f() {}", "()void", "f"),
186
a -> assertEditOutput(a, "/ed 1", "", () -> {
187
writeSource("\n\n\nvoid f() {}\n\n\n");
188
exit();
189
loadMethod(true, "void f() {}", "()void", "f");
190
}),
191
a -> assertEditOutput(a, "/ed 1",
192
"| replaced method f()", () -> {
193
writeSource("double f() { return 0; }");
194
exit();
195
loadMethod(true, "double f() { return 0; }", "()double", "f");
196
}),
197
a -> assertCommandCheckOutput(a, "/m", assertMethods())
198
);
199
}
200
201
@Test
202
public void testEditMethod2() {
203
testEditor(
204
a -> assertMethod(a, "void f() {}", "()void", "f"),
205
a -> assertEditOutput(a, "/ed 1", "| created method g()", () -> {
206
writeSource("void g() {}");
207
exit();
208
loadMethod(true, "void g() {}", "()void", "g");
209
}),
210
a -> assertEditOutput(a, "/ed 1",
211
"| replaced method f()", () -> {
212
writeSource("double f() { return 0; }");
213
exit();
214
loadMethod(true, "double f() { return 0; }", "()double", "f");
215
}),
216
a -> assertCommandCheckOutput(a, "/m", assertMethods())
217
);
218
}
219
220
@Test
221
public void testNoArguments() {
222
testEditor(
223
a -> assertVariable(a, "int", "a"),
224
a -> assertMethod(a, "void f() {}", "()void", "f"),
225
a -> assertClass(a, "class A {}", "class", "A"),
226
a -> assertEditInput(a, "/ed", s -> {
227
String[] ss = s.split("\n");
228
assertEquals(ss.length, 3, "Expected 3 lines: " + s);
229
assertEquals(ss[0], "int a;");
230
assertEquals(ss[1], "void f() {}");
231
assertEquals(ss[2], "class A {}");
232
}, this::exit)
233
);
234
}
235
236
@Test
237
public void testStartup() {
238
testEditor(true, new String[0],
239
a -> assertEditInput(a, "/ed", s -> assertTrue(s.isEmpty(), "Checking of startup: " + s), this::cancel),
240
a -> assertEditInput(a, "/ed s1", assertStartsWith("import"), this::cancel));
241
}
242
243
@Test
244
public void testCancel() {
245
testEditor(
246
a -> assertVariable(a, "int", "a"),
247
a -> assertEditOutput(a, "/ed a", "", () -> {
248
writeSource("int b = 10");
249
cancel();
250
})
251
);
252
}
253
254
@Test
255
public void testAccept() {
256
testEditor(
257
a -> assertVariable(a, "int", "a"),
258
a -> assertEditOutput(a, "/ed a", "b ==> 10", () -> {
259
writeSource("int b = 10");
260
accept();
261
exit();
262
})
263
);
264
}
265
266
@Test(enabled = false) // TODO JDK-8191875
267
public void testStatementMush() {
268
testEditor(
269
a -> assertCommand(a, "System.out.println(\"Hello\")",
270
"", "", null, "Hello\n", ""),
271
a -> assertEditOutput(a, "/ed", "b ==> 10", () -> {
272
writeSource(getSource() + "\nint b = 10");
273
exit();
274
}),
275
276
//TODO: this is a work-around to JDK-8170369
277
a -> assertCommand(a, "1234",
278
null, "", null, null, "")
279
);
280
}
281
282
public static ExecutorService getExecutor() {
283
if (executor == null) {
284
executor = Executors.newSingleThreadExecutor();
285
}
286
return executor;
287
}
288
289
public static void executorShutdown() {
290
if (executor != null) {
291
executor.shutdown();
292
executor = null;
293
}
294
}
295
296
interface Action {
297
void accept();
298
}
299
}
300
301