1 //===-- SourceManagerTest.cpp ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/SourceManager.h"
10 #include "lldb/Host/FileSystem.h"
11 #include "gtest/gtest.h"
13 #include "TestingSupport/MockTildeExpressionResolver.h"
16 using namespace lldb_private
;
18 class SourceFileCache
: public ::testing::Test
{
20 void SetUp() override
{
21 FileSystem::Initialize(std::unique_ptr
<TildeExpressionResolver
>(
22 new MockTildeExpressionResolver("Jonas", "/jonas")));
24 void TearDown() override
{ FileSystem::Terminate(); }
27 TEST_F(SourceFileCache
, FindSourceFileFound
) {
28 SourceManager::SourceFileCache cache
;
31 FileSpec
foo_file_spec("foo");
33 std::make_shared
<SourceManager::File
>(foo_file_spec
, lldb::DebuggerSP());
34 cache
.AddSourceFile(foo_file_spec
, foo_file_sp
);
36 // Query: foo, expect found.
37 FileSpec
another_foo_file_spec("foo");
38 ASSERT_EQ(cache
.FindSourceFile(another_foo_file_spec
), foo_file_sp
);
41 TEST_F(SourceFileCache
, FindSourceFileNotFound
) {
42 SourceManager::SourceFileCache cache
;
45 FileSpec
foo_file_spec("foo");
47 std::make_shared
<SourceManager::File
>(foo_file_spec
, lldb::DebuggerSP());
48 cache
.AddSourceFile(foo_file_spec
, foo_file_sp
);
50 // Query: bar, expect not found.
51 FileSpec
bar_file_spec("bar");
52 ASSERT_EQ(cache
.FindSourceFile(bar_file_spec
), nullptr);
55 TEST_F(SourceFileCache
, FindSourceFileByUnresolvedPath
) {
56 SourceManager::SourceFileCache cache
;
58 FileSpec
foo_file_spec("~/foo");
60 // Mimic the resolution in SourceManager::GetFile.
61 FileSpec resolved_foo_file_spec
= foo_file_spec
;
62 FileSystem::Instance().Resolve(resolved_foo_file_spec
);
64 // Create the file with the resolved file spec.
65 auto foo_file_sp
= std::make_shared
<SourceManager::File
>(
66 resolved_foo_file_spec
, lldb::DebuggerSP());
68 // Cache the result with the unresolved file spec.
69 cache
.AddSourceFile(foo_file_spec
, foo_file_sp
);
71 // Query the unresolved path.
72 EXPECT_EQ(cache
.FindSourceFile(FileSpec("~/foo")), foo_file_sp
);
74 // Query the resolved path.
75 EXPECT_EQ(cache
.FindSourceFile(FileSpec("/jonas/foo")), foo_file_sp
);