void tst_qqmlexpression::scriptString() { qmlRegisterType<TestObject>("Test", 1, 0, "TestObject"); QQmlEngine engine; QQmlComponent c(&engine, testFileUrl("scriptString.qml")); TestObject *testObj = qobject_cast<TestObject*>(c.create()); QVERIFY(testObj != 0); QQmlScriptString script = testObj->scriptString(); QVERIFY(!script.isEmpty()); QQmlExpression expression(script); QVariant value = expression.evaluate(); QCOMPARE(value.toInt(), 15); QQmlScriptString scriptError = testObj->scriptStringError(); QVERIFY(!scriptError.isEmpty()); //verify that the expression has the correct error location information QQmlExpression expressionError(scriptError); QVariant valueError = expressionError.evaluate(); QVERIFY(!valueError.isValid()); QVERIFY(expressionError.hasError()); QQmlError error = expressionError.error(); QCOMPARE(error.url(), c.url()); QCOMPARE(error.line(), 8); }
QV4::ReturnedValue QQmlJavaScriptExpression::qmlBinding(QQmlContextData *ctxt, QObject *qmlScope, const QString &code, const QString &filename, quint16 line, QV4::PersistentValue *qmlscope) { QQmlEngine *engine = ctxt->engine; QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine); QV4::ExecutionEngine *v4 = QV8Engine::getV4(ep->v8engine()); QV4::ExecutionContext *ctx = v4->currentContext(); QV4::Scope scope(v4); QV4::ScopedObject qmlScopeObject(scope, QV4::QmlContextWrapper::qmlScope(ep->v8engine(), ctxt, qmlScope)); QV4::Script script(v4, qmlScopeObject, code, filename, line); QV4::ScopedValue result(scope); script.parse(); if (!v4->hasException) result = script.qmlBinding(); if (v4->hasException) { QQmlError error = QV4::ExecutionEngine::catchExceptionAsQmlError(ctx); if (error.description().isEmpty()) error.setDescription(QLatin1String("Exception occurred during function evaluation")); if (error.line() == -1) error.setLine(line); if (error.url().isEmpty()) error.setUrl(QUrl::fromLocalFile(filename)); error.setObject(qmlScope); ep->warning(error); return QV4::Encode::undefined(); } if (qmlscope) *qmlscope = qmlScopeObject; return result.asReturnedValue(); }
void tst_qqmlerror::copy() { QQmlError error; error.setUrl(QUrl("http://www.qt-project.org/main.qml")); error.setDescription("An Error"); error.setLine(92); error.setColumn(13); QQmlError error2(error); QQmlError error3; error3 = error; error.setUrl(QUrl("http://www.qt-project.org/main.qml")); error.setDescription("Another Error"); error.setLine(2); error.setColumn(33); QCOMPARE(error.url(), QUrl("http://www.qt-project.org/main.qml")); QCOMPARE(error.description(), QString("Another Error")); QCOMPARE(error.line(), 2); QCOMPARE(error.column(), 33); QCOMPARE(error2.url(), QUrl("http://www.qt-project.org/main.qml")); QCOMPARE(error2.description(), QString("An Error")); QCOMPARE(error2.line(), 92); QCOMPARE(error2.column(), 13); QCOMPARE(error3.url(), QUrl("http://www.qt-project.org/main.qml")); QCOMPARE(error3.description(), QString("An Error")); QCOMPARE(error3.line(), 92); QCOMPARE(error3.column(), 13); }
static QString qmlErrorToString(const QQmlError &error) { return QStringLiteral("%1:%2:%3: %4") .arg(error.url().toString()) .arg(error.line()) .arg(error.column()) .arg(error.description()); }
foreach(QQmlError warning, warnings) { QString url; if (warning.url().isLocalFile()) url = warning.url().toLocalFile(); else url = warning.url().toString(); qCWarning(sambaLogQml, "%s:%d: %s", url.toLocal8Bit().constData(), warning.line(), warning.description().toLocal8Bit().constData()); }
QDebug operator<<(QDebug debug, const QQmlError &error) { debug << qPrintable(error.toString()); QUrl url = error.url(); if (error.line() > 0 && url.scheme() == QLatin1String("file")) { QString file = url.toLocalFile(); QFile f(file); if (f.open(QIODevice::ReadOnly)) { QByteArray data = f.readAll(); QTextStream stream(data, QIODevice::ReadOnly); #ifndef QT_NO_TEXTCODEC stream.setCodec("UTF-8"); #endif const QString code = stream.readAll(); const QStringList lines = code.split(QLatin1Char('\n')); if (lines.count() >= error.line()) { const QString &line = lines.at(error.line() - 1); debug << "\n " << qPrintable(line); if(error.column() > 0) { int column = qMax(0, error.column() - 1); column = qMin(column, line.length()); QByteArray ind; ind.reserve(column); for (int i = 0; i < column; ++i) { const QChar ch = line.at(i); if (ch.isSpace()) ind.append(ch.unicode()); else ind.append(' '); } ind.append('^'); debug << "\n " << ind.constData(); } } } } return debug; }
void tst_qqmlerror::line() { QQmlError error; QCOMPARE(error.line(), -1); error.setLine(102); QCOMPARE(error.line(), 102); QQmlError error2 = error; QCOMPARE(error2.line(), 102); error.setLine(4); QCOMPARE(error.line(), 4); QCOMPARE(error2.line(), 102); }
void QQmlDirParser::setError(const QQmlError &e) { _errors.clear(); reportError(e.line(), e.column(), e.description()); }