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
|
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8139829
* @summary test accessors of Snippet
* @build KullaTesting TestingInputStream
* @run testng SnippetTest
*/
import jdk.jshell.Snippet;
import jdk.jshell.DeclarationSnippet;
import org.testng.annotations.Test;
import jdk.jshell.MethodSnippet;
import jdk.jshell.Snippet.Status;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static jdk.jshell.Snippet.Status.VALID;
import static jdk.jshell.Snippet.Status.RECOVERABLE_DEFINED;
import static jdk.jshell.Snippet.Status.OVERWRITTEN;
import static jdk.jshell.Snippet.Status.RECOVERABLE_NOT_DEFINED;
import static jdk.jshell.Snippet.SubKind.*;
@Test
public class SnippetTest extends KullaTesting {
public void testImportKey() {
assertImportKeyMatch("import java.util.List;", "List", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID));
assertImportKeyMatch("import java.util.*;", "java.util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID));
assertImportKeyMatch("import static java.lang.String.*;", "java.lang.String.*", STATIC_IMPORT_ON_DEMAND_SUBKIND, added(VALID));
}
public void testClassKey() {
assertDeclarationKeyMatch("class X {}", false, "X", CLASS_SUBKIND, added(VALID));
}
public void testInterfaceKey() {
assertDeclarationKeyMatch("interface I {}", false, "I", INTERFACE_SUBKIND, added(VALID));
}
public void testEnumKey() {
assertDeclarationKeyMatch("enum E {}", false, "E", ENUM_SUBKIND, added(VALID));
}
public void testAnnotationKey() {
assertDeclarationKeyMatch("@interface A {}", false, "A", ANNOTATION_TYPE_SUBKIND, added(VALID));
}
public void testMethodKey() {
assertDeclarationKeyMatch("void m() {}", false, "m", METHOD_SUBKIND, added(VALID));
}
public void testVarDeclarationKey() {
assertVarKeyMatch("int a;", false, "a", VAR_DECLARATION_SUBKIND, "int", added(VALID));
}
public void testVarDeclarationWithInitializerKey() {
assertVarKeyMatch("double b = 9.0;", true, "b", VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, "double", added(VALID));
}
public void testTempVarExpressionKey() {
assertVarKeyMatch("47;", true, "$1", TEMP_VAR_EXPRESSION_SUBKIND, "int", added(VALID));
}
public void testVarValueKey() {
assertEval("double x = 4;", "4.0");
assertExpressionKeyMatch("x;", "x", VAR_VALUE_SUBKIND, "double");
}
public void testAssignmentKey() {
assertEval("int y;");
assertExpressionKeyMatch("y = 4;", "y", ASSIGNMENT_SUBKIND, "int");
}
public void testStatementKey() {
assertKeyMatch("if (true) {}", true, STATEMENT_SUBKIND, added(VALID));
assertKeyMatch("while (true) { break; }", true, STATEMENT_SUBKIND, added(VALID));
assertKeyMatch("do { } while (false);", true, STATEMENT_SUBKIND, added(VALID));
assertKeyMatch("for (;;) { break; }", true, STATEMENT_SUBKIND, added(VALID));
}
public void noKeys() {
assertActiveKeys(new DeclarationSnippet[0]);
}
public void testKeyId1() {
Snippet a = classKey(assertEval("class A { }"));
assertEval("void f() { }");
assertEval("int f;");
assertEval("interface A { }",
ste(MAIN_SNIPPET, VALID, VALID, true, null),
ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
assertKeys(method("()void", "f"), variable("int", "f"), clazz(KullaTesting.ClassType.INTERFACE, "A"));
assertActiveKeys();
}
@Test(enabled = false) // TODO 8081689
public void testKeyId2() {
Snippet g = methodKey(assertEval("void g() { f(); }", added(RECOVERABLE_DEFINED)));
Snippet f = methodKey(assertEval("void f() { }",
added(VALID),
ste(g, RECOVERABLE_DEFINED, VALID, false, null)));
assertEval("int f;");
assertEval("interface A { }");
assertKeys(method("()void", "g"), method("()void", "f"), variable("int", "f"),
clazz(KullaTesting.ClassType.INTERFACE, "A"));
assertActiveKeys();
assertEval("double f() { return 0.0; }",
ste(MAIN_SNIPPET, VALID, VALID, true, null),
ste(f, VALID, OVERWRITTEN, false, MAIN_SNIPPET),
ste(g, VALID, VALID, false, MAIN_SNIPPET));
assertKeys(method("()void", "g"), method("()double", "f"), variable("int", "f"),
clazz(KullaTesting.ClassType.INTERFACE, "A"));
assertActiveKeys();
}
public void testKeyId3() {
Snippet g = methodKey(assertEval("void g() { f(); }", added(RECOVERABLE_DEFINED)));
Snippet f = methodKey(assertEval("void f() { }",
added(VALID),
ste(g, RECOVERABLE_DEFINED, VALID, false, null)));
assertDeclareFail("qqqq;", "compiler.err.cant.resolve.location");
assertEval("interface A { }");
assertKeys(method("()void", "g"), method("()void", "f"),
clazz(KullaTesting.ClassType.INTERFACE, "A"));
assertActiveKeys();
assertEval("double f() { return 0.0; }",
ste(MAIN_SNIPPET, VALID, VALID, true, null),
ste(f, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
assertKeys(method("()void", "g"), clazz(KullaTesting.ClassType.INTERFACE, "A"),
method("()double", "f"));
assertActiveKeys();
}
public void testBooleanSnippetQueries() {
Snippet nd = varKey(assertEval("blort x;", added(RECOVERABLE_NOT_DEFINED)));
assertTrue(nd.kind().isPersistent(), "nd.isPersistent");
Status ndstat = getState().status(nd);
assertTrue(ndstat.isActive(), "nd.isActive");
assertFalse(ndstat.isDefined(), "nd.isDefined");
MethodSnippet g = methodKey(assertEval("void g() { f(); }", added(RECOVERABLE_DEFINED)));
assertTrue(g.kind().isPersistent(), "g.isPersistent");
Status gstat = getState().status(g);
assertTrue(gstat.isActive(), "g.isActive");
assertTrue(gstat.isDefined(), "g.isDefined");
getState().drop(g);
assertTrue(g.kind().isPersistent(), "drop isPersistent");
gstat = getState().status(g);
assertFalse(gstat.isActive(), "drop isActive");
assertFalse(gstat.isDefined(), "drop isDefined");
Snippet stmt = key(assertEval("if (true) {}", added(VALID)));
assertFalse(stmt.kind().isPersistent(), "stmt isPersistent");
Status stmtstat = getState().status(stmt);
assertTrue(stmtstat.isActive(), "stmt isActive");
assertTrue(stmtstat.isDefined(), "stmt isDefined");
}
}
|