mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-04 14:55:41 +02:00
Set up QML parser tests using TestSourceReader.
Change-Id: I40a3ef8dcf53d6189a980ae6ae7128746867c70f
This commit is contained in:
parent
7cf2a14cc9
commit
3b26dc26c4
4 changed files with 47 additions and 54 deletions
|
@ -4,7 +4,11 @@ Bundle-Name: QML Tests
|
||||||
Bundle-SymbolicName: org.eclipse.cdt.qt.core.tests
|
Bundle-SymbolicName: org.eclipse.cdt.qt.core.tests
|
||||||
Bundle-Version: 2.0.0.qualifier
|
Bundle-Version: 2.0.0.qualifier
|
||||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
|
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.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-ClassPath: .
|
||||||
|
Bundle-Activator: org.eclipse.cdt.qt.core.qml.tests.Activator
|
||||||
|
Bundle-ActivationPolicy: lazy
|
||||||
|
|
|
@ -1,46 +1,19 @@
|
||||||
package org.eclipse.cdt.qt.core.qml.tests;
|
package org.eclipse.cdt.qt.core.qml.tests;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.regex.Matcher;
|
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
||||||
import java.util.regex.Pattern;
|
import org.junit.Rule;
|
||||||
|
import org.junit.rules.TestName;
|
||||||
|
|
||||||
public class AbstractParserTest {
|
public class AbstractParserTest {
|
||||||
|
|
||||||
// TODO I don't think this really works especially on Hudson.
|
@Rule
|
||||||
// cdt.core.tests has TestSourceReader that is more complete.
|
public TestName testName = new TestName();
|
||||||
public static String extract() throws Exception {
|
|
||||||
StackTraceElement element = Thread.currentThread().getStackTrace()[2];
|
protected CharSequence getComment() throws IOException {
|
||||||
String className = element.getClassName();
|
return TestSourceReader.getContentsForTest(Activator.getBundle(), "src", getClass(), testName.getMethodName(), //$NON-NLS-1$
|
||||||
int lineNumber = element.getLineNumber();
|
1)[0];
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -21,11 +21,10 @@ import org.junit.Test;
|
||||||
|
|
||||||
public class QMLParserTest extends AbstractParserTest {
|
public class QMLParserTest extends AbstractParserTest {
|
||||||
|
|
||||||
public void runParser(String code, QMLListener listener) throws Exception {
|
public void runParser(CharSequence code, QMLListener listener) throws Exception {
|
||||||
ANTLRInputStream input = new ANTLRInputStream(code);
|
ANTLRInputStream input = new ANTLRInputStream(code.toString());
|
||||||
QMLLexer lexer = new QMLLexer(input);
|
QMLLexer lexer = new QMLLexer(input);
|
||||||
lexer.addErrorListener(new ANTLRErrorListener() {
|
lexer.addErrorListener(new ANTLRErrorListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
|
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
|
||||||
int charPositionInLine, String msg, RecognitionException e) {
|
int charPositionInLine, String msg, RecognitionException e) {
|
||||||
|
@ -35,22 +34,16 @@ public class QMLParserTest extends AbstractParserTest {
|
||||||
@Override
|
@Override
|
||||||
public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
|
public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
|
||||||
int prediction, ATNConfigSet configs) {
|
int prediction, ATNConfigSet configs) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
|
public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
|
||||||
BitSet conflictingAlts, ATNConfigSet configs) {
|
BitSet conflictingAlts, ATNConfigSet configs) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact,
|
public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact,
|
||||||
BitSet ambigAlts, ATNConfigSet configs) {
|
BitSet ambigAlts, ATNConfigSet configs) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
|
@ -81,15 +74,13 @@ public class QMLParserTest extends AbstractParserTest {
|
||||||
parser.qmlProgram();
|
parser.qmlProgram();
|
||||||
}
|
}
|
||||||
|
|
||||||
// testCode
|
// testCode;
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore // the test doesn't pass yet.
|
||||||
public void testCodeExtract() throws Exception {
|
public void testCodeExtract() throws Exception {
|
||||||
// see if you can use TestSourceReader out of the cdt.core.tests plugin
|
runParser(getComment(), new AbstractQMLListener() {
|
||||||
runParser(extract(), new AbstractQMLListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void exitQmlProgram(QmlProgramContext ctx) {
|
public void exitQmlProgram(QmlProgramContext ctx) {
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue