From 9c0de9063f0b2daee043d7356eb347c5bcca8d9c Mon Sep 17 00:00:00 2001 From: Valentin Fondaratov Date: Thu, 2 Feb 2017 00:37:40 +0300 Subject: [PATCH] change inlay hints testing api The current approach allows for greater test source flexibility. Also it enables the use of FileComparisonFailure to ease working with tests. --- .../inlays/JavaParameterNameHintsTest.kt | 69 ++++++++++--------- .../fixtures/CodeInsightTestFixture.java | 2 +- .../impl/CodeInsightTestFixtureImpl.java | 4 +- .../utils/inlays/InlayParameterHintsTest.kt | 18 +++-- 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/inlays/JavaParameterNameHintsTest.kt b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/inlays/JavaParameterNameHintsTest.kt index f47ec5929530..c4b6584ae89f 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/inlays/JavaParameterNameHintsTest.kt +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/inlays/JavaParameterNameHintsTest.kt @@ -22,7 +22,8 @@ import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase class JavaInlayParameterHintsTest : LightCodeInsightFixtureTestCase() { fun check(text: String) { - myFixture.testInlays("A.java", text) + myFixture.configureByText("A.java", text) + myFixture.testInlays() } fun `test insert literal arguments`() { @@ -51,11 +52,11 @@ class Groo { fun `test do not show for Exceptions`() { check(""" class Fooo { - + public void test() { Throwable t = new IllegalStateException("crime"); } - + } """) } @@ -77,7 +78,7 @@ class Fooo { }""") } - + fun `test no hints for generic builders`() { check(""" @@ -98,7 +99,7 @@ class Stream { """) } - + fun `test do not show hints on setters`() { check("""class Groo { @@ -114,7 +115,7 @@ class Stream { }""") } - + fun `test single varargs hint`() { check(""" @@ -150,7 +151,7 @@ public class VarArgTest { """) } - + fun `test multiple vararg hint`() { check(""" public class VarArgTest { @@ -167,8 +168,8 @@ public class VarArgTest { } """) } - - + + fun `test do not inline known subsequent parameter names`() { check(""" public class Test { @@ -196,7 +197,7 @@ public class Test { } """) } - + fun `test show if can be assigned`() { check(""" @@ -242,34 +243,34 @@ public class Test { } } """) - + } fun `test ignored methods`() { check(""" public class Test { - + List list = new ArrayList<>(); StringBuilder builder = new StringBuilder(); public void main() { System.out.println("A"); System.out.print("A"); - + list.add("sss"); list.get(1); list.set(1, "sss"); - + setNewIndex(10); "sss".contains("s"); builder.append("sdfsdf"); "sfsdf".startWith("s"); "sss".charAt(3); - + clearStatus(false); } - + void print(String s) {} void println(String s) {} void get(int index) {} @@ -365,7 +366,7 @@ public class VarArgTest { fun `test if any param matches inline all`() { check(""" public class VarArgTest { - + public void main() { check(10, 1000); } @@ -380,7 +381,7 @@ public class VarArgTest { fun `test inline common name pair if more that 2 args`() { check(""" public class VarArgTest { - + public void main() { String s = "su"; check(10, 1000, s); @@ -396,11 +397,11 @@ public class VarArgTest { fun `test ignore String methods`() { check(""" class Test { - + public void main() { String.format("line", "eee", "www"); } - + } """) } @@ -408,7 +409,7 @@ class Test { fun `test inline common name pair if more that 2 args xxx`() { check(""" public class VarArgTest { - + public void main() { check(10, 1000, "su"); } @@ -423,7 +424,7 @@ public class VarArgTest { fun `test inline this`() { check(""" public class VarArgTest { - + public void main() { check(this, 1000); } @@ -438,7 +439,7 @@ public class VarArgTest { fun `test inline strange methods`() { check(""" public class Test { - + void main() { createContent(null); createNewContent(this); @@ -462,13 +463,13 @@ class Builder { } class Test { - + public void test() { Builder builder = new Builder(); builder.await(true); builder.bwait(false).timeWait(100); } - + } """) } @@ -494,7 +495,7 @@ class Test { fun `test do not show single parameter hint if it is string literal`() { check(""" public class Test { - + public void test() { debug("Error message"); info("Error message", new Object()); @@ -502,11 +503,11 @@ public class Test { void debug(String message) {} void info(String message, Object error) {} - + } """) } - + fun `test show single`() { check(""" class Test { @@ -531,13 +532,13 @@ class Test { fun `test do not show for setters`() { check(""" class Test { - + void main() { set(10); } - + void set(int newValue) {} - + } """) } @@ -556,13 +557,13 @@ class Test { fun `test more blacklisted items`() { check(""" class Test { - + void test() { System.getProperty("aaa"); System.setProperty("aaa", "bbb"); new Key().create(10); } - + } class Key { @@ -592,7 +593,7 @@ class Test { check(""" class Test { void test() { - check(1000); + check(1000); } void check(int isShow) {} } diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/CodeInsightTestFixture.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/CodeInsightTestFixture.java index 342f9884b6c7..207ed7b12a5a 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/CodeInsightTestFixture.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/CodeInsightTestFixture.java @@ -539,7 +539,7 @@ public interface CodeInsightTestFixture extends IdeaProjectTestFixture { void testRainbow(@NotNull String fileName, @NotNull String text, boolean isRainbowOn, boolean withColor); - void testInlays(@NotNull String fileName, @NotNull String text); + void testInlays(); void assertPreferredCompletionItems(int selected, @NotNull String... expected); diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java index e3e6cacda9a7..661f6ce855ef 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -1696,11 +1696,11 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig } @Override - public void testInlays(@NotNull String fileName, @NotNull String text) { + public void testInlays() { InlayHintsChecker checker = new InlayHintsChecker(this); try { checker.setUp(); - checker.checkInlays(fileName, text); + checker.checkInlays(); } finally { checker.tearDown(); diff --git a/platform/testFramework/src/com/intellij/testFramework/utils/inlays/InlayParameterHintsTest.kt b/platform/testFramework/src/com/intellij/testFramework/utils/inlays/InlayParameterHintsTest.kt index d0576625fa29..9626e381d1d8 100644 --- a/platform/testFramework/src/com/intellij/testFramework/utils/inlays/InlayParameterHintsTest.kt +++ b/platform/testFramework/src/com/intellij/testFramework/utils/inlays/InlayParameterHintsTest.kt @@ -21,8 +21,10 @@ import com.intellij.codeInsight.hints.settings.ParameterNameHintsSettings import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.editor.Document import com.intellij.openapi.editor.ex.EditorSettingsExternalizable +import com.intellij.rt.execution.junit.FileComparisonFailure +import com.intellij.testFramework.VfsTestUtil import com.intellij.testFramework.fixtures.CodeInsightTestFixture -import org.junit.Assert.assertEquals +import junit.framework.ComparisonFailure import java.util.regex.Pattern @@ -51,18 +53,20 @@ class InlayHintsChecker(private val myFixture: CodeInsightTestFixture) { hintSettings.isDoNotShowIfMethodNameContainsParameterName = default.isDoNotShowIfMethodNameContainsParameterName } - fun checkInlays(fileName: String, text: String) { - myFixture.configureByText(fileName, text) - + fun checkInlays() { val file = myFixture.file val document = myFixture.getDocument(file) + val originalText = document.text val expectedInlays = extractInlays(document) val actualInlays = getActualInlays() - assertEquals(expectedInlays.size, actualInlays.size) + if (expectedInlays.size != actualInlays.size || actualInlays.zip(expectedInlays).any { it.second != it.first }) { + val proposedText = StringBuilder(document.text) + actualInlays.asReversed().forEach { proposedText.insert(it.offset, "") } - actualInlays.zip(expectedInlays).forEach { - assertEquals(it.second, it.first) + VfsTestUtil.TEST_DATA_FILE_PATH.get(file.virtualFile)?.let { originalPath -> + throw FileComparisonFailure("Hints differ", originalText, proposedText.toString(), originalPath) + } ?: throw ComparisonFailure("Hints differ", originalText, proposedText.toString()) } }