mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-19 23:15:24 +02:00
Converted two more refactoring tests to the new framework.
This commit is contained in:
parent
aab2dfc32b
commit
a8ef0a62b8
11 changed files with 129 additions and 734 deletions
|
@ -1,18 +0,0 @@
|
|||
//!Find function definition
|
||||
//#org.eclipse.cdt.ui.tests.refactoring.utils.DefinitionFinderTest
|
||||
//@.config
|
||||
filename=A.h
|
||||
//@A.h
|
||||
#ifndef A_H_
|
||||
#define A_H_
|
||||
|
||||
void foo();
|
||||
|
||||
#endif /*A_H_*/
|
||||
|
||||
//@A.cpp
|
||||
#include "A.h"
|
||||
|
||||
void foo() {
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
//!TestImplementFunctionTest
|
||||
//#ch.hsr.ifs.cute.refactoringpreview.togglefunction.tests.newimplement.NewImplementRefactoringTest
|
||||
//@.config
|
||||
filename=A.h
|
||||
//@A.h
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
class A {
|
||||
int /*$*/member/*$$*/();
|
||||
};
|
||||
//=
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
class A {
|
||||
int member() {
|
||||
return int();
|
||||
}
|
||||
};
|
|
@ -1,31 +0,0 @@
|
|||
//!before the class
|
||||
//#org.eclipse.cdt.ui.tests.refactoring.utils.TranslationUnitHelperTest
|
||||
//@.config
|
||||
filename=A.h
|
||||
offset=27
|
||||
//@A.h
|
||||
#ifndef A_H_
|
||||
#define A_H_
|
||||
|
||||
class A {
|
||||
public:
|
||||
A();
|
||||
void foo();
|
||||
};
|
||||
|
||||
#endif /*A_H_*/
|
||||
|
||||
|
||||
//!before a typedef
|
||||
//#org.eclipse.cdt.ui.tests.refactoring.utils.TranslationUnitHelperTest
|
||||
//@.config
|
||||
filename=A.h
|
||||
offset=0
|
||||
//@A.h
|
||||
typedef int nummere;
|
||||
|
||||
class A {
|
||||
public:
|
||||
A();
|
||||
};
|
||||
|
|
@ -1,118 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others
|
||||
* 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:
|
||||
* Institute for Software - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.ui.tests.refactoring;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.runtime.ILogListener;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.jface.text.TextSelection;
|
||||
|
||||
import org.eclipse.cdt.core.tests.BaseTestFramework;
|
||||
|
||||
/**
|
||||
* Don't create new tests based on this class. Use RefactoringTestBase instead.
|
||||
*
|
||||
* @author Guido Zgraggen IFS
|
||||
*/
|
||||
public abstract class RefactoringBaseTest extends BaseTestFramework implements ILogListener {
|
||||
protected static final NullProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor();
|
||||
|
||||
protected TreeMap<String, TestSourceFile> fileMap = new TreeMap<String, TestSourceFile>();
|
||||
protected String fileWithSelection;
|
||||
protected TextSelection selection;
|
||||
|
||||
protected RefactoringBaseTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public RefactoringBaseTest(String name, Collection<TestSourceFile> files) {
|
||||
super(name);
|
||||
for (TestSourceFile file : files) {
|
||||
fileMap.put(file.getName(), file);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected abstract void runTest() throws Throwable;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
for (TestSourceFile testFile : fileMap.values()) {
|
||||
if(testFile.getSource().length() > 0) {
|
||||
importFile(testFile.getName(), testFile.getSource());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void assertEquals(TestSourceFile file, IFile file2) throws Exception {
|
||||
String code = getCodeFromIFile(file2);
|
||||
assertEquals(file.getExpectedSource(), TestHelper.unifyNewLines(code));
|
||||
}
|
||||
|
||||
protected void compareFiles(Map<String,TestSourceFile> testResourceFiles) throws Exception {
|
||||
for (String fileName : testResourceFiles.keySet()) {
|
||||
String expectedSource = testResourceFiles.get(fileName).getExpectedSource();
|
||||
IFile iFile = project.getFile(new Path(fileName));
|
||||
String code = getCodeFromIFile(iFile);
|
||||
assertEquals(TestHelper.unifyNewLines(expectedSource), TestHelper.unifyNewLines(code));
|
||||
}
|
||||
}
|
||||
|
||||
protected String getCodeFromIFile(IFile file) throws Exception {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(file.getContents()));
|
||||
StringBuilder code = new StringBuilder();
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
code.append(line);
|
||||
code.append('\n');
|
||||
}
|
||||
br.close();
|
||||
return code.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
System.gc();
|
||||
fileManager.closeAllFiles();
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logging(IStatus status, String plugin) {
|
||||
Throwable ex = status.getException();
|
||||
StringBuilder stackTrace = new StringBuilder();
|
||||
if (ex != null) {
|
||||
stackTrace.append('\n');
|
||||
for (StackTraceElement ste : ex.getStackTrace()) {
|
||||
stackTrace.append(ste.toString());
|
||||
}
|
||||
}
|
||||
fail("Log-Message: " + status.getMessage() + stackTrace.toString()); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
public void setFileWithSelection(String fileWithSelection) {
|
||||
this.fileWithSelection = fileWithSelection;
|
||||
}
|
||||
|
||||
public void setSelection(TextSelection selection) {
|
||||
this.selection = selection;
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2009, 2012 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others.
|
||||
* 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:
|
||||
* Institute for Software (IFS)- initial API and implementation
|
||||
* Sergey Prigogin (Google)
|
||||
******************************************************************************/
|
||||
package org.eclipse.cdt.ui.tests.refactoring;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.net.URI;
|
||||
import java.util.Collection;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.eclipse.core.filesystem.URIUtil;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.ltk.core.refactoring.Refactoring;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringContext;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
||||
import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
|
||||
import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
|
||||
|
||||
/**
|
||||
* @author Emanuel Graf IFS
|
||||
*/
|
||||
public class RefactoringHistoryTest extends RefactoringTest {
|
||||
private TestSourceFile scriptFile;
|
||||
|
||||
public RefactoringHistoryTest(String name, Collection<TestSourceFile> files) {
|
||||
super(name, files);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureRefactoring(Properties refactoringProperties) {
|
||||
scriptFile = fileMap.get(refactoringProperties.getProperty("scriptFile", "refScript.xml"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
String xmlSource = scriptFile.getSource();
|
||||
URI uri= URIUtil.toURI(project.getLocation());
|
||||
xmlSource = xmlSource.replaceAll("\\$\\$projectPath\\$\\$", uri.getPath());
|
||||
RefactoringHistory refHist = RefactoringHistoryService.getInstance().readRefactoringHistory(
|
||||
new ByteArrayInputStream(xmlSource.getBytes()), 0);
|
||||
for (RefactoringDescriptorProxy proxy : refHist.getDescriptors()) {
|
||||
RefactoringStatus status = new RefactoringStatus();
|
||||
RefactoringDescriptor descriptor = proxy.requestDescriptor(new NullProgressMonitor());
|
||||
RefactoringContext context = descriptor.createRefactoringContext(status);
|
||||
assertTrue(status.isOK());
|
||||
Refactoring refactoring = context.getRefactoring();
|
||||
executeRefactoring(refactoring, context, false);
|
||||
compareFiles(fileMap);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,220 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others
|
||||
* 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:
|
||||
* Institute for Software - initial API and implementation
|
||||
* Sergey Prigogin (Google)
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.ui.tests.refactoring;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.ltk.core.refactoring.Change;
|
||||
import org.eclipse.ltk.core.refactoring.Refactoring;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringContext;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||
import org.eclipse.cdt.core.index.IIndexManager;
|
||||
import org.eclipse.cdt.ui.testplugin.CTestPlugin;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring2;
|
||||
import org.eclipse.cdt.internal.ui.refactoring.CRefactoringContext;
|
||||
|
||||
/**
|
||||
* Don't create new tests based on this class. Use RefactoringTestBase instead.
|
||||
*
|
||||
* @author Emanuel Graf
|
||||
*/
|
||||
public abstract class RefactoringTest extends RefactoringBaseTest {
|
||||
private static final String CONFIG_FILE_NAME = ".config"; //$NON-NLS-1$
|
||||
|
||||
protected String fileName;
|
||||
protected boolean fatalError;
|
||||
protected int initialErrors;
|
||||
protected int initialWarnings;
|
||||
protected int finalWarnings;
|
||||
protected int finalInfos;
|
||||
|
||||
public RefactoringTest(String name, Collection<TestSourceFile> files) {
|
||||
super(name, files);
|
||||
initializeConfiguration(files);
|
||||
}
|
||||
|
||||
protected abstract void configureRefactoring(Properties refactoringProperties);
|
||||
|
||||
protected void executeRefactoring(Refactoring refactoring) throws CoreException {
|
||||
RefactoringContext context = refactoring instanceof CRefactoring2 ?
|
||||
new CRefactoringContext((CRefactoring2) refactoring) :
|
||||
new RefactoringContext(refactoring);
|
||||
executeRefactoring(refactoring, context, true);
|
||||
}
|
||||
|
||||
protected void executeRefactoring(Refactoring refactoring, boolean withUserInput) throws CoreException {
|
||||
RefactoringContext context = refactoring instanceof CRefactoring2 ?
|
||||
new CRefactoringContext((CRefactoring2) refactoring) :
|
||||
new RefactoringContext(refactoring);
|
||||
executeRefactoring(refactoring, context, withUserInput);
|
||||
}
|
||||
|
||||
protected void executeRefactoring(Refactoring refactoring, RefactoringContext context,
|
||||
boolean withUserInput) throws CoreException {
|
||||
try {
|
||||
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
|
||||
|
||||
if (fatalError) {
|
||||
assertConditionsFatalError(checkInitialConditions);
|
||||
return;
|
||||
}
|
||||
if (initialErrors != 0) {
|
||||
assertConditionsError(checkInitialConditions, initialErrors);
|
||||
} else if (initialWarnings != 0) {
|
||||
assertConditionsFatalError(checkInitialConditions, initialWarnings);
|
||||
} else {
|
||||
assertConditionsOk(checkInitialConditions);
|
||||
}
|
||||
|
||||
if (withUserInput)
|
||||
simulateUserInput();
|
||||
|
||||
RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR);
|
||||
if (finalWarnings > 0) {
|
||||
assertConditionsWarning(finalConditions, finalWarnings);
|
||||
} else if (finalInfos > 0) {
|
||||
assertConditionsInfo(finalConditions, finalInfos);
|
||||
} else {
|
||||
assertConditionsOk(finalConditions);
|
||||
}
|
||||
Change change = refactoring.createChange(NULL_PROGRESS_MONITOR);
|
||||
change.perform(NULL_PROGRESS_MONITOR);
|
||||
} finally {
|
||||
if (context != null)
|
||||
context.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses can override to simulate user input.
|
||||
*/
|
||||
protected void simulateUserInput() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
CCorePlugin.getIndexManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER);
|
||||
CTestPlugin.getDefault().getLog().addLogListener(this);
|
||||
CCorePlugin.getIndexManager().reindex(cproject);
|
||||
boolean joined = CCorePlugin.getIndexManager().joinIndexer(IIndexManager.FOREVER, NULL_PROGRESS_MONITOR);
|
||||
assertTrue(joined);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
private void initializeConfiguration(Collection<TestSourceFile> files) {
|
||||
TestSourceFile configFile = null;
|
||||
|
||||
for (TestSourceFile currentFile : files) {
|
||||
if (currentFile.getName().equals(CONFIG_FILE_NAME)) {
|
||||
configFile = currentFile;
|
||||
}
|
||||
}
|
||||
|
||||
Properties refactoringProperties = new Properties();
|
||||
|
||||
try {
|
||||
if (configFile != null) {
|
||||
refactoringProperties.load(new ByteArrayInputStream(configFile.getSource().getBytes()));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Property initialization failed
|
||||
}
|
||||
|
||||
initCommonFields(refactoringProperties);
|
||||
configureRefactoring(refactoringProperties);
|
||||
files.remove(configFile);
|
||||
}
|
||||
|
||||
private void initCommonFields(Properties refactoringProperties) {
|
||||
fileName = refactoringProperties.getProperty("filename", "A.cpp"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
}
|
||||
|
||||
protected void assertConditionsOk(RefactoringStatus conditions) {
|
||||
assertTrue(conditions.isOK() ? "OK" : "Error or Warning in Conditions: " + conditions.getEntries()[0].getMessage(), //$NON-NLS-1$ //$NON-NLS-2$
|
||||
conditions.isOK());
|
||||
}
|
||||
|
||||
protected void assertConditionsWarning(RefactoringStatus conditions, int number) {
|
||||
if (number > 0) {
|
||||
assertTrue("Warning in Condition expected", conditions.hasWarning()); //$NON-NLS-1$
|
||||
}
|
||||
RefactoringStatusEntry[] entries = conditions.getEntries();
|
||||
int count = 0;
|
||||
for (RefactoringStatusEntry entry : entries) {
|
||||
if (entry.isWarning()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
assertEquals(number + " Warnings expected found " + count, count, number); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
protected void assertConditionsInfo(RefactoringStatus status, int number) {
|
||||
if (number > 0) {
|
||||
assertTrue("Info in condition expected", status.hasInfo()); //$NON-NLS-1$
|
||||
}
|
||||
RefactoringStatusEntry[] entries = status.getEntries();
|
||||
int count = 0;
|
||||
for (RefactoringStatusEntry entry : entries) {
|
||||
if (entry.isInfo()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
assertEquals(number + " Infos expected found " + count, number, count); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
protected void assertConditionsError(RefactoringStatus status, int number) {
|
||||
if (number > 0) {
|
||||
assertTrue("Error in condition expected", status.hasError()); //$NON-NLS-1$
|
||||
}
|
||||
RefactoringStatusEntry[] entries = status.getEntries();
|
||||
int count = 0;
|
||||
for (RefactoringStatusEntry entry : entries) {
|
||||
if (entry.isError()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
assertEquals(number + " Errors expected found " + count, number, count); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
protected void assertConditionsFatalError(RefactoringStatus status, int number) {
|
||||
if (number > 0) {
|
||||
assertTrue("Fatal Error in Condition expected", status.hasFatalError()); //$NON-NLS-1$
|
||||
}
|
||||
RefactoringStatusEntry[] entries = status.getEntries();
|
||||
int count = 0;
|
||||
for (RefactoringStatusEntry entry : entries) {
|
||||
if (entry.isFatalError()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
assertEquals(number + " Fatal Errors expected found " + count, number, count); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
protected void assertConditionsFatalError(RefactoringStatus conditions) {
|
||||
assertTrue("Fatal Error in Condition expected", conditions.hasFatalError()); //$NON-NLS-1$
|
||||
}
|
||||
}
|
|
@ -126,6 +126,9 @@ public abstract class RefactoringTestBase extends BaseTestCase {
|
|||
selectedFile = testFile;
|
||||
}
|
||||
}
|
||||
if (selectedFile == null && !testFiles.isEmpty()) {
|
||||
selectedFile = testFiles.iterator().next();
|
||||
}
|
||||
CCorePlugin.getIndexManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER);
|
||||
assertTrue(CCorePlugin.getIndexManager().joinIndexer(INDEXER_TIMEOUT_SEC * 1000,
|
||||
NULL_PROGRESS_MONITOR));
|
||||
|
|
|
@ -1,205 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others
|
||||
* 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:
|
||||
* Institute for Software - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.ui.tests.refactoring;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.eclipse.core.runtime.FileLocator;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.jface.text.TextSelection;
|
||||
import org.osgi.framework.Bundle;
|
||||
|
||||
import org.eclipse.cdt.ui.testplugin.CTestPlugin;
|
||||
|
||||
/**
|
||||
* @author Emanuel Graf
|
||||
*/
|
||||
public class RefactoringTester extends TestSuite{
|
||||
enum MatcherState{skip, inTest, inSource, inExpectedResult}
|
||||
|
||||
private static final String classRegexp = "//#(.*)\\s*(\\w*)*$"; //$NON-NLS-1$
|
||||
private static final String testRegexp = "//!(.*)\\s*(\\w*)*$"; //$NON-NLS-1$
|
||||
private static final String fileRegexp = "//@(.*)\\s*(\\w*)*$"; //$NON-NLS-1$
|
||||
private static final String resultRegexp = "//=.*$"; //$NON-NLS-1$
|
||||
|
||||
public static Test suite(String name, String file)throws Exception {
|
||||
BufferedReader in = createReader(file);
|
||||
|
||||
ArrayList<RefactoringBaseTest> testCases = createTests(in);
|
||||
in.close();
|
||||
return createSuite(testCases, name);
|
||||
}
|
||||
|
||||
protected static BufferedReader createReader(String file) throws IOException {
|
||||
Bundle bundle = CTestPlugin.getDefault().getBundle();
|
||||
Path path = new Path(file);
|
||||
String file2 = FileLocator.toFileURL(FileLocator.find(bundle, path, null)).getFile();
|
||||
return new BufferedReader(new FileReader(file2));
|
||||
}
|
||||
|
||||
private static ArrayList<RefactoringBaseTest> createTests(BufferedReader inputReader) throws Exception {
|
||||
String line;
|
||||
Collection<TestSourceFile> files = new ArrayList<TestSourceFile>();
|
||||
TestSourceFile actFile = null;
|
||||
MatcherState matcherState = MatcherState.skip;
|
||||
ArrayList<RefactoringBaseTest> testCases = new ArrayList<RefactoringBaseTest>();
|
||||
String testName = null;
|
||||
String className = null;
|
||||
boolean bevorFirstTest = true;
|
||||
|
||||
while ((line = inputReader.readLine()) != null) {
|
||||
if (lineMatchesBeginOfTest(line)) {
|
||||
if (!bevorFirstTest) {
|
||||
RefactoringBaseTest test = createTestClass(className, testName, files);
|
||||
testCases.add(test);
|
||||
files = new ArrayList<TestSourceFile>();
|
||||
className = null;
|
||||
testName = null;
|
||||
}
|
||||
matcherState = MatcherState.inTest;
|
||||
testName = getNameOfTest(line);
|
||||
bevorFirstTest = false;
|
||||
continue;
|
||||
} else if (lineMatchesBeginOfResult(line)) {
|
||||
matcherState = MatcherState.inExpectedResult;
|
||||
continue;
|
||||
} else if (lineMatchesFileName(line)) {
|
||||
matcherState = MatcherState.inSource;
|
||||
actFile = new TestSourceFile(getFileName(line));
|
||||
files.add(actFile);
|
||||
continue;
|
||||
} else if (lineMatchesClassName(line)) {
|
||||
className = getNameOfClass(line);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (matcherState) {
|
||||
case inSource:
|
||||
if (actFile != null) {
|
||||
actFile.addLineToSource(line);
|
||||
}
|
||||
break;
|
||||
case inExpectedResult:
|
||||
if (actFile != null) {
|
||||
actFile.addLineToExpectedSource(line);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
RefactoringBaseTest test = createTestClass(className, testName, files);
|
||||
testCases.add(test);
|
||||
return testCases;
|
||||
}
|
||||
|
||||
private static RefactoringBaseTest createTestClass(String className, String testName, Collection<TestSourceFile> files) throws Exception {
|
||||
try {
|
||||
Class<?> refClass = Class.forName(className);
|
||||
if (testName == null) {
|
||||
testName = refClass.getSimpleName();
|
||||
}
|
||||
Constructor<?>[] constructors = refClass.getConstructors();
|
||||
for (Constructor<?> ctor : constructors) {
|
||||
Object arglist[] = new Object[] { testName, files };
|
||||
if (ctor.getParameterTypes().length == arglist.length) {
|
||||
RefactoringBaseTest test = null;
|
||||
try {
|
||||
test = (RefactoringBaseTest) ctor.newInstance(arglist);
|
||||
} catch (IllegalArgumentException e) {
|
||||
continue;
|
||||
}
|
||||
for (TestSourceFile file : files) {
|
||||
TextSelection sel = file.getSelection();
|
||||
if (sel != null) {
|
||||
test.setFileWithSelection(file.getName());
|
||||
test.setSelection(sel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return test;
|
||||
}
|
||||
}
|
||||
throw new Exception(className + " class does not provide required constructor"); //$NON-NLS-1$
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new Exception("Unknown test class " + className); //$NON-NLS-1$
|
||||
} catch (Exception e) {
|
||||
throw new Exception(e.getClass().getSimpleName()
|
||||
+ " during creation of test " + className, e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
private static String getFileName(String line) {
|
||||
Matcher matcherBeginOfTest = createMatcherFromString(fileRegexp, line);
|
||||
if (matcherBeginOfTest.find())
|
||||
return matcherBeginOfTest.group(1);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getNameOfClass(String line) {
|
||||
Matcher matcherBeginOfTest = createMatcherFromString(classRegexp, line);
|
||||
if (matcherBeginOfTest.find())
|
||||
return matcherBeginOfTest.group(1);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean lineMatchesBeginOfTest(String line) {
|
||||
return createMatcherFromString(testRegexp, line).find();
|
||||
}
|
||||
|
||||
private static boolean lineMatchesClassName(String line) {
|
||||
return createMatcherFromString(classRegexp, line).find();
|
||||
}
|
||||
|
||||
private static boolean lineMatchesFileName(String line) {
|
||||
return createMatcherFromString(fileRegexp, line).find();
|
||||
}
|
||||
|
||||
protected static Matcher createMatcherFromString(String pattern, String line) {
|
||||
return Pattern.compile(pattern).matcher(line);
|
||||
}
|
||||
|
||||
private static String getNameOfTest(String line) {
|
||||
Matcher matcherBeginOfTest = createMatcherFromString(testRegexp, line);
|
||||
if (matcherBeginOfTest.find())
|
||||
return matcherBeginOfTest.group(1);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean lineMatchesBeginOfResult(String line) {
|
||||
return createMatcherFromString(resultRegexp, line).find();
|
||||
}
|
||||
|
||||
private static TestSuite createSuite(ArrayList<RefactoringBaseTest> testCases, String name) {
|
||||
TestSuite suite = new TestSuite(name);
|
||||
Iterator<RefactoringBaseTest> it = testCases.iterator();
|
||||
while(it.hasNext()) {
|
||||
RefactoringBaseTest subject =it.next();
|
||||
suite.addTest(subject);
|
||||
}
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
|
@ -11,65 +11,80 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.ui.tests.refactoring.utils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.OperationCanceledException;
|
||||
import org.eclipse.jface.viewers.ISelection;
|
||||
import org.eclipse.ltk.core.refactoring.Refactoring;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
||||
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest;
|
||||
import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile;
|
||||
import org.eclipse.cdt.core.model.ICElement;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTestBase;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring2;
|
||||
import org.eclipse.cdt.internal.ui.refactoring.CRefactoringContext;
|
||||
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
|
||||
import org.eclipse.cdt.internal.ui.refactoring.utils.DefinitionFinder;
|
||||
|
||||
public class DefinitionFinderTest extends RefactoringTest {
|
||||
public class DefinitionFinderTest extends RefactoringTestBase {
|
||||
private static class DummyRefactoring extends CRefactoring2 {
|
||||
public DummyRefactoring(ICElement element, ISelection selection, ICProject project) {
|
||||
super(element, selection, project);
|
||||
}
|
||||
|
||||
public DefinitionFinderTest(String name, Collection<TestSourceFile> files) {
|
||||
super(name, files);
|
||||
@Override
|
||||
protected RefactoringStatus checkFinalConditions(IProgressMonitor progressMonitor,
|
||||
CheckConditionsContext checkContext) throws CoreException, OperationCanceledException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RefactoringDescriptor getRefactoringDescriptor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
|
||||
throws CoreException, OperationCanceledException {
|
||||
}
|
||||
}
|
||||
|
||||
public DefinitionFinderTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DefinitionFinderTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureRefactoring(Properties refactoringProperties) {
|
||||
protected Refactoring createRefactoring() {
|
||||
return new DummyRefactoring(getSelectedTranslationUnit(), getSelection(), getCProject());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
IFile file = project.getFile(fileName);
|
||||
ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(file);
|
||||
CRefactoring2 refactoring = new CRefactoring2(tu, null, tu.getCProject()) {
|
||||
@Override
|
||||
protected RefactoringStatus checkFinalConditions(IProgressMonitor progressMonitor,
|
||||
CheckConditionsContext checkContext) throws CoreException, OperationCanceledException {
|
||||
return null;
|
||||
}
|
||||
//A.h
|
||||
//#ifndef A_H_
|
||||
//#define A_H_
|
||||
//
|
||||
//void foo();
|
||||
//
|
||||
//#endif /*A_H_*/
|
||||
|
||||
@Override
|
||||
protected RefactoringDescriptor getRefactoringDescriptor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
|
||||
throws CoreException, OperationCanceledException {
|
||||
}
|
||||
};
|
||||
|
||||
CRefactoringContext refactoringContext = new CRefactoringContext(refactoring);
|
||||
//A.cpp
|
||||
//#include "A.h"
|
||||
//
|
||||
//void foo() {
|
||||
//}
|
||||
public void testFindFunctionDefinition() throws Exception {
|
||||
CRefactoringContext refactoringContext = new CRefactoringContext((CRefactoring2) createRefactoring());
|
||||
try {
|
||||
IASTTranslationUnit ast = refactoringContext.getAST(tu, null);
|
||||
IASTTranslationUnit ast = refactoringContext.getAST(getSelectedTranslationUnit(), null);
|
||||
for (IASTDeclaration declaration : ast.getDeclarations()) {
|
||||
if (declaration instanceof IASTSimpleDeclaration) {
|
||||
assertNotNull(DefinitionFinder.getDefinition((IASTSimpleDeclaration) declaration,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
|
@ -8,41 +8,94 @@
|
|||
*
|
||||
* Contributors:
|
||||
* Institute for Software - initial API and implementation
|
||||
* Sergey Prigogin (Google)
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.ui.tests.refactoring.utils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.OperationCanceledException;
|
||||
import org.eclipse.jface.viewers.ISelection;
|
||||
import org.eclipse.ltk.core.refactoring.Refactoring;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
|
||||
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest;
|
||||
import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile;
|
||||
import org.eclipse.cdt.core.model.ICElement;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTestBase;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
|
||||
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
|
||||
import org.eclipse.cdt.internal.ui.refactoring.utils.TranslationUnitHelper;
|
||||
|
||||
/**
|
||||
* @author Mirko Stocker
|
||||
*/
|
||||
public class TranslationUnitHelperTest extends RefactoringTest {
|
||||
private int offset;
|
||||
public class TranslationUnitHelperTest extends RefactoringTestBase {
|
||||
private static class DummyRefactoring extends CRefactoring {
|
||||
|
||||
public TranslationUnitHelperTest(String name, Collection<TestSourceFile> files) {
|
||||
super(name, files);
|
||||
public DummyRefactoring(IFile file, ISelection selection, ICElement element, ICProject proj) {
|
||||
super(file, selection, element, proj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RefactoringStatus checkFinalConditions(IProgressMonitor progressMonitor) throws CoreException, OperationCanceledException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RefactoringDescriptor getRefactoringDescriptor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
|
||||
throws CoreException, OperationCanceledException {
|
||||
}
|
||||
}
|
||||
|
||||
public TranslationUnitHelperTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TranslationUnitHelperTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
IFile file = project.getFile(fileName);
|
||||
IASTTranslationUnit unit = TranslationUnitHelper.loadTranslationUnit(file, false);
|
||||
IASTNode firstNode = TranslationUnitHelper.getFirstNode(unit);
|
||||
protected Refactoring createRefactoring() {
|
||||
return new DummyRefactoring(getSelectedFile(), getSelection(), null, getCProject());
|
||||
}
|
||||
|
||||
private void assertFirstNodeIsAtOffset(int offset) throws Exception {
|
||||
IASTTranslationUnit ast = TranslationUnitHelper.loadTranslationUnit(getSelectedFile(), false);
|
||||
IASTNode firstNode = TranslationUnitHelper.getFirstNode(ast);
|
||||
assertEquals(offset, firstNode.getNodeLocations()[0].getNodeOffset());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureRefactoring(Properties refactoringProperties) {
|
||||
offset = new Integer(refactoringProperties.getProperty("offset", "0")).intValue(); //$NON-NLS-1$
|
||||
//A.h
|
||||
//#ifndef A_H_
|
||||
//#define A_H_
|
||||
//
|
||||
//class A {
|
||||
//public:
|
||||
// A();
|
||||
// void foo();
|
||||
//};
|
||||
//
|
||||
//#endif /*A_H_*/
|
||||
public void testBeforeClass() throws Exception {
|
||||
assertFirstNodeIsAtOffset(27);
|
||||
}
|
||||
|
||||
//A.h
|
||||
//typedef int nummere;
|
||||
//
|
||||
//class A {
|
||||
//public:
|
||||
// A();
|
||||
//};
|
||||
public void testBeforeTypedef() throws Exception {
|
||||
assertFirstNodeIsAtOffset(0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
|
@ -15,8 +15,6 @@ package org.eclipse.cdt.ui.tests.refactoring.utils;
|
|||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.eclipse.cdt.ui.tests.refactoring.RefactoringTester;
|
||||
|
||||
/**
|
||||
* @author Thomas Corbat
|
||||
*/
|
||||
|
@ -25,8 +23,8 @@ public class UtilTestSuite extends TestSuite {
|
|||
public static Test suite() throws Exception {
|
||||
UtilTestSuite suite = new UtilTestSuite();
|
||||
suite.addTest(IdentifierHelperTest.suite());
|
||||
suite.addTest(RefactoringTester.suite("TranslationUnitHelperTest", "resources/refactoring/TranslationunitHelper.rts")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
suite.addTest(RefactoringTester.suite("DefinitionFinderTest", "resources/refactoring/DefinitionFinder.rts")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
suite.addTestSuite(TranslationUnitHelperTest.class);
|
||||
suite.addTestSuite(DefinitionFinderTest.class);
|
||||
suite.addTestSuite(PseudoNameGeneratorTest.class);
|
||||
suite.addTestSuite(NameComposerTest.class);
|
||||
return suite;
|
||||
|
|
Loading…
Add table
Reference in a new issue