2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.lang.java.parser.partial;
18 import com.intellij.lang.PsiBuilder;
19 import com.intellij.lang.java.parser.JavaParser;
20 import com.intellij.lang.java.parser.JavaParsingTestCase;
22 public class DeclarationParserTest extends JavaParsingTestCase {
23 public DeclarationParserTest() {
24 super("parser-partial/declarations");
27 public void testEmptyBody0() { doParserTest("{ }"); }
28 public void testEmptyBody1() { doParserTest("{ "); }
29 public void testEmptyBody2() { doParserTest("{ @Null }"); }
31 public void testNoType() { doParserTest("{ new X(); }"); }
32 public void testExtraSemicolon() { doParserTest("{ class C { }; }"); }
33 public void testParameterizedClass() { doParserTest("{ public class A <T extends java.util.List> { } }"); }
34 public void testPines() { doParserTest("{ class A<T extends List<String>> extends List<List<Integer>> { } }"); }
35 public void testIncompleteAnnotation() { doParserTest("{ public class Foo { public void testSomething(); @Null } }"); }
36 public void testClassInit() { doParserTest("{ { /*comment*/ } }"); }
37 public void testAnnoDeclaration() { doParserTest("{ public @interface Annotation {} }"); }
38 public void testEnumSmartTypeCompletion() { doParserTest("{ @Preliminary(A.B\n#) public class TimeTravel {}\n" +
39 " @Preliminary(a=A.B\n#) public class TimeTravel {}\n" +
40 " @Preliminary(a=A.B\n#, b=c) public class TimeTravel {} }"); }
41 public void testTypeAnno() {
42 doParserTest("{ class C<@D T extends @F Object> extends @F Object {\n" +
43 " @F int @F[] method() throws @F Exception {\n" +
44 " a = this instanceof @F C;\n" +
45 " C<@F @G C> c = new @Q C<@F C>();\n" +
46 " c = (@F Object)c;\n" +
47 " Class c = @TA String.class;\n" +
52 public void testEnumBody0() { doParserTest("{ ; }", false, true); }
53 public void testEnumBody1() { doParserTest("{ RED, GREEN, BLUE; }", false, true); }
54 public void testEnumBody2() { doParserTest("{ RED, GREEN, BLUE }", false, true); }
55 public void testEnumBody3() { doParserTest("{ RED, GREEN, BLUE, }", false, true); }
56 public void testEnumBody4() { doParserTest("{ RED(0), GREEN(1), BLUE(2); }", false, true); }
57 public void testEnumBody5() { doParserTest("{ @ANNOTATION A(10) }", false, true); }
58 public void testEnumBody6() { doParserTest("{ RED, GREEN, BLUE\n OurEnum() {} }", false, true); }
59 public void testEnumWithInitializedConstants() { doParserTest("{ A(10) { },\n B { void method() {} } }", false, true); }
60 public void testEnumWithoutConstants() { doParserTest("{ private A }", false, true); }
62 public void testAnnoSimple() { doParserTest("{ int foo (); }", true, false); }
63 public void testAnnoDefault() { doParserTest("{ Class foo() default String.class; }", true, false); }
64 public void testAnnoNested() { doParserTest("{ @interface Inner { String bar () default \"<unspecified>\"; } }", true, false); }
65 public void testAnnoInner() { doParserTest("{ @interface Inner { double bar () default 0.0; } }"); }
66 public void testAnnoOtherMembers() { doParserTest("{ int field;\n void m() {}\n class C {}\n interface I {} }", true, false); }
67 public void testAnnoLoop() { doParserTest("{ @@@ int i; }"); }
69 public void testFieldSimple() { doParserTest("{ int field = 0; }"); }
70 public void testFieldMulti() { doParserTest("{ int field1 = 0, field2; }"); }
71 public void testUnclosedBracket() { doParserTest("{ int field[ }"); }
72 public void testMissingInitializer() { doParserTest("{ int field = }"); }
73 public void testUnclosedComma() { doParserTest("{ int field, }"); }
74 public void testUnclosedSemicolon() { doParserTest("{ int field }"); }
75 public void testMissingInitializerExpression() { doParserTest("{ int field=; }"); }
76 public void testMultiLineUnclosed() { doParserTest("{ int \n Object o; }"); }
77 public void testUnclosedField1() { doParserTest("{ String f1\n\n @Anno String f2; }"); }
78 public void testUnclosedField2() { doParserTest("{ String f1\n\n @Anno\n String f2; }"); }
80 public void testMethodNormal0() { doParserTest("{ void f() {} }"); }
81 public void testMethodNormal1() { doParserTest("{ void f(); }"); }
82 public void testMethodNormal2() { doParserTest("{ default public void f() { } }"); }
83 public void testSemicolons() { doParserTest("{ void f() {}; void g() {}; }"); }
84 public void testUnclosed0() { doParserTest("{ void f() }"); }
85 public void testExtension() { doParserTest("{ default int f() throws E { return 42; } }"); }
86 public void testUnclosed1() { doParserTest("{ void f( }"); }
87 public void testUnclosed2() { doParserTest("{ void f()\n void g(); }"); }
88 public void testUnclosed3() { doParserTest("{ void f(int a }"); }
89 public void testUnclosed4() { doParserTest("{ void f(int a,, }"); }
90 public void testUnclosed5() { doParserTest("{ void f(int a,); }"); }
91 public void testUnclosed6() { doParserTest("{ void f() default ; }", true, false); }
92 public void testUnclosed7() { doParserTest("{ void f() default {return 42;} }", true, false); }
93 public void testUnclosed8() { doParserTest("{ void f() default }"); }
94 public void testConstructorBrackets() { doParserTest("{ A() [] { } }"); }
95 public void testVarArgBrackets() { doParserTest("{ void foo(int... x[]); }"); }
97 public void testGenericMethod() { doParserTest("{ public static <E> test();\n" +
98 " <E> void test1();\n" +
99 " <E1 extends Integer, E2 extends Runnable> String test2(); }"); }
100 public void testGenericMethodErrors() { doParserTest("{ <Error sss /> test <error>(); }"); }
101 public void testErrors() { doParserTest("{ public static <error descr=\"2\">protected int f1 = 0; }"); }
102 public void testCompletionHack0() { doParserTest("{ <X IntelliJIdeaRulezz>\n String s = \"\"; }"); }
103 public void testCompletionHack1() { doParserTest("{ <X\n String s = \"\"; }"); }
104 public void testCompletionHack2() { doParserTest("{ String foo() def }", true, false); }
105 public void testWildcardParsing() { doParserTest("{ List<? extends B> x(Collection<? super B> x); }"); }
106 public void testParameterAnnotation() { doParserTest("{ void foo (@Annotation(value=77) int param) {} }"); }
107 public void testParameterizedMethod() { doParserTest("{ @Nullable <T> T bar() {} }"); }
108 public void testMethodNameOmitted() { doParserTest("{ void(); }"); }
110 private void doParserTest(String text) {
111 doParserTest(text, false, false);
114 private void doParserTest(String text, boolean isAnnotation, boolean isEnum) {
115 doParserTest(text, new MyTestParser(isAnnotation, isEnum));
118 private static class MyTestParser implements TestParser {
119 private final boolean myAnnotation;
120 private final boolean myAnEnum;
122 public MyTestParser(boolean annotation, boolean anEnum) {
123 myAnnotation = annotation;
128 public void parse(PsiBuilder builder) {
129 JavaParser.INSTANCE.getDeclarationParser().parseClassBodyWithBraces(builder, myAnnotation, myAnEnum);