langtools/test/jdk/jshell/KullaTesting.java
changeset 40304 0318f4e75c6d
parent 39807 ba0ff343d241
child 40515 819fc588bd19
equal deleted inserted replaced
40303:96a1226aca18 40304:0318f4e75c6d
    68 import jdk.jshell.UnresolvedReferenceException;
    68 import jdk.jshell.UnresolvedReferenceException;
    69 import org.testng.annotations.AfterMethod;
    69 import org.testng.annotations.AfterMethod;
    70 import org.testng.annotations.BeforeMethod;
    70 import org.testng.annotations.BeforeMethod;
    71 
    71 
    72 import jdk.jshell.Diag;
    72 import jdk.jshell.Diag;
       
    73 import static java.util.stream.Collectors.toList;
    73 import static jdk.jshell.Snippet.Status.*;
    74 import static jdk.jshell.Snippet.Status.*;
    74 import static org.testng.Assert.*;
    75 import static org.testng.Assert.*;
    75 import static jdk.jshell.Snippet.SubKind.METHOD_SUBKIND;
    76 import static jdk.jshell.Snippet.SubKind.METHOD_SUBKIND;
    76 
    77 
    77 public class KullaTesting {
    78 public class KullaTesting {
   181         idToSnippet = null;
   182         idToSnippet = null;
   182         classpath = null;
   183         classpath = null;
   183     }
   184     }
   184 
   185 
   185     public List<String> assertUnresolvedDependencies(DeclarationSnippet key, int unresolvedSize) {
   186     public List<String> assertUnresolvedDependencies(DeclarationSnippet key, int unresolvedSize) {
   186         List<String> unresolved = getState().unresolvedDependencies(key);
   187         List<String> unresolved = getState().unresolvedDependencies(key).collect(toList());
   187         assertEquals(unresolved.size(), unresolvedSize, "Input: " + key.source() + ", checking unresolved: ");
   188         assertEquals(unresolved.size(), unresolvedSize, "Input: " + key.source() + ", checking unresolved: ");
   188         return unresolved;
   189         return unresolved;
   189     }
   190     }
   190 
   191 
   191     public DeclarationSnippet assertUnresolvedDependencies1(DeclarationSnippet key, Status status, String name) {
   192     public DeclarationSnippet assertUnresolvedDependencies1(DeclarationSnippet key, Status status, String name) {
   200     public DeclarationSnippet assertEvalUnresolvedException(String input, String name, int unresolvedSize, int diagnosticsSize) {
   201     public DeclarationSnippet assertEvalUnresolvedException(String input, String name, int unresolvedSize, int diagnosticsSize) {
   201         List<SnippetEvent> events = assertEval(input, null, UnresolvedReferenceException.class, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null);
   202         List<SnippetEvent> events = assertEval(input, null, UnresolvedReferenceException.class, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null);
   202         SnippetEvent ste = events.get(0);
   203         SnippetEvent ste = events.get(0);
   203         DeclarationSnippet sn = ((UnresolvedReferenceException) ste.exception()).getSnippet();
   204         DeclarationSnippet sn = ((UnresolvedReferenceException) ste.exception()).getSnippet();
   204         assertEquals(sn.name(), name, "Given input: " + input + ", checking name");
   205         assertEquals(sn.name(), name, "Given input: " + input + ", checking name");
   205         assertEquals(getState().unresolvedDependencies(sn).size(), unresolvedSize, "Given input: " + input + ", checking unresolved");
   206         assertEquals(getState().unresolvedDependencies(sn).count(), unresolvedSize, "Given input: " + input + ", checking unresolved");
   206         assertEquals(getState().diagnostics(sn).size(), diagnosticsSize, "Given input: " + input + ", checking diagnostics");
   207         assertEquals(getState().diagnostics(sn).count(), (long) diagnosticsSize, "Given input: " + input + ", checking diagnostics");
   207         return sn;
   208         return sn;
   208     }
   209     }
   209 
   210 
   210     public Snippet assertKeyMatch(String input, boolean isExecutable, SubKind expectedSubKind, STEInfo mainInfo, STEInfo... updates) {
   211     public Snippet assertKeyMatch(String input, boolean isExecutable, SubKind expectedSubKind, STEInfo mainInfo, STEInfo... updates) {
   211         Snippet key = key(assertEval(input, IGNORE_VALUE, mainInfo, updates));
   212         Snippet key = key(assertEval(input, IGNORE_VALUE, mainInfo, updates));
   544                     assertTrue(eventChain.exceptionClass.isInstance(main.exception()),
   545                     assertTrue(eventChain.exceptionClass.isInstance(main.exception()),
   545                             "Expected an exception of class " + eventChain.exceptionClass +
   546                             "Expected an exception of class " + eventChain.exceptionClass +
   546                                     " got: " + main.exception().toString());
   547                                     " got: " + main.exception().toString());
   547                 }
   548                 }
   548             }
   549             }
   549             List<Diag> diagnostics = getState().diagnostics(mainKey);
   550             List<Diag> diagnostics = getState().diagnostics(mainKey).collect(toList());
   550             switch (diagMain) {
   551             switch (diagMain) {
   551                 case DIAG_OK:
   552                 case DIAG_OK:
   552                     assertEquals(diagnostics.size(), 0, "Expected no diagnostics, got: " + diagnosticsToString(diagnostics));
   553                     assertEquals(diagnostics.size(), 0, "Expected no diagnostics, got: " + diagnosticsToString(diagnostics));
   553                     break;
   554                     break;
   554                 case DIAG_WARNING:
   555                 case DIAG_WARNING:
   558                     assertTrue(hasFatalError(diagnostics), "Expected errors, got: " + diagnosticsToString(diagnostics));
   559                     assertTrue(hasFatalError(diagnostics), "Expected errors, got: " + diagnosticsToString(diagnostics));
   559                     break;
   560                     break;
   560             }
   561             }
   561             if (eventChain.mainInfo != null) {
   562             if (eventChain.mainInfo != null) {
   562                 for (STEInfo ste : eventChain.updates) {
   563                 for (STEInfo ste : eventChain.updates) {
   563                     diagnostics = getState().diagnostics(ste.snippet());
   564                     diagnostics = getState().diagnostics(ste.snippet()).collect(toList());
   564                     switch (diagUpdates) {
   565                     switch (diagUpdates) {
   565                         case DIAG_OK:
   566                         case DIAG_OK:
   566                             assertEquals(diagnostics.size(), 0, "Expected no diagnostics, got: " + diagnosticsToString(diagnostics));
   567                             assertEquals(diagnostics.size(), 0, "Expected no diagnostics, got: " + diagnosticsToString(diagnostics));
   567                             break;
   568                             break;
   568                         case DIAG_WARNING:
   569                         case DIAG_WARNING:
   635         List<SnippetEvent> events = assertEval(input, null, null,
   636         List<SnippetEvent> events = assertEval(input, null, null,
   636                 DiagCheck.DIAG_ERROR, DiagCheck.DIAG_IGNORE, mainInfo, updates);
   637                 DiagCheck.DIAG_ERROR, DiagCheck.DIAG_IGNORE, mainInfo, updates);
   637         SnippetEvent e = events.get(0);
   638         SnippetEvent e = events.get(0);
   638         Snippet key = e.snippet();
   639         Snippet key = e.snippet();
   639         assertEquals(getState().status(key), REJECTED);
   640         assertEquals(getState().status(key), REJECTED);
   640         List<Diag> diagnostics = getState().diagnostics(e.snippet());
   641         List<Diag> diagnostics = getState().diagnostics(e.snippet()).collect(toList());
   641         assertTrue(diagnostics.size() > 0, "Expected diagnostics, got none");
   642         assertTrue(diagnostics.size() > 0, "Expected diagnostics, got none");
   642         assertDiagnostic(input, diagnostics.get(0), expectedDiagnostic);
   643         assertDiagnostic(input, diagnostics.get(0), expectedDiagnostic);
   643         assertTrue(key != null, "key must never be null, but it was for: " + input);
   644         assertTrue(key != null, "key must never be null, but it was for: " + input);
   644         return key;
   645         return key;
   645     }
   646     }
   654 
   655 
   655     public Snippet assertDeclareWarn1(String input, ExpectedDiagnostic expectedDiagnostic, STEInfo mainInfo, STEInfo... updates) {
   656     public Snippet assertDeclareWarn1(String input, ExpectedDiagnostic expectedDiagnostic, STEInfo mainInfo, STEInfo... updates) {
   656         List<SnippetEvent> events = assertEval(input, IGNORE_VALUE, null,
   657         List<SnippetEvent> events = assertEval(input, IGNORE_VALUE, null,
   657                 DiagCheck.DIAG_WARNING, DiagCheck.DIAG_IGNORE, mainInfo, updates);
   658                 DiagCheck.DIAG_WARNING, DiagCheck.DIAG_IGNORE, mainInfo, updates);
   658         SnippetEvent e = events.get(0);
   659         SnippetEvent e = events.get(0);
   659         List<Diag> diagnostics = getState().diagnostics(e.snippet());
   660         List<Diag> diagnostics = getState().diagnostics(e.snippet()).collect(toList());
   660         if (expectedDiagnostic != null) assertDiagnostic(input, diagnostics.get(0), expectedDiagnostic);
   661         if (expectedDiagnostic != null) assertDiagnostic(input, diagnostics.get(0), expectedDiagnostic);
   661         return e.snippet();
   662         return e.snippet();
   662     }
   663     }
   663 
   664 
   664     private void assertDiagnostic(String input, Diag diagnostic, ExpectedDiagnostic expectedDiagnostic) {
   665     private void assertDiagnostic(String input, Diag diagnostic, ExpectedDiagnostic expectedDiagnostic) {
   702             int unressz, int othersz) {
   703             int unressz, int othersz) {
   703         assertKey(declarationKey, expectedStatus, expectedSubKind);
   704         assertKey(declarationKey, expectedStatus, expectedSubKind);
   704         String source = declarationKey.source();
   705         String source = declarationKey.source();
   705         assertEquals(declarationKey.name(), expectedName,
   706         assertEquals(declarationKey.name(), expectedName,
   706                 "Expected " + source + " to have the name: " + expectedName + ", got: " + declarationKey.name());
   707                 "Expected " + source + " to have the name: " + expectedName + ", got: " + declarationKey.name());
   707         List<String> unresolved = getState().unresolvedDependencies(declarationKey);
   708         long unresolved = getState().unresolvedDependencies(declarationKey).count();
   708         assertEquals(unresolved.size(), unressz, "Expected " + source + " to have " + unressz
   709         assertEquals(unresolved, unressz, "Expected " + source + " to have " + unressz
   709                 + " unresolved symbols, got: " + unresolved.size());
   710                 + " unresolved symbols, got: " + unresolved);
   710         List<Diag> otherCorralledErrors = getState().diagnostics(declarationKey);
   711         long otherCorralledErrorsCount = getState().diagnostics(declarationKey).count();
   711         assertEquals(otherCorralledErrors.size(), othersz, "Expected " + source + " to have " + othersz
   712         assertEquals(otherCorralledErrorsCount, othersz, "Expected " + source + " to have " + othersz
   712                 + " other errors, got: " + otherCorralledErrors.size());
   713                 + " other errors, got: " + otherCorralledErrorsCount);
   713     }
   714     }
   714 
   715 
   715     public void assertKey(Snippet key, Status expectedStatus, SubKind expectedSubKind) {
   716     public void assertKey(Snippet key, Status expectedStatus, SubKind expectedSubKind) {
   716         String source = key.source();
   717         String source = key.source();
   717         SubKind actualSubKind = key.subKind();
   718         SubKind actualSubKind = key.subKind();
   755             assertEquals(ci.completeness().isComplete(), isExpectedComplete, "Input : " + input + ", isComplete: ");
   756             assertEquals(ci.completeness().isComplete(), isExpectedComplete, "Input : " + input + ", isComplete: ");
   756         }
   757         }
   757     }
   758     }
   758 
   759 
   759     public void assertNumberOfActiveVariables(int cnt) {
   760     public void assertNumberOfActiveVariables(int cnt) {
   760         Collection<VarSnippet> variables = getState().variables();
   761         assertEquals(getState().variables().count(), cnt, "Variables : " + getState().variables().collect(toList()));
   761         assertEquals(variables.size(), cnt, "Variables : " + variables);
       
   762     }
   762     }
   763 
   763 
   764     public void assertNumberOfActiveMethods(int cnt) {
   764     public void assertNumberOfActiveMethods(int cnt) {
   765         Collection<MethodSnippet> methods = getState().methods();
   765         assertEquals(getState().methods().count(), cnt, "Methods : " + getState().methods().collect(toList()));
   766         assertEquals(methods.size(), cnt, "Methods : " + methods);
       
   767     }
   766     }
   768 
   767 
   769     public void assertNumberOfActiveClasses(int cnt) {
   768     public void assertNumberOfActiveClasses(int cnt) {
   770         Collection<TypeDeclSnippet> classes = getState().types();
   769         assertEquals(getState().types().count(), cnt, "Types : " + getState().types().collect(toList()));
   771         assertEquals(classes.size(), cnt, "Classes : " + classes);
       
   772     }
       
   773 
       
   774     public void assertMembers(Collection<? extends Snippet> members, Set<MemberInfo> expected) {
       
   775         assertEquals(members.size(), expected.size(), "Expected : " + expected + ", actual : " + members);
       
   776         assertEquals(members.stream()
       
   777                         .map(this::getMemberInfo)
       
   778                         .collect(Collectors.toSet()),
       
   779                 expected);
       
   780     }
   770     }
   781 
   771 
   782     public void assertKeys(MemberInfo... expected) {
   772     public void assertKeys(MemberInfo... expected) {
   783         int index = 0;
   773         int index = 0;
   784         List<Snippet> snippets = getState().snippets();
   774         List<Snippet> snippets = getState().snippets().collect(toList());
   785         assertEquals(allSnippets.size(), snippets.size());
   775         assertEquals(allSnippets.size(), snippets.size());
   786         for (Snippet sn : snippets) {
   776         for (Snippet sn : snippets) {
   787             if (sn.kind().isPersistent() && getState().status(sn).isActive()) {
   777             if (sn.kind().isPersistent() && getState().status(sn).isActive()) {
   788                 MemberInfo actual = getMemberInfo(sn);
   778                 MemberInfo actual = getMemberInfo(sn);
   789                 MemberInfo exp = expected[index];
   779                 MemberInfo exp = expected[index];
   799         assertActiveKeys(expected.toArray(new Snippet[expected.size()]));
   789         assertActiveKeys(expected.toArray(new Snippet[expected.size()]));
   800     }
   790     }
   801 
   791 
   802     public void assertActiveKeys(Snippet... expected) {
   792     public void assertActiveKeys(Snippet... expected) {
   803         int index = 0;
   793         int index = 0;
   804         for (Snippet key : getState().snippets()) {
   794         for (Snippet key : getState().snippets().collect(toList())) {
   805             if (state.status(key).isActive()) {
   795             if (state.status(key).isActive()) {
   806                 assertEquals(expected[index], key, String.format("Difference in #%d. Expected: %s, actual: %s", index, key, expected[index]));
   796                 assertEquals(expected[index], key, String.format("Difference in #%d. Expected: %s, actual: %s", index, key, expected[index]));
   807                 ++index;
   797                 ++index;
   808             }
   798             }
   809         }
   799         }
   810     }
   800     }
   811 
   801 
   812     private List<Snippet> filterDeclaredKeys(Predicate<Snippet> p) {
   802     private void assertActiveSnippets(Stream<? extends Snippet> snippets, Predicate<Snippet> p, String label) {
   813         return getActiveKeys().stream()
   803         Set<Snippet> active = getActiveKeys().stream()
   814                 .filter(p)
   804                 .filter(p)
   815                 .collect(Collectors.toList());
   805                 .collect(Collectors.toSet());
       
   806         Set<Snippet> got = snippets
       
   807                 .collect(Collectors.toSet());
       
   808         assertEquals(active, got, label);
   816     }
   809     }
   817 
   810 
   818     public void assertVariables() {
   811     public void assertVariables() {
   819         assertEquals(getState().variables(), filterDeclaredKeys((key) -> key instanceof VarSnippet), "Variables");
   812         assertActiveSnippets(getState().variables(), (key) -> key instanceof VarSnippet, "Variables");
   820     }
   813     }
   821 
   814 
   822     public void assertMethods() {
   815     public void assertMethods() {
   823         assertEquals(getState().methods(), filterDeclaredKeys((key) -> key instanceof MethodSnippet), "Methods");
   816         assertActiveSnippets(getState().methods(), (key) -> key instanceof MethodSnippet, "Methods");
   824     }
   817     }
   825 
   818 
   826     public void assertClasses() {
   819     public void assertClasses() {
   827         assertEquals(getState().types(), filterDeclaredKeys((key) -> key instanceof TypeDeclSnippet), "Classes");
   820         assertActiveSnippets(getState().types(), (key) -> key instanceof TypeDeclSnippet, "Classes");
       
   821     }
       
   822 
       
   823     public void assertMembers(Stream<? extends Snippet> members, MemberInfo...expectedInfos) {
       
   824         Set<MemberInfo> expected = Stream.of(expectedInfos).collect(Collectors.toSet());
       
   825         Set<MemberInfo> got = members
       
   826                         .map(this::getMemberInfo)
       
   827                         .collect(Collectors.toSet());
       
   828         assertEquals(got.size(), expected.size(), "Expected : " + expected + ", actual : " + members);
       
   829         assertEquals(got, expected);
   828     }
   830     }
   829 
   831 
   830     public void assertVariables(MemberInfo...expected) {
   832     public void assertVariables(MemberInfo...expected) {
   831         assertMembers(getState().variables(), Stream.of(expected).collect(Collectors.toSet()));
   833         assertMembers(getState().variables(), expected);
   832     }
   834     }
   833 
   835 
   834     public void assertMethods(MemberInfo...expected) {
   836     public void assertMethods(MemberInfo...expected) {
   835         assertMembers(getState().methods(), Stream.of(expected).collect(Collectors.toSet()));
   837         assertMembers(getState().methods(), expected);
   836         for (MethodSnippet methodKey : getState().methods()) {
   838         getState().methods().forEach(methodKey -> {
   837             MemberInfo expectedInfo = null;
   839             MemberInfo expectedInfo = null;
   838             for (MemberInfo info : expected) {
   840             for (MemberInfo info : expected) {
   839                 if (info.name.equals(methodKey.name()) && info.type.equals(methodKey.signature())) {
   841                 if (info.name.equals(methodKey.name()) && info.type.equals(methodKey.signature())) {
   840                     expectedInfo = getMemberInfo(methodKey);
   842                     expectedInfo = getMemberInfo(methodKey);
   841                 }
   843                 }
   842             }
   844             }
   843             assertNotNull(expectedInfo, "Not found method: " + methodKey.name());
   845             assertNotNull(expectedInfo, "Not found method: " + methodKey.name());
   844             int lastIndexOf = expectedInfo.type.lastIndexOf(')');
   846             int lastIndexOf = expectedInfo.type.lastIndexOf(')');
   845             assertEquals(methodKey.parameterTypes(), expectedInfo.type.substring(1, lastIndexOf), "Parameter types");
   847             assertEquals(methodKey.parameterTypes(), expectedInfo.type.substring(1, lastIndexOf), "Parameter types");
   846         }
   848         });
   847     }
   849     }
   848 
   850 
   849     public void assertClasses(MemberInfo...expected) {
   851     public void assertClasses(MemberInfo...expected) {
   850         assertMembers(getState().types(), Stream.of(expected).collect(Collectors.toSet()));
   852         assertMembers(getState().types(), expected);
   851     }
   853     }
   852 
   854 
   853     public void assertCompletion(String code, String... expected) {
   855     public void assertCompletion(String code, String... expected) {
   854         assertCompletion(code, null, expected);
   856         assertCompletion(code, null, expected);
   855     }
   857     }