From 4fdafaa433452f441b264855e3fbc15302d32a4d Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Tue, 16 Mar 2004 21:23:45 +0000 Subject: [PATCH] New Tests from Thomas for the error parser --- core/org.eclipse.cdt.core.tests/.classpath | 1 + core/org.eclipse.cdt.core.tests/ChangeLog | 7 + .../tests/GCCErrorParserTests.java | 163 +++++++++++++++ .../tests/GenericErrorParserTests.java | 188 ++++++++++++++++++ 4 files changed, 359 insertions(+) create mode 100644 core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/GCCErrorParserTests.java create mode 100644 core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/GenericErrorParserTests.java diff --git a/core/org.eclipse.cdt.core.tests/.classpath b/core/org.eclipse.cdt.core.tests/.classpath index 362b81cae72..b7c3ebbbe0b 100644 --- a/core/org.eclipse.cdt.core.tests/.classpath +++ b/core/org.eclipse.cdt.core.tests/.classpath @@ -1,6 +1,7 @@ + diff --git a/core/org.eclipse.cdt.core.tests/ChangeLog b/core/org.eclipse.cdt.core.tests/ChangeLog index 049667ecf56..3068ddb05b8 100644 --- a/core/org.eclipse.cdt.core.tests/ChangeLog +++ b/core/org.eclipse.cdt.core.tests/ChangeLog @@ -1,3 +1,10 @@ +2004-03-16 Alain Magloire + + Test from Thomas Fletcher for the Error Parser + New source folder. + + * misc/ + 2004-03-15 Andrew Niefer Added CompleteParseASTTest.testTemplateClassDeclaration CompleteParseASTTest.testTemplateFunction diff --git a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/GCCErrorParserTests.java b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/GCCErrorParserTests.java new file mode 100644 index 00000000000..07dbee40f87 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/GCCErrorParserTests.java @@ -0,0 +1,163 @@ +/********************************************************************** + * Copyright (c) 2004 QNX Software Systems Ltd and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v0.5 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v05.html +***********************************************************************/ +package org.eclipse.cdt.core.internal.errorparsers.tests; + +import java.io.StringBufferInputStream; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.ErrorParserManager; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IPath; + +/** + * This test is designed to exercise the error parser capabilities. + */ +public class GCCErrorParserTests extends GenericErrorParserTests { + public static final String GCC_ERROR_PARSER_ID = "org.eclipse.cdt.core.GCCErrorParser"; + public static final String [] GCC_ERROR_STREAM1 = { +"qcc -c -I/qnx630/target/qnx6/usr/include -I/qnx630/target/qnx6/usr/include/photon -V3.3.1,gcc_ntox86 -w5 -O2 -I. ../abmain.c abmain.o", +"In file included from ../globals.h:9,", +" from ../abmain.c:36:", +"../_combolist.h:34:24: warning: no newline at end of file", +}; + public static final int GCC_ERROR_STREAM1_WARNINGS = 1; + public static final int GCC_ERROR_STREAM1_ERRORS = 0; + public static final String [] GCC_ERROR_STREAM1_FILENAMES = { "_combolist.h" }; + + public static final String [] GCC_ERROR_STREAM2 = { +"C:/QNX630/workspace/System/inc/RPNEvaluator.hpp:234: warning: `", +" RPNEvaluator::OperandConstant' is implicitly a typename", +"C:/QNX630/workspace/System/inc/RPNEvaluator.hpp:234: warning: implicit typename", +" is deprecated, please see the documentation for details" +}; + public static final int GCC_ERROR_STREAM2_WARNINGS = 2; + public static final int GCC_ERROR_STREAM2_ERRORS = 0; + public static final String [] GCC_ERROR_STREAM2_FILENAMES = { "RPNEvaluator.hpp" }; + public static final String [] GCC_ERROR_STREAM2_DESCRIPTIONS = { "please see the documentation" }; + + public static final String [] GCC_ERROR_STREAM3 = { +"C:/QNX630/workspace/System/inc/RPNEvaluator.hpp:370: error: ISO C++ says that `", +" char& String::operator[](unsigned int)' and `operator[]' are ambiguous even ", +" though the worst conversion for the former is better than the worst ", +" conversion for the latter" +}; + public static final int GCC_ERROR_STREAM3_WARNINGS = 0; + public static final int GCC_ERROR_STREAM3_ERRORS = 1; + public static final String [] GCC_ERROR_STREAM3_FILENAMES = { "RPNEvaluator.hpp" }; + public static final String [] GCC_ERROR_STREAM3_DESCRIPTIONS = { "ISO C++", "are ambiguous", "worst conversion", "conversion for the latter" }; + + public static final String [] GCC_ERROR_STREAM4 = { +"C:/QNX630/workspace/System/inc/RPNEvaluator.hpp: In member function `", +" NumericType RPNEvaluator::evaluate(const char*) [with ", +" NumericType = int8]':", +"C:/QNX630/workspace/System/src/CommonScriptClasses.cpp:609: instantiated from here", +"C:/QNX630/workspace/System/inc/RPNEvaluator.hpp:370: error: ISO C++ says that `", +" char& String::operator[](unsigned int)' and `operator[]' are ambiguous even ", +" though the worst conversion for the former is better than the worst ", +" conversion for the latter" +}; + public static final int GCC_ERROR_STREAM4_WARNINGS = 0; + public static final int GCC_ERROR_STREAM4_ERRORS = 1; + public static final String [] GCC_ERROR_STREAM4_FILENAMES = { "RPNEvaluator.hpp" }; + public static final String [] GCC_ERROR_STREAM4_DESCRIPTIONS = { "ISO C++", "are ambiguous", "worst conversion for", "conversion for the latter" }; + + /** + * Constructor for IndexManagerTest. + * @param name + */ + public GCCErrorParserTests(String name) { + super(name); + } + + public static Test suite() { + TestSuite suite = new TestSuite(GCCErrorParserTests.class); + return suite; + } + + public void testMultipleIncludesError() { + runParserTest(GCC_ERROR_STREAM1, + GCC_ERROR_STREAM1_ERRORS, + GCC_ERROR_STREAM1_WARNINGS, + GCC_ERROR_STREAM1_FILENAMES, + null); + } + + public void testMultiLineDescriptionError() { + runParserTest(GCC_ERROR_STREAM2, + GCC_ERROR_STREAM2_ERRORS, + GCC_ERROR_STREAM2_WARNINGS, + GCC_ERROR_STREAM2_FILENAMES, + GCC_ERROR_STREAM2_DESCRIPTIONS); + } + + public void testLongMultiLineDescriptionError() { + runParserTest(GCC_ERROR_STREAM3, + GCC_ERROR_STREAM3_ERRORS, + GCC_ERROR_STREAM3_WARNINGS, + GCC_ERROR_STREAM3_FILENAMES, + GCC_ERROR_STREAM3_DESCRIPTIONS); + } + + public void testMultiFileMultiLineSingleError() { + runParserTest(GCC_ERROR_STREAM4, + GCC_ERROR_STREAM4_ERRORS, + GCC_ERROR_STREAM4_WARNINGS, + GCC_ERROR_STREAM4_FILENAMES, + GCC_ERROR_STREAM4_DESCRIPTIONS); + } + + private void runParserTest(String [] dataStream, + int expectedErrorCount, + int expectedWarningCount, + String [] expectedFileNames, + String [] expectedDescriptions ) { + String [] parserID = { GCC_ERROR_PARSER_ID }; + CountingMarkerGenerator markerGenerator = new CountingMarkerGenerator(); + + IProject project = getTempProject(); + assertNotNull(project); + + ErrorParserManager manager; + manager = new ImaginaryFilesErrorParserManager(project, markerGenerator, parserID); + + String errorStream = makeStringFromArray(dataStream, "\n"); + StringBufferInputStream inputStream = new StringBufferInputStream(errorStream); + assertNotNull(inputStream); + + try { + transferInputStreamToOutputStream(inputStream, manager.getOutputStream(), 1024); + } catch(Exception ex) { + assertTrue(false); + } finally { + try { + manager.close(); + } catch(Exception ex) { + /* Ignore */ + } + } + manager.reportProblems(); + + assertEquals(expectedErrorCount, markerGenerator.numErrors); + assertEquals(expectedWarningCount, markerGenerator.numWarnings); + assertEquals(expectedFileNames.length, markerGenerator.uniqFiles.size()); + for(int i= 0; i < expectedFileNames.length; i++) { + IPath path = ((IFile)markerGenerator.uniqFiles.get(i)).getLocation(); + assertEquals(expectedFileNames[i], path.lastSegment()); + } + + if(expectedDescriptions != null) { + assertNotNull(markerGenerator.lastDescription); + for(int i = 0; i < expectedDescriptions.length; i++) { + assertTrue(markerGenerator.lastDescription.matches(expectedDescriptions[i])); + } + } + } +} diff --git a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/GenericErrorParserTests.java b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/GenericErrorParserTests.java new file mode 100644 index 00000000000..a39f957de35 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/GenericErrorParserTests.java @@ -0,0 +1,188 @@ +/********************************************************************** + * Copyright (c) 2004 QNX Software Systems Ltd and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v0.5 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v05.html +***********************************************************************/ +package org.eclipse.cdt.core.internal.errorparsers.tests; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringBufferInputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +import junit.framework.TestCase; + +import org.eclipse.cdt.core.ErrorParserManager; +import org.eclipse.cdt.core.IMarkerGenerator; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.NullProgressMonitor; + +/** + * This test is designed to exercise the error parser capabilities. + */ +public class GenericErrorParserTests extends TestCase { + protected IProject fTempProject; + + /** + * Constructor for IndexManagerTest. + * @param name + */ + public GenericErrorParserTests(String name) { + super(name); + } + + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + fTempProject = ResourcesPlugin.getWorkspace().getRoot().getProject("temp-" + System.currentTimeMillis()); + if(!fTempProject.exists()) { + fTempProject.create(new NullProgressMonitor()); + } + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() { + try { + super.tearDown(); + } catch (Exception ex) { + } + try { + fTempProject.delete(true, true, new NullProgressMonitor()); + } catch (Exception ex) { + } + } + + protected IProject getTempProject() { + return fTempProject; + } + + protected String makeStringFromArray(String [] pieces, String sep) { + StringBuffer buf = new StringBuffer(); + for(int i = 0; i < pieces.length; i++) { + if(i != 0) { + buf.append(sep); + } + buf.append(pieces[i]); + } + return buf.toString(); + } + + protected void transferInputStreamToOutputStream(InputStream input, OutputStream output, int byteBlockSize) throws IOException { + byte [] buffer = new byte[byteBlockSize]; + int bytesRead; + + while((bytesRead = input.read(buffer)) >= 0) { + output.write(buffer, 0, bytesRead); + } + + buffer = null; + } + + class FileNameComparator implements Comparator { + /* (non-Javadoc) + * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) + */ + public int compare(Object arg0, Object arg1) { + try { + IFile f0 = (IFile)arg0; + IFile f1 = (IFile)arg1; + return f0.getName().compareToIgnoreCase(f1.getName()); + } catch(Exception ex) { + /* Ignore */ + } + return 1; + } + } + + /** + * Expand and grow this class to make it more usefull. + */ + class CountingMarkerGenerator implements IMarkerGenerator { + public int numErrors; + public int numWarnings; + public int numMarkers; + public ArrayList uniqFiles; + public String lastDescription; + private Comparator fFileNameComparator; + + public void addMarker(IResource file, int lineNumber, String errorDesc, int severity, String errorVar) { + int index = Collections.binarySearch(uniqFiles, file, fFileNameComparator); + if(index < 0) { + uniqFiles.add(-1*(index + 1), file); + } + + if(severity == SEVERITY_WARNING) { + numWarnings++; + } else if(severity == SEVERITY_ERROR_BUILD || severity == SEVERITY_ERROR_RESOURCE) { + numErrors++; + } + + lastDescription = errorDesc; + numMarkers++; + } + + public CountingMarkerGenerator() { + numErrors = 0; + numWarnings = 0; + uniqFiles = new ArrayList(0); + fFileNameComparator = new FileNameComparator(); + } + } + + /** + * This class allows us to run error parsers for files which don't + * really exist by just using the strings that come out as error codes. + */ + class ImaginaryFilesErrorParserManager extends ErrorParserManager { + IProject fProject; + + public ImaginaryFilesErrorParserManager(IProject project, IMarkerGenerator generator, String [] ids) { + super(project, generator, ids); + fProject = project; + } + + public IFile findFileName(String fileName) { + if(fileName.lastIndexOf('/') != -1) { + fileName = fileName.substring(fileName.lastIndexOf('/') + 1); + } + IFile file = fProject.getFile(fileName); + if(!file.exists()) { + try { + InputStream stream = new StringBufferInputStream("TestFile"); + file.create(stream, true, new NullProgressMonitor()); + stream.close(); + } catch(Exception ex) { + /* Ignore */ + } + } + return file; + } + + protected IFile findFileInWorkspace(IPath path) { + IFile file = fProject.getFile(path.lastSegment()); + if(!file.exists()) { + try { + InputStream stream = new StringBufferInputStream("TestFile"); + file.create(stream, true, new NullProgressMonitor()); + stream.close(); + } catch(Exception ex) { + /* Ignore */ + } + } + return file; + } + } +}