mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[formatter tests] migrated last GeneralCodeFormatterTest to new way of formatting model specification
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
package com.intellij.formatting;
|
||||
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.testFramework.LightPlatformTestCase;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
@NonNls
|
||||
public class GeneralCodeFormatterTest extends LightPlatformTestCase {
|
||||
|
||||
public void testLastLineIndent() throws Exception{
|
||||
final String initialText = "a\n";
|
||||
final TestFormattingModel model = new TestFormattingModel(initialText);
|
||||
|
||||
model.setRootBlock(new FormattingModelXmlReader(model).readTestBlock("lineIndent"));
|
||||
final CommonCodeStyleSettings.IndentOptions indentOptions = new CommonCodeStyleSettings.IndentOptions();
|
||||
indentOptions.CONTINUATION_INDENT_SIZE = 8;
|
||||
indentOptions.INDENT_SIZE = 4;
|
||||
indentOptions.LABEL_INDENT_SIZE = 1;
|
||||
final CodeStyleSettings settings = new CodeStyleSettings(false);
|
||||
settings.setDefaultRightMargin(120);
|
||||
try {
|
||||
FormatterEx.getInstanceEx().adjustLineIndent(model, settings, indentOptions, initialText.length() - 1, new TextRange(0, initialText.length()));
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
fail();
|
||||
}
|
||||
|
||||
assertEquals("a\n ", FormatterImpl.getText(model));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,11 @@ import com.intellij.formatting.engine.testModel.TestBlock
|
||||
import com.intellij.openapi.util.TextRange
|
||||
|
||||
|
||||
class BlockAttributes(val alignment: Alignment? = null, val wrap: Wrap? = null, val indent: Indent? = null, val spacing: Spacing? = null)
|
||||
class BlockAttributes(val alignment: Alignment? = null,
|
||||
val wrap: Wrap? = null,
|
||||
val indent: Indent? = null,
|
||||
val spacing: Spacing? = null,
|
||||
val isIncomplete: Boolean = false)
|
||||
|
||||
class CompositeTestBlock(startOffset: Int,
|
||||
attributes: BlockAttributes,
|
||||
@@ -66,8 +70,8 @@ abstract class TestBlockBase(val startOffset: Int,
|
||||
fun getSpacing() = attributes.spacing
|
||||
fun firstChild() = subBlocks.firstOrNull()
|
||||
|
||||
override fun getChildAttributes(newChildIndex: Int): ChildAttributes = ChildAttributes.DELEGATE_TO_NEXT_CHILD
|
||||
override fun isIncomplete() = false
|
||||
override fun getChildAttributes(newChildIndex: Int): ChildAttributes = ChildAttributes(indent, null)
|
||||
override fun isIncomplete() = attributes.isIncomplete
|
||||
|
||||
abstract val endOffset: Int
|
||||
|
||||
@@ -85,10 +89,13 @@ class AttributesProvider {
|
||||
val indent = getIndent(attributes)
|
||||
val spacing = getSpacing(attributes)
|
||||
val wrap = getWrap(attributes)
|
||||
val isIncomplete = isIncomplete(attributes)
|
||||
|
||||
return BlockAttributes(alignment, wrap, indent, spacing)
|
||||
return BlockAttributes(alignment, wrap, indent, spacing, isIncomplete)
|
||||
}
|
||||
|
||||
private fun isIncomplete(attributes: List<String>) = attributes.contains("incomplete")
|
||||
|
||||
private fun getWrap(attributes: List<String>): Wrap? {
|
||||
val wrap = attributes.find { it.startsWith("w_") }?.substring(2) ?: return null
|
||||
val matcher = "([a-z]*)([0-9]*)".toPattern().matcher(wrap)
|
||||
|
||||
+25
-13
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.formatting.engine
|
||||
|
||||
import com.intellij.formatting.Block
|
||||
import com.intellij.formatting.FormatterEx
|
||||
import com.intellij.formatting.engine.testModel.TestFormattingModel
|
||||
import com.intellij.formatting.engine.testModel.getRoot
|
||||
@@ -29,7 +30,7 @@ class FormatterEngineTests : LightPlatformTestCase() {
|
||||
|
||||
@Test
|
||||
fun `test simple alignment`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[a0]fooooo [a1]foo
|
||||
[a0]go [a1]boo
|
||||
@@ -42,7 +43,7 @@ go boo
|
||||
|
||||
@Test
|
||||
fun `test empty block alignment`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[a0]fooooo [a1]
|
||||
[a0]go [a1]boo
|
||||
@@ -56,23 +57,34 @@ go boo
|
||||
|
||||
}
|
||||
|
||||
fun doTest(before: String, expectedText: String, settings: CodeStyleSettings = CodeStyleSettings()) {
|
||||
var root = getRoot(before.trimStart())
|
||||
var beforeText = root.text
|
||||
class TestData(val rootBlock: Block, val textToFormat: String, val markerPosition: Int?)
|
||||
|
||||
val rightMargin = beforeText.indexOf('|')
|
||||
if (rightMargin > 0) {
|
||||
fun doReformatTest(before: String, expectedText: String, settings: CodeStyleSettings = CodeStyleSettings()) {
|
||||
val data = extractFormattingTestData(before)
|
||||
|
||||
val rightMargin = data.markerPosition
|
||||
if (rightMargin != null) {
|
||||
settings.setRightMargin(null, rightMargin)
|
||||
root = getRoot(before.trimStart().replace("|", ""))
|
||||
beforeText = root.text
|
||||
}
|
||||
|
||||
val rootFormattingBlock = root.toFormattingBlock(0)
|
||||
|
||||
val document = EditorFactory.getInstance().createDocument(beforeText)
|
||||
val model = TestFormattingModel(rootFormattingBlock, document)
|
||||
val document = EditorFactory.getInstance().createDocument(data.textToFormat)
|
||||
val model = TestFormattingModel(data.rootBlock, document)
|
||||
|
||||
FormatterEx.getInstanceEx().format(model, settings, settings.indentOptions, null)
|
||||
|
||||
TestCase.assertEquals(expectedText.trimStart(), document.text)
|
||||
}
|
||||
|
||||
fun extractFormattingTestData(before: String) : TestData {
|
||||
var root = getRoot(before.trimStart())
|
||||
var beforeText = root.text
|
||||
|
||||
val marker = beforeText.indexOf('|')
|
||||
if (marker > 0) {
|
||||
root = getRoot(before.trimStart().replace("|", ""))
|
||||
beforeText = root.text
|
||||
}
|
||||
|
||||
val rootBlock = root.toFormattingBlock(0)
|
||||
return TestData(rootBlock, beforeText, if (marker > 0) marker else null)
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.formatting.engine
|
||||
|
||||
import com.intellij.formatting.FormatterEx
|
||||
import com.intellij.formatting.engine.testModel.TestFormattingModel
|
||||
import com.intellij.openapi.editor.EditorFactory
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings
|
||||
import com.intellij.testFramework.LightPlatformTestCase
|
||||
import junit.framework.TestCase
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Migrated from GeneralCodeFormatterTest.java
|
||||
*/
|
||||
class GeneralAdjustLineIndentTest : LightPlatformTestCase() {
|
||||
|
||||
@Test
|
||||
fun `test adjust line indent`() {
|
||||
val before = "[i_norm incomplete]a\n"
|
||||
val expectedText = "a\n "
|
||||
|
||||
val data = extractFormattingTestData(before)
|
||||
|
||||
val document = EditorFactory.getInstance().createDocument(data.textToFormat)
|
||||
val model = TestFormattingModel(data.rootBlock.subBlocks[0], document)
|
||||
|
||||
val settings = CodeStyleSettings()
|
||||
val textRange = TextRange(0, document.textLength)
|
||||
|
||||
FormatterEx.getInstanceEx().adjustLineIndent(model, settings, settings.indentOptions, document.textLength - 1, textRange)
|
||||
|
||||
TestCase.assertEquals(expectedText.trimStart(), document.text)
|
||||
}
|
||||
|
||||
}
|
||||
+23
-23
@@ -26,7 +26,7 @@ class GeneralCodeFormatterTest : LightPlatformTestCase() {
|
||||
|
||||
@Test
|
||||
fun `test null indent is treated as continuation indent`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[]aaa []bbb []ccc
|
||||
[]ddd []eee []fff
|
||||
@@ -40,7 +40,7 @@ aaa bbb ccc
|
||||
|
||||
@Test
|
||||
fun `test continuation indent`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[i_none]a
|
||||
[i_none]([i_cont]b
|
||||
@@ -56,7 +56,7 @@ a
|
||||
|
||||
@Test
|
||||
fun `test normal indent`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[i_none]a
|
||||
[i_none]([i_norm]b
|
||||
@@ -72,7 +72,7 @@ a
|
||||
|
||||
@Test
|
||||
fun `test many nested blocks and continuation indent`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[]([]([]([]a
|
||||
[]b
|
||||
@@ -96,13 +96,13 @@ a
|
||||
fun `test indents composition`() {
|
||||
val settings = CodeStyleSettings()
|
||||
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
|
||||
doTest("""
|
||||
doReformatTest("""
|
||||
[i_none]aaa [i_none]bbb
|
||||
[i_none]([i_norm]ccc [i_norm]ddd
|
||||
[i_norm]([i_label]eee
|
||||
[i_label]fff))
|
||||
""",
|
||||
"""
|
||||
"""
|
||||
aaa bbb
|
||||
ccc ddd
|
||||
eee
|
||||
@@ -112,7 +112,7 @@ aaa bbb
|
||||
|
||||
@Test
|
||||
fun `test alignments on different block levels`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[]aaa [a1]bbb
|
||||
[a1]([i_label]ccc [i_label]ddd
|
||||
@@ -130,7 +130,7 @@ aaa bbb
|
||||
|
||||
@Test
|
||||
fun `test nested indents`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[]xxx
|
||||
[i_cont]([i_cont]([i_cont]yyy))
|
||||
@@ -144,7 +144,7 @@ xxx
|
||||
|
||||
@Test
|
||||
fun `test one more alignment test`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[]aaa [a1]bbb [a1]([i_label]ccc [i_label]ddd [i_label a1]([]eee
|
||||
[a1]fff))
|
||||
@@ -158,7 +158,7 @@ aaa bbb ccc ddd eee
|
||||
|
||||
@Test
|
||||
fun `test trivial spaces`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[]foo [s_min3]goo
|
||||
""",
|
||||
@@ -172,7 +172,7 @@ foo goo
|
||||
fun `test space properties`() {
|
||||
val settings = CodeStyleSettings()
|
||||
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[]aaa [s_min2_max2]bbb [s_min2_max2]([i_norm]ccc
|
||||
[i_norm s_min2_max2]ddd [i_norm s_min2_max2]([i_label]eee [i_label s_min2_max2_minlf2]fff))
|
||||
@@ -187,7 +187,7 @@ aaa bbb ccc
|
||||
|
||||
@Test
|
||||
fun `test remove all spaces`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[s_min0_max0_keepLb0]0 [s_min0_max0_keepLb0]1
|
||||
[s_min0_max0_keepLb0]2
|
||||
@@ -198,12 +198,12 @@ aaa bbb ccc
|
||||
|
||||
@Test
|
||||
fun `test no wrap object no text wrap`() {
|
||||
doTest("[]aaa []bbb []ccc []ddd []eee []f|ff", "aaa bbb ccc ddd eee fff")
|
||||
doReformatTest("[]aaa []bbb []ccc []ddd []eee []f|ff", "aaa bbb ccc ddd eee fff")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test simple wrap`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"""
|
||||
[]aaa [w_normal i_cont]b|bb
|
||||
""",
|
||||
@@ -218,7 +218,7 @@ aaa
|
||||
val settings = CodeStyleSettings()
|
||||
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
|
||||
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"[i_none w_always]aaa " +
|
||||
"[i_none s_min2_max2 w_always]bbb " +
|
||||
"[i_none s_min2_max2]" +
|
||||
@@ -241,7 +241,7 @@ bbb
|
||||
val settings = CodeStyleSettings()
|
||||
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
|
||||
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"[w_normal]aaa " +
|
||||
"[s_min2_max2 w_normal]bbb " +
|
||||
"[s_min2_max2 w_normal]" +
|
||||
@@ -258,7 +258,7 @@ bbb
|
||||
val settings = CodeStyleSettings()
|
||||
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
|
||||
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"[w_chop1]aaa [s_min2_max2 w_chop1]bbb " +
|
||||
"[]([i_label s_min2_max2 w_chop1]cc|c " +
|
||||
"[i_label s_min2_max2 w_chop1]ddd " +
|
||||
@@ -277,7 +277,7 @@ aaa
|
||||
fun `test wrap in the middle`() {
|
||||
val settings = CodeStyleSettings()
|
||||
settings.indentOptions!!.LABEL_INDENT_SIZE = 1
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"[]aaa [s_min2_max2]bbb " +
|
||||
"[]" +
|
||||
"([i_label s_min2_max2]ccc " +
|
||||
@@ -292,23 +292,23 @@ aaa bbb ccc
|
||||
|
||||
@Test
|
||||
fun `test multiple wrap`() {
|
||||
doTest("[w_normal]a[w_normal]b|[w_normal]([w_normal]c)",
|
||||
"ab\n" +
|
||||
doReformatTest("[w_normal]a[w_normal]b|[w_normal]([w_normal]c)",
|
||||
"ab\n" +
|
||||
" c")
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test nested calls`() {
|
||||
doTest("[i_none]1 [i_none]([i_cont]a[i_none]([i_cont]2 [i_none]([i_cont]b[i_none]([i_cont]3\n[i_none]([i_cont]c)))))",
|
||||
"""
|
||||
doReformatTest("[i_none]1 [i_none]([i_cont]a[i_none]([i_cont]2 [i_none]([i_cont]b[i_none]([i_cont]3\n[i_none]([i_cont]c)))))",
|
||||
"""
|
||||
1 a2 b3
|
||||
c""")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test different wraps`() {
|
||||
doTest(
|
||||
doReformatTest(
|
||||
"[i_none w_normal]([w_normal]x[w_normal]a[w_normal]b[w_normal]x)" +
|
||||
"[i_none w_normal]([w_normal]x[w_normal]c[w_normal]d[w_normal]x)" +
|
||||
"[i_none w_normal]([w_normal]x[w_normal]e[w_normal]|f[w_normal]x)",
|
||||
|
||||
Reference in New Issue
Block a user