1 | //===-- SourceManagerTest.cpp ---------------------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Core/SourceManager.h" |
10 | #include "lldb/Host/FileSystem.h" |
11 | #include "gtest/gtest.h" |
12 | |
13 | #include "TestingSupport/MockTildeExpressionResolver.h" |
14 | |
15 | using namespace lldb; |
16 | using namespace lldb_private; |
17 | |
18 | class SourceFileCache : public ::testing::Test { |
19 | public: |
20 | void SetUp() override { |
21 | FileSystem::Initialize(t: std::unique_ptr<TildeExpressionResolver>( |
22 | new MockTildeExpressionResolver("Jonas" , "/jonas" ))); |
23 | } |
24 | void TearDown() override { FileSystem::Terminate(); } |
25 | }; |
26 | |
27 | TEST_F(SourceFileCache, FindSourceFileFound) { |
28 | SourceManager::SourceFileCache cache; |
29 | |
30 | // Insert: foo |
31 | FileSpec foo_file_spec("foo" ); |
32 | auto foo_file_sp = |
33 | std::make_shared<SourceManager::File>(args&: foo_file_spec, args: lldb::DebuggerSP()); |
34 | cache.AddSourceFile(file_spec: foo_file_spec, file_sp: foo_file_sp); |
35 | |
36 | // Query: foo, expect found. |
37 | FileSpec another_foo_file_spec("foo" ); |
38 | ASSERT_EQ(cache.FindSourceFile(another_foo_file_spec), foo_file_sp); |
39 | } |
40 | |
41 | TEST_F(SourceFileCache, FindSourceFileNotFound) { |
42 | SourceManager::SourceFileCache cache; |
43 | |
44 | // Insert: foo |
45 | FileSpec foo_file_spec("foo" ); |
46 | auto foo_file_sp = |
47 | std::make_shared<SourceManager::File>(args&: foo_file_spec, args: lldb::DebuggerSP()); |
48 | cache.AddSourceFile(file_spec: foo_file_spec, file_sp: foo_file_sp); |
49 | |
50 | // Query: bar, expect not found. |
51 | FileSpec bar_file_spec("bar" ); |
52 | ASSERT_EQ(cache.FindSourceFile(bar_file_spec), nullptr); |
53 | } |
54 | |
55 | TEST_F(SourceFileCache, FindSourceFileByUnresolvedPath) { |
56 | SourceManager::SourceFileCache cache; |
57 | |
58 | FileSpec foo_file_spec("~/foo" ); |
59 | |
60 | // Mimic the resolution in SourceManager::GetFile. |
61 | FileSpec resolved_foo_file_spec = foo_file_spec; |
62 | FileSystem::Instance().Resolve(file_spec&: resolved_foo_file_spec); |
63 | |
64 | // Create the file with the resolved file spec. |
65 | auto foo_file_sp = std::make_shared<SourceManager::File>( |
66 | args&: resolved_foo_file_spec, args: lldb::DebuggerSP()); |
67 | |
68 | // Cache the result with the unresolved file spec. |
69 | cache.AddSourceFile(file_spec: foo_file_spec, file_sp: foo_file_sp); |
70 | |
71 | // Query the unresolved path. |
72 | EXPECT_EQ(cache.FindSourceFile(FileSpec("~/foo" )), foo_file_sp); |
73 | |
74 | // Query the resolved path. |
75 | EXPECT_EQ(cache.FindSourceFile(FileSpec("/jonas/foo" )), foo_file_sp); |
76 | } |
77 | |