mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 06:32:10 +02:00
Quick test framework for QML parser tests.
Change-Id: Id731074c35f3dc23fbfc2ea249da46fa2404fa25
This commit is contained in:
parent
23833f697e
commit
f047a404b0
5 changed files with 333 additions and 10 deletions
|
@ -6,3 +6,5 @@ Bundle-Version: 1.0.0.qualifier
|
|||
Fragment-Host: org.eclipse.cdt.qt.qml.core;bundle-version="1.0.0"
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
|
||||
Require-Bundle: org.junit
|
||||
Bundle-ClassPath: src/,
|
||||
.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
source.. = src/
|
||||
output.. = bin/
|
||||
bin.includes = META-INF/,\
|
||||
.
|
||||
.,\
|
||||
src/
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package org.eclipse.cdt.qt.qml.core.tests;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class AbstractParserTest {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,207 @@
|
|||
package org.eclipse.cdt.qt.qml.core.tests;
|
||||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.tree.ErrorNode;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLListener;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.FunctionDeclarationContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlHeaderItemContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlImportDeclarationContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlMemberContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlMembersContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlObjectLiteralContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlObjectRootContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlPragmaDeclarationContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlProgramContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlPropertyTypeContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlQualifiedIdContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.SemiContext;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.SingleExpressionContext;
|
||||
|
||||
public class AbstractQMLListener implements QMLListener {
|
||||
|
||||
public AbstractQMLListener() {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitTerminal(TerminalNode node) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitErrorNode(ErrorNode node) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterEveryRule(ParserRuleContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitEveryRule(ParserRuleContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlProgram(QmlProgramContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlProgram(QmlProgramContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlHeaderItem(QmlHeaderItemContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlHeaderItem(QmlHeaderItemContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlImportDeclaration(QmlImportDeclarationContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlImportDeclaration(QmlImportDeclarationContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlQualifiedId(QmlQualifiedIdContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlQualifiedId(QmlQualifiedIdContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlPragmaDeclaration(QmlPragmaDeclarationContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlPragmaDeclaration(QmlPragmaDeclarationContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlObjectRoot(QmlObjectRootContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlObjectRoot(QmlObjectRootContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlObjectLiteral(QmlObjectLiteralContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlObjectLiteral(QmlObjectLiteralContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlMembers(QmlMembersContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlMembers(QmlMembersContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlMember(QmlMemberContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlMember(QmlMemberContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterQmlPropertyType(QmlPropertyTypeContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitQmlPropertyType(QmlPropertyTypeContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterSemi(SemiContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitSemi(SemiContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterSingleExpression(SingleExpressionContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitSingleExpression(SingleExpressionContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterFunctionDeclaration(FunctionDeclarationContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitFunctionDeclaration(FunctionDeclarationContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,25 +1,94 @@
|
|||
package org.eclipse.cdt.qt.qml.core.tests;
|
||||
|
||||
import org.antlr.v4.runtime.ANTLRFileStream;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
import org.antlr.v4.runtime.ANTLRErrorListener;
|
||||
import org.antlr.v4.runtime.ANTLRInputStream;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.Parser;
|
||||
import org.antlr.v4.runtime.RecognitionException;
|
||||
import org.antlr.v4.runtime.Recognizer;
|
||||
import org.antlr.v4.runtime.atn.ATNConfigSet;
|
||||
import org.antlr.v4.runtime.dfa.DFA;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLLexer;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLListener;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser;
|
||||
import org.eclipse.cdt.qt.qml.core.parser.QMLParser.QmlProgramContext;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public class QMLParserTest {
|
||||
public class QMLParserTest extends AbstractParserTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String path = "/Users/dschaefer/Qt/5.5/clang_64/qml/QtTest/TestCase.qml";
|
||||
ANTLRFileStream input = new ANTLRFileStream(path);
|
||||
public void runParser(String code, QMLListener listener) throws Exception {
|
||||
ANTLRInputStream input = new ANTLRInputStream(code);
|
||||
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) {
|
||||
fail(msg);
|
||||
}
|
||||
|
||||
@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);
|
||||
QMLParser parser = new QMLParser(tokens);
|
||||
parser.addParseListener(listener);
|
||||
parser.addErrorListener(new ANTLRErrorListener() {
|
||||
@Override
|
||||
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
|
||||
int charPositionInLine, String msg, RecognitionException e) {
|
||||
fail(msg);
|
||||
}
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
@Override
|
||||
public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
|
||||
int prediction, ATNConfigSet configs) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
|
||||
BitSet conflictingAlts, ATNConfigSet configs) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact,
|
||||
BitSet ambigAlts, ATNConfigSet configs) {
|
||||
}
|
||||
});
|
||||
parser.qmlProgram();
|
||||
System.out.println("time: " + (System.currentTimeMillis() - start) + "ms.");
|
||||
}
|
||||
|
||||
// testCode
|
||||
@Test
|
||||
public void testCodeExtract() throws Exception {
|
||||
runParser(extract(), new AbstractQMLListener() {
|
||||
@Override
|
||||
public void exitQmlProgram(QmlProgramContext ctx) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue