mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
There's currently a bug with empty files, in that it initializes SourceBuffer with an invalid StringRef that results in a crash. That got me looking at the std::optional TODO, but the issue is that there are really three states: - Buffered - mmapped (not buffered) - Moved out of (no longer initialized) Technically an optional could work if we initialize the buffer on move out, indicating the mmap is gone. But the mode setup felt better to me. And then this also adds the size check. Which is really how I started looking at this.
76 lines
2.2 KiB
C++
76 lines
2.2 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/source/source_buffer.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
#include "llvm/ADT/Twine.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
TEST(SourceBufferTest, StringRep) {
|
|
auto buffer = SourceBuffer::CreateFromText(llvm::Twine("Hello") + " World");
|
|
EXPECT_EQ("/text", buffer->Filename());
|
|
EXPECT_EQ("Hello World", buffer->Text());
|
|
}
|
|
|
|
TEST(SourceBufferText, StringRepWithFilename) {
|
|
// Give a custom filename.
|
|
auto buffer =
|
|
SourceBuffer::CreateFromText("Hello World Again!", "/custom/text");
|
|
EXPECT_EQ("/custom/text", buffer->Filename());
|
|
EXPECT_EQ("Hello World Again!", buffer->Text());
|
|
}
|
|
|
|
auto CreateTestFile(llvm::StringRef text) -> std::string {
|
|
int fd = -1;
|
|
llvm::SmallString<1024> path;
|
|
auto error_code =
|
|
llvm::sys::fs::createTemporaryFile("test_file", ".txt", fd, path);
|
|
if (error_code) {
|
|
llvm::report_fatal_error(llvm::Twine("Failed to create temporary file: ") +
|
|
error_code.message());
|
|
}
|
|
|
|
llvm::raw_fd_ostream out_stream(fd, /*shouldClose=*/true);
|
|
out_stream << text;
|
|
out_stream.close();
|
|
|
|
return path.str().str();
|
|
}
|
|
|
|
TEST(SourceBufferTest, FileRep) {
|
|
auto test_file_path = CreateTestFile("Hello World");
|
|
|
|
auto expected_buffer = SourceBuffer::CreateFromFile(test_file_path);
|
|
ASSERT_TRUE(static_cast<bool>(expected_buffer))
|
|
<< "Error message: " << toString(expected_buffer.takeError());
|
|
|
|
SourceBuffer& buffer = *expected_buffer;
|
|
|
|
EXPECT_EQ(test_file_path, buffer.Filename());
|
|
EXPECT_EQ("Hello World", buffer.Text());
|
|
}
|
|
|
|
TEST(SourceBufferTest, FileRepEmpty) {
|
|
auto test_file_path = CreateTestFile("");
|
|
|
|
auto expected_buffer = SourceBuffer::CreateFromFile(test_file_path);
|
|
ASSERT_TRUE(static_cast<bool>(expected_buffer))
|
|
<< "Error message: " << toString(expected_buffer.takeError());
|
|
|
|
SourceBuffer& buffer = *expected_buffer;
|
|
|
|
EXPECT_EQ(test_file_path, buffer.Filename());
|
|
EXPECT_EQ("", buffer.Text());
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|