Skip to content

Commit b590aac

Browse files
committed
adapt to custom matchers for expected<T, Error>
Signed-off-by: Junwang Zhao <[email protected]>
1 parent b6a8047 commit b590aac

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/iceberg/file_io.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class ICEBERG_EXPORT FileIO {
4949
virtual expected<std::string, Error> ReadFile(const std::string& file_location,
5050
std::optional<size_t> length) {
5151
// The following line is to avoid Windows linker error LNK2019.
52-
// If this function is defined as pure virtual function, the `unexpected<Error>` will
53-
// not be instantiated and exported in libiceberg.
52+
// If this function is defined as pure virtual function, the `expected<std::string,
53+
// Error>` and `unexpected<Error>` will not be instantiated and exported in
54+
// libiceberg.
5455
return unexpected<Error>{
5556
{.kind = ErrorKind::kNotImplemented, .message = "ReadFile not implemented"}};
5657
}
@@ -65,8 +66,8 @@ class ICEBERG_EXPORT FileIO {
6566
virtual expected<void, Error> WriteFile(const std::string& file_location,
6667
std::string_view content, bool overwrite) {
6768
// The following line is to avoid Windows linker error LNK2019.
68-
// If this function is defined as pure virtual function, the `unexpected<Error>` will
69-
// not be instantiated and exported in libiceberg.
69+
// If this function is defined as pure virtual function, the `expected<void, Error>`
70+
// will not be instantiated and exported in libiceberg.
7071
return unexpected<Error>{
7172
{.kind = ErrorKind::kNotImplemented, .message = "WriteFile not implemented"}};
7273
}

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ if(ICEBERG_BUILD_BUNDLE)
4949
add_executable(arrow_fs_file_io_test)
5050
target_sources(arrow_fs_file_io_test PRIVATE arrow_fs_file_io_test.cc)
5151
target_link_libraries(arrow_fs_file_io_test PRIVATE iceberg_bundle_static
52-
GTest::gtest_main)
52+
GTest::gtest_main GTest::gmock)
5353
add_test(NAME arrow_fs_file_io_test COMMAND arrow_fs_file_io_test)
5454
endif()

test/arrow_fs_file_io_test.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,46 @@
2424
#include <arrow/filesystem/localfs.h>
2525
#include <gtest/gtest.h>
2626

27+
#include "matchers.h"
28+
29+
namespace iceberg {
30+
2731
class LocalFileIOTest : public testing::Test {
2832
protected:
2933
void SetUp() override {
30-
local_fs_ = std::make_shared<arrow::fs::LocalFileSystem>();
34+
local_fs_ = std::make_shared<::arrow::fs::LocalFileSystem>();
3135
file_io_ = std::make_shared<iceberg::arrow::io::ArrowFileSystemFileIO>(local_fs_);
3236
}
3337

34-
std::shared_ptr<arrow::fs::LocalFileSystem> local_fs_;
38+
std::shared_ptr<::arrow::fs::LocalFileSystem> local_fs_;
3539
std::shared_ptr<iceberg::FileIO> file_io_;
3640
std::filesystem::path tmpfile = std::filesystem::temp_directory_path() / "123.txt";
3741
};
3842

3943
TEST_F(LocalFileIOTest, ReadWriteFile) {
4044
auto read_res = file_io_->ReadFile(tmpfile.string(), 1024);
41-
EXPECT_EQ(read_res.error().kind, iceberg::ErrorKind::kInvalidArgument);
45+
EXPECT_THAT(read_res, IsError(ErrorKind::kInvalidArgument));
46+
EXPECT_THAT(read_res, HasErrorMessage("Length is not supported"));
4247

4348
read_res = file_io_->ReadFile(tmpfile.string(), std::nullopt);
44-
EXPECT_EQ(read_res.error().kind, iceberg::ErrorKind::kIOError);
49+
EXPECT_THAT(read_res, IsError(ErrorKind::kIOError));
50+
EXPECT_THAT(read_res, HasErrorMessage("No such file or directory"));
4551

4652
auto write_res = file_io_->WriteFile(tmpfile.string(), "hello world", false);
47-
EXPECT_TRUE(write_res.has_value());
53+
EXPECT_THAT(write_res, IsOk());
4854

4955
read_res = file_io_->ReadFile(tmpfile.string(), std::nullopt);
56+
EXPECT_THAT(read_res, IsOk());
5057
EXPECT_EQ(read_res.value(), "hello world");
5158
}
5259

5360
TEST_F(LocalFileIOTest, DeleteFile) {
5461
auto del_res = file_io_->DeleteFile(tmpfile.string());
55-
EXPECT_TRUE(del_res.has_value());
62+
EXPECT_THAT(del_res, IsOk());
5663

5764
del_res = file_io_->DeleteFile(tmpfile.string());
58-
EXPECT_EQ(del_res.error().kind, iceberg::ErrorKind::kIOError);
65+
EXPECT_THAT(del_res, IsError(ErrorKind::kIOError));
66+
EXPECT_THAT(del_res, HasErrorMessage("No such file or directory"));
5967
}
68+
69+
} // namespace iceberg

0 commit comments

Comments
 (0)