Document::Ptr QmlJSRefactoringFile::qmljsDocument() const { if (!m_qmljsDocument) { const QString source = document()->toPlainText(); const QString name = fileName(); const Snapshot &snapshot = data()->m_snapshot; Document::MutablePtr newDoc = snapshot.documentFromSource(source, name, languageOfFile(name)); newDoc->parse(); m_qmljsDocument = newDoc; } return m_qmljsDocument; }
void tst_Reformatter::test() { QFETCH(QString, path); Document::MutablePtr doc = Document::create(path, ModelManagerInterface::guessLanguageOfFile(path)); QFile file(doc->fileName()); file.open(QFile::ReadOnly | QFile::Text); QString source = QString::fromUtf8(file.readAll()); doc->setSource(source); file.close(); doc->parse(); QVERIFY(!doc->source().isEmpty()); QVERIFY(doc->diagnosticMessages().isEmpty()); QString rewritten = reformat(doc); QStringList sourceLines = source.split(QLatin1Char('\n')); QStringList newLines = rewritten.split(QLatin1Char('\n')); // compare line by line int commonLines = qMin(newLines.size(), sourceLines.size()); for (int i = 0; i < commonLines; ++i) { // names intentional to make 'Actual (sourceLine): ...\nExpected (newLinee): ...' line up const QString &sourceLine = sourceLines.at(i); const QString &newLinee = newLines.at(i); if (sourceLine.trimmed().isEmpty() && newLinee.trimmed().isEmpty()) continue; bool fail = !QCOMPARE_NOEXIT(sourceLine, newLinee); if (fail) { qDebug() << "in line" << (i + 1); return; } } QCOMPARE(sourceLines.size(), newLines.size()); }
void tst_Check::test() { QFETCH(QString, path); Snapshot snapshot; Document::MutablePtr doc = Document::create(path, Document::QmlLanguage); QFile file(doc->fileName()); file.open(QFile::ReadOnly | QFile::Text); doc->setSource(file.readAll()); file.close(); doc->parse(); snapshot.insert(doc); QVERIFY(!doc->source().isEmpty()); QVERIFY(doc->diagnosticMessages().isEmpty()); ContextPtr context = Link(snapshot, QStringList(), LibraryInfo())(); Check checker(doc, context); QList<Message> messages = checker(); std::sort(messages.begin(), messages.end(), &offsetComparator); const QRegExp messagePattern(" (\\d+) (\\d+) (\\d+)"); QList<Message> expectedMessages; foreach (const AST::SourceLocation &comment, doc->engine()->comments()) { const QString text = doc->source().mid(comment.begin(), comment.end() - comment.begin()); if (messagePattern.indexIn(text) == -1) continue; const int type = messagePattern.cap(1).toInt(); const int columnStart = messagePattern.cap(2).toInt(); const int columnEnd = messagePattern.cap(3).toInt() + 1; Message message; message.location = SourceLocation( comment.offset - comment.startColumn + columnStart, columnEnd - columnStart, comment.startLine, columnStart), message.type = static_cast<Type>(type); expectedMessages += message; } for (int i = 0; i < messages.size(); ++i) { if (i >= expectedMessages.size()) break; Message actual = messages.at(i); Message expected = expectedMessages.at(i); bool fail = false; fail |= !QCOMPARE_NOEXIT(actual.location.startLine, expected.location.startLine); fail |= !QCOMPARE_NOEXIT((int)actual.type, (int)expected.type); if (fail) return; fail |= !QCOMPARE_NOEXIT(actual.location.startColumn, expected.location.startColumn); fail |= !QCOMPARE_NOEXIT(actual.location.offset, expected.location.offset); fail |= !QCOMPARE_NOEXIT(actual.location.length, expected.location.length); if (fail) { qDebug() << "Failed for message on line" << actual.location.startLine << actual.message; return; } } if (expectedMessages.size() > messages.size()) { for (int i = messages.size(); i < expectedMessages.size(); ++i) { Message missingMessage = expectedMessages.at(i); qDebug() << "expected message of type" << missingMessage.type << "on line" << missingMessage.location.startLine; } QFAIL("more messages expected"); } if (expectedMessages.size() < messages.size()) { for (int i = expectedMessages.size(); i < messages.size(); ++i) { Message extraMessage = messages.at(i); qDebug() << "unexpected message of type" << extraMessage.type << "on line" << extraMessage.location.startLine; } QFAIL("fewer messages expected"); } }