mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Added more tests.
This commit is contained in:
parent
f405944e2d
commit
00c29ec7eb
3 changed files with 120 additions and 6 deletions
|
@ -0,0 +1,111 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2012 Google, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Alex Ruiz - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.internal.checkers.externaltool;
|
||||
|
||||
import org.eclipse.cdt.codan.core.externaltool.IProblemDisplay;
|
||||
import org.eclipse.cdt.codan.core.externaltool.InvocationParameters;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemLocation;
|
||||
import org.eclipse.cdt.codan.core.test.CodanTestCase;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
|
||||
/**
|
||||
* Tests for <code>{@link CppcheckOutputParser}</code>.
|
||||
*
|
||||
* @author alruiz@google.com (Alex Ruiz)
|
||||
*/
|
||||
@SuppressWarnings("nls")
|
||||
public class CppcheckOutputParserTest extends CodanTestCase {
|
||||
private static final String MATCHING_LINE_FORMAT = "[%s:%d]: (%s) %s";
|
||||
|
||||
private ProblemDisplayStub problemDisplay;
|
||||
|
||||
private CppcheckOutputParser parser;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
problemDisplay = new ProblemDisplayStub();
|
||||
loadcode("void f(int x) {}", "test.cpp");
|
||||
InvocationParameters parameters = new InvocationParameters(currentIFile, currentIFile,
|
||||
actualFilePath(), null);
|
||||
parser = new CppcheckOutputParser(parameters, problemDisplay);
|
||||
}
|
||||
|
||||
public void testProblemIsReportedWhenParsingMatchingLine() {
|
||||
int lineNumber = 2;
|
||||
String severity = "warning";
|
||||
String description = "The scope of the variable 'i' can be reduced";
|
||||
boolean parsed = parser.parse(createMatchingLine(lineNumber, severity, description));
|
||||
assertTrue(parsed);
|
||||
problemDisplay.assertThatLocationHasFile(currentIFile);
|
||||
problemDisplay.assertThatLocationHasLineNumber(lineNumber);
|
||||
problemDisplay.assertThatLocationHasNoStartingAndEndingChars();
|
||||
problemDisplay.assertThatReceivedDescription(description);
|
||||
problemDisplay.assertThatReceivedSeverity(severity);
|
||||
}
|
||||
|
||||
private String createMatchingLine(int lineNumber, String severity, String description) {
|
||||
return String.format(MATCHING_LINE_FORMAT, actualFilePath(), lineNumber, severity, description);
|
||||
}
|
||||
|
||||
private String actualFilePath() {
|
||||
return currentIFile.getLocation().toOSString();
|
||||
}
|
||||
|
||||
public void testProblemIsNotReportedWhenLineDoesNotMatch() {
|
||||
boolean parsed = parser.parse("Checking usage of global functions..");
|
||||
assertFalse(parsed);
|
||||
problemDisplay.assertThatProblemWasNotReported();
|
||||
}
|
||||
|
||||
private static class ProblemDisplayStub implements IProblemDisplay {
|
||||
private boolean problemReported;
|
||||
private IProblemLocation location;
|
||||
private String description;
|
||||
private String severity;
|
||||
|
||||
public void reportProblem(IProblemLocation location, String description) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void reportProblem(IProblemLocation location, String description, String severity) {
|
||||
problemReported = true;
|
||||
this.location = location;
|
||||
this.description = description;
|
||||
this.severity = severity;
|
||||
}
|
||||
|
||||
void assertThatLocationHasFile(IResource expected) {
|
||||
assertSame(expected, location.getFile());
|
||||
}
|
||||
|
||||
void assertThatLocationHasLineNumber(int expected) {
|
||||
assertEquals(expected, location.getLineNumber());
|
||||
}
|
||||
|
||||
void assertThatLocationHasNoStartingAndEndingChars() {
|
||||
assertEquals(-1, location.getStartingChar());
|
||||
assertEquals(-1, location.getEndingChar());
|
||||
}
|
||||
|
||||
void assertThatReceivedDescription(String expected) {
|
||||
assertEquals(expected, description);
|
||||
}
|
||||
|
||||
void assertThatReceivedSeverity(String expected) {
|
||||
assertEquals(expected, severity);
|
||||
}
|
||||
|
||||
void assertThatProblemWasNotReported() {
|
||||
assertFalse(problemReported);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -101,7 +101,9 @@ public class ExternalToolInvokerTest extends CodanTestCase {
|
|||
// class C {
|
||||
// };
|
||||
public void testInvokesProcessCorrectly() throws Throwable {
|
||||
loadcode(getAboveComment());
|
||||
String aboveComment = getAboveComment();
|
||||
System.out.println(aboveComment);
|
||||
loadcode(aboveComment);
|
||||
String expectedCommand = expectedCommand();
|
||||
InvocationParameters parameters = new InvocationParameters(currentIFile, currentIFile,
|
||||
actualFilePath(), workingDirectory);
|
||||
|
|
|
@ -49,7 +49,7 @@ public class ExternalToolInvoker {
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* Constructor, for testing purposes only.
|
||||
* @param consolePrinterProvider creates an Eclipse console that uses the name of an external
|
||||
* tool as its own.
|
||||
* @param processInvoker executes a command in a separate process.
|
||||
|
@ -72,8 +72,8 @@ public class ExternalToolInvoker {
|
|||
* @throws Throwable if the external tool cannot be launched.
|
||||
*/
|
||||
public void invoke(InvocationParameters parameters, ConfigurationSettings configurationSettings,
|
||||
IArgsSeparator argsSeparator,
|
||||
List<AbstractOutputParser> parsers) throws InvocationFailure, Throwable {
|
||||
IArgsSeparator argsSeparator, List<AbstractOutputParser> parsers)
|
||||
throws InvocationFailure, Throwable {
|
||||
IPath executablePath = executablePath(configurationSettings);
|
||||
String[] args = argsToPass(parameters, configurationSettings, argsSeparator);
|
||||
boolean shouldDisplayOutput = configurationSettings.getShouldDisplayOutput().getValue();
|
||||
|
@ -92,9 +92,9 @@ public class ExternalToolInvoker {
|
|||
|
||||
private String[] argsToPass(InvocationParameters parameters,
|
||||
ConfigurationSettings configurationSettings, IArgsSeparator argsSeparator) {
|
||||
String[] configuredArgs = configuredArgs(configurationSettings, argsSeparator);
|
||||
String actualFilePath = parameters.getActualFilePath();
|
||||
return addFilePathToArgs(actualFilePath, configuredArgs);
|
||||
String[] args = configuredArgs(configurationSettings, argsSeparator);
|
||||
return addFilePathToArgs(actualFilePath, args);
|
||||
}
|
||||
|
||||
private String[] configuredArgs(ConfigurationSettings configurationSettings,
|
||||
|
@ -168,6 +168,7 @@ public class ExternalToolInvoker {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void reset(List<AbstractOutputParser> parsers) {
|
||||
for (AbstractOutputParser parser : parsers) {
|
||||
parser.reset();
|
||||
|
|
Loading…
Add table
Reference in a new issue