Search in sources :

Example 6 with ComparisonFailure

use of junit.framework.ComparisonFailure in project intellij-community by JetBrains.

the class CodeInsightTestFixtureImpl method checkResult.

private void checkResult(@NotNull String expectedFile, boolean stripTrailingSpaces, @NotNull SelectionAndCaretMarkupLoader loader, @NotNull String actualText) {
    assertInitialized();
    Project project = getProject();
    Editor editor = getEditor();
    if (editor instanceof EditorWindow) {
        editor = ((EditorWindow) editor).getDelegate();
    }
    UsefulTestCase.doPostponedFormatting(getProject());
    if (stripTrailingSpaces) {
        actualText = stripTrailingSpaces(actualText);
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    String newFileText1 = loader.newFileText;
    if (stripTrailingSpaces) {
        newFileText1 = stripTrailingSpaces(newFileText1);
    }
    actualText = StringUtil.convertLineSeparators(actualText);
    if (!Comparing.equal(newFileText1, actualText)) {
        if (loader.filePath != null) {
            throw new FileComparisonFailure(expectedFile, newFileText1, actualText, loader.filePath);
        } else {
            throw new ComparisonFailure(expectedFile, newFileText1, actualText);
        }
    }
    EditorTestUtil.verifyCaretAndSelectionState(editor, loader.caretState, expectedFile);
}
Also used : Project(com.intellij.openapi.project.Project) FileComparisonFailure(com.intellij.rt.execution.junit.FileComparisonFailure) ComparisonFailure(junit.framework.ComparisonFailure) com.intellij.openapi.fileEditor(com.intellij.openapi.fileEditor) EditorWindow(com.intellij.injected.editor.EditorWindow) FileComparisonFailure(com.intellij.rt.execution.junit.FileComparisonFailure)

Example 7 with ComparisonFailure

use of junit.framework.ComparisonFailure in project kotlin by JetBrains.

the class AbstractQuickFixMultiFileTest method doMultiFileTest.

protected void doMultiFileTest(final String beforeFileName) throws Exception {
    String multifileText = FileUtil.loadFile(new File(beforeFileName), true);
    boolean withRuntime = InTextDirectivesUtils.isDirectiveDefined(multifileText, "// WITH_RUNTIME");
    if (withRuntime) {
        ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk());
    }
    try {
        final List<TestFile> subFiles = KotlinTestUtils.createTestFiles("single.kt", multifileText, new KotlinTestUtils.TestFileFactoryNoModules<TestFile>() {

            @NotNull
            @Override
            public TestFile create(@NotNull String fileName, @NotNull String text, @NotNull Map<String, String> directives) {
                if (text.startsWith("// FILE")) {
                    String firstLineDropped = StringUtil.substringAfter(text, "\n");
                    assert firstLineDropped != null;
                    text = firstLineDropped;
                }
                return new TestFile(fileName, text);
            }
        });
        final TestFile afterFile = CollectionsKt.firstOrNull(subFiles, new Function1<TestFile, Boolean>() {

            @Override
            public Boolean invoke(TestFile file) {
                return file.path.contains(".after");
            }
        });
        final TestFile beforeFile = CollectionsKt.firstOrNull(subFiles, new Function1<TestFile, Boolean>() {

            @Override
            public Boolean invoke(TestFile file) {
                return file.path.contains(".before");
            }
        });
        assert beforeFile != null;
        subFiles.remove(beforeFile);
        if (afterFile != null) {
            subFiles.remove(afterFile);
        }
        configureMultiFileTest(subFiles, beforeFile);
        CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {

            @Override
            public void run() {
                try {
                    PsiFile psiFile = getFile();
                    Pair<String, Boolean> pair = LightQuickFixTestCase.parseActionHint(psiFile, beforeFile.content);
                    String text = pair.getFirst();
                    boolean actionShouldBeAvailable = pair.getSecond();
                    if (psiFile instanceof KtFile) {
                        DirectiveBasedActionUtils.INSTANCE.checkForUnexpectedErrors((KtFile) psiFile);
                    }
                    doAction(text, actionShouldBeAvailable, getTestName(false));
                    String actualText = getFile().getText();
                    String afterText = new StringBuilder(actualText).insert(getEditor().getCaretModel().getOffset(), "<caret>").toString();
                    if (pair.second) {
                        assertNotNull(".after file should exist", afterFile);
                        if (!afterText.equals(afterFile.content)) {
                            StringBuilder actualTestFile = new StringBuilder();
                            actualTestFile.append("// FILE: ").append(beforeFile.path).append("\n").append(beforeFile.content);
                            for (TestFile file : subFiles) {
                                actualTestFile.append("// FILE: ").append(file.path).append("\n").append(file.content);
                            }
                            actualTestFile.append("// FILE: ").append(afterFile.path).append("\n").append(afterText);
                            KotlinTestUtils.assertEqualsToFile(new File(beforeFileName), actualTestFile.toString());
                        }
                    } else {
                        assertNull(".after file should not exist", afterFile);
                    }
                } catch (ComparisonFailure e) {
                    throw e;
                } catch (AssertionError e) {
                    throw e;
                } catch (Throwable e) {
                    e.printStackTrace();
                    fail(getTestName(true));
                }
            }
        }, "", "");
    } finally {
        if (withRuntime) {
            ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk());
        }
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) KotlinTestUtils(org.jetbrains.kotlin.test.KotlinTestUtils) ComparisonFailure(junit.framework.ComparisonFailure) PsiFile(com.intellij.psi.PsiFile) KtFile(org.jetbrains.kotlin.psi.KtFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File) Pair(com.intellij.openapi.util.Pair)

