From 095e4340d67889759bb342fe9bbef6ab617f7d2e Mon Sep 17 00:00:00 2001 From: Denis Zhdanov Date: Tue, 31 May 2011 13:17:33 +0400 Subject: [PATCH] IDEA-70123 Test Assistant: Make it possible to work with test data for existing test classes that are not properly annotated Improved test data recognition --- .../TestDataGuessByExistingFilesUtil.java | 48 ++++++++++++------- .../TestDataLineMarkerProvider.java | 2 +- .../TestDataReferenceCollector.java | 1 + 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataGuessByExistingFilesUtil.java b/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataGuessByExistingFilesUtil.java index 6127a7470585..0704a388aa23 100644 --- a/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataGuessByExistingFilesUtil.java +++ b/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataGuessByExistingFilesUtil.java @@ -143,8 +143,8 @@ public class TestDataGuessByExistingFilesUtil { } if (cached.second > System.currentTimeMillis()) { return null; - } - } + } + } TestFramework[] frameworks = Extensions.getExtensions(TestFramework.EXTENSION_NAME); TestFramework framework = null; @@ -257,27 +257,39 @@ public class TestDataGuessByExistingFilesUtil { if (!current.isComplete()) { continue; } - String pattern = current.filePrefix.toLowerCase(); - if (!StringUtil.isEmpty(current.filePrefix)) { - // Handle situations like the one below: - // *) test class has tests with names 'testAlignedParameters' and 'testNonAlignedParameters'; - // *) test data files with the following names present: 'AlignedParameters.java' and 'NonAlignedParameters.java'; - // *) we're processing the following (test; test data file) pair - ('testAlignedParameters'; 'NonAlignedParameters.java'); - // We don't want to store descriptor with file prefix 'Non' here. - boolean skip = false; - for (String testName : testNamesLowerCase) { - if (testName.startsWith(pattern)) { - skip = true; - break; - } + + // Handle situations like the one below: + // *) test class has tests with names 'testAlignedParameters' and 'testNonAlignedParameters'; + // *) test data files with the following names present: 'AlignedParameters.java' and 'NonAlignedParameters.java'; + // *) we're processing the following (test; test data file) pair - ('testAlignedParameters'; 'NonAlignedParameters.java'); + // We don't want to store descriptor with file prefix 'Non' here. + // The same is true for suffixes, e.g. tests like 'testLeaveValidCodeBlock()' and 'testLeaveValidCodeBlockWithEmptyLineAfterIt()' + String prefixPattern = current.filePrefix.toLowerCase(); + boolean checkPrefix = !StringUtil.isEmpty(prefixPattern); + String suffixPattern = current.fileSuffix; + for (TestLocationDescriptor descriptor : descriptors) { + if (suffixPattern.endsWith(descriptor.fileSuffix)) { + suffixPattern = suffixPattern.substring(0, suffixPattern.length() - descriptor.fileSuffix.length()); } - if (skip) { + } + suffixPattern = suffixPattern.toLowerCase(); + boolean checkSuffix = !StringUtil.isEmpty(suffixPattern); + boolean skip = false; + for (String testName : testNamesLowerCase) { + if (testName.equals(trinity.second)) { continue; } + if ((checkPrefix && testName.startsWith(prefixPattern)) || (checkSuffix && testName.endsWith(suffixPattern))) { + skip = true; + break; + } + } + if (skip) { + continue; } currentNameProcessed = true; - if (descriptors.isEmpty() || descriptors.iterator().next().dir.equals(current.dir)) { + if (descriptors.isEmpty() || (descriptors.iterator().next().dir.equals(current.dir) && !descriptors.contains(current))) { descriptors.add(current); continue; } @@ -364,7 +376,7 @@ public class TestDataGuessByExistingFilesUtil { } if (i < 0) { return; - } + } filePrefix = fileName.substring(0, i); fileSuffix = fileName.substring(i + testName.length()); diff --git a/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataLineMarkerProvider.java b/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataLineMarkerProvider.java index b7a94d4e9f9d..708644a7ee95 100644 --- a/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataLineMarkerProvider.java +++ b/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataLineMarkerProvider.java @@ -52,7 +52,7 @@ public class TestDataLineMarkerProvider implements LineMarkerProvider { return false; } List fileNames = new TestDataReferenceCollector(testDataPath, name.substring(4)).collectTestDataReferences(method); - return !fileNames.isEmpty(); + return fileNames != null && !fileNames.isEmpty(); } public void collectSlowLineMarkers(List elements, Collection result) { diff --git a/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataReferenceCollector.java b/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataReferenceCollector.java index b08af991e85a..a5c34b66d4ab 100644 --- a/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataReferenceCollector.java +++ b/plugins/IdeaTestAssistant/src/com/intellij/testAssistant/TestDataReferenceCollector.java @@ -40,6 +40,7 @@ public class TestDataReferenceCollector { myTestName = testName; } + @Nullable List collectTestDataReferences(@NotNull final PsiMethod method) { myContainingClass = method.getContainingClass(); List result = collectTestDataReferences(method, new HashMap>());