From 00c29ec7eb32e3120d1023bfc3252d2f80fd7f01 Mon Sep 17 00:00:00 2001 From: Alex Ruiz Date: Thu, 23 Feb 2012 13:25:46 -0800 Subject: [PATCH] Added more tests. --- .../CppcheckOutputParserTest.java | 111 ++++++++++++++++++ .../externaltool/ExternalToolInvokerTest.java | 4 +- .../externaltool/ExternalToolInvoker.java | 11 +- 3 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/externaltool/CppcheckOutputParserTest.java diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/externaltool/CppcheckOutputParserTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/externaltool/CppcheckOutputParserTest.java new file mode 100644 index 00000000000..aa08d16bd78 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/externaltool/CppcheckOutputParserTest.java @@ -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 {@link CppcheckOutputParser}. + * + * @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); + } + } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/core/externaltool/ExternalToolInvokerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/core/externaltool/ExternalToolInvokerTest.java index ef86e72194c..0aa87c66cb2 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/core/externaltool/ExternalToolInvokerTest.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/core/externaltool/ExternalToolInvokerTest.java @@ -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); diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/externaltool/ExternalToolInvoker.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/externaltool/ExternalToolInvoker.java index 5d7d0ab8691..1743ed39d38 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/externaltool/ExternalToolInvoker.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/externaltool/ExternalToolInvoker.java @@ -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 parsers) throws InvocationFailure, Throwable { + IArgsSeparator argsSeparator, List 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 parsers) { for (AbstractOutputParser parser : parsers) { parser.reset();