Skip to content

Commit ab2dac6

Browse files
committed
Updates formatter tests
1 parent 408e09e commit ab2dac6

File tree

8 files changed

+103
-115
lines changed

8 files changed

+103
-115
lines changed

kotlin-eclipse-core/src/org/jetbrains/kotlin/core/formatting/KotlinCodeStyleManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ object KotlinCodeStyleManager {
2424
get() = (predefinedStyles.keys + stylesCache.keys).sorted()
2525

2626
// Can be used in the future to provide user defined code styles
27-
fun getOrCreate(id: String, settingsApplier: (CodeStyleSettings) -> Unit): CodeStyleSettings =
28-
stylesCache.getOrPut(id) { CodeStyleSettings().also { settingsApplier(it) } }
27+
fun getOrCreate(id: String, settingsApplier: CodeStyleSettings.() -> Unit): CodeStyleSettings =
28+
stylesCache.getOrPut(id) { CodeStyleSettings().also { it.settingsApplier() } }
2929

3030
fun get(id: String): CodeStyleSettings? = stylesCache[id] ?: createStyleFromPredef(id)
3131

kotlin-eclipse-test-framework/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
55
<classpathentry kind="src" path="src"/>
6+
<classpathentry kind="con" path="org.jetbrains.kotlin.core.KOTLIN_CONTAINER"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>

kotlin-eclipse-test-framework/.project

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
<projects>
66
</projects>
77
<buildSpec>
8+
<buildCommand>
9+
<name>org.jetbrains.kotlin.ui.kotlinBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
813
<buildCommand>
914
<name>org.eclipse.jdt.core.javabuilder</name>
1015
<arguments>
@@ -24,5 +29,13 @@
2429
<natures>
2530
<nature>org.eclipse.pde.PluginNature</nature>
2631
<nature>org.eclipse.jdt.core.javanature</nature>
32+
<nature>org.jetbrains.kotlin.core.kotlinNature</nature>
2733
</natures>
34+
<linkedResources>
35+
<link>
36+
<name>kotlin_bin</name>
37+
<type>2</type>
38+
<locationURI>org.jetbrains.kotlin.core.filesystem:/kotlin-eclipse-test-framework/kotlin_bin</locationURI>
39+
</link>
40+
</linkedResources>
2841
</projectDescription>

kotlin-eclipse-test-framework/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,26 @@
1313

1414
<artifactId>org.jetbrains.kotlin.testframework</artifactId>
1515
<packaging>eclipse-plugin</packaging>
16+
17+
<build>
18+
<sourceDirectory>src</sourceDirectory>
19+
20+
<plugins>
21+
<plugin>
22+
<groupId>org.jetbrains.kotlin</groupId>
23+
<artifactId>kotlin-maven-plugin</artifactId>
24+
<version>${kotlin.version}</version>
25+
26+
<executions>
27+
<execution>
28+
<id>compile</id>
29+
<phase>process-sources</phase>
30+
<goals>
31+
<goal>compile</goal>
32+
</goals>
33+
</execution>
34+
</executions>
35+
</plugin>
36+
</plugins>
37+
</build>
1638
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.jetbrains.kotlin.testframework.utils
2+
3+
import org.eclipse.core.resources.IProject
4+
import org.eclipse.core.resources.ProjectScope
5+
import org.jetbrains.kotlin.core.formatting.KotlinCodeStyleManager
6+
import org.jetbrains.kotlin.core.preferences.KotlinCodeStyleProperties
7+
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
8+
import java.util.*
9+
import kotlin.reflect.KFunction
10+
import kotlin.reflect.KMutableProperty
11+
12+
object CodeStyleConfigurator {
13+
fun configure(project: IProject, fileText: String) {
14+
val generatedId = UUID.randomUUID().toString()
15+
16+
KotlinCodeStyleProperties(ProjectScope(project)).apply {
17+
codeStyleId = generatedId
18+
globalsOverridden = true
19+
saveChanges()
20+
}
21+
22+
KotlinCodeStyleManager.getOrCreate(generatedId) {
23+
val kotlinSettings = getCustomSettings(KotlinCodeStyleSettings::class.java)
24+
25+
InTextDirectivesUtils.findListWithPrefixes(fileText, "SET_TRUE:")
26+
.forEach { kotlinSettings[it] = true }
27+
28+
InTextDirectivesUtils.findListWithPrefixes(fileText, "SET_FALSE:")
29+
.forEach { kotlinSettings[it] = false }
30+
31+
InTextDirectivesUtils.findListWithPrefixes(fileText, "SET_INT:")
32+
.map { it.split("=", limit = 2) }
33+
.forEach { (prop, value) -> kotlinSettings[prop] = value.toInt() }
34+
}
35+
}
36+
37+
fun deconfigure(project: IProject) {
38+
KotlinCodeStyleProperties(ProjectScope(project)).apply {
39+
globalsOverridden = false
40+
codeStyleId?.also { KotlinCodeStyleManager.invalidate(it) }
41+
saveChanges()
42+
}
43+
}
44+
45+
private operator fun Any.set(name: String, value: Any) {
46+
this::class.members.single { it.name in setOf(name) }.let {
47+
when (it) {
48+
is KMutableProperty -> it.setter.call(this, value)
49+
is KFunction -> it.call(this, value)
50+
else -> throw AssertionError("Field or method with name $name does not exist")
51+
}
52+
}
53+
}
54+
}

kotlin-eclipse-test-framework/src/org/jetbrains/kotlin/testframework/utils/TestJavaProject.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ public IJavaProject getJavaProject() {
213213
return javaProject;
214214
}
215215

216+
public IProject getProject() {
217+
return project;
218+
}
219+
216220
public void addKotlinRuntime() throws CoreException {
217221
ProjectUtils.addKotlinRuntime(javaProject);
218222
}

kotlin-eclipse-ui-test/src/org/jetbrains/kotlin/ui/tests/editors/KotlinAutoIndentTestCase.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import org.eclipse.ui.editors.text.EditorsUI
77
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants
88
import org.jetbrains.kotlin.testframework.utils.EditorTestUtils
99
import org.jetbrains.kotlin.ui.tests.editors.formatter.KotlinFormatActionTestCase
10-
import org.jetbrains.kotlin.ui.formatter.settings
1110
import com.intellij.psi.codeStyle.CodeStyleSettings
11+
import org.jetbrains.kotlin.testframework.utils.CodeStyleConfigurator
1212
import org.junit.After
1313

1414
abstract class KotlinAutoIndentTestCase : KotlinEditorWithAfterFileTestCase() {
@@ -19,11 +19,11 @@ abstract class KotlinAutoIndentTestCase : KotlinEditorWithAfterFileTestCase() {
1919

2020
@After
2121
fun setDefaultSettings() {
22-
settings = CodeStyleSettings()
22+
CodeStyleConfigurator.deconfigure(testProject.project)
2323
}
2424

2525
override fun performTest(fileText: String, expectedFileText: String) {
26-
KotlinFormatActionTestCase.configureSettings(fileText)
26+
CodeStyleConfigurator.configure(testProject.project, fileText)
2727

2828
EditorsUI.getPreferenceStore().setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS, true)
2929
EditorsUI.getPreferenceStore().setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH, 4)

kotlin-eclipse-ui-test/src/org/jetbrains/kotlin/ui/tests/editors/formatter/KotlinFormatActionTestCase.java

Lines changed: 4 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,18 @@
1616
*******************************************************************************/
1717
package org.jetbrains.kotlin.ui.tests.editors.formatter;
1818

19-
import java.lang.reflect.Field;
20-
import java.lang.reflect.InvocationTargetException;
21-
import java.lang.reflect.Method;
22-
import java.util.Arrays;
23-
import java.util.List;
24-
2519
import org.eclipse.jface.text.BadLocationException;
2620
import org.eclipse.jface.text.IDocument;
2721
import org.eclipse.jface.text.TextUtilities;
2822
import org.eclipse.ui.editors.text.EditorsUI;
2923
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
30-
import org.jetbrains.kotlin.idea.KotlinLanguage;
31-
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings;
3224
import org.jetbrains.kotlin.testframework.editor.KotlinEditorWithAfterFileTestCase;
25+
import org.jetbrains.kotlin.testframework.utils.CodeStyleConfigurator;
3326
import org.jetbrains.kotlin.testframework.utils.EditorTestUtils;
34-
import org.jetbrains.kotlin.testframework.utils.InTextDirectivesUtils;
3527
import org.junit.After;
3628
import org.junit.Assert;
3729
import org.junit.Before;
3830

39-
import com.intellij.psi.codeStyle.CodeStyleSettings;
40-
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
41-
42-
import kotlin.Pair;
43-
import kotlin.collections.CollectionsKt;
44-
import kotlin.jvm.functions.Function1;
45-
import org.jetbrains.kotlin.ui.formatter.KotlinFormatterKt;
46-
4731
public abstract class KotlinFormatActionTestCase extends KotlinEditorWithAfterFileTestCase {
4832
@Before
4933
public void before() {
@@ -52,7 +36,7 @@ public void before() {
5236

5337
@After
5438
public void setDefaultSettings() {
55-
KotlinFormatterKt.setSettings(new CodeStyleSettings());
39+
CodeStyleConfigurator.INSTANCE.deconfigure(getTestProject().getProject());
5640
}
5741

5842
@Override
@@ -61,8 +45,8 @@ protected void performTest(String fileText, String content) {
6145

6246
EditorsUI.getPreferenceStore().setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS, true);
6347
EditorsUI.getPreferenceStore().setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH, 4);
64-
65-
configureSettings(fileText);
48+
49+
CodeStyleConfigurator.INSTANCE.configure(getTestProject().getProject(), fileText);
6650

6751
getTestEditor().runFormatAction();
6852

@@ -79,94 +63,4 @@ private void assertLineDelimiters(String expectedLineDelimiter, IDocument docume
7963
throw new RuntimeException(e);
8064
}
8165
}
82-
83-
public static void configureSettings(String fileText) {
84-
List<String> settingsToTrue = InTextDirectivesUtils.findListWithPrefixes(fileText, "SET_TRUE:");
85-
List<String> settingsToFalse = InTextDirectivesUtils.findListWithPrefixes(fileText, "SET_FALSE:");
86-
List<Pair> settingsToIntValue = CollectionsKt.map(InTextDirectivesUtils.findListWithPrefixes(fileText, "SET_INT:"), new Function1<String, Pair>() {
87-
@Override
88-
public Pair<String, Integer> invoke(String s) {
89-
String[] tokens = s.split("=");
90-
return new Pair<String, Integer>(tokens[0].trim(), Integer.valueOf(tokens[1].trim()));
91-
}
92-
});
93-
94-
KotlinCodeStyleSettings kotlinSettings = KotlinFormatterKt.getSettings().getCustomSettings(KotlinCodeStyleSettings.class);
95-
CommonCodeStyleSettings commonSettings = KotlinFormatterKt.getSettings().getCommonSettings(KotlinLanguage.INSTANCE);
96-
97-
List<Object> objects = Arrays.asList(kotlinSettings, commonSettings);
98-
99-
for (String trueSetting : settingsToTrue) {
100-
setBooleanSetting(trueSetting, true, objects);
101-
}
102-
103-
for (String falseSetting : settingsToFalse) {
104-
setBooleanSetting(falseSetting, false, objects);
105-
}
106-
107-
for (Pair<String, Integer> setting : settingsToIntValue) {
108-
setIntSetting(setting.getFirst(), setting.getSecond(), objects);
109-
}
110-
111-
String rightMarginString = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// RIGHT_MARGIN: ");
112-
if (rightMarginString != null) {
113-
Integer rightMargin = Integer.parseInt(rightMarginString);
114-
commonSettings.RIGHT_MARGIN = rightMargin;
115-
}
116-
117-
}
118-
119-
public static void setBooleanSetting(String setting, Boolean value, List<Object> objects) {
120-
setSettingValue(setting, value, boolean.class, objects);
121-
}
122-
123-
public static void setIntSetting(String setting, Integer value, List<Object> objects) {
124-
setSettingValue(setting, value, int.class, objects);
125-
}
126-
127-
public static void setSettingValue(String settingName, Object value, Class<?> valueType, List<Object> objects) {
128-
for (Object object : objects) {
129-
if (setSettingWithField(settingName, object, value) || setSettingWithMethod(settingName, object, value, valueType)) {
130-
return;
131-
}
132-
}
133-
134-
throw new IllegalArgumentException(String.format(
135-
"There's no property or method with name '%s' in given objects: %s", settingName, objects));
136-
}
137-
138-
private static boolean setSettingWithField(String settingName, Object object, Object value) {
139-
try {
140-
Field field = object.getClass().getDeclaredField(settingName);
141-
field.set(object, value);
142-
return true;
143-
}
144-
catch (IllegalAccessException e) {
145-
throw new IllegalArgumentException(String.format("Can't set property with the name %s in object %s", settingName, object));
146-
}
147-
catch (NoSuchFieldException e) {
148-
// Do nothing - will try other variants
149-
}
150-
151-
return false;
152-
}
153-
154-
private static boolean setSettingWithMethod(String setterName, Object object, Object value, Class<?> valueType) {
155-
try {
156-
Method method = object.getClass().getMethod(setterName, valueType);
157-
method.invoke(object, value);
158-
return true;
159-
}
160-
catch (InvocationTargetException e) {
161-
throw new IllegalArgumentException(String.format("Can't call method with name %s for object %s", setterName, object));
162-
}
163-
catch (IllegalAccessException e) {
164-
throw new IllegalArgumentException(String.format("Can't access to method with name %s for object %s", setterName, object));
165-
}
166-
catch (NoSuchMethodException e) {
167-
// Do nothing - will try other variants
168-
}
169-
170-
return false;
171-
}
17266
}

0 commit comments

Comments
 (0)