From 44f8838364b0572fefeed84ad1304e4dd5575fec Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Tue, 1 Aug 2017 18:50:31 +0300 Subject: [PATCH] Code Style settings tests grouped together Base CodeStyleTestCase class --- .../projectCodeStyleSettings.xml | 0 .../importSettings/standardCodeStyleXml.xml | 0 .../CodeStyleConfigurationTest.java | 131 ++++++++++++++++++ .../CodeStyleSchemesModelTest.java | 3 +- .../codeStyleSettings/CodeStyleTestCase.java | 47 +++++++ .../CodeStyleXmlImporterTest.java | 23 ++- 6 files changed, 197 insertions(+), 7 deletions(-) rename platform/{lang-impl => platform-tests}/testData/codeStyle/importSettings/projectCodeStyleSettings.xml (100%) rename platform/{lang-impl => platform-tests}/testData/codeStyle/importSettings/standardCodeStyleXml.xml (100%) create mode 100644 platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleConfigurationTest.java create mode 100644 platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleTestCase.java rename platform/{lang-impl/testSources/com/intellij/application/options/codeStyle/importSettings => platform-tests/testSrc/com/intellij/ide/codeStyleSettings}/CodeStyleXmlImporterTest.java (72%) diff --git a/platform/lang-impl/testData/codeStyle/importSettings/projectCodeStyleSettings.xml b/platform/platform-tests/testData/codeStyle/importSettings/projectCodeStyleSettings.xml similarity index 100% rename from platform/lang-impl/testData/codeStyle/importSettings/projectCodeStyleSettings.xml rename to platform/platform-tests/testData/codeStyle/importSettings/projectCodeStyleSettings.xml diff --git a/platform/lang-impl/testData/codeStyle/importSettings/standardCodeStyleXml.xml b/platform/platform-tests/testData/codeStyle/importSettings/standardCodeStyleXml.xml similarity index 100% rename from platform/lang-impl/testData/codeStyle/importSettings/standardCodeStyleXml.xml rename to platform/platform-tests/testData/codeStyle/importSettings/standardCodeStyleXml.xml diff --git a/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleConfigurationTest.java b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleConfigurationTest.java new file mode 100644 index 000000000000..e12449aaa18c --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleConfigurationTest.java @@ -0,0 +1,131 @@ +/* + * Copyright 2000-2017 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.ide.codeStyleSettings; + +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable; +import com.intellij.psi.codeStyle.CommonCodeStyleSettings; +import org.jdom.Element; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +/** + * @author Rustam Vishnyakov + */ +public class CodeStyleConfigurationTest extends CodeStyleTestCase { + + /** + * Check that indent options are correcly read if mixed with other language options + */ + public void testIndentOptionsRead() throws Exception { + org.jdom.Element rootElement = new Element("option"); + + Element langCodeStyle = new Element("codeStyleSettings"); + langCodeStyle.setAttribute("language", "JavaScript"); + langCodeStyle.addContent(createOption("IF_BRACE_FORCE", "3")); + Element indentOptionsElement = new Element("indentOptions"); + indentOptionsElement.addContent(createOption("INDENT_SIZE", "2")); + indentOptionsElement.addContent(createOption("CONTINUATION_INDENT_SIZE", "3")); + indentOptionsElement.addContent(createOption("TAB_SIZE", "2")); + indentOptionsElement.addContent(createOption("USE_TAB_CHARACTER", "true")); + langCodeStyle.addContent(indentOptionsElement); + rootElement.addContent(langCodeStyle); + + CodeStyleSettings settings = new CodeStyleSettings(); + settings.readExternal(rootElement); + CommonCodeStyleSettings langSettings = settings.getCommonSettings("JavaScript"); + assert langSettings != null; + CommonCodeStyleSettings.IndentOptions indentOptions = langSettings.getIndentOptions(); + assert indentOptions != null; + assertEquals(2, indentOptions.INDENT_SIZE); + assertEquals(3, indentOptions.CONTINUATION_INDENT_SIZE); + assertEquals(2, indentOptions.TAB_SIZE); + assertEquals(true, indentOptions.USE_TAB_CHARACTER); + assertEquals(3, langSettings.IF_BRACE_FORCE); + } + + public void testCodeStyleSettingsCustomizableOptions() { + Field[] commonFields = CommonCodeStyleSettings.class.getFields(); + for (Field field : commonFields) { + boolean fieldExistsInCustomizable = false; + String fieldName = field.getName(); + int fieldModifiers = field.getModifiers(); + if (Modifier.isPublic(fieldModifiers) && + !Modifier.isStatic(fieldModifiers) && + !"PARENT_SETTINGS_INSTALLED".equals(fieldName) && + !"FORCE_REARRANGE_MODE".equals(fieldName) + ) { + for (CodeStyleSettingsCustomizable.SpacingOption option : CodeStyleSettingsCustomizable.SpacingOption.values()) { + if (option.toString().equals(field.getName())) { + fieldExistsInCustomizable = true; + break; + } + } + if (!fieldExistsInCustomizable) { + for (CodeStyleSettingsCustomizable.WrappingOrBraceOption option : CodeStyleSettingsCustomizable.WrappingOrBraceOption.values()) { + if (option.toString().equals(field.getName())) { + fieldExistsInCustomizable = true; + break; + } + } + } + if (!fieldExistsInCustomizable) { + for (CodeStyleSettingsCustomizable.BlankLinesOption option : CodeStyleSettingsCustomizable.BlankLinesOption.values()) { + if (option.toString().equals(field.getName())) { + fieldExistsInCustomizable = true; + break; + } + } + } + if (!fieldExistsInCustomizable) { + for (CodeStyleSettingsCustomizable.CommenterOption option : CodeStyleSettingsCustomizable.CommenterOption.values()) { + if (option.toString().equals(field.getName())) { + fieldExistsInCustomizable = true; + break; + } + } + } + assertTrue("Field " + field.getName() + " is not declared in CodeStyleSettingsCustomizable", fieldExistsInCustomizable); + } + } + } + + public void testSaveOtherOptionsChanged() throws Exception { + CodeStyleSettings settings = new CodeStyleSettings(); + settings.OTHER_INDENT_OPTIONS.INDENT_SIZE = 2; + Element root = createOption("config", "root"); + settings.writeExternal(root); + assertXmlOutputEquals( + "\n" + + "", + root); + } + + +} diff --git a/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleSchemesModelTest.java b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleSchemesModelTest.java index a1351ef9c394..30a32da42618 100644 --- a/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleSchemesModelTest.java +++ b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleSchemesModelTest.java @@ -20,11 +20,10 @@ import com.intellij.psi.codeStyle.CodeStyleScheme; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeImpl; -import com.intellij.testFramework.LightPlatformTestCase; import java.util.List; -public class CodeStyleSchemesModelTest extends LightPlatformTestCase { +public class CodeStyleSchemesModelTest extends CodeStyleTestCase { private CodeStyleSchemesModel myModel; private CodeStyleScheme myDefaultScheme; diff --git a/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleTestCase.java b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleTestCase.java new file mode 100644 index 000000000000..d94db376e7d9 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleTestCase.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2017 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.ide.codeStyleSettings; + +import com.intellij.openapi.application.ex.PathManagerEx; +import com.intellij.testFramework.LightPlatformTestCase; +import org.jdom.Element; +import org.jdom.output.Format; +import org.jdom.output.XMLOutputter; + +import java.io.IOException; +import java.io.StringWriter; + +public abstract class CodeStyleTestCase extends LightPlatformTestCase { + + protected static final String BASE_PATH = PathManagerEx.getTestDataPath("/../../../platform/platform-tests/testData/codeStyle/"); + + @SuppressWarnings("Duplicates") + public static void assertXmlOutputEquals(String expected, Element root) throws IOException { + StringWriter writer = new StringWriter(); + Format format = Format.getPrettyFormat(); + format.setLineSeparator("\n"); + new XMLOutputter(format).output(root, writer); + String actual = writer.toString(); + assertEquals(expected, actual); + } + + protected static Element createOption(String name, String value) { + Element optionElement = new Element("option"); + optionElement.setAttribute("name", name); + optionElement.setAttribute("value", value); + return optionElement; + } +} diff --git a/platform/lang-impl/testSources/com/intellij/application/options/codeStyle/importSettings/CodeStyleXmlImporterTest.java b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleXmlImporterTest.java similarity index 72% rename from platform/lang-impl/testSources/com/intellij/application/options/codeStyle/importSettings/CodeStyleXmlImporterTest.java rename to platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleXmlImporterTest.java index fba88b0da9ee..20983b1084f6 100644 --- a/platform/lang-impl/testSources/com/intellij/application/options/codeStyle/importSettings/CodeStyleXmlImporterTest.java +++ b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleXmlImporterTest.java @@ -1,6 +1,20 @@ -package com.intellij.application.options.codeStyle.importSettings; +/* + * Copyright 2000-2017 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.ide.codeStyleSettings; -import com.intellij.openapi.application.ex.PathManagerEx; import com.intellij.openapi.options.SchemeFactory; import com.intellij.openapi.options.SchemeImportException; import com.intellij.openapi.vfs.VfsUtil; @@ -9,13 +23,12 @@ import com.intellij.psi.codeStyle.CodeStyleScheme; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeImpl; import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeXmlImporter; -import com.intellij.testFramework.LightPlatformTestCase; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; -public class CodeStyleXmlImporterTest extends LightPlatformTestCase { +public class CodeStyleXmlImporterTest extends CodeStyleTestCase { public void testStandardCodeStyleXml() throws SchemeImportException { CodeStyleSettings settings= importSettings(); assertEquals(false, settings.AUTODETECT_INDENTS); @@ -47,7 +60,7 @@ public class CodeStyleXmlImporterTest extends LightPlatformTestCase { @NotNull protected String getTestDataPath() { - return PathManagerEx.getTestDataPath() + "/../../../platform/lang-impl/testData/codeStyle/importSettings/"; + return BASE_PATH + "importSettings/"; } }