SnippetTest.java revision 3062:15bdc18525ff
1/* 2 * Copyright (c) 2014, 2015, 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/* 25 * @test 26 * @summary test accessors of Snippet 27 * @build KullaTesting TestingInputStream 28 * @run testng SnippetTest 29 */ 30 31import jdk.jshell.Snippet; 32import jdk.jshell.DeclarationSnippet; 33import org.testng.annotations.Test; 34 35import static jdk.jshell.Snippet.Status.VALID; 36import static jdk.jshell.Snippet.Status.RECOVERABLE_DEFINED; 37import static jdk.jshell.Snippet.Status.OVERWRITTEN; 38import static jdk.jshell.Snippet.SubKind.*; 39 40@Test 41public class SnippetTest extends KullaTesting { 42 43 public void testImportKey() { 44 assertImportKeyMatch("import java.util.List;", "List", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID)); 45 assertImportKeyMatch("import java.util.*;", "java.util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID)); 46 assertImportKeyMatch("import static java.lang.String.*;", "java.lang.String.*", STATIC_IMPORT_ON_DEMAND_SUBKIND, added(VALID)); 47 } 48 49 public void testClassKey() { 50 assertDeclarationKeyMatch("class X {}", false, "X", CLASS_SUBKIND, added(VALID)); 51 } 52 53 public void testInterfaceKey() { 54 assertDeclarationKeyMatch("interface I {}", false, "I", INTERFACE_SUBKIND, added(VALID)); 55 } 56 57 public void testEnumKey() { 58 assertDeclarationKeyMatch("enum E {}", false, "E", ENUM_SUBKIND, added(VALID)); 59 } 60 61 public void testAnnotationKey() { 62 assertDeclarationKeyMatch("@interface A {}", false, "A", ANNOTATION_TYPE_SUBKIND, added(VALID)); 63 } 64 65 public void testMethodKey() { 66 assertDeclarationKeyMatch("void m() {}", false, "m", METHOD_SUBKIND, added(VALID)); 67 } 68 69 public void testVarDeclarationKey() { 70 assertVarKeyMatch("int a;", false, "a", VAR_DECLARATION_SUBKIND, "int", added(VALID)); 71 } 72 73 public void testVarDeclarationWithInitializerKey() { 74 assertVarKeyMatch("double b = 9.0;", true, "b", VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, "double", added(VALID)); 75 } 76 77 public void testTempVarExpressionKey() { 78 assertVarKeyMatch("47;", true, "$1", TEMP_VAR_EXPRESSION_SUBKIND, "int", added(VALID)); 79 } 80 81 public void testVarValueKey() { 82 assertEval("double x = 4;", "4.0"); 83 assertExpressionKeyMatch("x;", "x", VAR_VALUE_SUBKIND, "double"); 84 } 85 86 public void testAssignmentKey() { 87 assertEval("int y;"); 88 assertExpressionKeyMatch("y = 4;", "y", ASSIGNMENT_SUBKIND, "int"); 89 } 90 91 public void testStatementKey() { 92 assertKeyMatch("if (true) {}", true, STATEMENT_SUBKIND, added(VALID)); 93 assertKeyMatch("while (true) { break; }", true, STATEMENT_SUBKIND, added(VALID)); 94 assertKeyMatch("do { } while (false);", true, STATEMENT_SUBKIND, added(VALID)); 95 assertKeyMatch("for (;;) { break; }", true, STATEMENT_SUBKIND, added(VALID)); 96 } 97 98 public void noKeys() { 99 assertActiveKeys(new DeclarationSnippet[0]); 100 } 101 102 public void testKeyId1() { 103 Snippet a = classKey(assertEval("class A { }")); 104 assertEval("void f() { }"); 105 assertEval("int f;"); 106 assertEval("interface A { }", 107 ste(MAIN_SNIPPET, VALID, VALID, true, null), 108 ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); 109 assertKeys(method("()void", "f"), variable("int", "f"), clazz(KullaTesting.ClassType.INTERFACE, "A")); 110 assertActiveKeys(); 111 } 112 113 @Test(enabled = false) // TODO 8081689 114 public void testKeyId2() { 115 Snippet g = methodKey(assertEval("void g() { f(); }", added(RECOVERABLE_DEFINED))); 116 Snippet f = methodKey(assertEval("void f() { }", 117 added(VALID), 118 ste(g, RECOVERABLE_DEFINED, VALID, false, null))); 119 assertEval("int f;"); 120 assertEval("interface A { }"); 121 assertKeys(method("()void", "g"), method("()void", "f"), variable("int", "f"), 122 clazz(KullaTesting.ClassType.INTERFACE, "A")); 123 assertActiveKeys(); 124 assertEval("double f() { return 0.0; }", 125 ste(MAIN_SNIPPET, VALID, VALID, true, null), 126 ste(f, VALID, OVERWRITTEN, false, MAIN_SNIPPET), 127 ste(g, VALID, VALID, false, MAIN_SNIPPET)); 128 assertKeys(method("()void", "g"), method("()double", "f"), variable("int", "f"), 129 clazz(KullaTesting.ClassType.INTERFACE, "A")); 130 assertActiveKeys(); 131 } 132 133 public void testKeyId3() { 134 Snippet g = methodKey(assertEval("void g() { f(); }", added(RECOVERABLE_DEFINED))); 135 Snippet f = methodKey(assertEval("void f() { }", 136 added(VALID), 137 ste(g, RECOVERABLE_DEFINED, VALID, false, null))); 138 assertDeclareFail("qqqq;", "compiler.err.cant.resolve.location"); 139 assertEval("interface A { }"); 140 assertKeys(method("()void", "g"), method("()void", "f"), 141 clazz(KullaTesting.ClassType.INTERFACE, "A")); 142 assertActiveKeys(); 143 assertEval("double f() { return 0.0; }", 144 ste(MAIN_SNIPPET, VALID, VALID, true, null), 145 ste(f, VALID, OVERWRITTEN, false, MAIN_SNIPPET), 146 ste(g, VALID, VALID, false, MAIN_SNIPPET)); 147 assertKeys(method("()void", "g"), clazz(KullaTesting.ClassType.INTERFACE, "A"), 148 method("()double", "f")); 149 assertActiveKeys(); 150 } 151} 152