1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Set up QML parser tests using TestSourceReader.

Change-Id: I40a3ef8dcf53d6189a980ae6ae7128746867c70f
This commit is contained in:
Doug Schaefer 2015-09-30 19:45:36 -06:00
parent 7cf2a14cc9
commit 3b26dc26c4
4 changed files with 47 additions and 54 deletions

View file

@ -4,7 +4,11 @@ Bundle-Name: QML Tests
Bundle-SymbolicName: org.eclipse.cdt.qt.core.tests
Bundle-Version: 2.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.junit,
Require-Bundle: org.eclipse.osgi;bundle-version="3.10.100",
org.junit,
org.antlr.runtime;bundle-version="4.5.1",
org.eclipse.cdt.qt.core;bundle-version="2.0.0"
org.eclipse.cdt.qt.core;bundle-version="2.0.0",
org.eclipse.cdt.core.tests;bundle-version="5.4.0"
Bundle-ClassPath: .
Bundle-Activator: org.eclipse.cdt.qt.core.qml.tests.Activator
Bundle-ActivationPolicy: lazy

View file

@ -1,46 +1,19 @@
package org.eclipse.cdt.qt.core.qml.tests;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.IOException;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.junit.Rule;
import org.junit.rules.TestName;
public class AbstractParserTest {
// TODO I don't think this really works especially on Hudson.
// cdt.core.tests has TestSourceReader that is more complete.
public static String extract() throws Exception {
StackTraceElement element = Thread.currentThread().getStackTrace()[2];
String className = element.getClassName();
int lineNumber = element.getLineNumber();
Class<?> cls = Class.forName(className);
String fqn = className.replace('.', '/');
fqn = fqn.indexOf("$") == -1 ? fqn : fqn.substring(0, fqn.indexOf("$")); //$NON-NLS-1$ //$NON-NLS-2$
String srcFile = "/" + fqn + ".java"; //$NON-NLS-1$ //$NON-NLS-2$
StringBuffer code = new StringBuffer();
Pattern pattern = Pattern.compile("\\s*//\\s*(.*)"); //$NON-NLS-1$
boolean inComment = false;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(cls.getResourceAsStream(srcFile)))) {
int n = 0;
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (++n >= lineNumber) {
return code.toString();
} else {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
if (!inComment) {
code = new StringBuffer();
}
inComment = true;
code.append(matcher.group(1));
code.append('\n');
} else {
inComment = false;
}
}
}
}
return null;
@Rule
public TestName testName = new TestName();
protected CharSequence getComment() throws IOException {
return TestSourceReader.getContentsForTest(Activator.getBundle(), "src", getClass(), testName.getMethodName(), //$NON-NLS-1$
1)[0];
}
}

View file

@ -0,0 +1,25 @@
package org.eclipse.cdt.qt.core.qml.tests;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
private static BundleContext context;
@Override
public void start(BundleContext context) throws Exception {
Activator.context = context;
}
@Override
public void stop(BundleContext context) throws Exception {
Activator.context = null;
}
public static Bundle getBundle() {
return context != null ? context.getBundle() : null;
}
}

View file

@ -21,11 +21,10 @@ import org.junit.Test;
public class QMLParserTest extends AbstractParserTest {
public void runParser(String code, QMLListener listener) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(code);
public void runParser(CharSequence code, QMLListener listener) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(code.toString());
QMLLexer lexer = new QMLLexer(input);
lexer.addErrorListener(new ANTLRErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
@ -35,22 +34,16 @@ public class QMLParserTest extends AbstractParserTest {
@Override
public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
int prediction, ATNConfigSet configs) {
// TODO Auto-generated method stub
}
@Override
public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
BitSet conflictingAlts, ATNConfigSet configs) {
// TODO Auto-generated method stub
}
@Override
public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact,
BitSet ambigAlts, ATNConfigSet configs) {
// TODO Auto-generated method stub
}
});
CommonTokenStream tokens = new CommonTokenStream(lexer);
@ -81,15 +74,13 @@ public class QMLParserTest extends AbstractParserTest {
parser.qmlProgram();
}
// testCode
// testCode;
@Test
@Ignore
@Ignore // the test doesn't pass yet.
public void testCodeExtract() throws Exception {
// see if you can use TestSourceReader out of the cdt.core.tests plugin
runParser(extract(), new AbstractQMLListener() {
runParser(getComment(), new AbstractQMLListener() {
@Override
public void exitQmlProgram(QmlProgramContext ctx) {
}
});
}