Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,28 @@
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.rewrite;

import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.eclipse.cdt.core.tests.BaseTestFramework;
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.junit.jupiter.api.AfterEach;

/**
* @author Guido Zgraggen IFS
*/
public abstract class RewriteBaseTest extends BaseTestFramework implements ILogListener {
protected static final NullProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor();

protected TreeMap<String, TestSourceFile> fileMap = new TreeMap<>();
protected String fileWithSelection;
protected TextSelection selection;

protected RewriteBaseTest(String name) {
super(name);
}

public RewriteBaseTest(String name, List<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 {
StringBuilder code = getCodeFromFile(file2);
assertEquals(file.getExpectedSource(), TestHelper.unifyNewLines(code.toString()));
}

protected void compareFiles(Map<String, TestSourceFile> testResourceFiles) throws Exception {
for (String fileName : testResourceFiles.keySet()) {
TestSourceFile file = testResourceFiles.get(fileName);
IFile iFile = project.getFile(new Path(fileName));
StringBuilder code = getCodeFromFile(iFile);
assertEquals(TestHelper.unifyNewLines(file.getExpectedSource()), TestHelper.unifyNewLines(code.toString()));
}
}

protected StringBuilder getCodeFromFile(IFile file) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(file.getContents()));
StringBuilder code = new StringBuilder();
Expand All @@ -88,11 +48,10 @@ protected StringBuilder getCodeFromFile(IFile file) throws Exception {
return code;
}

@Override
protected void tearDown() throws Exception {
@AfterEach
protected void closeAllFiles() throws Exception {
System.gc();
fileManager.closeAllFiles();
super.tearDown();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,32 @@
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.rewrite;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.TextSelection;
import org.junit.jupiter.params.provider.Arguments;
import org.osgi.framework.Bundle;

import junit.framework.Test;
import junit.framework.TestSuite;

/**
* This is not actually a test, but a test provider. See loadTests and its uses
*
* The possibly unusual structure here is a result of migrating this JUnit 3 test
* suite creator to JUnit 5
*
* @author Emanuel Graf
*/
public class RewriteTester extends TestSuite {
public class RewriteTester {
enum MatcherState {
skip, inTest, inSource, inExpectedResult
}
Expand All @@ -47,12 +49,12 @@ enum MatcherState {
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 {
public static List<Arguments> loadTests(Class<? extends CommentHandlingTest> clazz, String file) throws Exception {
BufferedReader in = createReader(file);

ArrayList<RewriteBaseTest> testCases = createTests(in);
List<Arguments> testCases = createTests(clazz, in);
in.close();
return createSuite(testCases, name);
return testCases;
}

protected static BufferedReader createReader(String file) throws IOException {
Expand All @@ -62,20 +64,21 @@ protected static BufferedReader createReader(String file) throws IOException {
return new BufferedReader(new FileReader(file2));
}

private static ArrayList<RewriteBaseTest> createTests(BufferedReader inputReader) throws Exception {
private static List<Arguments> createTests(Class<? extends CommentHandlingTest> clazz, BufferedReader inputReader)
throws Exception {
String line;
List<TestSourceFile> files = new ArrayList<>();
TestSourceFile actFile = null;
MatcherState matcherState = MatcherState.skip;
ArrayList<RewriteBaseTest> testCases = new ArrayList<>();
List<Arguments> testCases = new ArrayList<>();
String testName = null;
String className = null;
boolean bevorFirstTest = true;

while ((line = inputReader.readLine()) != null) {
if (lineMatchesBeginOfTest(line)) {
if (!bevorFirstTest) {
RewriteBaseTest test = createTestClass(className, testName, files);
Arguments test = createTestClass(clazz, className, testName, files);
testCases.add(test);
files = new ArrayList<>();
className = null;
Expand Down Expand Up @@ -113,42 +116,17 @@ private static ArrayList<RewriteBaseTest> createTests(BufferedReader inputReader
break;
}
}
RewriteBaseTest test = createTestClass(className, testName, files);
Arguments test = createTestClass(clazz, className, testName, files);
testCases.add(test);
return testCases;
}

private static RewriteBaseTest createTestClass(String className, String testName, List<TestSourceFile> files)
throws Exception {
try {
Class<?> refClass = Class.forName(className);
Constructor<?> ct = refClass.getConstructor(new Class[] { String.class, List.class });
RewriteBaseTest test = (RewriteBaseTest) ct.newInstance(new Object[] { testName, files });
for (TestSourceFile file : files) {
TextSelection sel = file.getSelection();
if (sel != null) {
test.setFileWithSelection(file.getName());
test.setSelection(sel);
break;
}
}
return test;
} catch (ClassNotFoundException e) {
throw new Exception("Unknown TestClass: " + e.getMessage()
+ ". Make sure the test's sourcefile specifies a valid test class.");
} catch (SecurityException e) {
throw new Exception("Security Exception during Test creation", e);
} catch (NoSuchMethodException e) {
throw new Exception("Test class does not provied required constructor.");
} catch (IllegalArgumentException e) {
throw new Exception("IllegalArgumentException during Test creation", e);
} catch (InstantiationException e) {
throw new Exception("InstantiationException during Test creation", e);
} catch (IllegalAccessException e) {
throw new Exception("IllegalAccessException during Test creation", e);
} catch (InvocationTargetException e) {
throw new Exception("InvocationTargetException during Test creation", e);
}
private static Arguments createTestClass(Class<? extends CommentHandlingTest> clazz, String className,
String testName, List<TestSourceFile> files) throws Exception {
// For historical reasons, the Java classname of the test exists in the rts files.
// This is a check that the rts file matches the test currently being loaded.
assertEquals(clazz.getName(), className);
return Arguments.argumentSet(testName, files);
}

private static String getFileName(String line) {
Expand Down Expand Up @@ -191,14 +169,4 @@ private static String getNameOfTest(String line) {
private static boolean lineMatchesBeginOfResult(String line) {
return createMatcherFromString(resultRegexp, line).find();
}

private static TestSuite createSuite(ArrayList<RewriteBaseTest> testCases, String name) {
TestSuite suite = new TestSuite(name);
Iterator<RewriteBaseTest> it = testCases.iterator();
while (it.hasNext()) {
RewriteBaseTest subject = it.next();
suite.addTest(subject);
}
return suite;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.rewrite.astwriter;

import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.parser.ISourceCodeParser;
Expand Down Expand Up @@ -43,47 +45,47 @@
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.TextSelection;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* @author Guido Zgraggen
*/
public abstract class ASTWriterTester extends RewriteBaseTest {
public class ASTWriterTester extends RewriteBaseTest {

private static final IParserLogService NULL_LOG = new NullLogService();

private IFile file;

public ASTWriterTester(String name, ASTWriterTestSourceFile file) {
super(name);
fileMap.put(file.getName(), file);
public static List<Arguments> loadTests() throws Exception {
return SourceRewriteTest.loadTests();
}

@Override
protected void setUp() throws Exception {
super.setUp();
for (TestSourceFile testFile : fileMap.values()) {
if (testFile.getSource().length() > 0) {
file = importFile(testFile.getName(), testFile.getSource());
}
@ParameterizedTest
@MethodSource("loadTests")
protected void test(ASTWriterTestSourceFile testFile) throws Throwable {
if (testFile.getSource().length() > 0) {
importFile(testFile.getName(), testFile.getSource());
}
TextSelection sel = testFile.getSelection();
if (sel != null) {
setFileWithSelection(testFile.getName());
setSelection(sel);
}
}

@Override
protected void runTest() throws Throwable {
file = project.getFile("ASTWritterTest.h"); //$NON-NLS-1$
compareFiles(fileMap);
compareFiles(testFile);
}

@Override
protected void compareFiles(Map<String, TestSourceFile> testResourceFiles) throws Exception {
for (String fileName : testResourceFiles.keySet()) {
TestSourceFile testFile = testResourceFiles.get(fileName);
String code = generateSource(testFile);
assertEquals(TestHelper.unifyNewLines(testFile.getExpectedSource()),
TestHelper.unifyNewLines(code + System.getProperty("line.separator"))); //$NON-NLS-1$
}
private void compareFiles(ASTWriterTestSourceFile testFile) throws Exception {
String code = generateSource(testFile);
assertEquals(TestHelper.unifyNewLines(testFile.getExpectedSource()),
TestHelper.unifyNewLines(code + System.getProperty("line.separator"))); //$NON-NLS-1$
}

public String generateSource(TestSourceFile testFile) throws Exception {
private String generateSource(TestSourceFile testFile) throws Exception {
IASTTranslationUnit unit = getParser(testFile).parse();
NodeCommentMap commentMap = ASTCommenter.getCommentedNodeMap(unit);
ASTModificationMap map = new ASTModificationMap();
Expand All @@ -92,7 +94,7 @@ public String generateSource(TestSourceFile testFile) throws Exception {
return writer.write(unit, commentMap);
}

protected ISourceCodeParser getParser(TestSourceFile testFile) throws Exception {
private ISourceCodeParser getParser(TestSourceFile testFile) throws Exception {
FileContent codeReader = FileContent.create(file);

ParserLanguage language = getLanguage(testFile);
Expand Down
Loading
Loading