mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Stop passing file size into InputFile c'tor.
PiperOrigin-RevId: 845989622
This commit is contained in:
committed by
Copybara-Service
parent
0fe6eb566f
commit
b88fbc6636
@@ -14,10 +14,8 @@
|
||||
|
||||
#include "internal/platform/implementation/shared/file.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#include "file/util/temp_path.h"
|
||||
@@ -40,11 +38,8 @@ class FileTest : public ::testing::Test {
|
||||
void WriteToFile(absl::string_view text) {
|
||||
file_ << text;
|
||||
file_.flush();
|
||||
size_ += text.size();
|
||||
}
|
||||
|
||||
size_t GetSize() const { return size_; }
|
||||
|
||||
void AssertEquals(const ExceptionOr<ByteArray>& bytes,
|
||||
const std::string& expected) {
|
||||
EXPECT_TRUE(bytes.ok());
|
||||
@@ -61,44 +56,43 @@ class FileTest : public ::testing::Test {
|
||||
std::unique_ptr<TempPath> temp_path_;
|
||||
std::string path_;
|
||||
std::fstream file_;
|
||||
size_t size_ = 0;
|
||||
};
|
||||
|
||||
TEST_F(FileTest, IOFile_NonExistentPathInput) {
|
||||
auto io_file =
|
||||
shared::IOFile::CreateInputFile("/not/a/valid/path.txt", GetSize());
|
||||
shared::IOFile::CreateInputFile("/not/a/valid/path.txt");
|
||||
ExceptionOr<ByteArray> read_result = io_file->Read(kMaxSize);
|
||||
EXPECT_FALSE(read_result.ok());
|
||||
EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, IOFile_GetFilePath) {
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_);
|
||||
EXPECT_EQ(io_file->GetFilePath(), path_);
|
||||
}
|
||||
|
||||
TEST_F(FileTest, IOFile_EmptyFileEOF) {
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_);
|
||||
AssertEmpty(io_file->Read(kMaxSize));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, IOFile_ReadWorks) {
|
||||
WriteToFile("abc");
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_);
|
||||
io_file->Read(kMaxSize);
|
||||
SUCCEED();
|
||||
}
|
||||
|
||||
TEST_F(FileTest, IOFile_ReadUntilEOF) {
|
||||
WriteToFile("abc");
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_);
|
||||
AssertEquals(io_file->Read(kMaxSize), "abc");
|
||||
AssertEmpty(io_file->Read(kMaxSize));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, IOFile_ReadWithSize) {
|
||||
WriteToFile("abc");
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_);
|
||||
AssertEquals(io_file->Read(2), "ab");
|
||||
AssertEquals(io_file->Read(1), "c");
|
||||
AssertEmpty(io_file->Read(kMaxSize));
|
||||
@@ -106,7 +100,7 @@ TEST_F(FileTest, IOFile_ReadWithSize) {
|
||||
|
||||
TEST_F(FileTest, IOFile_GetTotalSize) {
|
||||
WriteToFile("abc");
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_);
|
||||
EXPECT_EQ(io_file->GetTotalSize(), 3);
|
||||
AssertEquals(io_file->Read(1), "a");
|
||||
EXPECT_EQ(io_file->GetTotalSize(), 3);
|
||||
@@ -114,7 +108,7 @@ TEST_F(FileTest, IOFile_GetTotalSize) {
|
||||
|
||||
TEST_F(FileTest, IOFile_CloseInput) {
|
||||
WriteToFile("abc");
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_, GetSize());
|
||||
auto io_file = shared::IOFile::CreateInputFile(path_);
|
||||
io_file->Close();
|
||||
ExceptionOr<ByteArray> read_result = io_file->Read(kMaxSize);
|
||||
EXPECT_FALSE(read_result.ok());
|
||||
@@ -134,7 +128,7 @@ TEST_F(FileTest, IOFile_Write) {
|
||||
EXPECT_EQ(io_file_output->Write(bytes1), Exception{Exception::kSuccess});
|
||||
EXPECT_EQ(io_file_output->Write(bytes2), Exception{Exception::kSuccess});
|
||||
auto io_file_input =
|
||||
shared::IOFile::CreateInputFile(io_file_output->GetFilePath(), GetSize());
|
||||
shared::IOFile::CreateInputFile(io_file_output->GetFilePath());
|
||||
AssertEquals(io_file_input->Read(kMaxSize), "abc");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user