summaryrefslogtreecommitdiff
path: root/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterBracesTest.java
blob: 52dc555c981b6063cbd6048ba1b25728168db56d (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.psi.formatter.java;

import com.intellij.psi.codeStyle.CommonCodeStyleSettings;

/**
 * Is intended to hold specific java formatting tests for 'braces placement' settings (
 * <code>Project Settings - Code Style - Alignment and Braces</code>).
 *
 * @author Denis Zhdanov
 * @since Apr 27, 2010 6:39:24 PM
 */
public class JavaFormatterBracesTest extends AbstractJavaFormatterTest {

  public void testBracePositioningAtPreviousLine() throws Exception {
    // Inspired by IDEADEV-18529
    doTextTest(
      "public class TestBed\n" +
      "{\n" +
      "    public void methodOne()\n" +
      "    {\n" +
      "        //code...\n" +
      "    }\n" +
      "\n" +
      "    @SomeAnnotation\n" +
      "            <T extends Comparable> void methodTwo(T item) {\n" +
      "        //code...\n" +
      "    }\n" +
      "\n" +
      "    private void methodThree(String s) {\n" +
      "        //code...\n" +
      "    }\n" +
      "}",

      "public class TestBed {\n" +
      "    public void methodOne() {\n" +
      "        //code...\n" +
      "    }\n" +
      "\n" +
      "    @SomeAnnotation\n" +
      "    <T extends Comparable> void methodTwo(T item) {\n" +
      "        //code...\n" +
      "    }\n" +
      "\n" +
      "    private void methodThree(String s) {\n" +
      "        //code...\n" +
      "    }\n" +
      "}");
  }

  public void testSimpleBlockInOneLinesAndForceBraces() throws Exception {
    // Inspired by IDEA-19328
    getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;
    getSettings().IF_BRACE_FORCE = CommonCodeStyleSettings.FORCE_BRACES_ALWAYS;

    doMethodTest(
      "if (x > y) System.out.println(\"foo!\");",

      "if (x > y) { System.out.println(\"foo!\"); }"
    );
  }

  public void testEnforcingBracesForExpressionEndingWithLineComment() throws Exception {
    // Inspired by IDEA-57936
    getSettings().IF_BRACE_FORCE = CommonCodeStyleSettings.FORCE_BRACES_ALWAYS;

    doMethodTest(
      "if (true) i = 1; // Cool if\n" +
      "else i = 2;",

      "if (true) {\n" +
      "    i = 1; // Cool if\n" +
      "} else {\n" +
      "    i = 2;\n" +
      "}"
    );
  }

  public void testMoveBraceOnNextLineForAnnotatedMethod() throws Exception {
    // Inspired by IDEA-59336
    getSettings().METHOD_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
    getSettings().KEEP_SIMPLE_METHODS_IN_ONE_LINE = true;

    doClassTest(
      "@Override\n" +
      "public int hashCode() {\n" +
      "}\n" +
      "@Deprecated\n" +
      "void foo() {\n" +
      "}",
      "@Override\n" +
      "public int hashCode()\n" +
      "{\n" +
      "}\n" +
      "\n" +
      "@Deprecated\n" +
      "void foo()\n" +
      "{\n" +
      "}"
    );
  }
  
  public void testKeepSimpleClassesAndInterfacesInOneLine() {
    // Inspired by IDEA-65433
    getSettings().KEEP_SIMPLE_CLASSES_IN_ONE_LINE = true;
    
    String[] tests = {
      "class Test {}",
      
      "interface Test {}",
      
      "class Test {\n" +
      "    void test() {\n" +
      "        new Object() {};\n" +
      "    }\n" +
      "}",
      
      "class Test {\n" +
      "    void test() {\n" +
      "        bind(new TypeLiteral<MyType>() {}).toProvider(MyProvider.class);\n" +
      "    }\n" +
      "}"
    };

    for (String test : tests) {
      doTextTest(test, test);
    }
  }

  public void testKeepSimpleClassesInOneLineAndLeftBraceOnNextLine() throws Exception {
    // Inspired by IDEA-75053.
    getSettings().KEEP_SIMPLE_CLASSES_IN_ONE_LINE = true;
    getSettings().CLASS_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
    
    String text =
      "class Test\n" +
      "{\n" +
      "    void foo() {\n" +
      "        bind(new TypeLiteral<MyType>() {}).toProvider(MyProvider.class);\n" +
      "    }\n" +
      "}";
    doTextTest(text, text);
  }

  public void testSimpleMethodsInOneLineEvenIfExceedsRightMargin() {
    getSettings().KEEP_SIMPLE_METHODS_IN_ONE_LINE = true;
    getSettings().getRootSettings().RIGHT_MARGIN = 90;
    String text = "public class Repr2 {\n" +
                  "    public void start() { System.out.println(\"kfjsdkfjsdkfjskdjfslkdjfklsdjfklsdjfksjdfkljsdkfjsd!\"); }\n" +
                  "}";
    doTextTest(text, text);
  }

  public void testKeepSimpleBlocksInOneLine_OnIfStatementsThenBlock() throws Exception {
    getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;
    String singleLine = "if (2 > 3) { System.out.println(\"AA!\"); }";

    getSettings().BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
    doMethodTest(singleLine, singleLine);

    getSettings().BRACE_STYLE = CommonCodeStyleSettings.END_OF_LINE;
    doMethodTest(singleLine, singleLine);
  }

  public void testKeepSimpleBlocksInOneLine_OnIfStatementsElseBlock() throws Exception {
    getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;

    String before = "if (2 > 3) {\n" +
                    "    System.out.println(\"AA!\");\n" +
                    "} else { int a = 3; }";

    String afterNextLineOption = "if (2 > 3)\n" +
                                 "{\n" +
                                 "    System.out.println(\"AA!\");\n" +
                                 "} else { int a = 3; }";

    getSettings().BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
    doMethodTest(before, afterNextLineOption);

    getSettings().BRACE_STYLE = CommonCodeStyleSettings.END_OF_LINE;
    doMethodTest(before, before);
  }

  public void testIfStatement_WhenBraceOnNextLine_AndKeepSimpleBlockInOneLineEnabled() throws Exception {
    getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;
    getSettings().BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
    String before = "if (2 > 3) {\n" +
                    "    System.out.println(\"AA!\");\n" +
                    "}";
    String after = "if (2 > 3)\n" +
                   "{\n" +
                   "    System.out.println(\"AA!\");\n" +
                   "}";
    doMethodTest(before, after);
  }

  public void testIfStatementElseBranchIsOnNewLine() throws Exception {
    getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;
    getSettings().BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
    String before = "if (2 > 3) {\n" +
                    "    System.out.println(\"AA!\");\n" +
                    "} else {\n" +
                    "    int a = 3;\n" +
                    "}";
    String after = "if (2 > 3)\n" +
                   "{\n" +
                   "    System.out.println(\"AA!\");\n" +
                   "} else\n" +
                   "{\n" +
                   "    int a = 3;\n" +
                   "}";
    doMethodTest(before, after);
  }

  public void testIfElseBranchesKeepedInOneLine() throws Exception {
    getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;
    getSettings().BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;

    String singleLine = "if (2 > 3) { System.out.println(\"AA!\"); } else { System.out.println(\"BBB!!\"); }";
    String multiLine = "if (2 > 3) { System.out.println(\"AA!\"); }\n" +
                       "else { System.out.println(\"BBB!!\"); }";

    getSettings().ELSE_ON_NEW_LINE = false;
    doMethodTest(singleLine, singleLine);
    doMethodTest(multiLine, singleLine);

    getSettings().ELSE_ON_NEW_LINE = true;
    doMethodTest(singleLine, multiLine);
    doMethodTest(multiLine, multiLine);
  }
}