forked from qt-creator/qt-creator
		
	Different types are introduced for normalized and native file path. So the compiler is warning you if you try the wrong format. Change-Id: I1da0686b142cbf9bb7578468c2b50f90a94cebf9 Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io> Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
		
			
				
	
	
		
			149 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			149 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/****************************************************************************
 | 
						|
**
 | 
						|
** 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 "googletest.h"
 | 
						|
 | 
						|
#include <refactoringdatabaseinitializer.h>
 | 
						|
#include <filepathcaching.h>
 | 
						|
#include <includecollector.h>
 | 
						|
 | 
						|
#include <sqlitedatabase.h>
 | 
						|
 | 
						|
#include <QDir>
 | 
						|
 | 
						|
using testing::AllOf;
 | 
						|
using testing::Contains;
 | 
						|
using testing::Not;
 | 
						|
using testing::ElementsAre;
 | 
						|
using testing::UnorderedElementsAre;
 | 
						|
 | 
						|
using ClangBackEnd::FilePathId;
 | 
						|
using ClangBackEnd::FilePathView;
 | 
						|
 | 
						|
namespace {
 | 
						|
 | 
						|
class IncludeCollector : public ::testing::Test
 | 
						|
{
 | 
						|
protected:
 | 
						|
    void SetUp();
 | 
						|
    FilePathId id(const Utils::SmallStringView &path);
 | 
						|
 | 
						|
protected:
 | 
						|
    Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory};
 | 
						|
    ClangBackEnd::RefactoringDatabaseInitializer<Sqlite::Database> databaseInitializer{database};
 | 
						|
    ClangBackEnd::FilePathCaching filePathCache{database};
 | 
						|
    ClangBackEnd::IncludeCollector collector{filePathCache};
 | 
						|
    ClangBackEnd::IncludeCollector emptyCollector{filePathCache};
 | 
						|
    Utils::PathStringVector excludePaths = {TESTDATA_DIR "/includecollector_main.h",
 | 
						|
                                            TESTDATA_DIR "/includecollector_main2.h",
 | 
						|
                                            TESTDATA_DIR "/includecollector_header1.h",
 | 
						|
                                            TESTDATA_DIR "/includecollector_header2.h",
 | 
						|
                                            TESTDATA_DIR "/includecollector_generated_file.h"};
 | 
						|
};
 | 
						|
 | 
						|
TEST_F(IncludeCollector, IncludesExternalHeader)
 | 
						|
{
 | 
						|
    collector.collectIncludes();
 | 
						|
 | 
						|
    ASSERT_THAT(collector.takeIncludeIds(),
 | 
						|
                AllOf(Contains(id(TESTDATA_DIR "/includecollector_external1.h")),
 | 
						|
                      Contains(id(TESTDATA_DIR "/includecollector_external2.h"))));
 | 
						|
}
 | 
						|
 | 
						|
TEST_F(IncludeCollector, DoesNotIncludesInternalHeader)
 | 
						|
{
 | 
						|
    collector.collectIncludes();
 | 
						|
 | 
						|
    ASSERT_THAT(collector.takeIncludeIds(), Not(Contains(id(TESTDATA_DIR "/includecollector_header1.h"))));
 | 
						|
}
 | 
						|
 | 
						|
TEST_F(IncludeCollector, NoDuplicate)
 | 
						|
{
 | 
						|
    collector.collectIncludes();
 | 
						|
 | 
						|
    ASSERT_THAT(collector.takeIncludeIds(),
 | 
						|
                UnorderedElementsAre(id(TESTDATA_DIR "/includecollector_external1.h"),
 | 
						|
                                     id(TESTDATA_DIR "/includecollector_external2.h"),
 | 
						|
                                     id(TESTDATA_DIR "/includecollector_external3.h")));
 | 
						|
}
 | 
						|
 | 
						|
TEST_F(IncludeCollector, IncludesAreSorted)
 | 
						|
{
 | 
						|
    collector.collectIncludes();
 | 
						|
 | 
						|
    ASSERT_THAT(collector.takeIncludeIds(),
 | 
						|
                SizeIs(3));
 | 
						|
}
 | 
						|
 | 
						|
TEST_F(IncludeCollector, If)
 | 
						|
{
 | 
						|
    emptyCollector.addFile(TESTDATA_DIR, "includecollector_if.cpp", "", {"cc", "includecollector_if.cpp"});
 | 
						|
 | 
						|
    emptyCollector.collectIncludes();
 | 
						|
 | 
						|
    ASSERT_THAT(emptyCollector.takeIncludeIds(),
 | 
						|
                ElementsAre(id(TESTDATA_DIR "/includecollector_true.h")));
 | 
						|
}
 | 
						|
 | 
						|
TEST_F(IncludeCollector, LocalPath)
 | 
						|
{
 | 
						|
    emptyCollector.addFile(TESTDATA_DIR, "includecollector_main.cpp", "", {"cc", "includecollector_main.cpp"});
 | 
						|
 | 
						|
    emptyCollector.collectIncludes();
 | 
						|
 | 
						|
    ASSERT_THAT(emptyCollector.takeIncludeIds(),
 | 
						|
                UnorderedElementsAre(id(TESTDATA_DIR "/includecollector_external1.h"),
 | 
						|
                                     id(TESTDATA_DIR "/includecollector_external2.h"),
 | 
						|
                                     id(TESTDATA_DIR "/includecollector_external3.h")));
 | 
						|
}
 | 
						|
 | 
						|
TEST_F(IncludeCollector, IgnoreMissingFile)
 | 
						|
{
 | 
						|
    emptyCollector.addFile(TESTDATA_DIR, "includecollector_missingfile.cpp", "", {"cc", "includecollector_missingfile.cpp"});
 | 
						|
 | 
						|
    emptyCollector.collectIncludes();
 | 
						|
 | 
						|
    ASSERT_THAT(emptyCollector.takeIncludeIds(),
 | 
						|
                UnorderedElementsAre(id(TESTDATA_DIR "/includecollector_external1.h")));
 | 
						|
}
 | 
						|
 | 
						|
void IncludeCollector::SetUp()
 | 
						|
{
 | 
						|
    collector.addFile(TESTDATA_DIR, "includecollector_main.cpp", "", {"cc", "includecollector_main.cpp"});
 | 
						|
    collector.addFile(TESTDATA_DIR, "includecollector_main2.cpp", "", {"cc", "includecollector_main2.cpp"});
 | 
						|
 | 
						|
    collector.addUnsavedFiles({{{TESTDATA_DIR, "includecollector_generated_file.h"}, "#pragma once", {}}});
 | 
						|
 | 
						|
    collector.setExcludedIncludes(excludePaths.clone());
 | 
						|
    emptyCollector.setExcludedIncludes(excludePaths.clone());
 | 
						|
}
 | 
						|
 | 
						|
FilePathId IncludeCollector::id(const Utils::SmallStringView &path)
 | 
						|
{
 | 
						|
    return filePathCache.filePathId(FilePathView{path});
 | 
						|
}
 | 
						|
 | 
						|
}
 |