Example 8 with ComparisonFailure

use of junit.framework.ComparisonFailure in project kotlin by JetBrains.

the class AbstractIntentionTest method doTestFor.

private void doTestFor(String mainFilePath, Map<String, PsiFile> pathToFiles, final IntentionAction intentionAction, String fileText) throws Exception {
    String isApplicableString = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// " + isApplicableDirectiveName() + ": ");
    boolean isApplicableExpected = isApplicableString == null || isApplicableString.equals("true");
    boolean isApplicableOnPooled = ApplicationManager.getApplication().executeOnPooledThread(new java.util.concurrent.Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    return intentionAction.isAvailable(getProject(), getEditor(), getFile());
                }
            });
        }
    }).get();
    boolean isApplicableOnEdt = intentionAction.isAvailable(getProject(), getEditor(), getFile());
    Assert.assertEquals("There should not be any difference what thread isApplicable is called from", isApplicableOnPooled, isApplicableOnEdt);
    Assert.assertTrue("isAvailable() for " + intentionAction.getClass() + " should return " + isApplicableExpected, isApplicableExpected == isApplicableOnEdt);
    String intentionTextString = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// " + intentionTextDirectiveName() + ": ");
    if (intentionTextString != null) {
        assertEquals("Intention text mismatch.", intentionTextString, intentionAction.getText());
    }
    String shouldFailString = StringUtil.join(InTextDirectivesUtils.findListWithPrefixes(fileText, "// SHOULD_FAIL_WITH: "), ", ");
    try {
        if (isApplicableExpected) {
            ApplicationUtilsKt.executeWriteCommand(getProject(), intentionAction.getText(), null, new Function0<Object>() {

                @Override
                public Object invoke() {
                    intentionAction.invoke(getProject(), getEditor(), getFile());
                    return null;
                }
            });
            // Don't bother checking if it should have failed.
            if (shouldFailString.isEmpty()) {
                for (Map.Entry<String, PsiFile> entry : pathToFiles.entrySet()) {
                    String filePath = entry.getKey();
                    String canonicalPathToExpectedFile = PathUtil.getCanonicalPath(filePath + afterFileNameSuffix());
                    if (filePath.equals(mainFilePath)) {
                        try {
                            checkResultByFile(canonicalPathToExpectedFile);
                        } catch (ComparisonFailure e) {
                            KotlinTestUtils.assertEqualsToFile(new File(canonicalPathToExpectedFile), getEditor().getDocument().getText());
                        }
                    } else {
                        KotlinTestUtils.assertEqualsToFile(new File(canonicalPathToExpectedFile), entry.getValue().getText());
                    }
                }
            }
        }
        assertEquals("Expected test to fail.", "", shouldFailString);
    } catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
        assertEquals("Failure message mismatch.", shouldFailString, StringUtil.join(e.getMessages(), ", "));
    } catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
        assertEquals("Failure message mismatch.", shouldFailString, e.getMessage().replace('\n', ' '));
    }
}
Also used : BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) ComparisonFailure(junit.framework.ComparisonFailure) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) PsiFile(com.intellij.psi.PsiFile) Map(java.util.Map) PsiFile(com.intellij.psi.PsiFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 9 with ComparisonFailure

use of junit.framework.ComparisonFailure in project poi by apache.

the class TestSharedFormulaRecord method confirmOperandClasses.

private static void confirmOperandClasses(Ptg[] originalPtgs, Ptg[] convertedPtgs) {
    assertEquals(originalPtgs.length, convertedPtgs.length);
    for (int i = 0; i < convertedPtgs.length; i++) {
        Ptg originalPtg = originalPtgs[i];
        Ptg convertedPtg = convertedPtgs[i];
        if (originalPtg.getPtgClass() != convertedPtg.getPtgClass()) {
            throw new ComparisonFailure("Different operand class for token[" + i + "]", String.valueOf(originalPtg.getPtgClass()), String.valueOf(convertedPtg.getPtgClass()));
        }
    }
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) ComparisonFailure(junit.framework.ComparisonFailure)

Example 10 with ComparisonFailure

use of junit.framework.ComparisonFailure in project poi by apache.

the class TestNumberToTextConverter method testAll.

/**
	 * Confirms that <tt>ExcelNumberToTextConverter.toText(d)</tt> produces the right results.
	 * As part of preparing this test class, the <tt>ExampleConversion</tt> instances should be set
	 * up to contain the rendering as produced by Excel.
	 */
public void testAll() {
    int failureCount = 0;
    ExampleConversion[] examples = NumberToTextConversionExamples.getExampleConversions();
    for (int i = 0; i < examples.length; i++) {
        ExampleConversion example = examples[i];
        try {
            if (example.isNaN()) {
                confirmNaN(example.getRawDoubleBits(), example.getExcelRendering());
                continue;
            }
            String actual = NumberToTextConverter.toText(example.getDoubleValue());
            if (!example.getExcelRendering().equals(actual)) {
                failureCount++;
                String msg = "Error rendering for examples[" + i + "] " + formatExample(example) + " " + " bad-result='" + actual + "' " + new ComparisonFailure(null, example.getExcelRendering(), actual).getMessage();
                System.err.println(msg);
                continue;
            }
        } catch (RuntimeException e) {
            failureCount++;
            System.err.println("Error in excel rendering for examples[" + i + "] " + formatExample(example) + "':" + e.getMessage());
            e.printStackTrace();
        }
    }
    if (failureCount > 0) {
        throw new AssertionFailedError(failureCount + " error(s) in excel number to text conversion (see std-err)");
    }
}
Also used : ExampleConversion(org.apache.poi.ss.util.NumberToTextConversionExamples.ExampleConversion) ComparisonFailure(junit.framework.ComparisonFailure) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

ComparisonFailure (junit.framework.ComparisonFailure)10 File (java.io.File)5 PsiFile (com.intellij.psi.PsiFile)4 KtFile (org.jetbrains.kotlin.psi.KtFile)3 Pair (com.intellij.openapi.util.Pair)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 EditorWindow (com.intellij.injected.editor.EditorWindow)1 com.intellij.openapi.fileEditor (com.intellij.openapi.fileEditor)1 Project (com.intellij.openapi.project.Project)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 BaseRefactoringProcessor (com.intellij.refactoring.BaseRefactoringProcessor)1 CommonRefactoringUtil (com.intellij.refactoring.util.CommonRefactoringUtil)1 FileComparisonFailure (com.intellij.rt.execution.junit.FileComparisonFailure)1 FilenameFilter (java.io.FilenameFilter)1 Map (java.util.Map)1 Random (java.util.Random)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 AssertionFailedError (junit.framework.AssertionFailedError)1 Ptg (org.apache.poi.ss.formula.ptg.Ptg)1