2015-08-24 18:26:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://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 http://www.qt.io/terms-conditions. For further information
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "gmock/gmock-matchers.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest-qt-printing.h"
|
|
|
|
|
|
|
|
#include <clangfixitoperation.h>
|
|
|
|
#include <fixitcontainer.h>
|
|
|
|
|
|
|
|
#include <utils/changeset.h>
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QVector>
|
|
|
|
|
|
|
|
using ClangBackEnd::FixItContainer;
|
|
|
|
using ClangCodeModel::ClangFixItOperation;
|
|
|
|
|
|
|
|
using ::testing::PrintToString;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
QString unsavedFileContent(const QString &unsavedFilePath)
|
|
|
|
{
|
|
|
|
QFile unsavedFileContentFile(unsavedFilePath);
|
|
|
|
const bool isOpen = unsavedFileContentFile.open(QFile::ReadOnly | QFile::Text);
|
|
|
|
if (!isOpen)
|
|
|
|
ADD_FAILURE() << "File with the unsaved content cannot be opened!";
|
|
|
|
|
|
|
|
return QString::fromUtf8(unsavedFileContentFile.readAll());
|
|
|
|
}
|
|
|
|
|
2015-12-02 20:07:29 +01:00
|
|
|
MATCHER_P(MatchText, expectedText,
|
|
|
|
std::string(negation ? "hasn't" : "has")
|
|
|
|
+ " expected text:\n" + PrintToString(expectedText))
|
2015-08-24 18:26:09 +02:00
|
|
|
{
|
2015-12-02 20:07:29 +01:00
|
|
|
const ::ClangFixItOperation &operation = arg;
|
|
|
|
QString resultText = operation.refactoringFileContent_forTestOnly();
|
2015-08-24 18:26:09 +02:00
|
|
|
|
|
|
|
if (resultText != expectedText) {
|
|
|
|
*result_listener << "\n" << resultText.toUtf8().constData();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClangFixItOperation : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
2015-12-02 20:07:29 +01:00
|
|
|
Utf8String semicolonFilePath{TESTDATA_DIR"/diagnostic_semicolon_fixit.cpp", -1};
|
|
|
|
Utf8String compareFilePath{TESTDATA_DIR"/diagnostic_comparison_fixit.cpp", -1};
|
2015-08-24 18:26:09 +02:00
|
|
|
Utf8String diagnosticText{Utf8StringLiteral("expected ';' at end of declaration")};
|
|
|
|
FixItContainer semicolonFixItContainer{Utf8StringLiteral(";"),
|
2015-12-02 20:07:29 +01:00
|
|
|
{{semicolonFilePath, 3u, 13u},
|
|
|
|
{semicolonFilePath, 3u, 13u}}};
|
|
|
|
QString semicolonErrorFile{semicolonFilePath.toString()};
|
2015-11-24 11:07:51 +01:00
|
|
|
QString semicolonExpectedFile{QString::fromUtf8(TESTDATA_DIR"/diagnostic_semicolon_fixit_expected.cpp")};
|
2015-12-02 20:07:29 +01:00
|
|
|
QString compareWarningFile{compareFilePath.toString()};
|
2015-11-24 11:07:51 +01:00
|
|
|
QString compareExpected1File{QString::fromUtf8(TESTDATA_DIR"/diagnostic_comparison_fixit_expected1.cpp")};
|
|
|
|
QString compareExpected2File{QString::fromUtf8(TESTDATA_DIR"/diagnostic_comparison_fixit_expected2.cpp")};
|
2015-08-24 18:26:09 +02:00
|
|
|
FixItContainer compareFixItContainer{Utf8StringLiteral("=="),
|
2015-12-02 20:07:29 +01:00
|
|
|
{{compareFilePath, 4u, 11u},
|
|
|
|
{compareFilePath, 4u, 12u}}};
|
2015-08-24 18:26:09 +02:00
|
|
|
FixItContainer assignmentFixItContainerParenLeft{Utf8StringLiteral("("),
|
2015-12-02 20:07:29 +01:00
|
|
|
{{compareFilePath, 4u, 9u},
|
|
|
|
{compareFilePath, 4u, 9u}}};
|
2015-08-24 18:26:09 +02:00
|
|
|
FixItContainer assignmentFixItContainerParenRight{Utf8StringLiteral(")"),
|
2015-12-02 20:07:29 +01:00
|
|
|
{{compareFilePath, 4u, 14u},
|
|
|
|
{compareFilePath, 4u, 14u}}};
|
2015-08-24 18:26:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ClangFixItOperation, Description)
|
|
|
|
{
|
2015-12-02 20:07:29 +01:00
|
|
|
::ClangFixItOperation operation(semicolonFilePath, diagnosticText, {semicolonFixItContainer});
|
2015-08-24 18:26:09 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(operation.description(),
|
|
|
|
QStringLiteral("Apply Fix: expected ';' at end of declaration"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ClangFixItOperation, AppendSemicolon)
|
|
|
|
{
|
2015-12-02 20:07:29 +01:00
|
|
|
::ClangFixItOperation operation(semicolonFilePath, diagnosticText, {semicolonFixItContainer});
|
2015-08-24 18:26:09 +02:00
|
|
|
|
2015-12-02 20:07:29 +01:00
|
|
|
operation.perform();
|
|
|
|
|
|
|
|
ASSERT_THAT(operation, MatchText(unsavedFileContent(semicolonExpectedFile)));
|
2015-08-24 18:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ClangFixItOperation, ComparisonVersusAssignmentChooseComparison)
|
|
|
|
{
|
2015-12-02 20:07:29 +01:00
|
|
|
::ClangFixItOperation operation(compareFilePath, diagnosticText, {compareFixItContainer});
|
|
|
|
|
|
|
|
operation.perform();
|
2015-08-24 18:26:09 +02:00
|
|
|
|
2015-12-02 20:07:29 +01:00
|
|
|
ASSERT_THAT(operation, MatchText(unsavedFileContent(compareExpected1File)));
|
2015-08-24 18:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ClangFixItOperation, ComparisonVersusAssignmentChooseParentheses)
|
|
|
|
{
|
2015-12-02 20:07:29 +01:00
|
|
|
::ClangFixItOperation operation(compareFilePath,
|
2015-08-24 18:26:09 +02:00
|
|
|
diagnosticText,
|
|
|
|
{assignmentFixItContainerParenLeft,
|
|
|
|
assignmentFixItContainerParenRight});
|
|
|
|
|
2015-12-02 20:07:29 +01:00
|
|
|
operation.perform();
|
|
|
|
|
|
|
|
ASSERT_THAT(operation, MatchText(unsavedFileContent(compareExpected2File)));
|
2015-08-24 18:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|