From 7f9a448eeb3b4f1ff77aecdff82c298aea4f7a30 Mon Sep 17 00:00:00 2001 From: Bill King Date: Thu, 19 Aug 2010 11:54:51 +1000 Subject: Fixes: Tests should have unique names. Details: Autotests need to be named uniquely, otherwise all sorts of databases get lost and confused. --- .../qml/qmleditor/codeformatter/codeformatter.pro | 24 - .../qmleditor/codeformatter/tst_codeformatter.cpp | 888 --------------------- .../qml/qmleditor/lookup/data/localIdLookup.qml | 47 -- .../qml/qmleditor/lookup/data/localRootLookup.qml | 16 - .../qml/qmleditor/lookup/data/localScopeLookup.qml | 17 - .../lookup/data/localScriptMethodLookup.qml | 32 - tests/auto/qml/qmleditor/lookup/lookup.pro | 22 - tests/auto/qml/qmleditor/lookup/testfiles.qrc | 8 - tests/auto/qml/qmleditor/lookup/tst_lookup.cpp | 324 -------- .../qmlcodeformatter/qmlcodeformatter.pro | 22 + .../qmlcodeformatter/tst_qmlcodeformatter.cpp | 888 +++++++++++++++++++++ tests/auto/qml/qmleditor/qmleditor.pro | 3 +- .../qml/qmleditor/qmllookup/data/localIdLookup.qml | 47 ++ .../qmleditor/qmllookup/data/localRootLookup.qml | 16 + .../qmleditor/qmllookup/data/localScopeLookup.qml | 17 + .../qmllookup/data/localScriptMethodLookup.qml | 32 + tests/auto/qml/qmleditor/qmllookup/qmllookup.pro | 17 + tests/auto/qml/qmleditor/qmllookup/testfiles.qrc | 8 + .../auto/qml/qmleditor/qmllookup/tst_qmllookup.cpp | 324 ++++++++ 19 files changed, 1372 insertions(+), 1380 deletions(-) delete mode 100644 tests/auto/qml/qmleditor/codeformatter/codeformatter.pro delete mode 100644 tests/auto/qml/qmleditor/codeformatter/tst_codeformatter.cpp delete mode 100644 tests/auto/qml/qmleditor/lookup/data/localIdLookup.qml delete mode 100644 tests/auto/qml/qmleditor/lookup/data/localRootLookup.qml delete mode 100644 tests/auto/qml/qmleditor/lookup/data/localScopeLookup.qml delete mode 100644 tests/auto/qml/qmleditor/lookup/data/localScriptMethodLookup.qml delete mode 100644 tests/auto/qml/qmleditor/lookup/lookup.pro delete mode 100644 tests/auto/qml/qmleditor/lookup/testfiles.qrc delete mode 100644 tests/auto/qml/qmleditor/lookup/tst_lookup.cpp create mode 100644 tests/auto/qml/qmleditor/qmlcodeformatter/qmlcodeformatter.pro create mode 100644 tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp create mode 100644 tests/auto/qml/qmleditor/qmllookup/data/localIdLookup.qml create mode 100644 tests/auto/qml/qmleditor/qmllookup/data/localRootLookup.qml create mode 100644 tests/auto/qml/qmleditor/qmllookup/data/localScopeLookup.qml create mode 100644 tests/auto/qml/qmleditor/qmllookup/data/localScriptMethodLookup.qml create mode 100644 tests/auto/qml/qmleditor/qmllookup/qmllookup.pro create mode 100644 tests/auto/qml/qmleditor/qmllookup/testfiles.qrc create mode 100644 tests/auto/qml/qmleditor/qmllookup/tst_qmllookup.cpp diff --git a/tests/auto/qml/qmleditor/codeformatter/codeformatter.pro b/tests/auto/qml/qmleditor/codeformatter/codeformatter.pro deleted file mode 100644 index ee95456908..0000000000 --- a/tests/auto/qml/qmleditor/codeformatter/codeformatter.pro +++ /dev/null @@ -1,24 +0,0 @@ -TEMPLATE = app -CONFIG += qt warn_on console depend_includepath -QT += testlib network - -SRCDIR = ../../../../../src - -#DEFINES += QML_BUILD_STATIC_LIB -#include($$SRCDIR/../qtcreator.pri) -include($$SRCDIR/libs/qmljs/qmljs-lib.pri) -include($$SRCDIR/libs/utils/utils-lib.pri) -#LIBS += -L$$IDE_LIBRARY_PATH - -SOURCES += \ - tst_codeformatter.cpp \ - $$SRCDIR/plugins/qmljseditor/qmljseditorcodeformatter.cpp \ - $$SRCDIR/plugins/texteditor/basetextdocumentlayout.cpp - -HEADERS += \ - $$SRCDIR/plugins/qmljseditor/qmljseditorcodeformatter.h \ - $$SRCDIR/plugins/texteditor/basetextdocumentlayout.h \ - -INCLUDEPATH += $$SRCDIR/plugins $$SRCDIR/libs - -TARGET=tst_$$TARGET diff --git a/tests/auto/qml/qmleditor/codeformatter/tst_codeformatter.cpp b/tests/auto/qml/qmleditor/codeformatter/tst_codeformatter.cpp deleted file mode 100644 index 336e642eaf..0000000000 --- a/tests/auto/qml/qmleditor/codeformatter/tst_codeformatter.cpp +++ /dev/null @@ -1,888 +0,0 @@ -#include -#include -#include -#include -#include - -#include - -using namespace QmlJSEditor; - -class tst_CodeFormatter: public QObject -{ - Q_OBJECT - -private Q_SLOTS: - void objectDefinitions1(); - void objectDefinitions2(); - void expressionEndSimple(); - void expressionEnd(); - void expressionEndBracket(); - void expressionEndParen(); - void objectBinding(); - void arrayBinding(); - void functionDeclaration(); - void functionExpression(); - void propertyDeclarations(); - void signalDeclarations(); - void ifBinding1(); - void ifBinding2(); - void ifStatementWithoutBraces1(); - void ifStatementWithoutBraces2(); - void ifStatementWithBraces1(); - void ifStatementWithBraces2(); - void ifStatementMixed(); - void ifStatementAndComments(); - void ifStatementLongCondition(); - void moreIfThenElse(); - void strayElse(); - void oneLineIf(); - void forStatement(); - void whileStatement(); - void tryStatement(); - void doWhile(); - void cStyleComments(); - void cppStyleComments(); - void qmlKeywords(); - void ternary(); - void switch1(); -// void gnuStyle(); -// void whitesmithsStyle(); - void expressionContinuation(); -}; - -struct Line { - Line(QString l) - : line(l) - { - for (int i = 0; i < l.size(); ++i) { - if (!l.at(i).isSpace()) { - expectedIndent = i; - return; - } - } - expectedIndent = l.size(); - } - - Line(QString l, int expect) - : line(l), expectedIndent(expect) - {} - - QString line; - int expectedIndent; -}; - -QString concatLines(QList lines) -{ - QString result; - foreach (const Line &l, lines) { - result += l.line; - result += "\n"; - } - return result; -} - -void checkIndent(QList data, int style = 0) -{ - Q_UNUSED(style) - - QString text = concatLines(data); - QTextDocument document(text); - QtStyleCodeFormatter formatter; - - int i = 0; - foreach (const Line &l, data) { - QTextBlock b = document.findBlockByLineNumber(i); - if (l.expectedIndent != -1) { - int actualIndent = formatter.indentFor(b); - if (actualIndent != l.expectedIndent) { - QFAIL(QString("Wrong indent in line %1 with text '%2', expected indent %3, got %4").arg( - QString::number(i+1), l.line, QString::number(l.expectedIndent), QString::number(actualIndent)).toLatin1().constData()); - } - } - formatter.updateLineStateChange(b); - ++i; - } -} - -void tst_CodeFormatter::objectDefinitions1() -{ - QList data; - data << Line("import Qt 4.7") - << Line("") - << Line("Rectangle {") - << Line(" foo: bar;") - << Line(" Item {") - << Line(" x: 42;") - << Line(" y: x;") - << Line(" }") - << Line(" Component.onCompleted: foo;") - << Line(" ") - << Line(" Foo.Bar {") - << Line(" width: 12 + 54;") - << Line(" anchors.fill: parent;") - << Line(" }") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::objectDefinitions2() -{ - QList data; - data << Line("import Qt 4.7") - << Line("") - << Line("Rectangle {") - << Line(" foo: bar;") - << Line(" Image { source: \"a+b+c\"; x: 42; y: 12 }") - << Line(" Component.onCompleted: foo;") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::expressionEndSimple() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar +") - << Line(" foo(4, 5) +") - << Line(" 7") - << Line(" x: 42") - << Line(" y: 43") - << Line(" width: 10") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::expressionEnd() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar +") - << Line(" foo(4") - << Line(" + 5)") - << Line(" + 7") - << Line(" x: 42") - << Line(" + 43") - << Line(" + 10") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::expressionEndParen() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar") - << Line(" (foo(4") - << Line(" + 5)") - << Line(" + 7,") - << Line(" abc)") - << Line(" x: a + b(fpp, ba + 12) + foo(") - << Line(" bar,") - << Line(" 10)") - << Line(" + 10") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::expressionEndBracket() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar") - << Line(" [foo[4") - << Line(" + 5]") - << Line(" + 7,") - << Line(" abc]") - << Line(" x: a + b[fpp, ba + 12] + foo[") - << Line(" bar,") - << Line(" 10]") - << Line(" + 10") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::objectBinding() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar") - << Line(" x: 3") - << Line(" foo: Gradient {") - << Line(" x: 12") - << Line(" y: x") - << Line(" }") - << Line(" Item {") - << Line(" states: State {}") - << Line(" }") - << Line(" x: 1") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::arrayBinding() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar") - << Line(" x: 3") - << Line(" foo: [") - << Line(" State {") - << Line(" y: x") - << Line(" },") - << Line(" State") - << Line(" {") - << Line(" }") - << Line(" ]") - << Line(" foo: [") - << Line(" 1 +") - << Line(" 2") - << Line(" + 345 * foo(") - << Line(" bar, car,") - << Line(" dar),") - << Line(" x, y,") - << Line(" z,") - << Line(" ]") - << Line(" x: 1") - << Line("}") - ; - checkIndent(data); -} - - - -void tst_CodeFormatter::moreIfThenElse() -{ - QList data; - data << Line("Image {") - << Line(" source: {") - << Line(" if(type == 1) {") - << Line(" \"pics/blueStone.png\";") - << Line(" } else if (type == 2) {") - << Line(" \"pics/head.png\";") - << Line(" } else {") - << Line(" \"pics/redStone.png\";") - << Line(" }") - << Line(" }") - << Line("}"); - checkIndent(data); -} - - -void tst_CodeFormatter::functionDeclaration() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar") - << Line(" function foo(a, b, c) {") - << Line(" if (a)") - << Line(" b;") - << Line(" }") - << Line(" property alias boo :") - << Line(" foo") - << Line(" Item {") - << Line(" property variant g : Gradient {") - << Line(" v: 12") - << Line(" }") - << Line(" function bar(") - << Line(" a, b,") - << Line(" c)") - << Line(" {") - << Line(" var b") - << Line(" }") - << Line(" function bar(a,") - << Line(" a, b,") - << Line(" c)") - << Line(" {") - << Line(" var b") - << Line(" }") - << Line(" }") - << Line(" x: 1") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::functionExpression() -{ - QList data; - data << Line("Rectangle {") - << Line("onFoo: {", 4) - << Line(" function foo(a, b, c) {") - << Line(" if (a)") - << Line(" b;") - << Line(" }") - << Line(" return function(a, b) { return a + b; }") - << Line(" return function foo(a, b) {") - << Line(" return a") - << Line(" }") - << Line("}") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::propertyDeclarations() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar") - << Line(" property int foo : 2 +") - << Line(" x") - << Line(" property list bar") - << Line(" property alias boo :") - << Line(" foo") - << Line(" Item {") - << Line(" property variant g : Gradient {") - << Line(" v: 12") - << Line(" }") - << Line(" default property Item g") - << Line(" default property Item g : parent.foo") - << Line(" }") - << Line(" x: 1") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::signalDeclarations() -{ - QList data; - data << Line("Rectangle {") - << Line(" foo: bar") - << Line(" signal foo") - << Line(" x: bar") - << Line(" signal bar(a, int b)") - << Line(" signal bar2()") - << Line(" Item {") - << Line(" signal property") - << Line(" signal import(a, b);") - << Line(" }") - << Line(" x: 1") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::ifBinding1() -{ - QList data; - data << Line("A.Rectangle {") - << Line(" foo: bar") - << Line(" x: if (a) b") - << Line(" x: if (a)") - << Line(" b") - << Line(" x: if (a) b;") - << Line(" x: if (a)") - << Line(" b;") - << Line(" x: if (a) b; else c") - << Line(" x: if (a) b") - << Line(" else c") - << Line(" x: if (a) b;") - << Line(" else c") - << Line(" x: if (a) b;") - << Line(" else") - << Line(" c") - << Line(" x: if (a)") - << Line(" b") - << Line(" else") - << Line(" c") - << Line(" x: if (a) b; else c;") - << Line(" x: 1") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::ifBinding2() -{ - QList data; - data << Line("A.Rectangle {") - << Line(" foo: bar") - << Line(" x: if (a) b +") - << Line(" 5 +") - << Line(" 5 * foo(") - << Line(" 1, 2)") - << Line(" else a =") - << Line(" foo(15,") - << Line(" bar(") - << Line(" 1),") - << Line(" bar)") - << Line(" x: if (a) b") - << Line(" + 5") - << Line(" + 5") - << Line(" x: if (a)") - << Line(" b") - << Line(" + 5") - << Line(" + 5") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::ifStatementWithoutBraces1() -{ - QList data; - data << Line("Rectangle {") - << Line(" x: if (a)") - << Line(" if (b)") - << Line(" foo") - << Line(" else if (c)") - << Line(" foo") - << Line(" else") - << Line(" if (d)") - << Line(" foo;") - << Line(" else") - << Line(" a + b + ") - << Line(" c") - << Line(" else") - << Line(" foo;") - << Line(" y: 2") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::ifStatementWithoutBraces2() -{ - QList data; - data << Line("Rectangle {") - << Line(" x: {") - << Line(" if (a)") - << Line(" if (b)") - << Line(" foo;") - << Line(" if (a) b();") - << Line(" if (a) b(); else") - << Line(" foo;") - << Line(" if (a)") - << Line(" if (b)") - << Line(" foo;") - << Line(" else if (c)") - << Line(" foo;") - << Line(" else") - << Line(" if (d)") - << Line(" foo;") - << Line(" else") - << Line(" e") - << Line(" else") - << Line(" foo;") - << Line(" }") - << Line(" foo: bar") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::ifStatementWithBraces1() -{ - QList data; - data << Line("Rectangle {") - << Line("onClicked: {", 4) - << Line(" if (a) {") - << Line(" if (b) {") - << Line(" foo;") - << Line(" } else if (c) {") - << Line(" foo") - << Line(" } else {") - << Line(" if (d) {") - << Line(" foo") - << Line(" } else {") - << Line(" foo;") - << Line(" }") - << Line(" }") - << Line(" } else {") - << Line(" foo;") - << Line(" }") - << Line("}") - << Line("}"); - checkIndent(data); -} - -void tst_CodeFormatter::ifStatementWithBraces2() -{ - QList data; - data << Line("Rectangle {") - << Line("onClicked:", 4) - << Line(" if (a)") - << Line(" {") - << Line(" if (b)") - << Line(" {") - << Line(" foo") - << Line(" }") - << Line(" else if (c)") - << Line(" {") - << Line(" foo;") - << Line(" }") - << Line(" else") - << Line(" {") - << Line(" if (d)") - << Line(" {") - << Line(" foo;") - << Line(" }") - << Line(" else") - << Line(" {") - << Line(" foo") - << Line(" }") - << Line(" }") - << Line(" }") - << Line(" else") - << Line(" {") - << Line(" foo") - << Line(" }") - << Line("}"); - checkIndent(data); -} - -void tst_CodeFormatter::ifStatementMixed() -{ - QList data; - data << Line("Rectangle {") - << Line("onClicked:", 4) - << Line(" if (foo)") - << Line(" if (bar)") - << Line(" {") - << Line(" foo;") - << Line(" }") - << Line(" else") - << Line(" if (car)") - << Line(" {}") - << Line(" else doo") - << Line(" else abc") - << Line("}"); - checkIndent(data); -} - -void tst_CodeFormatter::ifStatementAndComments() -{ - QList data; - data << Line("Rectangle {") - << Line("onClicked: {", 4) - << Line(" if (foo)") - << Line(" ; // bla") - << Line(" else if (bar)") - << Line(" ;") - << Line(" if (foo)") - << Line(" ; /*bla") - << Line(" bla */") - << Line(" else if (bar)") - << Line(" // foobar") - << Line(" ;") - << Line(" else if (bar)") - << Line(" /* bla") - << Line(" bla */") - << Line(" ;") - << Line("}") - << Line("}"); - checkIndent(data); -} - -void tst_CodeFormatter::ifStatementLongCondition() -{ - QList data; - data << Line("Rectangle {") - << Line("onClicked: {", 4) - << Line(" if (foo &&") - << Line(" bar") - << Line(" || (a + b > 4") - << Line(" && foo(bar)") - << Line(" )") - << Line(" ) {") - << Line(" foo;") - << Line(" }") - << Line("}"); - checkIndent(data); -} - -void tst_CodeFormatter::strayElse() -{ - QList data; - data << Line("Rectangle {") - << Line("onClicked: {", 4) - << Line(" while( true ) {}") - << Line(" else", -1) - << Line(" else {", -1) - << Line(" }", -1) - << Line("}"); - checkIndent(data); -} - -void tst_CodeFormatter::oneLineIf() -{ - QList data; - data << Line("Rectangle {") - << Line(" onClicked: { if (showIt) show(); }") - << Line(" x: 2") - << Line("};") - ; - checkIndent(data); -} - -void tst_CodeFormatter::forStatement() -{ - QList data; - data << Line("for (var i = 0; i < 20; ++i) {") - << Line(" print(i);") - << Line("}") - << Line("for (var x in [a, b, c, d])") - << Line(" x += 5") - << Line("var z") - << Line("for (var x in [a, b, c, d])") - << Line(" for (;;)") - << Line(" {") - << Line(" for (a(); b(); c())") - << Line(" for (a();") - << Line(" b(); c())") - << Line(" for (a(); b(); c())") - << Line(" print(3*d)") - << Line(" }") - << Line("z = 2") - ; - checkIndent(data); -} - -void tst_CodeFormatter::whileStatement() -{ - QList data; - data << Line("while (i < 20) {") - << Line(" print(i);") - << Line("}") - << Line("while (x in [a, b, c, d])") - << Line(" x += 5") - << Line("var z") - << Line("while (a + b > 0") - << Line(" && b + c > 0)") - << Line(" for (;;)") - << Line(" {") - << Line(" for (a(); b(); c())") - << Line(" while (a())") - << Line(" for (a(); b(); c())") - << Line(" print(3*d)") - << Line(" }") - << Line("z = 2") - ; - checkIndent(data); -} - -void tst_CodeFormatter::tryStatement() -{ - QList data; - data << Line("try {") - << Line(" print(i);") - << Line("} catch (foo) {") - << Line(" print(foo)") - << Line("} finally {") - << Line(" var z") - << Line(" while (a + b > 0") - << Line(" && b + c > 0)") - << Line(" ;") - << Line("}") - << Line("z = 2") - ; - checkIndent(data); -} - -void tst_CodeFormatter::doWhile() -{ - QList data; - data << Line("function foo() {") - << Line(" do { if (c) foo; } while(a);") - << Line(" do {") - << Line(" if(a);") - << Line(" } while(a);") - << Line(" do") - << Line(" foo;") - << Line(" while(a);") - << Line(" do foo; while(a);") - << Line("};") - ; - checkIndent(data); -} - -void tst_CodeFormatter::cStyleComments() -{ - QList data; - data << Line("/*") - << Line(" ") - << Line(" foo") - << Line(" ") - << Line(" foo") - << Line(" ") - << Line("*/") - << Line("Rectangle {") - << Line(" /*") - << Line(" ") - << Line(" foo") - << Line(" ") - << Line(" */") - << Line(" /* bar */") - << Line("}") - << Line("Item {") - << Line(" /* foo */") - << Line(" /*") - << Line(" ") - << Line(" foo") - << Line(" ") - << Line(" */") - << Line(" /* bar */") - ; - checkIndent(data); -} - -void tst_CodeFormatter::cppStyleComments() -{ - QList data; - data << Line("// abc") - << Line("Item { ") - << Line(" // ghij") - << Line(" // def") - << Line(" // ghij") - << Line(" x: 4 // hik") - << Line(" // doo") - << Line("} // ba") - << Line("// ba") - ; - checkIndent(data); -} - -void tst_CodeFormatter::ternary() -{ - QList data; - data << Line("function foo() {") - << Line(" var i = a ? b : c;") - << Line(" foo += a_bigger_condition ?") - << Line(" b") - << Line(" : c;") - << Line(" foo += a_bigger_condition") - << Line(" ? b") - << Line(" : c;") - << Line(" var i = a ?") - << Line(" b : c;") - << Line(" var i = aooo ? b") - << Line(" : c +") - << Line(" 2;") - << Line(" var i = (a ? b : c) + (foo") - << Line(" bar);") - << Line("}") - ; - checkIndent(data); -} - -void tst_CodeFormatter::switch1() -{ - QList data; - data << Line("function foo() {") - << Line(" switch (a) {") - << Line(" case 1:") - << Line(" foo;") - << Line(" if (a);") - << Line(" case 2:") - << Line(" case 3: {") - << Line(" foo") - << Line(" }") - << Line(" case 4:") - << Line(" {") - << Line(" foo;") - << Line(" }") - << Line(" case bar:") - << Line(" break") - << Line(" }") - << Line(" case 4:") - << Line(" {") - << Line(" if (a) {") - << Line(" }") - << Line(" }") - << Line("}") - ; - checkIndent(data); -} - -//void tst_CodeFormatter::gnuStyle() -//{ -// QList data; -// data << Line("struct S") -// << Line("{") -// << Line(" void foo()") -// << Line(" {") -// << Line(" if (a)") -// << Line(" {") -// << Line(" fpp;") -// << Line(" }") -// << Line(" else if (b)") -// << Line(" {") -// << Line(" fpp;") -// << Line(" }") -// << Line(" else") -// << Line(" {") -// << Line(" }") -// << Line(" if (b) {") -// << Line(" fpp;") -// << Line(" }") -// << Line(" }") -// << Line("};") -// ; -// checkIndent(data, 1); -//} - -//void tst_CodeFormatter::whitesmithsStyle() -//{ -// QList data; -// data << Line("struct S") -// << Line(" {") -// << Line(" void foo()") -// << Line(" {") -// << Line(" if (a)") -// << Line(" {") -// << Line(" fpp;") -// << Line(" }") -// << Line(" if (b) {") -// << Line(" fpp;") -// << Line(" }") -// << Line(" }") -// << Line(" };") -// ; -// checkIndent(data, 2); -//} - -void tst_CodeFormatter::qmlKeywords() -{ - QList data; - data << Line("Rectangle {") - << Line(" on: 2") - << Line(" property: 2") - << Line(" signal: 2") - << Line(" list: 2") - << Line(" as: 2") - << Line(" import: 2") - << Line(" Item {") - << Line(" }") - << Line(" x: 2") - << Line("};") - ; - checkIndent(data); -} - -void tst_CodeFormatter::expressionContinuation() -{ - QList data; - data << Line("var x = 1 ? 2") - << Line(" + 3 : 4") - << Line("++x") - << Line("++y--") - << Line("x +=") - << Line(" y++") - << Line("var z") - ; - checkIndent(data); -} - -QTEST_APPLESS_MAIN(tst_CodeFormatter) -#include "tst_codeformatter.moc" - - diff --git a/tests/auto/qml/qmleditor/lookup/data/localIdLookup.qml b/tests/auto/qml/qmleditor/lookup/data/localIdLookup.qml deleted file mode 100644 index f3df6cd5e3..0000000000 --- a/tests/auto/qml/qmleditor/lookup/data/localIdLookup.qml +++ /dev/null @@ -1,47 +0,0 @@ -import Qt 4.7 - -// Try to look up x, y, z, opacity and visible -// in all symbol contexts. It should always get the right item. - -Item { - id: x - Script { - function x() {} - function y() {} - function z() {} - function opacity() {} - function visible() {} - } - property variant x: "wrong"; - property variant y: "wrong"; - property variant z: "wrong"; - property variant opacity: "wrong"; - property variant visible: "wrong"; - Item { - id: y - property variant x: "wrong"; - property variant y: "wrong"; - property variant z: "wrong"; - property variant opacity: "wrong"; - property variant visible: "wrong"; - Item { - id: z - property variant x: "wrong"; - property variant y: "wrong"; - property variant z: "wrong"; - property variant opacity: "wrong"; - property variant visible: "wrong"; - Item { - id: opacity - property variant x: "wrong"; - property variant y: "wrong"; - property variant z: "wrong"; - property variant opacity: "wrong"; - property variant visible: "wrong"; - Item { - id: visible - } - } - } - } -} \ No newline at end of file diff --git a/tests/auto/qml/qmleditor/lookup/data/localRootLookup.qml b/tests/auto/qml/qmleditor/lookup/data/localRootLookup.qml deleted file mode 100644 index 74562a03e6..0000000000 --- a/tests/auto/qml/qmleditor/lookup/data/localRootLookup.qml +++ /dev/null @@ -1,16 +0,0 @@ -import Qt 4.7 - -// Try to look up prop in child. -// It should get the root's prop. - -Rectangle { - id: theRoot - property variant prop - Item { - id: theParent - property variant prop - Item { - id: theChild - } - } -} diff --git a/tests/auto/qml/qmleditor/lookup/data/localScopeLookup.qml b/tests/auto/qml/qmleditor/lookup/data/localScopeLookup.qml deleted file mode 100644 index e103232e4b..0000000000 --- a/tests/auto/qml/qmleditor/lookup/data/localScopeLookup.qml +++ /dev/null @@ -1,17 +0,0 @@ -import Qt 4.7 - -// Try to look up prop in all symbol contexts. -// It should always get the local one. - -Item { - id: theRoot - property variant prop - Item { - id: theParent - property variant prop - Item { - id: theChild - property variant prop - } - } -} diff --git a/tests/auto/qml/qmleditor/lookup/data/localScriptMethodLookup.qml b/tests/auto/qml/qmleditor/lookup/data/localScriptMethodLookup.qml deleted file mode 100644 index 9d3f39bef4..0000000000 --- a/tests/auto/qml/qmleditor/lookup/data/localScriptMethodLookup.qml +++ /dev/null @@ -1,32 +0,0 @@ -import Qt 4.7 - -// Try to look up x, y, z -// in all symbol contexts. It should always get the function. - -Item { - id: theRoot - Script { - function x() {} - } - property variant x: "wrong"; - property variant y: "wrong"; - property variant z: "wrong"; - Item { - id: theParent - Script { - function y() {} - } - property variant x: "wrong"; - property variant y: "wrong"; - property variant z: "wrong"; - Item { - id: theChild - Script { - function z() {} - } - property variant x: "wrong"; - property variant y: "wrong"; - property variant z: "wrong"; - } - } -} \ No newline at end of file diff --git a/tests/auto/qml/qmleditor/lookup/lookup.pro b/tests/auto/qml/qmleditor/lookup/lookup.pro deleted file mode 100644 index 1c7d8b2adc..0000000000 --- a/tests/auto/qml/qmleditor/lookup/lookup.pro +++ /dev/null @@ -1,22 +0,0 @@ -TEMPLATE = app -CONFIG += qt warn_on console depend_includepath -QT += testlib -include(../../../../../src/libs/qmljs/qmljs-lib.pri) -DEFINES += QML_BUILD_STATIC_LIB -EDITOR_DIR=../../../../../src/plugins/qmljseditor - -INCLUDEPATH += $$EDITOR_DIR - -TARGET=tst_$$TARGET - -SOURCES += tst_lookup.cpp \ - $$EDITOR_DIR/qmllookupcontext.cpp - -HEADERS += $$EDITOR_DIR/qmllookupcontext.h -RESOURCES += testfiles.qrc - -OTHER_FILES += \ - data/localIdLookup.qml \ - data/localScriptMethodLookup.qml \ - data/localScopeLookup.qml \ - data/localRootLookup.qml diff --git a/tests/auto/qml/qmleditor/lookup/testfiles.qrc b/tests/auto/qml/qmleditor/lookup/testfiles.qrc deleted file mode 100644 index b780e4149e..0000000000 --- a/tests/auto/qml/qmleditor/lookup/testfiles.qrc +++ /dev/null @@ -1,8 +0,0 @@ - - - data/localIdLookup.qml - data/localScriptMethodLookup.qml - data/localScopeLookup.qml - data/localRootLookup.qml - - diff --git a/tests/auto/qml/qmleditor/lookup/tst_lookup.cpp b/tests/auto/qml/qmleditor/lookup/tst_lookup.cpp deleted file mode 100644 index b43bc5e5e9..0000000000 --- a/tests/auto/qml/qmleditor/lookup/tst_lookup.cpp +++ /dev/null @@ -1,324 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -#include - -#include - -using namespace QmlJSEditor; -using namespace QmlJSEditor::Internal; -using namespace QmlJS; -using namespace QmlJS::AST; - -class tst_Lookup: public QObject -{ - Q_OBJECT - -public: - tst_Lookup(): _typeSystem(0) {} - ~tst_Lookup() { if (_typeSystem) resetTypeSystem(); } - - void resetTypeSystem() { if (_typeSystem) { delete _typeSystem; _typeSystem = 0; }} - -private Q_SLOTS: - void basicSymbolTest(); - void basicLookupTest(); - void localIdLookup(); - void localScriptMethodLookup(); - void localScopeLookup(); - void localRootLookup(); - -protected: - Document::Ptr basicSymbolTest(const QString &input) const - { - const QLatin1String filename(""); - Document::Ptr doc = Document::create(filename); - doc->setSource(input); - doc->parseQml(); - - QList msgs = doc->diagnosticMessages(); - foreach (const DiagnosticMessage &msg, msgs) { - if (msg.isError()) { - qDebug() << "Error:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; - } else if (msg.isWarning()) { - qDebug() << "Warning:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; - } else { - qDebug() << "Diagnostic:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; - } - } - - return doc; - } - - Snapshot snapshot(const Document::Ptr &doc) const - { - Snapshot snapshot; - snapshot.insert(doc); - return snapshot; - } - - TypeSystem *typeSystem() { - if (!_typeSystem) - _typeSystem = new TypeSystem; - return _typeSystem; - } - -private: - TypeSystem *_typeSystem; -}; - -void tst_Lookup::basicSymbolTest() -{ - const QLatin1String input( - "import Qt 4.6\n" - "\n" - "Rectangle {\n" - " x: 10\n" - " y: 10\n" - " width: 10\n" - " height: 10\n" - "}\n" - ); - - Document::Ptr doc = basicSymbolTest(input); - QVERIFY(doc->isParsedCorrectly()); - - UiProgram *program = doc->qmlProgram(); - QVERIFY(program); - QVERIFY(program->members); - QVERIFY(program->members->member); - UiObjectDefinition *rectDef = cast(program->members->member); - QVERIFY(rectDef); - QVERIFY(rectDef->qualifiedTypeNameId->name); - QCOMPARE(rectDef->qualifiedTypeNameId->name->asString(), QLatin1String("Rectangle")); - QVERIFY(rectDef->initializer); - UiObjectMemberList *rectMembers = rectDef->initializer->members; - QVERIFY(rectMembers); - QVERIFY(rectMembers->member); - - UiScriptBinding *xBinding = cast(rectMembers->member); - QVERIFY(xBinding); - QVERIFY(xBinding->qualifiedId); - QVERIFY(xBinding->qualifiedId->name); - QCOMPARE(xBinding->qualifiedId->name->asString(), QLatin1String("x")); - - Symbol::List docSymbols = doc->symbols(); - QCOMPARE(docSymbols.size(), 1); - - Symbol *rectSymbol = docSymbols.at(0); - QCOMPARE(rectSymbol->name(), QLatin1String("Rectangle")); - QCOMPARE(rectSymbol->members().size(), 4); - - SymbolFromFile *rectFromFile = rectSymbol->asSymbolFromFile(); - QVERIFY(rectFromFile); - SymbolFromFile *xSymbol = rectFromFile->findMember(xBinding); - QVERIFY(xSymbol); - QCOMPARE(xSymbol->name(), QLatin1String("x")); -} - -void tst_Lookup::basicLookupTest() -{ - const QLatin1String input( - "import Qt 4.6\n" - "Item{}\n" - ); - - Document::Ptr doc = basicSymbolTest(input); - QVERIFY(doc->isParsedCorrectly()); - - UiProgram *program = doc->qmlProgram(); - QVERIFY(program); - - QStack emptyScope; - QmlLookupContext context(emptyScope, doc, snapshot(doc), typeSystem()); - Symbol *rectSymbol = context.resolveType(QLatin1String("Text")); - QVERIFY(rectSymbol); - - PrimitiveSymbol *buildInRect = rectSymbol->asPrimitiveSymbol(); - QVERIFY(buildInRect); - QCOMPARE(buildInRect->name(), QLatin1String("Text")); - - Symbol::List allBuildInRectMembers = buildInRect->members(true); - QVERIFY(!allBuildInRectMembers.isEmpty()); - bool xPropFound = false; - bool fontPropFound = false; - foreach (Symbol *symbol, allBuildInRectMembers) { - if (symbol->name() == QLatin1String("x")) - xPropFound = true; - else if (symbol->name() == QLatin1String("font")) - fontPropFound = true; - } - QVERIFY(xPropFound); - QVERIFY(fontPropFound); - - Symbol::List buildInRectMembers = buildInRect->members(false); - QVERIFY(!buildInRectMembers.isEmpty()); - - QSKIP("Getting properties _without_ the inerited properties doesn't work.", SkipSingle); - fontPropFound = false; - foreach (Symbol *symbol, buildInRectMembers) { - if (symbol->name() == QLatin1String("x")) - QFAIL("Text has x property"); - else if (symbol->name() == QLatin1String("font")) - fontPropFound = true; - } - QVERIFY(fontPropFound); -} - -void tst_Lookup::localIdLookup() -{ - QFile input(":/data/localIdLookup.qml"); - QVERIFY(input.open(QIODevice::ReadOnly)); - - Document::Ptr doc = basicSymbolTest(input.readAll()); - QVERIFY(doc->isParsedCorrectly()); - - QStringList symbolNames; - symbolNames.append("x"); - symbolNames.append("y"); - symbolNames.append("z"); - symbolNames.append("opacity"); - symbolNames.append("visible"); - - // check symbol existence - foreach (const QString &symbolName, symbolNames) { - QVERIFY(doc->ids()[symbolName]); - } - - // try lookup - QStack scopes; - foreach (const QString &contextSymbolName, symbolNames) { - scopes.push_back(doc->ids()[contextSymbolName]->parentNode()); - QmlLookupContext context(scopes, doc, snapshot(doc), typeSystem()); - - foreach (const QString &lookupSymbolName, symbolNames) { - Symbol *resolvedSymbol = context.resolve(lookupSymbolName); - IdSymbol *targetSymbol = doc->ids()[lookupSymbolName]; - QCOMPARE(resolvedSymbol, targetSymbol); - - IdSymbol *resolvedId = resolvedSymbol->asIdSymbol(); - QVERIFY(resolvedId); - QCOMPARE(resolvedId->parentNode(), targetSymbol->parentNode()); - } - } -} - -void tst_Lookup::localScriptMethodLookup() -{ - QFile input(":/data/localScriptMethodLookup.qml"); - QVERIFY(input.open(QIODevice::ReadOnly)); - - Document::Ptr doc = basicSymbolTest(input.readAll()); - QVERIFY(doc->isParsedCorrectly()); - - QStringList symbolNames; - symbolNames.append("theRoot"); - symbolNames.append("theParent"); - symbolNames.append("theChild"); - - QStringList functionNames; - functionNames.append("x"); - functionNames.append("y"); - functionNames.append("z"); - - // check symbol existence - foreach (const QString &symbolName, symbolNames) { - QVERIFY(doc->ids()[symbolName]); - } - - // try lookup - QStack scopes; - foreach (const QString &contextSymbolName, symbolNames) { - scopes.push_back(doc->ids()[contextSymbolName]->parentNode()); - QmlLookupContext context(scopes, doc, snapshot(doc), typeSystem()); - - foreach (const QString &functionName, functionNames) { - Symbol *symbol = context.resolve(functionName); - QVERIFY(symbol); - QVERIFY(!symbol->isProperty()); - // verify that it's a function - } - } -} - -void tst_Lookup::localScopeLookup() -{ - QFile input(":/data/localScopeLookup.qml"); - QVERIFY(input.open(QIODevice::ReadOnly)); - - Document::Ptr doc = basicSymbolTest(input.readAll()); - QVERIFY(doc->isParsedCorrectly()); - - QStringList symbolNames; - symbolNames.append("theRoot"); - symbolNames.append("theParent"); - symbolNames.append("theChild"); - - // check symbol existence - foreach (const QString &symbolName, symbolNames) { - QVERIFY(doc->ids()[symbolName]); - } - - // try lookup - QStack scopes; - foreach (const QString &contextSymbolName, symbolNames) { - Symbol *parent = doc->ids()[contextSymbolName]->parentNode(); - scopes.push_back(parent); - QmlLookupContext context(scopes, doc, snapshot(doc), typeSystem()); - - Symbol *symbol; - symbol = context.resolve("prop"); - QVERIFY(symbol); - QVERIFY(symbol->isPropertyDefinitionSymbol()); - QVERIFY(parent->members().contains(symbol)); - - symbol = context.resolve("x"); - QVERIFY(symbol); - QVERIFY(symbol->isProperty()); - // how do we know we got the right x? - } -} - -void tst_Lookup::localRootLookup() -{ - QFile input(":/data/localRootLookup.qml"); - QVERIFY(input.open(QIODevice::ReadOnly)); - - Document::Ptr doc = basicSymbolTest(input.readAll()); - QVERIFY(doc->isParsedCorrectly()); - - QStringList symbolNames; - symbolNames.append("theRoot"); - symbolNames.append("theParent"); - symbolNames.append("theChild"); - - // check symbol existence and build scopes - QStack scopes; - foreach (const QString &symbolName, symbolNames) { - IdSymbol *id = doc->ids()[symbolName]; - QVERIFY(id); - scopes.push_back(id->parentNode()); - } - - // try lookup - Symbol *parent = scopes.front(); - QmlLookupContext context(scopes, doc, snapshot(doc), typeSystem()); - - Symbol *symbol; - symbol = context.resolve("prop"); - QVERIFY(symbol); - QVERIFY(symbol->isPropertyDefinitionSymbol()); - QVERIFY(parent->members().contains(symbol)); - - symbol = context.resolve("color"); - QVERIFY(symbol); - QVERIFY(symbol->isProperty()); -} - -QTEST_APPLESS_MAIN(tst_Lookup) -#include "tst_lookup.moc" diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/qmlcodeformatter.pro b/tests/auto/qml/qmleditor/qmlcodeformatter/qmlcodeformatter.pro new file mode 100644 index 0000000000..6464c16bb4 --- /dev/null +++ b/tests/auto/qml/qmleditor/qmlcodeformatter/qmlcodeformatter.pro @@ -0,0 +1,22 @@ +TEMPLATE = app +CONFIG += qt warn_on console depend_includepath qtestlib testcase +QT += network + +SRCDIR = ../../../../../src + +#DEFINES += QML_BUILD_STATIC_LIB +#include($$SRCDIR/../qtcreator.pri) +include($$SRCDIR/libs/qmljs/qmljs-lib.pri) +include($$SRCDIR/libs/utils/utils-lib.pri) +#LIBS += -L$$IDE_LIBRARY_PATH + +SOURCES += \ + tst_qmlcodeformatter.cpp \ + $$SRCDIR/plugins/qmljseditor/qmljseditorcodeformatter.cpp \ + $$SRCDIR/plugins/texteditor/basetextdocumentlayout.cpp + +HEADERS += \ + $$SRCDIR/plugins/qmljseditor/qmljseditorcodeformatter.h \ + $$SRCDIR/plugins/texteditor/basetextdocumentlayout.h \ + +INCLUDEPATH += $$SRCDIR/plugins $$SRCDIR/libs diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp new file mode 100644 index 0000000000..4f5d8464ba --- /dev/null +++ b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp @@ -0,0 +1,888 @@ +#include +#include +#include +#include +#include + +#include + +using namespace QmlJSEditor; + +class tst_QMLCodeFormatter: public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void objectDefinitions1(); + void objectDefinitions2(); + void expressionEndSimple(); + void expressionEnd(); + void expressionEndBracket(); + void expressionEndParen(); + void objectBinding(); + void arrayBinding(); + void functionDeclaration(); + void functionExpression(); + void propertyDeclarations(); + void signalDeclarations(); + void ifBinding1(); + void ifBinding2(); + void ifStatementWithoutBraces1(); + void ifStatementWithoutBraces2(); + void ifStatementWithBraces1(); + void ifStatementWithBraces2(); + void ifStatementMixed(); + void ifStatementAndComments(); + void ifStatementLongCondition(); + void moreIfThenElse(); + void strayElse(); + void oneLineIf(); + void forStatement(); + void whileStatement(); + void tryStatement(); + void doWhile(); + void cStyleComments(); + void cppStyleComments(); + void qmlKeywords(); + void ternary(); + void switch1(); +// void gnuStyle(); +// void whitesmithsStyle(); + void expressionContinuation(); +}; + +struct Line { + Line(QString l) + : line(l) + { + for (int i = 0; i < l.size(); ++i) { + if (!l.at(i).isSpace()) { + expectedIndent = i; + return; + } + } + expectedIndent = l.size(); + } + + Line(QString l, int expect) + : line(l), expectedIndent(expect) + {} + + QString line; + int expectedIndent; +}; + +QString concatLines(QList lines) +{ + QString result; + foreach (const Line &l, lines) { + result += l.line; + result += "\n"; + } + return result; +} + +void checkIndent(QList data, int style = 0) +{ + Q_UNUSED(style) + + QString text = concatLines(data); + QTextDocument document(text); + QtStyleCodeFormatter formatter; + + int i = 0; + foreach (const Line &l, data) { + QTextBlock b = document.findBlockByLineNumber(i); + if (l.expectedIndent != -1) { + int actualIndent = formatter.indentFor(b); + if (actualIndent != l.expectedIndent) { + QFAIL(QString("Wrong indent in line %1 with text '%2', expected indent %3, got %4").arg( + QString::number(i+1), l.line, QString::number(l.expectedIndent), QString::number(actualIndent)).toLatin1().constData()); + } + } + formatter.updateLineStateChange(b); + ++i; + } +} + +void tst_QMLCodeFormatter::objectDefinitions1() +{ + QList data; + data << Line("import Qt 4.7") + << Line("") + << Line("Rectangle {") + << Line(" foo: bar;") + << Line(" Item {") + << Line(" x: 42;") + << Line(" y: x;") + << Line(" }") + << Line(" Component.onCompleted: foo;") + << Line(" ") + << Line(" Foo.Bar {") + << Line(" width: 12 + 54;") + << Line(" anchors.fill: parent;") + << Line(" }") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::objectDefinitions2() +{ + QList data; + data << Line("import Qt 4.7") + << Line("") + << Line("Rectangle {") + << Line(" foo: bar;") + << Line(" Image { source: \"a+b+c\"; x: 42; y: 12 }") + << Line(" Component.onCompleted: foo;") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::expressionEndSimple() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar +") + << Line(" foo(4, 5) +") + << Line(" 7") + << Line(" x: 42") + << Line(" y: 43") + << Line(" width: 10") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::expressionEnd() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar +") + << Line(" foo(4") + << Line(" + 5)") + << Line(" + 7") + << Line(" x: 42") + << Line(" + 43") + << Line(" + 10") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::expressionEndParen() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar") + << Line(" (foo(4") + << Line(" + 5)") + << Line(" + 7,") + << Line(" abc)") + << Line(" x: a + b(fpp, ba + 12) + foo(") + << Line(" bar,") + << Line(" 10)") + << Line(" + 10") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::expressionEndBracket() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar") + << Line(" [foo[4") + << Line(" + 5]") + << Line(" + 7,") + << Line(" abc]") + << Line(" x: a + b[fpp, ba + 12] + foo[") + << Line(" bar,") + << Line(" 10]") + << Line(" + 10") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::objectBinding() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar") + << Line(" x: 3") + << Line(" foo: Gradient {") + << Line(" x: 12") + << Line(" y: x") + << Line(" }") + << Line(" Item {") + << Line(" states: State {}") + << Line(" }") + << Line(" x: 1") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::arrayBinding() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar") + << Line(" x: 3") + << Line(" foo: [") + << Line(" State {") + << Line(" y: x") + << Line(" },") + << Line(" State") + << Line(" {") + << Line(" }") + << Line(" ]") + << Line(" foo: [") + << Line(" 1 +") + << Line(" 2") + << Line(" + 345 * foo(") + << Line(" bar, car,") + << Line(" dar),") + << Line(" x, y,") + << Line(" z,") + << Line(" ]") + << Line(" x: 1") + << Line("}") + ; + checkIndent(data); +} + + + +void tst_QMLCodeFormatter::moreIfThenElse() +{ + QList data; + data << Line("Image {") + << Line(" source: {") + << Line(" if(type == 1) {") + << Line(" \"pics/blueStone.png\";") + << Line(" } else if (type == 2) {") + << Line(" \"pics/head.png\";") + << Line(" } else {") + << Line(" \"pics/redStone.png\";") + << Line(" }") + << Line(" }") + << Line("}"); + checkIndent(data); +} + + +void tst_QMLCodeFormatter::functionDeclaration() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar") + << Line(" function foo(a, b, c) {") + << Line(" if (a)") + << Line(" b;") + << Line(" }") + << Line(" property alias boo :") + << Line(" foo") + << Line(" Item {") + << Line(" property variant g : Gradient {") + << Line(" v: 12") + << Line(" }") + << Line(" function bar(") + << Line(" a, b,") + << Line(" c)") + << Line(" {") + << Line(" var b") + << Line(" }") + << Line(" function bar(a,") + << Line(" a, b,") + << Line(" c)") + << Line(" {") + << Line(" var b") + << Line(" }") + << Line(" }") + << Line(" x: 1") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::functionExpression() +{ + QList data; + data << Line("Rectangle {") + << Line("onFoo: {", 4) + << Line(" function foo(a, b, c) {") + << Line(" if (a)") + << Line(" b;") + << Line(" }") + << Line(" return function(a, b) { return a + b; }") + << Line(" return function foo(a, b) {") + << Line(" return a") + << Line(" }") + << Line("}") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::propertyDeclarations() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar") + << Line(" property int foo : 2 +") + << Line(" x") + << Line(" property list bar") + << Line(" property alias boo :") + << Line(" foo") + << Line(" Item {") + << Line(" property variant g : Gradient {") + << Line(" v: 12") + << Line(" }") + << Line(" default property Item g") + << Line(" default property Item g : parent.foo") + << Line(" }") + << Line(" x: 1") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::signalDeclarations() +{ + QList data; + data << Line("Rectangle {") + << Line(" foo: bar") + << Line(" signal foo") + << Line(" x: bar") + << Line(" signal bar(a, int b)") + << Line(" signal bar2()") + << Line(" Item {") + << Line(" signal property") + << Line(" signal import(a, b);") + << Line(" }") + << Line(" x: 1") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifBinding1() +{ + QList data; + data << Line("A.Rectangle {") + << Line(" foo: bar") + << Line(" x: if (a) b") + << Line(" x: if (a)") + << Line(" b") + << Line(" x: if (a) b;") + << Line(" x: if (a)") + << Line(" b;") + << Line(" x: if (a) b; else c") + << Line(" x: if (a) b") + << Line(" else c") + << Line(" x: if (a) b;") + << Line(" else c") + << Line(" x: if (a) b;") + << Line(" else") + << Line(" c") + << Line(" x: if (a)") + << Line(" b") + << Line(" else") + << Line(" c") + << Line(" x: if (a) b; else c;") + << Line(" x: 1") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifBinding2() +{ + QList data; + data << Line("A.Rectangle {") + << Line(" foo: bar") + << Line(" x: if (a) b +") + << Line(" 5 +") + << Line(" 5 * foo(") + << Line(" 1, 2)") + << Line(" else a =") + << Line(" foo(15,") + << Line(" bar(") + << Line(" 1),") + << Line(" bar)") + << Line(" x: if (a) b") + << Line(" + 5") + << Line(" + 5") + << Line(" x: if (a)") + << Line(" b") + << Line(" + 5") + << Line(" + 5") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifStatementWithoutBraces1() +{ + QList data; + data << Line("Rectangle {") + << Line(" x: if (a)") + << Line(" if (b)") + << Line(" foo") + << Line(" else if (c)") + << Line(" foo") + << Line(" else") + << Line(" if (d)") + << Line(" foo;") + << Line(" else") + << Line(" a + b + ") + << Line(" c") + << Line(" else") + << Line(" foo;") + << Line(" y: 2") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifStatementWithoutBraces2() +{ + QList data; + data << Line("Rectangle {") + << Line(" x: {") + << Line(" if (a)") + << Line(" if (b)") + << Line(" foo;") + << Line(" if (a) b();") + << Line(" if (a) b(); else") + << Line(" foo;") + << Line(" if (a)") + << Line(" if (b)") + << Line(" foo;") + << Line(" else if (c)") + << Line(" foo;") + << Line(" else") + << Line(" if (d)") + << Line(" foo;") + << Line(" else") + << Line(" e") + << Line(" else") + << Line(" foo;") + << Line(" }") + << Line(" foo: bar") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifStatementWithBraces1() +{ + QList data; + data << Line("Rectangle {") + << Line("onClicked: {", 4) + << Line(" if (a) {") + << Line(" if (b) {") + << Line(" foo;") + << Line(" } else if (c) {") + << Line(" foo") + << Line(" } else {") + << Line(" if (d) {") + << Line(" foo") + << Line(" } else {") + << Line(" foo;") + << Line(" }") + << Line(" }") + << Line(" } else {") + << Line(" foo;") + << Line(" }") + << Line("}") + << Line("}"); + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifStatementWithBraces2() +{ + QList data; + data << Line("Rectangle {") + << Line("onClicked:", 4) + << Line(" if (a)") + << Line(" {") + << Line(" if (b)") + << Line(" {") + << Line(" foo") + << Line(" }") + << Line(" else if (c)") + << Line(" {") + << Line(" foo;") + << Line(" }") + << Line(" else") + << Line(" {") + << Line(" if (d)") + << Line(" {") + << Line(" foo;") + << Line(" }") + << Line(" else") + << Line(" {") + << Line(" foo") + << Line(" }") + << Line(" }") + << Line(" }") + << Line(" else") + << Line(" {") + << Line(" foo") + << Line(" }") + << Line("}"); + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifStatementMixed() +{ + QList data; + data << Line("Rectangle {") + << Line("onClicked:", 4) + << Line(" if (foo)") + << Line(" if (bar)") + << Line(" {") + << Line(" foo;") + << Line(" }") + << Line(" else") + << Line(" if (car)") + << Line(" {}") + << Line(" else doo") + << Line(" else abc") + << Line("}"); + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifStatementAndComments() +{ + QList data; + data << Line("Rectangle {") + << Line("onClicked: {", 4) + << Line(" if (foo)") + << Line(" ; // bla") + << Line(" else if (bar)") + << Line(" ;") + << Line(" if (foo)") + << Line(" ; /*bla") + << Line(" bla */") + << Line(" else if (bar)") + << Line(" // foobar") + << Line(" ;") + << Line(" else if (bar)") + << Line(" /* bla") + << Line(" bla */") + << Line(" ;") + << Line("}") + << Line("}"); + checkIndent(data); +} + +void tst_QMLCodeFormatter::ifStatementLongCondition() +{ + QList data; + data << Line("Rectangle {") + << Line("onClicked: {", 4) + << Line(" if (foo &&") + << Line(" bar") + << Line(" || (a + b > 4") + << Line(" && foo(bar)") + << Line(" )") + << Line(" ) {") + << Line(" foo;") + << Line(" }") + << Line("}"); + checkIndent(data); +} + +void tst_QMLCodeFormatter::strayElse() +{ + QList data; + data << Line("Rectangle {") + << Line("onClicked: {", 4) + << Line(" while( true ) {}") + << Line(" else", -1) + << Line(" else {", -1) + << Line(" }", -1) + << Line("}"); + checkIndent(data); +} + +void tst_QMLCodeFormatter::oneLineIf() +{ + QList data; + data << Line("Rectangle {") + << Line(" onClicked: { if (showIt) show(); }") + << Line(" x: 2") + << Line("};") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::forStatement() +{ + QList data; + data << Line("for (var i = 0; i < 20; ++i) {") + << Line(" print(i);") + << Line("}") + << Line("for (var x in [a, b, c, d])") + << Line(" x += 5") + << Line("var z") + << Line("for (var x in [a, b, c, d])") + << Line(" for (;;)") + << Line(" {") + << Line(" for (a(); b(); c())") + << Line(" for (a();") + << Line(" b(); c())") + << Line(" for (a(); b(); c())") + << Line(" print(3*d)") + << Line(" }") + << Line("z = 2") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::whileStatement() +{ + QList data; + data << Line("while (i < 20) {") + << Line(" print(i);") + << Line("}") + << Line("while (x in [a, b, c, d])") + << Line(" x += 5") + << Line("var z") + << Line("while (a + b > 0") + << Line(" && b + c > 0)") + << Line(" for (;;)") + << Line(" {") + << Line(" for (a(); b(); c())") + << Line(" while (a())") + << Line(" for (a(); b(); c())") + << Line(" print(3*d)") + << Line(" }") + << Line("z = 2") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::tryStatement() +{ + QList data; + data << Line("try {") + << Line(" print(i);") + << Line("} catch (foo) {") + << Line(" print(foo)") + << Line("} finally {") + << Line(" var z") + << Line(" while (a + b > 0") + << Line(" && b + c > 0)") + << Line(" ;") + << Line("}") + << Line("z = 2") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::doWhile() +{ + QList data; + data << Line("function foo() {") + << Line(" do { if (c) foo; } while(a);") + << Line(" do {") + << Line(" if(a);") + << Line(" } while(a);") + << Line(" do") + << Line(" foo;") + << Line(" while(a);") + << Line(" do foo; while(a);") + << Line("};") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::cStyleComments() +{ + QList data; + data << Line("/*") + << Line(" ") + << Line(" foo") + << Line(" ") + << Line(" foo") + << Line(" ") + << Line("*/") + << Line("Rectangle {") + << Line(" /*") + << Line(" ") + << Line(" foo") + << Line(" ") + << Line(" */") + << Line(" /* bar */") + << Line("}") + << Line("Item {") + << Line(" /* foo */") + << Line(" /*") + << Line(" ") + << Line(" foo") + << Line(" ") + << Line(" */") + << Line(" /* bar */") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::cppStyleComments() +{ + QList data; + data << Line("// abc") + << Line("Item { ") + << Line(" // ghij") + << Line(" // def") + << Line(" // ghij") + << Line(" x: 4 // hik") + << Line(" // doo") + << Line("} // ba") + << Line("// ba") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::ternary() +{ + QList data; + data << Line("function foo() {") + << Line(" var i = a ? b : c;") + << Line(" foo += a_bigger_condition ?") + << Line(" b") + << Line(" : c;") + << Line(" foo += a_bigger_condition") + << Line(" ? b") + << Line(" : c;") + << Line(" var i = a ?") + << Line(" b : c;") + << Line(" var i = aooo ? b") + << Line(" : c +") + << Line(" 2;") + << Line(" var i = (a ? b : c) + (foo") + << Line(" bar);") + << Line("}") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::switch1() +{ + QList data; + data << Line("function foo() {") + << Line(" switch (a) {") + << Line(" case 1:") + << Line(" foo;") + << Line(" if (a);") + << Line(" case 2:") + << Line(" case 3: {") + << Line(" foo") + << Line(" }") + << Line(" case 4:") + << Line(" {") + << Line(" foo;") + << Line(" }") + << Line(" case bar:") + << Line(" break") + << Line(" }") + << Line(" case 4:") + << Line(" {") + << Line(" if (a) {") + << Line(" }") + << Line(" }") + << Line("}") + ; + checkIndent(data); +} + +//void tst_QMLCodeFormatter::gnuStyle() +//{ +// QList data; +// data << Line("struct S") +// << Line("{") +// << Line(" void foo()") +// << Line(" {") +// << Line(" if (a)") +// << Line(" {") +// << Line(" fpp;") +// << Line(" }") +// << Line(" else if (b)") +// << Line(" {") +// << Line(" fpp;") +// << Line(" }") +// << Line(" else") +// << Line(" {") +// << Line(" }") +// << Line(" if (b) {") +// << Line(" fpp;") +// << Line(" }") +// << Line(" }") +// << Line("};") +// ; +// checkIndent(data, 1); +//} + +//void tst_QMLCodeFormatter::whitesmithsStyle() +//{ +// QList data; +// data << Line("struct S") +// << Line(" {") +// << Line(" void foo()") +// << Line(" {") +// << Line(" if (a)") +// << Line(" {") +// << Line(" fpp;") +// << Line(" }") +// << Line(" if (b) {") +// << Line(" fpp;") +// << Line(" }") +// << Line(" }") +// << Line(" };") +// ; +// checkIndent(data, 2); +//} + +void tst_QMLCodeFormatter::qmlKeywords() +{ + QList data; + data << Line("Rectangle {") + << Line(" on: 2") + << Line(" property: 2") + << Line(" signal: 2") + << Line(" list: 2") + << Line(" as: 2") + << Line(" import: 2") + << Line(" Item {") + << Line(" }") + << Line(" x: 2") + << Line("};") + ; + checkIndent(data); +} + +void tst_QMLCodeFormatter::expressionContinuation() +{ + QList data; + data << Line("var x = 1 ? 2") + << Line(" + 3 : 4") + << Line("++x") + << Line("++y--") + << Line("x +=") + << Line(" y++") + << Line("var z") + ; + checkIndent(data); +} + +QTEST_APPLESS_MAIN(tst_CodeFormatter) +#include "tst_qmlcodeformatter.moc" + + diff --git a/tests/auto/qml/qmleditor/qmleditor.pro b/tests/auto/qml/qmleditor/qmleditor.pro index 7c5fddb9a7..bd9d6f37a3 100644 --- a/tests/auto/qml/qmleditor/qmleditor.pro +++ b/tests/auto/qml/qmleditor/qmleditor.pro @@ -1,3 +1,2 @@ TEMPLATE = subdirs - -SUBDIRS += lookup codeformatter +SUBDIRS += qmllookup qmlcodeformatter diff --git a/tests/auto/qml/qmleditor/qmllookup/data/localIdLookup.qml b/tests/auto/qml/qmleditor/qmllookup/data/localIdLookup.qml new file mode 100644 index 0000000000..f3df6cd5e3 --- /dev/null +++ b/tests/auto/qml/qmleditor/qmllookup/data/localIdLookup.qml @@ -0,0 +1,47 @@ +import Qt 4.7 + +// Try to look up x, y, z, opacity and visible +// in all symbol contexts. It should always get the right item. + +Item { + id: x + Script { + function x() {} + function y() {} + function z() {} + function opacity() {} + function visible() {} + } + property variant x: "wrong"; + property variant y: "wrong"; + property variant z: "wrong"; + property variant opacity: "wrong"; + property variant visible: "wrong"; + Item { + id: y + property variant x: "wrong"; + property variant y: "wrong"; + property variant z: "wrong"; + property variant opacity: "wrong"; + property variant visible: "wrong"; + Item { + id: z + property variant x: "wrong"; + property variant y: "wrong"; + property variant z: "wrong"; + property variant opacity: "wrong"; + property variant visible: "wrong"; + Item { + id: opacity + property variant x: "wrong"; + property variant y: "wrong"; + property variant z: "wrong"; + property variant opacity: "wrong"; + property variant visible: "wrong"; + Item { + id: visible + } + } + } + } +} \ No newline at end of file diff --git a/tests/auto/qml/qmleditor/qmllookup/data/localRootLookup.qml b/tests/auto/qml/qmleditor/qmllookup/data/localRootLookup.qml new file mode 100644 index 0000000000..74562a03e6 --- /dev/null +++ b/tests/auto/qml/qmleditor/qmllookup/data/localRootLookup.qml @@ -0,0 +1,16 @@ +import Qt 4.7 + +// Try to look up prop in child. +// It should get the root's prop. + +Rectangle { + id: theRoot + property variant prop + Item { + id: theParent + property variant prop + Item { + id: theChild + } + } +} diff --git a/tests/auto/qml/qmleditor/qmllookup/data/localScopeLookup.qml b/tests/auto/qml/qmleditor/qmllookup/data/localScopeLookup.qml new file mode 100644 index 0000000000..e103232e4b --- /dev/null +++ b/tests/auto/qml/qmleditor/qmllookup/data/localScopeLookup.qml @@ -0,0 +1,17 @@ +import Qt 4.7 + +// Try to look up prop in all symbol contexts. +// It should always get the local one. + +Item { + id: theRoot + property variant prop + Item { + id: theParent + property variant prop + Item { + id: theChild + property variant prop + } + } +} diff --git a/tests/auto/qml/qmleditor/qmllookup/data/localScriptMethodLookup.qml b/tests/auto/qml/qmleditor/qmllookup/data/localScriptMethodLookup.qml new file mode 100644 index 0000000000..9d3f39bef4 --- /dev/null +++ b/tests/auto/qml/qmleditor/qmllookup/data/localScriptMethodLookup.qml @@ -0,0 +1,32 @@ +import Qt 4.7 + +// Try to look up x, y, z +// in all symbol contexts. It should always get the function. + +Item { + id: theRoot + Script { + function x() {} + } + property variant x: "wrong"; + property variant y: "wrong"; + property variant z: "wrong"; + Item { + id: theParent + Script { + function y() {} + } + property variant x: "wrong"; + property variant y: "wrong"; + property variant z: "wrong"; + Item { + id: theChild + Script { + function z() {} + } + property variant x: "wrong"; + property variant y: "wrong"; + property variant z: "wrong"; + } + } +} \ No newline at end of file diff --git a/tests/auto/qml/qmleditor/qmllookup/qmllookup.pro b/tests/auto/qml/qmleditor/qmllookup/qmllookup.pro new file mode 100644 index 0000000000..6a6210ec36 --- /dev/null +++ b/tests/auto/qml/qmleditor/qmllookup/qmllookup.pro @@ -0,0 +1,17 @@ +TEMPLATE = app +CONFIG += qt warn_on console depend_includepath qtestlib testcase +include(../../../../../src/libs/qmljs/qmljs-lib.pri) +DEFINES += QML_BUILD_STATIC_LIB +EDITOR_DIR=../../../../../src/plugins/qmljseditor + +INCLUDEPATH += $$EDITOR_DIR + +SOURCES += tst_qmllookup.cpp + +RESOURCES += testfiles.qrc + +OTHER_FILES += \ + data/localIdLookup.qml \ + data/localScriptMethodLookup.qml \ + data/localScopeLookup.qml \ + data/localRootLookup.qml diff --git a/tests/auto/qml/qmleditor/qmllookup/testfiles.qrc b/tests/auto/qml/qmleditor/qmllookup/testfiles.qrc new file mode 100644 index 0000000000..b780e4149e --- /dev/null +++ b/tests/auto/qml/qmleditor/qmllookup/testfiles.qrc @@ -0,0 +1,8 @@ + + + data/localIdLookup.qml + data/localScriptMethodLookup.qml + data/localScopeLookup.qml + data/localRootLookup.qml + + diff --git a/tests/auto/qml/qmleditor/qmllookup/tst_qmllookup.cpp b/tests/auto/qml/qmleditor/qmllookup/tst_qmllookup.cpp new file mode 100644 index 0000000000..198e1e00b3 --- /dev/null +++ b/tests/auto/qml/qmleditor/qmllookup/tst_qmllookup.cpp @@ -0,0 +1,324 @@ +#include +#include +#include +#include + +#include +#include + +#include + +#include + +using namespace QmlJSEditor; +using namespace QmlJSEditor::Internal; +using namespace QmlJS; +using namespace QmlJS::AST; + +class tst_QMLLookup: public QObject +{ + Q_OBJECT + +public: + tst_QMLLookup(): _typeSystem(0) {} + ~tst_QMLLookup() { if (_typeSystem) resetTypeSystem(); } + + void resetTypeSystem() { if (_typeSystem) { delete _typeSystem; _typeSystem = 0; }} + +private Q_SLOTS: + void basicSymbolTest(); + void basicLookupTest(); + void localIdLookup(); + void localScriptMethodLookup(); + void localScopeLookup(); + void localRootLookup(); + +protected: + Document::Ptr basicSymbolTest(const QString &input) const + { + const QLatin1String filename(""); + Document::Ptr doc = Document::create(filename); + doc->setSource(input); + doc->parseQml(); + + QList msgs = doc->diagnosticMessages(); + foreach (const DiagnosticMessage &msg, msgs) { + if (msg.isError()) { + qDebug() << "Error:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; + } else if (msg.isWarning()) { + qDebug() << "Warning:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; + } else { + qDebug() << "Diagnostic:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; + } + } + + return doc; + } + + Snapshot snapshot(const Document::Ptr &doc) const + { + Snapshot snapshot; + snapshot.insert(doc); + return snapshot; + } + + TypeSystem *typeSystem() { + if (!_typeSystem) + _typeSystem = new TypeSystem; + return _typeSystem; + } + +private: + TypeSystem *_typeSystem; +}; + +void tst_QMLLookup::basicSymbolTest() +{ + const QLatin1String input( + "import Qt 4.6\n" + "\n" + "Rectangle {\n" + " x: 10\n" + " y: 10\n" + " width: 10\n" + " height: 10\n" + "}\n" + ); + + Document::Ptr doc = basicSymbolTest(input); + QVERIFY(doc->isParsedCorrectly()); + + UiProgram *program = doc->qmlProgram(); + QVERIFY(program); + QVERIFY(program->members); + QVERIFY(program->members->member); + UiObjectDefinition *rectDef = cast(program->members->member); + QVERIFY(rectDef); + QVERIFY(rectDef->qualifiedTypeNameId->name); + QCOMPARE(rectDef->qualifiedTypeNameId->name->asString(), QLatin1String("Rectangle")); + QVERIFY(rectDef->initializer); + UiObjectMemberList *rectMembers = rectDef->initializer->members; + QVERIFY(rectMembers); + QVERIFY(rectMembers->member); + + UiScriptBinding *xBinding = cast(rectMembers->member); + QVERIFY(xBinding); + QVERIFY(xBinding->qualifiedId); + QVERIFY(xBinding->qualifiedId->name); + QCOMPARE(xBinding->qualifiedId->name->asString(), QLatin1String("x")); + + Symbol::List docSymbols = doc->symbols(); + QCOMPARE(docSymbols.size(), 1); + + Symbol *rectSymbol = docSymbols.at(0); + QCOMPARE(rectSymbol->name(), QLatin1String("Rectangle")); + QCOMPARE(rectSymbol->members().size(), 4); + + SymbolFromFile *rectFromFile = rectSymbol->asSymbolFromFile(); + QVERIFY(rectFromFile); + SymbolFromFile *xSymbol = rectFromFile->findMember(xBinding); + QVERIFY(xSymbol); + QCOMPARE(xSymbol->name(), QLatin1String("x")); +} + +void tst_QMLLookup::basicLookupTest() +{ + const QLatin1String input( + "import Qt 4.6\n" + "Item{}\n" + ); + + Document::Ptr doc = basicSymbolTest(input); + QVERIFY(doc->isParsedCorrectly()); + + UiProgram *program = doc->qmlProgram(); + QVERIFY(program); + + QStack emptyScope; + QmlLookupContext context(emptyScope, doc, snapshot(doc), typeSystem()); + Symbol *rectSymbol = context.resolveType(QLatin1String("Text")); + QVERIFY(rectSymbol); + + PrimitiveSymbol *buildInRect = rectSymbol->asPrimitiveSymbol(); + QVERIFY(buildInRect); + QCOMPARE(buildInRect->name(), QLatin1String("Text")); + + Symbol::List allBuildInRectMembers = buildInRect->members(true); + QVERIFY(!allBuildInRectMembers.isEmpty()); + bool xPropFound = false; + bool fontPropFound = false; + foreach (Symbol *symbol, allBuildInRectMembers) { + if (symbol->name() == QLatin1String("x")) + xPropFound = true; + else if (symbol->name() == QLatin1String("font")) + fontPropFound = true; + } + QVERIFY(xPropFound); + QVERIFY(fontPropFound); + + Symbol::List buildInRectMembers = buildInRect->members(false); + QVERIFY(!buildInRectMembers.isEmpty()); + + QSKIP("Getting properties _without_ the inerited properties doesn't work.", SkipSingle); + fontPropFound = false; + foreach (Symbol *symbol, buildInRectMembers) { + if (symbol->name() == QLatin1String("x")) + QFAIL("Text has x property"); + else if (symbol->name() == QLatin1String("font")) + fontPropFound = true; + } + QVERIFY(fontPropFound); +} + +void tst_QMLLookup::localIdLookup() +{ + QFile input(":/data/localIdLookup.qml"); + QVERIFY(input.open(QIODevice::ReadOnly)); + + Document::Ptr doc = basicSymbolTest(input.readAll()); + QVERIFY(doc->isParsedCorrectly()); + + QStringList symbolNames; + symbolNames.append("x"); + symbolNames.append("y"); + symbolNames.append("z"); + symbolNames.append("opacity"); + symbolNames.append("visible"); + + // check symbol existence + foreach (const QString &symbolName, symbolNames) { + QVERIFY(doc->ids()[symbolName]); + } + + // try lookup + QStack scopes; + foreach (const QString &contextSymbolName, symbolNames) { + scopes.push_back(doc->ids()[contextSymbolName]->parentNode()); + QmlLookupContext context(scopes, doc, snapshot(doc), typeSystem()); + + foreach (const QString &lookupSymbolName, symbolNames) { + Symbol *resolvedSymbol = context.resolve(lookupSymbolName); + IdSymbol *targetSymbol = doc->ids()[lookupSymbolName]; + QCOMPARE(resolvedSymbol, targetSymbol); + + IdSymbol *resolvedId = resolvedSymbol->asIdSymbol(); + QVERIFY(resolvedId); + QCOMPARE(resolvedId->parentNode(), targetSymbol->parentNode()); + } + } +} + +void tst_QMLLookup::localScriptMethodLookup() +{ + QFile input(":/data/localScriptMethodLookup.qml"); + QVERIFY(input.open(QIODevice::ReadOnly)); + + Document::Ptr doc = basicSymbolTest(input.readAll()); + QVERIFY(doc->isParsedCorrectly()); + + QStringList symbolNames; + symbolNames.append("theRoot"); + symbolNames.append("theParent"); + symbolNames.append("theChild"); + + QStringList functionNames; + functionNames.append("x"); + functionNames.append("y"); + functionNames.append("z"); + + // check symbol existence + foreach (const QString &symbolName, symbolNames) { + QVERIFY(doc->ids()[symbolName]); + } + + // try lookup + QStack scopes; + foreach (const QString &contextSymbolName, symbolNames) { + scopes.push_back(doc->ids()[contextSymbolName]->parentNode()); + QmlLookupContext context(scopes, doc, snapshot(doc), typeSystem()); + + foreach (const QString &functionName, functionNames) { + Symbol *symbol = context.resolve(functionName); + QVERIFY(symbol); + QVERIFY(!symbol->isProperty()); + // verify that it's a function + } + } +} + +void tst_QMLLookup::localScopeLookup() +{ + QFile input(":/data/localScopeLookup.qml"); + QVERIFY(input.open(QIODevice::ReadOnly)); + + Document::Ptr doc = basicSymbolTest(input.readAll()); + QVERIFY(doc->isParsedCorrectly()); + + QStringList symbolNames; + symbolNames.append("theRoot"); + symbolNames.append("theParent"); + symbolNames.append("theChild"); + + // check symbol existence + foreach (const QString &symbolName, symbolNames) { + QVERIFY(doc->ids()[symbolName]); + } + + // try lookup + QStack scopes; + foreach (const QString &contextSymbolName, symbolNames) { + Symbol *parent = doc->ids()[contextSymbolName]->parentNode(); + scopes.push_back(parent); + QmlLookupContext context(scopes, doc, snapshot(doc), typeSystem()); + + Symbol *symbol; + symbol = context.resolve("prop"); + QVERIFY(symbol); + QVERIFY(symbol->isPropertyDefinitionSymbol()); + QVERIFY(parent->members().contains(symbol)); + + symbol = context.resolve("x"); + QVERIFY(symbol); + QVERIFY(symbol->isProperty()); + // how do we know we got the right x? + } +} + +void tst_QMLLookup::localRootLookup() +{ + QFile input(":/data/localRootLookup.qml"); + QVERIFY(input.open(QIODevice::ReadOnly)); + + Document::Ptr doc = basicSymbolTest(input.readAll()); + QVERIFY(doc->isParsedCorrectly()); + + QStringList symbolNames; + symbolNames.append("theRoot"); + symbolNames.append("theParent"); + symbolNames.append("theChild"); + + // check symbol existence and build scopes + QStack scopes; + foreach (const QString &symbolName, symbolNames) { + IdSymbol *id = doc->ids()[symbolName]; + QVERIFY(id); + scopes.push_back(id->parentNode()); + } + + // try lookup + Symbol *parent = scopes.front(); + QmlLookupContext context(scopes, doc, snapshot(doc), typeSystem()); + + Symbol *symbol; + symbol = context.resolve("prop"); + QVERIFY(symbol); + QVERIFY(symbol->isPropertyDefinitionSymbol()); + QVERIFY(parent->members().contains(symbol)); + + symbol = context.resolve("color"); + QVERIFY(symbol); + QVERIFY(symbol->isProperty()); +} + +QTEST_APPLESS_MAIN(tst_Lookup) +#include "tst_qmllookup.moc" -- cgit v1.2.1