Path: blob/master/test/langtools/jdk/jshell/AnalyzeSnippetTest.java
40931 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 818227026* @summary test non-eval Snippet analysis27* @build KullaTesting TestingInputStream28* @run testng AnalyzeSnippetTest29*/3031import java.io.ByteArrayOutputStream;32import java.io.PrintStream;33import java.util.List;34import jdk.jshell.Snippet;35import jdk.jshell.DeclarationSnippet;36import org.testng.annotations.Test;3738import jdk.jshell.JShell;39import jdk.jshell.MethodSnippet;40import static org.testng.Assert.assertFalse;41import static org.testng.Assert.assertTrue;42import static org.testng.Assert.assertEquals;43import org.testng.annotations.AfterMethod;44import org.testng.annotations.BeforeMethod;45import jdk.jshell.ErroneousSnippet;46import jdk.jshell.ExpressionSnippet;47import jdk.jshell.ImportSnippet;48import jdk.jshell.Snippet.SubKind;49import jdk.jshell.SourceCodeAnalysis;50import jdk.jshell.StatementSnippet;51import jdk.jshell.TypeDeclSnippet;52import jdk.jshell.VarSnippet;53import static jdk.jshell.Snippet.SubKind.*;5455@Test56public class AnalyzeSnippetTest {5758JShell state;59SourceCodeAnalysis sca;6061@BeforeMethod62public void setUp() {63state = JShell.builder()64.out(new PrintStream(new ByteArrayOutputStream()))65.err(new PrintStream(new ByteArrayOutputStream()))66.build();67sca = state.sourceCodeAnalysis();68}6970@AfterMethod71public void tearDown() {72if (state != null) {73state.close();74}75state = null;76sca = null;77}7879public void testImport() {80ImportSnippet sn = (ImportSnippet) assertSnippet("import java.util.List;",81SubKind.SINGLE_TYPE_IMPORT_SUBKIND);82assertEquals(sn.name(), "List");83sn = (ImportSnippet) assertSnippet("import static java.nio.file.StandardOpenOption.CREATE;",84SubKind.SINGLE_STATIC_IMPORT_SUBKIND);85assertTrue(sn.isStatic());86}8788public void testClass() {89TypeDeclSnippet sn = (TypeDeclSnippet) assertSnippet("class C {}",90SubKind.CLASS_SUBKIND);91assertEquals(sn.name(), "C");92sn = (TypeDeclSnippet) assertSnippet("enum EE {A, B , C}",93SubKind.ENUM_SUBKIND);94}9596public void testMethod() {97MethodSnippet sn = (MethodSnippet) assertSnippet("int m(int x) { return x + x; }",98SubKind.METHOD_SUBKIND);99assertEquals(sn.name(), "m");100assertEquals(sn.signature(), "(int)int");101}102103public void testVar() {104VarSnippet sn = (VarSnippet) assertSnippet("int i;",105SubKind.VAR_DECLARATION_SUBKIND);106assertEquals(sn.name(), "i");107assertEquals(sn.typeName(), "int");108sn = (VarSnippet) assertSnippet("int jj = 6;",109SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND);110sn = (VarSnippet) assertSnippet("2 + 2",111SubKind.TEMP_VAR_EXPRESSION_SUBKIND);112}113114public void testExpression() {115state.eval("int aa = 10;");116ExpressionSnippet sn = (ExpressionSnippet) assertSnippet("aa",117SubKind.VAR_VALUE_SUBKIND);118assertEquals(sn.name(), "aa");119assertEquals(sn.typeName(), "int");120sn = (ExpressionSnippet) assertSnippet("aa;",121SubKind.VAR_VALUE_SUBKIND);122assertEquals(sn.name(), "aa");123assertEquals(sn.typeName(), "int");124sn = (ExpressionSnippet) assertSnippet("aa = 99",125SubKind.ASSIGNMENT_SUBKIND);126}127128public void testStatement() {129StatementSnippet sn = (StatementSnippet) assertSnippet("System.out.println(33)",130SubKind.STATEMENT_SUBKIND);131sn = (StatementSnippet) assertSnippet("if (true) System.out.println(33);",132SubKind.STATEMENT_SUBKIND);133}134135public void testErroneous() {136ErroneousSnippet sn = (ErroneousSnippet) assertSnippet("+++",137SubKind.UNKNOWN_SUBKIND);138sn = (ErroneousSnippet) assertSnippet("abc",139SubKind.UNKNOWN_SUBKIND);140}141142public void testNoStateChange() {143assertSnippet("int a = 5;", SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND);144assertSnippet("a", SubKind.UNKNOWN_SUBKIND);145VarSnippet vsn = (VarSnippet) state.eval("int aa = 10;").get(0).snippet();146assertSnippet("++aa;", SubKind.TEMP_VAR_EXPRESSION_SUBKIND);147assertEquals(state.varValue(vsn), "10");148assertSnippet("class CC {}", SubKind.CLASS_SUBKIND);149assertSnippet("new CC();", SubKind.UNKNOWN_SUBKIND);150}151152private Snippet assertSnippet(String input, SubKind sk) {153List<Snippet> sns = sca.sourceToSnippets(input);154assertEquals(sns.size(), 1, "snippet count");155Snippet sn = sns.get(0);156assertEquals(sn.id(), "*UNASSOCIATED*");157assertEquals(sn.subKind(), sk);158return sn;159}160}161162163