diff options
author | Christian Kandeler <christian.kandeler@qt.io> | 2020-07-10 16:28:44 +0200 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@qt.io> | 2020-07-14 14:32:40 +0000 |
commit | bfd49fe5ceca5648cf5d237e61574e1eaca19241 (patch) | |
tree | 4dad1ed491668e89b049af957524b8972a21f144 /src/plugins/cppeditor/cppquickfix_test.cpp | |
parent | 3ab1713680e8cbe51be47f6ed02f470eacaa6494 (diff) |
CppEditor: Ensure proper namespace for new getter/setter functions
If the class is in a namespace and the cpp file does not yet have any
implementations in that namespace, then we would erroneously put the
getters in the global namespace.
Fixes: QTCREATORBUG-14886
Change-Id: I836537abddfdd92ced783d60e1226b082bbc238e
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src/plugins/cppeditor/cppquickfix_test.cpp')
-rw-r--r-- | src/plugins/cppeditor/cppquickfix_test.cpp | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 2512a9b936..8d4085d0a6 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -2018,6 +2018,156 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_basicGetterWithPrefixAn QuickFixOperationTest(testDocuments, &factory); } +void CppEditorPlugin::test_quickfix_GenerateGetterSetter_createNamespaceInCpp_data() +{ + QTest::addColumn<QByteArrayList>("headers"); + QTest::addColumn<QByteArrayList>("sources"); + + QByteArray originalSource; + QByteArray expectedSource; + + const QByteArray originalHeader = + "namespace N1 {\n" + "namespace N2 {\n" + "class Something\n" + "{\n" + " int @it;\n" + "};\n" + "}\n" + "}\n"; + const QByteArray expectedHeader = + "namespace N1 {\n" + "namespace N2 {\n" + "class Something\n" + "{\n" + " int it;\n" + "\n" + "public:\n" + " int getIt() const;\n" + " void setIt(int value);\n" + "};\n" + "}\n" + "}\n"; + + originalSource = "#include \"file.h\"\n"; + expectedSource = + "#include \"file.h\"\n\n\n" + "namespace N1 {\n" + "namespace N2 {\n" + "int Something::getIt() const\n" + "{\n" + " return it;\n" + "}\n" + "\n" + "void Something::setIt(int value)\n" + "{\n" + " it = value;\n" + "}\n\n" + "}\n" + "}\n"; + QTest::addRow("insert new namespaces") + << QByteArrayList{originalHeader, expectedHeader} + << QByteArrayList{originalSource, expectedSource}; + + originalSource = + "#include \"file.h\"\n" + "namespace N2 {} // decoy\n"; + expectedSource = + "#include \"file.h\"\n" + "namespace N2 {} // decoy\n\n\n" + "namespace N1 {\n" + "namespace N2 {\n" + "int Something::getIt() const\n" + "{\n" + " return it;\n" + "}\n" + "\n" + "void Something::setIt(int value)\n" + "{\n" + " it = value;\n" + "}\n\n" + "}\n" + "}\n"; + QTest::addRow("insert new namespaces (with decoy)") + << QByteArrayList{originalHeader, expectedHeader} + << QByteArrayList{originalSource, expectedSource}; + + originalSource = + "#include \"file.h\"\n" + "namespace N2 {} // decoy\n" + "namespace {\n" + "namespace N1 {\n" + "namespace {\n" + "}\n" + "}\n" + "}\n"; + expectedSource = + "#include \"file.h\"\n" + "namespace N2 {} // decoy\n" + "namespace {\n" + "namespace N1 {\n\n" + "namespace N2 {\n" + "int Something::getIt() const\n" + "{\n" + " return it;\n" + "}\n" + "\n" + "void Something::setIt(int value)\n" + "{\n" + " it = value;\n" + "}\n\n" + "}\n\n" + "namespace {\n" + "}\n" + "}\n" + "}\n"; + QTest::addRow("insert inner namespace (with decoy)") + << QByteArrayList{originalHeader, expectedHeader} + << QByteArrayList{originalSource, expectedSource}; + + originalSource = + "#include \"file.h\"\n" + "namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "}\n" + "}\n" + "}\n"; + expectedSource = + "#include \"file.h\"\n" + "namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "}\n\n" + "int Something::getIt() const\n" + "{\n" + " return it;\n" + "}\n" + "\n" + "void Something::setIt(int value)\n" + "{\n" + " it = value;\n" + "}\n\n" + "}\n" + "}\n"; + QTest::addRow("all namespaces already present") + << QByteArrayList{originalHeader, expectedHeader} + << QByteArrayList{originalSource, expectedSource}; +} + +void CppEditorPlugin::test_quickfix_GenerateGetterSetter_createNamespaceInCpp() +{ + QFETCH(QByteArrayList, headers); + QFETCH(QByteArrayList, sources); + + QList<QuickFixTestDocument::Ptr> testDocuments({ + QuickFixTestDocument::create("file.h", headers.at(0), headers.at(1)), + QuickFixTestDocument::create("file.cpp", sources.at(0), sources.at(1))}); + + GenerateGetterSetter factory; + QuickFixOperationTest(testDocuments, &factory); +} + /// Checks: Only generate getter void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlyGetter() { |