[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdevelop] languages/clang/tests: Add UnknownDeclarationProblem assistant tests
From:       Olivier JG <olivier.jg () gmail ! com>
Date:       2015-10-09 14:57:43
Message-ID: E1ZkZ79-0004om-N1 () scm ! kde ! org
[Download RAW message or body]

Git commit 0c39406d84d8accd11c6c418054db7bf01cadc87 by Olivier JG.
Committed on 09/10/2015 at 15:02.
Pushed by olivierjg into branch 'master'.

Add UnknownDeclarationProblem assistant tests

M  +96   -5    languages/clang/tests/test_assistants.cpp
M  +2    -0    languages/clang/tests/test_assistants.h

http://commits.kde.org/kdevelop/0c39406d84d8accd11c6c418054db7bf01cadc87

diff --git a/languages/clang/tests/test_assistants.cpp \
b/languages/clang/tests/test_assistants.cpp index 166bff6..305a38e 100644
--- a/languages/clang/tests/test_assistants.cpp
+++ b/languages/clang/tests/test_assistants.cpp
@@ -95,7 +95,14 @@ public:
         CppDoc
     };
 
-    Testbed(const QString& headerContents, const QString& cppContents)
+    enum IncludeBehavior
+    {
+        NoAutoInclude,
+        AutoInclude,
+    };
+
+    Testbed(const QString& headerContents, const QString& cppContents, \
IncludeBehavior include = AutoInclude) +        : m_includeBehavior(include)
     {
         static int i = 0;
         int id = i;
@@ -103,11 +110,15 @@ public:
         m_headerDocument.url = createFile(headerContents,".h",id);
         m_headerDocument.textDoc = openDocument(m_headerDocument.url);
 
-        m_cppDocument.url = createFile(QString("#include \
\"%1\"\n").arg(m_headerDocument.url.toLocalFile()) + cppContents,".cpp",id); +        \
QString preamble; +        if (include == AutoInclude)
+            preamble = QStringLiteral("#include \
\"%1\"\n").arg(m_headerDocument.url.toLocalFile()); +        m_cppDocument.url = \
createFile(preamble + cppContents,".cpp",id);  m_cppDocument.textDoc = \
openDocument(m_cppDocument.url);  }
     ~Testbed()
     {
+        Core::self()->documentController()->documentForUrl(m_cppDocument.url)->textDocument();
                
         Core::self()->documentController()->documentForUrl(m_cppDocument.url)->close(KDevelop::IDocument::Discard);
                
         Core::self()->documentController()->documentForUrl(m_headerDocument.url)->close(KDevelop::IDocument::Discard);
  
@@ -120,8 +131,10 @@ public:
         if (which == CppDoc)
         {
             document = m_cppDocument;
-            where = Range(where.start().line() + 1, where.start().column(),
-                                        where.end().line() + 1, \
where.end().column()); //The include adds a line +            if (m_includeBehavior \
== AutoInclude) { +                where = Range(where.start().line() + 1, \
where.start().column(), +                              where.end().line() + 1, \
where.end().column()); //The include adds a line +            }
         }
         else {
             document = m_headerDocument;
@@ -149,11 +162,23 @@ public:
         {
             //The CPP document text shouldn't include the autogenerated include line
             QString text = m_cppDocument.textDoc->text();
-            return text.mid(text.indexOf("\n") + 1);
+            return m_includeBehavior == AutoInclude ? text.mid(text.indexOf("\n") + \
1) : text;  }
         else
             return m_headerDocument.textDoc->text();
     }
+
+    QString includeFileName() const
+    {
+        return m_headerDocument.url.toLocalFile();
+    }
+
+    KTextEditor::Document *document(TestDoc which) const
+    {
+        return Core::self()->documentController()->documentForUrl(
+            which == CppDoc ? m_cppDocument.url : \
m_headerDocument.url)->textDocument(); +    }
+
 private:
     struct TestDocument {
         QUrl url;
@@ -167,6 +192,7 @@ private:
         return Core::self()->documentController()->documentForUrl(url)->textDocument();
  }
 
+    IncludeBehavior m_includeBehavior;
     TestDocument m_headerDocument;
     TestDocument m_cppDocument;
 };
