37# include <gtest/gtest.h>
57 long long process_id() noexcept
60 return static_cast<long long>(_getpid());
62 return static_cast<long long>(getpid());
67 make_temp_file_path(
const std::string & stem)
69 namespace fs = std::filesystem;
77 static std::atomic<unsigned long long>
counter{0};
78 return fs::temp_directory_path() /
79 (
"aleph_" + stem +
"_" +
80 std::to_string(std::chrono::steady_clock::now().time_since_epoch().
count()) +
81 "_" + std::to_string(process_id()) +
82 "_" + std::to_string(
counter++) +
88TEST(SourceManager, AddVirtualFileStoresNameAndText)
95 EXPECT_EQ(sm.
file_text(
id),
"let x = 1;\n");
100TEST(SourceManager, PositionResolvesLineAndColumn)
106 EXPECT_EQ(p0.line, 1u);
107 EXPECT_EQ(p0.column, 1u);
110 EXPECT_EQ(p2.line, 1u);
111 EXPECT_EQ(p2.column, 3u);
114 EXPECT_EQ(p3.line, 2u);
115 EXPECT_EQ(p3.column, 1u);
118 EXPECT_EQ(p7.line, 3u);
119 EXPECT_EQ(p7.column, 1u);
123TEST(SourceManager, SpanSliceReturnsExpectedSubstring)
127 const auto sp = sm.
span(
id, 6, 10);
129 EXPECT_FALSE(sp.empty());
130 EXPECT_EQ(sp.size(), 4u);
131 EXPECT_TRUE(sp.contains(7));
132 EXPECT_EQ(sm.
slice(sp),
"beta");
136TEST(SourceManager, SnippetMarksSingleLineSpan)
140 const auto sp = sm.
span(
id, 6, 10);
141 const auto snip = sm.
snippet(sp);
143 EXPECT_EQ(snip.file_name,
"main.aw");
144 EXPECT_EQ(snip.line, 1u);
145 EXPECT_EQ(snip.column, 7u);
146 EXPECT_EQ(snip.line_text,
"alpha beta gamma");
147 EXPECT_EQ(snip.highlight_column, 7u);
148 EXPECT_EQ(snip.highlight_width, 4u);
152TEST(SourceManager, EmptySpanIsAllowedAndProducesCaretWidthOne)
156 const auto sp = sm.
span(
id, 1, 1);
157 const auto snip = sm.
snippet(sp);
159 EXPECT_TRUE(sp.empty());
160 EXPECT_EQ(sm.
slice(sp),
"");
161 EXPECT_EQ(snip.highlight_column, 2u);
162 EXPECT_EQ(snip.highlight_width, 1u);
166TEST(SourceManager, LoadFileReadsContent)
168 namespace fs = std::filesystem;
170 const fs::path path = make_temp_file_path(
"source_manager");
172 std::ofstream
out(path, std::ios::binary);
173 out <<
"line1\nline2\n";
177 const auto id = sm.
load_file(path.string());
178 EXPECT_EQ(sm.
file_name(
id), path.string());
179 EXPECT_EQ(sm.
file_text(
id),
"line1\nline2\n");
185TEST(SourceManager, InvalidOffsetThrows)
190 EXPECT_THROW((
void) sm.
position(
id, 4), std::out_of_range);
191 EXPECT_THROW((
void) sm.
span(
id, 1, 4), std::out_of_range);
192 EXPECT_THROW((
void) sm.
file(999), std::out_of_range);
Source file and span management utilities for compiler-style tooling.
size_t size_t int32_t * out
Stores source files and resolves offsets into human-readable data.
const std::string & file_name(const Source_File_Id id) const
Returns the display name of file id.
std::string slice(const Source_Span &span) const
Returns the exact text covered by span.
Source_Snippet snippet(const Source_Span &span) const
Builds a single-line diagnostic snippet from span.
const std::string & file_text(const Source_File_Id id) const
Returns the full text of file id.
const Source_File & file(const Source_File_Id id) const
Returns the registered file record for id.
Source_Span span(const Source_File_Id id, const Source_Offset begin, const Source_Offset end) const
Creates a validated span in file id.
Source_File_Id load_file(const std::string &path)
Loads a file from disk and registers it.
size_t num_files() const noexcept
Returns the number of registered files.
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
size_t file_size(const Source_File_Id id) const
Returns the size in bytes of file id.
Source_Position position(const Source_File_Id id, const Source_Offset off) const
Resolves off to file/offset/line/column information.
Main namespace for Aleph-w library functions.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.