diff options
author | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2016-04-29 11:35:42 +0200 |
---|---|---|
committer | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2016-05-03 12:19:13 +0000 |
commit | 17c1325cc45eb563333d28725a51c04e33cd01f2 (patch) | |
tree | e4c466fd247633ee74c6e34f5681c2c450ab79d3 /tests | |
parent | 9b69b789ab8a7ccc0f7c251f364f232f2a41c7fd (diff) | |
download | qt-creator-17c1325cc45eb563333d28725a51c04e33cd01f2.tar.gz |
Clang: Fix dot to arrow correction
The position of the dot character was determined on an outdated
translation unit.
We queried the translation unit for the source location of the dot
character, but apparently clang_codeCompleteAt() does not update the
source locations for the translation unit. And we do not want to reparse
since this is expensive. Thus, determine the byte position manually by
scanning over the document until the right line/column is reached.
Change-Id: I25e256bb81a83bb71c7e46a0fb3e927bf4031b16
Reviewed-by: Erik Verbruggen <erik.verbruggen@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Diffstat (limited to 'tests')
6 files changed, 207 insertions, 1 deletions
diff --git a/tests/unit/unittest/codecompletiontest.cpp b/tests/unit/unittest/codecompletiontest.cpp index 8ace4a8606..a370f93586 100644 --- a/tests/unit/unittest/codecompletiontest.cpp +++ b/tests/unit/unittest/codecompletiontest.cpp @@ -112,6 +112,18 @@ protected: readFileContent(QStringLiteral("/complete_withDotArrowCorrectionForPointer.cpp")), true }; + ClangBackEnd::FileContainer dotArrowCorrectionForPointerFileContainerInitial{ + Utf8StringLiteral(TESTDATA_DIR"/complete_withDotArrowCorrectionForPointer.cpp"), + projectPart.projectPartId(), + readFileContent(QStringLiteral("/complete_withDotArrowCorrectionForPointerInitial.cpp")), + true + }; + ClangBackEnd::FileContainer dotArrowCorrectionForPointerFileContainerUpdated{ + Utf8StringLiteral(TESTDATA_DIR"/complete_withDotArrowCorrectionForPointer.cpp"), + projectPart.projectPartId(), + readFileContent(QStringLiteral("/complete_withDotArrowCorrectionForPointerUpdated.cpp")), + true + }; ClangBackEnd::FileContainer noDotArrowCorrectionForObjectFileContainer{ Utf8StringLiteral(TESTDATA_DIR"/complete_withNoDotArrowCorrectionForObject.cpp"), projectPart.projectPartId(), @@ -308,6 +320,14 @@ TEST_F(CodeCompleter, HasDotAt) ASSERT_TRUE(myCompleter.hasDotAt(5, 8)); } +TEST_F(CodeCompleter, HasDotAtWithUpdatedUnsavedFile) +{ + auto myCompleter = setupCompleter(dotArrowCorrectionForPointerFileContainerInitial); + unsavedFiles.createOrUpdate({dotArrowCorrectionForPointerFileContainerUpdated}); + + ASSERT_TRUE(myCompleter.hasDotAt(5, 8)); +} + TEST_F(CodeCompleter, HasNoDotAtDueToMissingUnsavedFile) { const ClangBackEnd::FileContainer fileContainer = dotArrowCorrectionForPointerFileContainer; diff --git a/tests/unit/unittest/data/complete_withDotArrowCorrectionForPointerInitial.cpp b/tests/unit/unittest/data/complete_withDotArrowCorrectionForPointerInitial.cpp new file mode 100644 index 0000000000..0472cfacaa --- /dev/null +++ b/tests/unit/unittest/data/complete_withDotArrowCorrectionForPointerInitial.cpp @@ -0,0 +1,6 @@ +struct Foo { int member; }; + +void g(Foo *foo) +{ + +} diff --git a/tests/unit/unittest/data/complete_withDotArrowCorrectionForPointerUpdated.cpp b/tests/unit/unittest/data/complete_withDotArrowCorrectionForPointerUpdated.cpp new file mode 100644 index 0000000000..9f8d3645b2 --- /dev/null +++ b/tests/unit/unittest/data/complete_withDotArrowCorrectionForPointerUpdated.cpp @@ -0,0 +1,6 @@ +struct Foo { int member; }; + +void g(Foo *foo) +{ + foo. +} diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro index 55e35621ed..dab05e794f 100644 --- a/tests/unit/unittest/unittest.pro +++ b/tests/unit/unittest/unittest.pro @@ -68,7 +68,8 @@ SOURCES += \ unsavedfiletest.cpp \ clangisdiagnosticrelatedtolocationtest.cpp \ smallstringtest.cpp \ - sizedarraytest.cpp + sizedarraytest.cpp \ + utf8positionfromlinecolumntest.cpp exists($$GOOGLEBENCHMARK_DIR) { SOURCES += \ diff --git a/tests/unit/unittest/unsavedfiletest.cpp b/tests/unit/unittest/unsavedfiletest.cpp index 80f22ded35..3f0ae6d90b 100644 --- a/tests/unit/unittest/unsavedfiletest.cpp +++ b/tests/unit/unittest/unsavedfiletest.cpp @@ -171,4 +171,11 @@ TEST_F(UnsavedFile, HasCharacterForCorrectOffset) ASSERT_TRUE(unsavedFile.hasCharacterAt(0, 'c')); } +TEST_F(UnsavedFile, HasCharacterForLastLineColumn) +{ + ::UnsavedFile unsavedFile(filePath, fileContent); + + ASSERT_TRUE(unsavedFile.hasCharacterAt(1, 7, 't')); +} + } // anonymous namespace diff --git a/tests/unit/unittest/utf8positionfromlinecolumntest.cpp b/tests/unit/unittest/utf8positionfromlinecolumntest.cpp new file mode 100644 index 0000000000..683483ddab --- /dev/null +++ b/tests/unit/unittest/utf8positionfromlinecolumntest.cpp @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "gtest/gtest.h" +#include "gmock/gmock-matchers.h" +#include "gmock/gmock.h" +#include "gtest-qt-printing.h" + +#include <utf8positionfromlinecolumn.h> +#include <utf8string.h> + +using ClangBackEnd::Utf8PositionFromLineColumn; +using ::testing::PrintToString; + +namespace { + +class Utf8PositionFromLineColumn : public ::testing::Test +{ +protected: + Utf8String empty; + Utf8String asciiWord = Utf8StringLiteral("FOO"); + Utf8String asciiMultiLine = Utf8StringLiteral("FOO\nBAR"); + Utf8String asciiEmptyMultiLine = Utf8StringLiteral("\n\n"); + // U+00FC - 2 code units in UTF8, 1 in UTF16 - LATIN SMALL LETTER U WITH DIAERESIS + // U+4E8C - 3 code units in UTF8, 1 in UTF16 - CJK UNIFIED IDEOGRAPH-4E8C + // U+10302 - 4 code units in UTF8, 2 in UTF16 - OLD ITALIC LETTER KE + Utf8String nonAsciiMultiLine = Utf8StringLiteral("\xc3\xbc" "\n" + "\xe4\xba\x8c" "\n" + "\xf0\x90\x8c\x82" "X"); +}; + +TEST_F(Utf8PositionFromLineColumn, FindsNothingForEmptyContents) +{ + ::Utf8PositionFromLineColumn converter(empty.constData()); + + ASSERT_FALSE(converter.find(1, 1)); +} + +TEST_F(Utf8PositionFromLineColumn, FindsNothingForNoContents) +{ + ::Utf8PositionFromLineColumn converter(0); + + ASSERT_FALSE(converter.find(1, 1)); +} + +TEST_F(Utf8PositionFromLineColumn, FindsNothingForLineZero) +{ + ::Utf8PositionFromLineColumn converter(asciiWord.constData()); + + ASSERT_FALSE(converter.find(0, 1)); +} + +TEST_F(Utf8PositionFromLineColumn, FindsNothingForColumnZero) +{ + ::Utf8PositionFromLineColumn converter(asciiWord.constData()); + + ASSERT_FALSE(converter.find(1, 0)); +} + +TEST_F(Utf8PositionFromLineColumn, FindsFirstForAsciiWord) +{ + ::Utf8PositionFromLineColumn converter(asciiWord.constData()); + + ASSERT_TRUE(converter.find(1, 1)); + ASSERT_THAT(converter.position(), 0); +} + +TEST_F(Utf8PositionFromLineColumn, FindsLastForAsciiWord) +{ + ::Utf8PositionFromLineColumn converter(asciiWord.constData()); + + ASSERT_TRUE(converter.find(1, 3)); + ASSERT_THAT(converter.position(), 2); +} + +TEST_F(Utf8PositionFromLineColumn, FindsFirstForSecondLineAscii) +{ + ::Utf8PositionFromLineColumn converter(asciiMultiLine.constData()); + + ASSERT_TRUE(converter.find(2, 1)); + ASSERT_THAT(converter.position(), 4); +} + +TEST_F(Utf8PositionFromLineColumn, FindsLastForAsciiMultiLine) +{ + ::Utf8PositionFromLineColumn converter(asciiMultiLine.constData()); + + ASSERT_TRUE(converter.find(2, 3)); + ASSERT_THAT(converter.position(), 6); +} + +TEST_F(Utf8PositionFromLineColumn, FindsLastInFirstLine) +{ + ::Utf8PositionFromLineColumn converter(asciiMultiLine.constData()); + + ASSERT_TRUE(converter.find(1, 3)); + ASSERT_THAT(converter.position(), 2); +} + +TEST_F(Utf8PositionFromLineColumn, FindsNothingBeyondMaxColumn) +{ + ::Utf8PositionFromLineColumn converter(asciiWord.constData()); + + ASSERT_FALSE(converter.find(1, 4)); +} + +TEST_F(Utf8PositionFromLineColumn, FindsNothingBeyondMaxColumnForMultiLine) +{ + ::Utf8PositionFromLineColumn converter(asciiMultiLine.constData()); + + ASSERT_FALSE(converter.find(1, 4)); +} + +TEST_F(Utf8PositionFromLineColumn, FindsNothingBeyondMaxLine) +{ + ::Utf8PositionFromLineColumn converter(asciiWord.constData()); + + ASSERT_FALSE(converter.find(2, 1)); +} + +TEST_F(Utf8PositionFromLineColumn, FindsNothingForLineWithoutContent) +{ + ::Utf8PositionFromLineColumn converter(asciiEmptyMultiLine.constData()); + + ASSERT_FALSE(converter.find(1, 1)); +} + +TEST_F(Utf8PositionFromLineColumn, FindsFirstOnNonAsciiMultiLine) +{ + ::Utf8PositionFromLineColumn converter(nonAsciiMultiLine.constData()); + + ASSERT_TRUE(converter.find(1, 1)); + ASSERT_THAT(converter.position(), 0); +} + +TEST_F(Utf8PositionFromLineColumn, FindsLastOnNonAsciiMultiLine) +{ + ::Utf8PositionFromLineColumn converter(nonAsciiMultiLine.constData()); + + ASSERT_TRUE(converter.find(3, 2)); + ASSERT_THAT(converter.position(), 11); +} + +} // anonymous namespace |