@@ -372,3 +398,68 @@ void TestAssistants::testSignatureAssistant()
     QCOMPARE(testbed.documentText(Testbed::HeaderDoc), finalHeaderContents);
     QCOMPARE(testbed.documentText(Testbed::CppDoc), finalCppContents);
 }
+
+enum UnknownDeclarationActions
+{
+    NoUnknownDeclaration = 0x0,
+    ForwardDecls = 0x1,
+    MissingInclude = 0x2
+};
+
+Q_DECLARE_METATYPE(UnknownDeclarationActions)
+
+void TestAssistants::testUnknownDeclarationAssistant_data()
+{
+    QTest::addColumn<QString>("headerContents");
+    QTest::addColumn<QString>("globalText");
+    QTest::addColumn<QString>("functionText");
+    QTest::addColumn<UnknownDeclarationActions>("actions");
+
+    QTest::newRow("unincluded_struct") << "struct test{};" << "" << "test"
+        << static_cast<UnknownDeclarationActions>(ForwardDecls | MissingInclude);
+    QTest::newRow("forward_declared_struct") << "struct test{};" << "struct test;" \
<< "test *f; f->" +        << static_cast<UnknownDeclarationActions>(MissingInclude);
+    QTest::newRow("unknown_struct") << "" << "" << "test"
+        << static_cast<UnknownDeclarationActions>(ForwardDecls);
+}
+
+void TestAssistants::testUnknownDeclarationAssistant()
+{
+    QFETCH(QString, headerContents);
+    QFETCH(QString, globalText);
+    QFETCH(QString, functionText);
+    QFETCH(UnknownDeclarationActions, actions);
+
+    static const auto cppContents = QStringLiteral("%1\nvoid f_u_n_c_t_i_o_n() \
{\n}"); +    Testbed testbed(headerContents, cppContents.arg(globalText), \
Testbed::NoAutoInclude); +    const int line = \
testbed.document(Testbed::CppDoc)->lines() - 1; +    \
testbed.changeDocument(Testbed::CppDoc, Range(line, 0, line, 0), functionText, true); \
+ +    if (actions == NoUnknownDeclaration) {
+        QVERIFY(!staticAssistantsManager()->activeAssistant());
+        return;
+    }
+
+    QVERIFY(staticAssistantsManager()->activeAssistant());
+    const auto assistantActions = \
staticAssistantsManager()->activeAssistant()->actions(); +    QStringList \
actionDescriptions; +    for (auto action: assistantActions) {
+        actionDescriptions << action->description();
+    }
+
+    {
+        const bool hasForwardDecls =
+            actionDescriptions.contains(QObject::tr("Forward declare as 'struct'")) \
&& +            actionDescriptions.contains(QObject::tr("Forward declare as \
'class'")); +        QCOMPARE(hasForwardDecls, static_cast<bool>(actions & \
ForwardDecls)); +    }
+
+    {
+        auto fileName = testbed.includeFileName();
+        fileName = fileName.mid(fileName.lastIndexOf('/') + 1);
+        const auto description = QObject::tr("Insert \'%1\'")
+            .arg(QStringLiteral("#include \"%1\"").arg(fileName));
+        const bool hasMissingInclude = actionDescriptions.contains(description);
+        QCOMPARE(hasMissingInclude, static_cast<bool>(actions & MissingInclude));
+    }
+}
diff --git a/languages/clang/tests/test_assistants.h \
b/languages/clang/tests/test_assistants.h index 6eb33f5..43d168e 100644
--- a/languages/clang/tests/test_assistants.h
+++ b/languages/clang/tests/test_assistants.h
@@ -34,6 +34,8 @@ private slots:
     void testRenameAssistantUndoRename();
     void testSignatureAssistant_data();
     void testSignatureAssistant();
+    void testUnknownDeclarationAssistant_data();
+    void testUnknownDeclarationAssistant();
 };
 
 #endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic