From 98fa7cbb3f0269032ff68b172c7de82a46178875 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Wed, 13 Dec 2017 17:35:00 +0100 Subject: Clang: Use FilePathId in ChangedFilePathCompressor Change-Id: I622d9196ec12c6fe6e07ef9c953586f2f40b9a5c Reviewed-by: Ivan Donchevskii --- .../unittest/changedfilepathcompressor-test.cpp | 53 ++++++++++++++-------- tests/unit/unittest/clangpathwatcher-test.cpp | 4 +- tests/unit/unittest/faketimer.cpp | 46 ------------------- tests/unit/unittest/faketimer.h | 46 ------------------- tests/unit/unittest/google-using-declarations.h | 2 + .../unit/unittest/mockchangedfilepathcompressor.h | 43 ------------------ tests/unit/unittest/mocktimer.cpp | 46 +++++++++++++++++++ tests/unit/unittest/mocktimer.h | 51 +++++++++++++++++++++ tests/unit/unittest/unittest.pro | 9 ++-- 9 files changed, 138 insertions(+), 162 deletions(-) delete mode 100644 tests/unit/unittest/faketimer.cpp delete mode 100644 tests/unit/unittest/faketimer.h delete mode 100644 tests/unit/unittest/mockchangedfilepathcompressor.h create mode 100644 tests/unit/unittest/mocktimer.cpp create mode 100644 tests/unit/unittest/mocktimer.h (limited to 'tests') diff --git a/tests/unit/unittest/changedfilepathcompressor-test.cpp b/tests/unit/unittest/changedfilepathcompressor-test.cpp index d65eac708cf..ab31136842d 100644 --- a/tests/unit/unittest/changedfilepathcompressor-test.cpp +++ b/tests/unit/unittest/changedfilepathcompressor-test.cpp @@ -25,7 +25,11 @@ #include "googletest.h" -#include +#include "mocktimer.h" + +#include +#include +#include namespace { @@ -35,56 +39,65 @@ using testing::IsEmpty; using testing::NiceMock; using ClangBackEnd::FilePath; - +using ClangBackEnd::FilePathId; class ChangedFilePathCompressor : public testing::Test { protected: - void SetUp(); + void SetUp() + { + compressor.setCallback(mockCompressor.AsStdFunction()); + } + + FilePathId filePathId(const QString &filePath) + { + Utils::SmallString utf8FilePath{filePath}; + + return filePathCache.filePathId(ClangBackEnd::FilePathView{utf8FilePath}); + } protected: - NiceMock mockCompressor; - ClangBackEnd::ChangedFilePathCompressor compressor; + Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory}; + ClangBackEnd::RefactoringDatabaseInitializer initializer{database}; + ClangBackEnd::FilePathCaching filePathCache{database}; + NiceMock> mockCompressor; + ClangBackEnd::ChangedFilePathCompressor> compressor{filePathCache}; + NiceMock &mockTimer = compressor.timer(); QString filePath1{"filePath1"}; QString filePath2{"filePath2"}; }; TEST_F(ChangedFilePathCompressor, AddFilePath) { - mockCompressor.addFilePath(filePath1); + compressor.addFilePath(filePath1); - ASSERT_THAT(mockCompressor.takeFilePaths(), ElementsAre(FilePath{filePath1})); + ASSERT_THAT(compressor.takeFilePathIds(), ElementsAre(filePathId(filePath1))); } TEST_F(ChangedFilePathCompressor, NoFilePathsAferTakenThem) { - mockCompressor.addFilePath(filePath1); + compressor.addFilePath(filePath1); - mockCompressor.takeFilePaths(); + compressor.takeFilePathIds(); - ASSERT_THAT(mockCompressor.takeFilePaths(), IsEmpty()); + ASSERT_THAT(compressor.takeFilePathIds(), IsEmpty()); } TEST_F(ChangedFilePathCompressor, CallRestartTimerAfterAddingPath) { - EXPECT_CALL(mockCompressor, restartTimer()); + EXPECT_CALL(mockTimer, start(20)); - mockCompressor.addFilePath(filePath1); + compressor.addFilePath(filePath1); } TEST_F(ChangedFilePathCompressor, CallTimeOutAfterAddingPath) { - EXPECT_CALL(mockCompressor, callbackCalled(ElementsAre(FilePath{filePath1}, FilePath{filePath2}))); + auto id1 = filePathId(filePath1); + auto id2 = filePathId(filePath2); + EXPECT_CALL(mockCompressor, Call(ElementsAre(id1, id2))); compressor.addFilePath(filePath1); compressor.addFilePath(filePath2); } -void ChangedFilePathCompressor::SetUp() -{ - compressor.setCallback([&] (ClangBackEnd::FilePaths &&filePaths) { - mockCompressor.callbackCalled(filePaths); - }); -} - } diff --git a/tests/unit/unittest/clangpathwatcher-test.cpp b/tests/unit/unittest/clangpathwatcher-test.cpp index f1a85f5f4b8..cb3a7a609a1 100644 --- a/tests/unit/unittest/clangpathwatcher-test.cpp +++ b/tests/unit/unittest/clangpathwatcher-test.cpp @@ -25,7 +25,7 @@ #include "googletest.h" -#include "faketimer.h" +#include "mocktimer.h" #include "mockfilepathcaching.h" #include "mockqfilesystemwatcher.h" #include "mockclangpathwatchernotifier.h" @@ -42,7 +42,7 @@ using testing::IsEmpty; using testing::SizeIs; using testing::NiceMock; -using Watcher = ClangBackEnd::ClangPathWatcher, FakeTimer>; +using Watcher = ClangBackEnd::ClangPathWatcher, NiceMock>; using ClangBackEnd::WatcherEntry; using ClangBackEnd::FilePath; using ClangBackEnd::FilePathView; diff --git a/tests/unit/unittest/faketimer.cpp b/tests/unit/unittest/faketimer.cpp deleted file mode 100644 index 61e26893734..00000000000 --- a/tests/unit/unittest/faketimer.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/**************************************************************************** -** -** 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 "faketimer.h" - -FakeTimer::~FakeTimer() -{ - emitTimoutIfStarted(); -} - -void FakeTimer::start(int) -{ - m_isStarted = true; -} - -void FakeTimer::setSingleShot(bool) -{ -} - -void FakeTimer::emitTimoutIfStarted() -{ - if (m_isStarted) - emit timeout(); -} diff --git a/tests/unit/unittest/faketimer.h b/tests/unit/unittest/faketimer.h deleted file mode 100644 index ef6945205c7..00000000000 --- a/tests/unit/unittest/faketimer.h +++ /dev/null @@ -1,46 +0,0 @@ -/**************************************************************************** -** -** 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. -** -****************************************************************************/ -#pragma once - -#include - -class FakeTimer : public QObject -{ - Q_OBJECT - -public: - ~FakeTimer(); - - void start(int interval); - void setSingleShot(bool); - - void emitTimoutIfStarted(); - -signals: - void timeout(); - -private: - bool m_isStarted = false; -}; diff --git a/tests/unit/unittest/google-using-declarations.h b/tests/unit/unittest/google-using-declarations.h index 73994bd2bc2..4627e04427d 100644 --- a/tests/unit/unittest/google-using-declarations.h +++ b/tests/unit/unittest/google-using-declarations.h @@ -31,6 +31,7 @@ using testing::_; using testing::AllOf; using testing::AnyNumber; using testing::AnyOf; +using testing::Assign; using testing::Contains; using testing::ElementsAre; using testing::Field; @@ -38,6 +39,7 @@ using testing::HasSubstr; using testing::InSequence; using testing::IsEmpty; using testing::Mock; +using testing::MockFunction; using testing::NiceMock; using testing::Not; using testing::Pair; diff --git a/tests/unit/unittest/mockchangedfilepathcompressor.h b/tests/unit/unittest/mockchangedfilepathcompressor.h deleted file mode 100644 index f1b556ba2e1..00000000000 --- a/tests/unit/unittest/mockchangedfilepathcompressor.h +++ /dev/null @@ -1,43 +0,0 @@ -/**************************************************************************** -** -** 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. -** -****************************************************************************/ - -#pragma once - -#include "googletest.h" - -#include "faketimer.h" - -#include - -class MockChangedFilePathCompressor : public ClangBackEnd::ChangedFilePathCompressor -{ -public: - MOCK_METHOD0(restartTimer, - void ()); - - MOCK_METHOD1(callbackCalled, - void (const ClangBackEnd::FilePaths &filePaths)); -}; - diff --git a/tests/unit/unittest/mocktimer.cpp b/tests/unit/unittest/mocktimer.cpp new file mode 100644 index 00000000000..8653f884913 --- /dev/null +++ b/tests/unit/unittest/mocktimer.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** 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 "mocktimer.h" + +MockTimer::MockTimer() +{ + ON_CALL(*this, start(_)).WillByDefault(Assign(&m_isStarted, true)); +} + +MockTimer::~MockTimer() +{ + emitTimoutIfStarted(); +} + +void MockTimer::setSingleShot(bool) +{ +} + +void MockTimer::emitTimoutIfStarted() +{ + if (m_isStarted) + emit timeout(); +} diff --git a/tests/unit/unittest/mocktimer.h b/tests/unit/unittest/mocktimer.h new file mode 100644 index 00000000000..aaf389764fd --- /dev/null +++ b/tests/unit/unittest/mocktimer.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** 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. +** +****************************************************************************/ +#pragma once + +#include "googletest.h" + +#include + +class MockTimer : public QObject +{ + Q_OBJECT + +public: + MockTimer(); + ~MockTimer(); + + MOCK_METHOD1(start, + void (int)); + + void setSingleShot(bool); + + void emitTimoutIfStarted(); + +signals: + void timeout(); + +private: + bool m_isStarted = false; +}; diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro index 41f5a6113c8..a16a13e88bf 100644 --- a/tests/unit/unittest/unittest.pro +++ b/tests/unit/unittest/unittest.pro @@ -48,7 +48,6 @@ SOURCES += \ cppprojectinfogenerator-test.cpp \ cppprojectpartchooser-test.cpp \ fakeprocess.cpp \ - faketimer.cpp \ filepath-test.cpp \ filepathview-test.cpp \ gtest-creator-printing.cpp \ @@ -85,7 +84,8 @@ SOURCES += \ filepathstoragesqlitestatementfactory-test.cpp \ processcreator-test.cpp \ nativefilepath-test.cpp \ - nativefilepathview-test.cpp + nativefilepathview-test.cpp \ + mocktimer.cpp !isEmpty(LIBCLANG_LIBS) { SOURCES += \ @@ -180,13 +180,11 @@ HEADERS += \ dynamicastmatcherdiagnosticcontainer-matcher.h \ eventspy.h \ fakeprocess.h \ - faketimer.h \ filesystem-utilities.h \ googletest.h \ gtest-creator-printing.h \ gtest-qt-printing.h \ mimedatabase-utilities.h \ - mockchangedfilepathcompressor.h \ mockclangcodemodelclient.h \ mockclangcodemodelserver.h \ mockclangpathwatcher.h \ @@ -221,7 +219,8 @@ HEADERS += \ unittest-utility-functions.h \ mocksymbolquery.h \ runprojectcreateorupdate-utility.h \ - rundocumentparse-utility.h + rundocumentparse-utility.h \ + mocktimer.h !isEmpty(LIBCLANG_LIBS) { HEADERS += \ chunksreportedmonitor.h \ -- cgit v1.2.3